--- /dev/null
+/*
+ * Copyright 2005 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.photos.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.inject.Inject;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.wamblee.photos.model.Album;
+import org.wamblee.photos.model.Photo;
+import org.wamblee.photos.model.PhotoEntry;
+import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
+
+/**
+ * Sends an image (either thumbnail or full size) from the photo album.
+ * <p/>
+ * The returned picture is defined based on
+ * {@link javax.servlet.http.HttpServletRequest#getPathInfo}. as follows. If the
+ * path info starts with the string defined by {@link #THUMBNAIL_NAME}, then a
+ * thumbnail image is served and the rest of the path info is the relative path
+ * of the picture. Otherwise the path info is identical to the relative path of
+ * the picture.
+ */
+public class ImageSender extends HttpServlet {
+
+ @Inject
+ @AuthorizedPhotos
+ private Album root;
+
+ static final long serialVersionUID = 4069997717483260853L;
+
+ enum ImageType {
+ thumbnail,
+ photo,
+ resource
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
+ * , javax.servlet.http.HttpServletResponse)
+ */
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doPost(request, response);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
+ * , javax.servlet.http.HttpServletResponse)
+ */
+ protected void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse)
+ throws ServletException, IOException {
+
+ String entryPath = aRequest.getPathInfo();
+ if (entryPath == null) {
+ entryPath = "/";
+ }
+
+ ImageType type = null;
+ for (ImageType t : ImageType.values()) {
+ if (entryPath.startsWith("/" + t + "/")) {
+ entryPath = entryPath.substring(t.toString().length() + 1);
+ type = t;
+ break;
+ }
+ }
+
+ if (type == null) {
+ throw new RuntimeException("unsupported URL " + aRequest.getPathInfo());
+ }
+
+ switch (type) {
+ case thumbnail:
+ case photo: {
+ if (entryPath.endsWith(".jpg")) {
+ entryPath = entryPath.substring(0, entryPath.length() - ".jpg".length());
+ }
+ PhotoEntry entry = root.getEntry(entryPath);
+
+ Photo photo = (Photo) entry;
+ InputStream is = null;
+ if (type == ImageType.thumbnail) {
+ is = photo.getThumbNail();
+ } else {
+ is = photo.getPhoto();
+ }
+ sendImage(aResponse, is);
+ break;
+ }
+ case resource: {
+ try (InputStream is = getServletContext().getResourceAsStream("/images/" + entryPath)) {
+ ServletOutputStream os = aResponse.getOutputStream();
+ byte[] buffer = new byte[4096];
+ int n;
+ while ((n = is.read(buffer)) > 0) {
+ os.write(buffer, 0, n);
+ }
+ }
+ break;
+ }
+
+ default: {
+ //throw new RuntimeException("Unknown type " + type);
+ }
+ }
+ }
+
+ /**
+ * Sends the image.
+ *
+ * @param response Response
+ * @param is Input stream of the image.
+ * @throws IOException In case an IO problem occurs.
+ */
+
+ private void sendImage(HttpServletResponse response, InputStream is) throws IOException {
+ int c;
+ while ((c = is.read()) >= 0) {
+ response.getOutputStream().write(c);
+ }
+ }
+}