KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > servlet > ImageServlet


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.servlet;
21
22 import za.org.coefficient.core.CoefficientInterceptor;
23 import za.org.coefficient.util.common.UploadedFile;
24
25 import org.apache.commons.collections.LRUMap;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30
31 import java.util.Collections JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.ServletConfig JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.http.HttpServlet JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39
40 /**
41  * This servlet is used to serve up binary content off of the classpath.
42  * Usage will look like this:
43  * &lt;img SRC="images?image=/za/org/coefficient/modules/issueTracker/images/bullet_creme.gif"$gt;<br>
44  * In the above example the image= value is the path, in the classpath, to the
45  * desired resource.
46  *
47  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
48  * @version $Revision: 1.7 $
49  *
50  * @web.servlet
51  * name="ImageServlet"
52  * display-name="Image Servlet"
53  * description="This servlet is used to get images from the classpath"
54  * @web.servlet-mapping
55  * url-pattern="/images"
56  */

57 public class ImageServlet extends HttpServlet JavaDoc {
58
59     //~ Instance fields ========================================================
60

61     private Map contentCache;
62
63     private CoefficientInterceptor interceptor;
64
65     //~ Methods ================================================================
66

67     /**
68      * Called by the servlet's container to indicate to the servlet that the
69      * servlet is being taken out of service. Note: super.destroy() is NOT
70      * called. This method does nothing.
71      */

72     public void destroy() {
73     }
74
75     /**
76      * Called by the servlet container to indicate to a servlet that
77      * the servlet is being placed into service. Note: this method
78      * calls super.init(cfg).
79      *
80      * @param cfg The ServletConfig instance used by the Servlet
81      * container to the current servlet.
82      *
83      * @throws ServletException
84      *
85      */

86     public void init(ServletConfig JavaDoc cfg) throws ServletException JavaDoc {
87         //Call super
88
super.init(cfg);
89         contentCache = Collections.synchronizedMap(new LRUMap(100));
90     }
91
92     /**
93      * Called by the serlet container (via the service method)
94      * to allow a servlet to handle a GET request making this
95      * method the entry point for the Project Engine user.
96      *
97      * @param request The HttpServletRequest instance.
98      * @param response The HttpServletResponse instance.
99      *
100      * @see #execute(HttpServletRequest,HttpServletResponse) execute(request,response) execute(request,response).
101      *
102      */

103     protected void doGet(HttpServletRequest JavaDoc request,
104         HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
105         String JavaDoc imageURL = request.getParameter("image");
106         InputStream JavaDoc is = null;
107         UploadedFile uf = null;
108         if (imageURL != null) {
109             // first try to get from the cache
110
uf = (UploadedFile)contentCache.get(imageURL);
111             if(uf == null) {
112                 ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
113                 try {
114                     String JavaDoc contentType = null;
115                     is = ImageServlet.class.getResourceAsStream(imageURL);
116                     if (is != null) {
117                         int slashIdx = imageURL.lastIndexOf("/");
118                         if (slashIdx > -1) {
119                             contentType =
120                                 getContentType(imageURL.substring(slashIdx + 1,
121                                                                   imageURL.length()));
122                         } else {
123                             contentType = getContentType(imageURL);
124                         }
125                         
126                         byte[] bytes = new byte[is.available()];
127                         int numBytes = is.read(bytes);
128                         while (numBytes != -1 && numBytes != 0) {
129                             baos.write(bytes, 0, numBytes);
130                             bytes = new byte[is.available()];
131                             numBytes = is.read(bytes);
132                         }
133                         uf = new UploadedFile(contentType, baos.toByteArray(),
134                                               imageURL);
135                         contentCache.put(imageURL, uf);
136                     }
137                 } catch (Exception JavaDoc ex) {
138                     ex.printStackTrace();
139                 } finally {
140                     try {
141                         if(baos != null) {
142                             baos.close();
143                         }
144                         if (is != null) {
145                             is.close();
146                         }
147                     } catch (Exception JavaDoc e) {
148                     }
149                 }
150             }
151             if (uf == null) {
152                 System.out.println("ImageServlet.doGet() -> Image was not found!");
153             }else {
154                 response.setContentType(uf.getContentType());
155                 response.getOutputStream().write(uf.getContent(), 0,
156                                                  uf.getContent().length);
157             }
158             
159         }
160     }
161
162     /**
163      * Called by the servlet container to handle a POST request. This
164      * method calls doGet(request, response) to perform the job.
165      *
166      * @see #doGet(HttpServletRequest, HttpServletResponse)
167      */

168     protected void doPost(HttpServletRequest JavaDoc request,
169         HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
170         doGet(request, response);
171     }
172
173     private String JavaDoc getContentType(String JavaDoc s) {
174         if (s == null) {
175             return "text/html";
176         }
177         int i;
178         if ((i = s.lastIndexOf(".")) <= 0) {
179             return "text/plain";
180         }
181         if (i == (s.length() - 1)) {
182             return "text/html";
183         }
184         String JavaDoc s1 = s.substring(i + 1)
185                      .toUpperCase();
186         if (s1.equals("GIF")) {
187             return "image/gif";
188         }
189         if (s1.equals("PNG")) {
190             return "image/png";
191         }
192         if (s1.equals("TIF") || s1.equals("TIFF")) {
193             return "image/tiff";
194         }
195         if (s1.equals("JPG") || s1.equals("JPEG") || s1.equals("JPE")) {
196             return "image/jpeg";
197         }
198         if (s1.equals("TXT") || s1.equals("JAVA") || s1.equals("C")
199             || s1.equals("CPP") || s1.startsWith("HTM") || s1.equals("SHTML")
200             || s1.equals("JHTML") || s1.equals("JSP") || s1.equals("PHP")
201             || s1.equals("BAT")) {
202             return "text/plain";
203         }
204         if (s1.equals("AU") || s1.equals("SND")) {
205             return "audio/basic";
206         }
207         if (s1.equals("AVI")) {
208             return "video/avi";
209         }
210         if (s1.equals("DVI")) {
211             return "application/x-dvi";
212         }
213         if (s1.equals("GZ")) {
214             return "application/x-gzip";
215         }
216         if (s1.equals("PS") || s1.equals("EPS") || s1.equals("AI")) {
217             return "application/postscript";
218         }
219         if (s1.equals("PDF")) {
220             return "application/pdf";
221         }
222         if (s1.equals("MAN")) {
223             return "application/x-troff-man";
224         }
225         if (s1.equals("MP3")) {
226             return "audio/mpeg";
227         }
228         if (s1.equals("MPEG") || s1.equals("MPE") || s1.equals("MPG")
229             || s1.equals("MPA")) {
230             return "video/mpeg";
231         }
232         if (s1.equals("MID") || s1.equals("MIDI")) {
233             return "audio/mid";
234         }
235         if (s1.equals("MTS")) {
236             return "application/metastream";
237         }
238         if (s1.equals("MOV") || s1.equals("QT")) {
239             return "video/quicktime";
240         }
241         if (s1.equals("PPT")) {
242             return "application/powerpoint";
243         }
244         if (s1.equals("RPM")) {
245             return "audio/x-pn-realaudio-plugin";
246         }
247         if (s1.equals("RTX")) {
248             return "text/richtext";
249         }
250         if (s1.equals("RAM") || s1.startsWith("RA")) {
251             return "audio/x-pn-realaudio";
252         }
253         if (s1.equals("SVF")) {
254             return "application/x-shockwave-flash";
255         }
256         if (s1.equals("TEX")) {
257             return "application/x-tex";
258         }
259         if (s1.equals("T") || s1.equals("TR") || s1.equals("ROFF")) {
260             return "application/x-troff";
261         }
262         if (s1.equals("TCL")) {
263             return "application/x-tcl";
264         }
265         if (s1.equals("WAV")) {
266             return "audio/wav";
267         }
268         if (s1.equals("WRL")) {
269             return "x-world/x-vrml";
270         }
271         if (s1.equals("DOC") || s1.equals("RTF") || s1.equals("DOT")) {
272             return "application/vnd.ms-word";
273         }
274         if (s1.equals("XLS") || s1.equals("XLM") || s1.equals("XLT")) {
275             return "application/vnd.ms-excel";
276         }
277         if (s1.equals("XML") || s1.equals("XSL")) {
278             return "text/xml";
279         }
280         if (s1.equals("Z")) {
281             return "application/x-compress";
282         }
283         if (s1.equals("ZIP")) {
284             return "application/zip";
285         } else {
286             return "application/octet-stream";
287         }
288     }
289 }
290
Popular Tags