1 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 ; 28 import java.io.InputStream ; 29 import java.io.ByteArrayOutputStream ; 30 31 import java.util.Collections ; 32 import java.util.Map ; 33 34 import javax.servlet.ServletConfig ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.http.HttpServlet ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 57 public class ImageServlet extends HttpServlet { 58 59 61 private Map contentCache; 62 63 private CoefficientInterceptor interceptor; 64 65 67 72 public void destroy() { 73 } 74 75 86 public void init(ServletConfig cfg) throws ServletException { 87 super.init(cfg); 89 contentCache = Collections.synchronizedMap(new LRUMap(100)); 90 } 91 92 103 protected void doGet(HttpServletRequest request, 104 HttpServletResponse response) throws ServletException , IOException { 105 String imageURL = request.getParameter("image"); 106 InputStream is = null; 107 UploadedFile uf = null; 108 if (imageURL != null) { 109 uf = (UploadedFile)contentCache.get(imageURL); 111 if(uf == null) { 112 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 113 try { 114 String 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 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 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 168 protected void doPost(HttpServletRequest request, 169 HttpServletResponse response) throws ServletException , IOException { 170 doGet(request, response); 171 } 172 173 private String getContentType(String 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 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 |