a7341846288b21d374e5ff027933b92029cc733f
[photos] / src / main / java / org / wamblee / photos / servlet / ImageSender.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.photos.servlet;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20
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;
27
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;
32
33 /**
34  * Sends an image (either thumbnail or full size) from the photo album.
35  * <p/>
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
41  * the picture.
42  */
43 public class ImageSender extends HttpServlet {
44
45     @Inject
46     @AuthorizedPhotos
47     private Album root;
48
49     static final long serialVersionUID = 4069997717483260853L;
50
51     enum ImageType {
52         thumbnail,
53         photo,
54         resource
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see
61      * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
62      * , javax.servlet.http.HttpServletResponse)
63      */
64     protected void doGet(HttpServletRequest request, HttpServletResponse response)
65             throws ServletException, IOException {
66         doPost(request, response);
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see
73      * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
74      * , javax.servlet.http.HttpServletResponse)
75      */
76     protected void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse)
77             throws ServletException, IOException {
78
79         String entryPath = aRequest.getPathInfo();
80         if (entryPath == null) {
81             entryPath = "/";
82         }
83
84         ImageType type = null;
85         for (ImageType t : ImageType.values()) {
86             if (entryPath.startsWith("/" + t + "/")) {
87                 entryPath = entryPath.substring(t.toString().length() + 1);
88                 type = t;
89                 break;
90             }
91         }
92
93         if (type == null) {
94             throw new RuntimeException("unsupported URL " + aRequest.getPathInfo());
95         }
96
97         switch (type) {
98             case thumbnail:
99             case photo: {
100                 if (entryPath.endsWith(".jpg")) {
101                     entryPath = entryPath.substring(0, entryPath.length() - ".jpg".length());
102                 }
103                 PhotoEntry entry = root.getEntry(entryPath);
104
105                 Photo photo = (Photo) entry;
106                 InputStream is = null;
107                 if (type == ImageType.thumbnail) {
108                     is = photo.getThumbNail();
109                 } else {
110                     is = photo.getPhoto();
111                 }
112                 sendImage(aResponse, is);
113                 break;
114             }
115             case resource: {
116                 try (InputStream is = getServletContext().getResourceAsStream("/images/" + entryPath)) {
117                     ServletOutputStream os = aResponse.getOutputStream();
118                     byte[] buffer = new byte[4096];
119                     int n;
120                     while ((n = is.read(buffer)) > 0) {
121                         os.write(buffer, 0, n);
122                     }
123                 }
124                 break;
125             }
126
127             default: {
128                 //throw new RuntimeException("Unknown type " + type);
129             }
130         }
131     }
132
133     /**
134      * Sends the image.
135      *
136      * @param response Response
137      * @param is       Input stream of the image.
138      * @throws IOException In case an IO problem occurs.
139      */
140
141     private void sendImage(HttpServletResponse response, InputStream is) throws IOException {
142         int c;
143         while ((c = is.read()) >= 0) {
144             response.getOutputStream().write(c);
145         }
146     }
147 }