KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > dispatcher > ImageDispatcher


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

19
20 package org.efs.openreports.dispatcher;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import javax.servlet.ServletConfig JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.ServletOutputStream JavaDoc;
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.commons.io.FileUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.log4j.Logger;
35 import org.efs.openreports.providers.DirectoryProvider;
36 import org.efs.openreports.providers.DirectoryProviderAware;
37 import org.efs.openreports.services.ReportService;
38 import org.efs.openreports.util.ORComponentManager;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.web.context.support.WebApplicationContextUtils;
41
42
43
44 /**
45  * Basic servlet for image delivery. Parses image file name from RequestURI
46  * and tries to load image from the image directory or image temp directory.
47  *
48  * @author Erik Swenson
49  */

50
51 public class ImageDispatcher extends HttpServlet JavaDoc implements DirectoryProviderAware
52 {
53     private static Logger log = Logger.getLogger(ImageDispatcher.class.getName());
54     
55     private DirectoryProvider directoryProvider;
56     
57     private String JavaDoc imageDirectory;
58     private String JavaDoc imageTempDirectory;
59     
60     public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
61     {
62         ORComponentManager.initializeObject(this);
63         
64         //If not using WebWork IOC try Spring...
65
if (directoryProvider == null)
66         {
67             ApplicationContext appContext = WebApplicationContextUtils
68                 .getRequiredWebApplicationContext(servletConfig.getServletContext());
69     
70             directoryProvider = (DirectoryProvider) appContext.getBean("directoryProvider", ReportService.class);
71         }
72         
73         imageDirectory = directoryProvider.getReportImageDirectory();
74         imageTempDirectory = directoryProvider.getReportImageTempDirectory();
75
76         super.init(servletConfig);
77
78         log.info("Started...");
79     }
80
81     protected void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
82             throws ServletException JavaDoc, IOException JavaDoc
83     {
84         String JavaDoc fileName = "";
85         
86         try
87         {
88             fileName = StringUtils.substringAfterLast(request.getRequestURI(), "/");
89
90             File JavaDoc file = new File JavaDoc(imageDirectory + fileName);
91             if (!file.exists())
92             {
93                 file = new File JavaDoc(imageTempDirectory + fileName);
94             }
95
96             byte[] imageData = FileUtils.readFileToByteArray(file);
97
98             response.setContentLength(imageData.length);
99             ServletOutputStream JavaDoc ouputStream = response.getOutputStream();
100
101             ouputStream.write(imageData, 0, imageData.length);
102             ouputStream.flush();
103             ouputStream.close();
104         }
105         catch (Exception JavaDoc e)
106         {
107             String JavaDoc message = "Image Not Found: " + fileName;
108             response.getOutputStream().write(message.getBytes());
109         }
110     }
111
112     public void setDirectoryProvider(DirectoryProvider directoryProvider)
113     {
114         this.directoryProvider = directoryProvider;
115     }
116 }
117
Popular Tags