Skip to content

Commit

Permalink
For #85 - implemented pictures loading with GET request to the 'Pictu…
Browse files Browse the repository at this point in the history
…resServlet'
  • Loading branch information
vitalidze committed May 21, 2015
1 parent 425b9c5 commit 1809856
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/main/java/org/traccar/web/server/model/PicturesServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.inject.persist.Transactional;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -129,4 +128,30 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
FileUtils.deleteQuietly(file);
}
}

@Transactional(rollbackOn = { IOException.class, RuntimeException.class })
@RequireUser
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
long pictureId;
try {
pictureId = Long.parseLong(req.getPathInfo().substring(1));
} catch (NumberFormatException nfe) {
resp.getWriter().write("Incorrect picture id: " + req.getPathInfo().substring(1));
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}

Picture picture = entityManager.get().find(Picture.class, pictureId);
if (picture == null) {
resp.getWriter().write("Picture with id " + pictureId + " does not exist");
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}

resp.setContentType("image/" + picture.getMimeType());
resp.setContentLength(picture.getData().length);
IOUtils.write(picture.getData(), resp.getOutputStream());
resp.setStatus(HttpServletResponse.SC_OK);
}
}

0 comments on commit 1809856

Please sign in to comment.