/* * 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.servlet.ServletException; 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; /** * Sends an image (either thumbnail or full size) from the photo album. * * 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 { static final long serialVersionUID = 4069997717483260853L; /** * Name of the request parameter indicating that a thumbnail must * be viewed */ public static String THUMBNAIL_NAME = "thumbnail"; /* * (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 = "/"; } boolean thumbnail = aRequest.getParameter(THUMBNAIL_NAME) != null; if ( entryPath.startsWith("/thumbnail")) { entryPath = entryPath.substring("/thumbnail".length()); thumbnail = true; } Album root = new PhotoSession(aRequest.getSession()).getAlbum(); PhotoEntry entry = root.getEntry(entryPath); Photo photo = (Photo) entry; InputStream is; if (thumbnail) { is = photo.getThumbNail(); } else { is = photo.getPhoto(); } sendImage(aResponse, is); } /** * 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); } } }