2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.photos.servlet;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import javax.inject.Inject;
22 import javax.servlet.ServletException;
23 import javax.servlet.ServletOutputStream;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
28 import org.wamblee.photos.model.Album;
29 import org.wamblee.photos.model.Photo;
30 import org.wamblee.photos.model.PhotoEntry;
31 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
34 * Sends an image (either thumbnail or full size) from the photo album.
36 * The returned picture is defined based on
37 * {@link javax.servlet.http.HttpServletRequest#getPathInfo}. as follows. If the
38 * path info starts with the string defined by {@link #THUMBNAIL_NAME}, then a
39 * thumbnail image is served and the rest of the path info is the relative path
40 * of the picture. Otherwise the path info is identical to the relative path of
43 public class ImageSender extends HttpServlet {
49 static final long serialVersionUID = 4069997717483260853L;
61 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
62 * , javax.servlet.http.HttpServletResponse)
64 protected void doGet(HttpServletRequest request, HttpServletResponse response)
65 throws ServletException, IOException {
66 doPost(request, response);
73 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
74 * , javax.servlet.http.HttpServletResponse)
76 protected void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse)
77 throws ServletException, IOException {
79 String entryPath = aRequest.getPathInfo();
80 if (entryPath == null) {
84 ImageType type = null;
85 for (ImageType t : ImageType.values()) {
86 if (entryPath.startsWith("/" + t + "/")) {
87 entryPath = entryPath.substring(t.toString().length() + 1);
94 throw new RuntimeException("unsupported URL " + aRequest.getPathInfo());
100 if (entryPath.endsWith(".jpg")) {
101 entryPath = entryPath.substring(0, entryPath.length() - ".jpg".length());
103 PhotoEntry entry = root.getEntry(entryPath);
105 Photo photo = (Photo) entry;
106 InputStream is = null;
107 if (type == ImageType.thumbnail) {
108 is = photo.getThumbNail();
110 is = photo.getPhoto();
112 sendImage(aResponse, is);
116 try (InputStream is = getServletContext().getResourceAsStream("/images/" + entryPath)) {
117 ServletOutputStream os = aResponse.getOutputStream();
118 byte[] buffer = new byte[4096];
120 while ((n = is.read(buffer)) > 0) {
121 os.write(buffer, 0, n);
128 //throw new RuntimeException("Unknown type " + type);
136 * @param response Response
137 * @param is Input stream of the image.
138 * @throws IOException In case an IO problem occurs.
141 private void sendImage(HttpServletResponse response, InputStream is) throws IOException {
143 while ((c = is.read()) >= 0) {
144 response.getOutputStream().write(c);