001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.servlet.pic; 020 021import java.io.FileNotFoundException; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.OutputStream; 025 026import javax.servlet.ServletException; 027import javax.servlet.http.HttpServlet; 028import javax.servlet.http.HttpServletRequest; 029import javax.servlet.http.HttpServletResponse; 030 031import lucee.commons.io.IOUtil; 032 033 034/** 035 * Die Klasse PicServlet wird verwendet um Bilder darzustellen, 036 * alle Bilder die innerhalb des Deployer angezeigt werden, 037 * werden ueber diese Klasse aus der lucee.jar Datei geladen, 038 * das macht die Applikation flexibler 039 * und verlangt nicht das die Bilder fuer die Applikation an einem bestimmten Ort abgelegt sein muessen. 040 */ 041public final class PicServlet extends HttpServlet { 042 043 /** 044 * Verzeichnis in welchem die bilder liegen 045 */ 046 public final static String PIC_SOURCE= "/resource/img/"; 047 048 /** 049 * Interpretiert den Script-Name und laedt das entsprechende Bild aus den internen Resourcen. 050 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 051 */ 052 protected void service(HttpServletRequest req, HttpServletResponse rsp) 053 throws ServletException, IOException { 054 // get out Stream 055 056 // pic 057 String[] arrPath=(req.getServletPath()).split("\\."); 058 String pic=PIC_SOURCE+"404.gif"; 059 if(arrPath.length>=3) { 060 pic=PIC_SOURCE+((arrPath[arrPath.length-3]+"."+arrPath[arrPath.length-2]).replaceFirst("/","")); 061 062 // mime type 063 String mime="image/"+arrPath[arrPath.length-2]; 064 rsp.setContentType(mime); 065 } 066 067 // write data from pic input to response output 068 OutputStream os=null; 069 InputStream is=null; 070 try { 071 os = rsp.getOutputStream(); 072 is = getClass().getResourceAsStream(pic); 073 if(is==null) { 074 is = getClass().getResourceAsStream(PIC_SOURCE+"404.gif"); 075 } 076 077 byte[] buf = new byte[4*1024]; 078 int nread = 0; 079 while ((nread = is.read(buf)) >= 0) { 080 os.write(buf, 0, nread); 081 } 082 } catch (FileNotFoundException e) { 083 } catch (IOException e) { 084 } finally { 085 IOUtil.closeEL(is, os); 086 } 087 } 088 089 090}