KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > servlets > Spool


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.servlets;
14
15 import info.magnolia.cms.core.Path;
16
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletOutputStream JavaDoc;
22 import javax.servlet.http.HttpServlet JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.apache.log4j.Logger;
27
28
29 /**
30  * Use this servlet to spool static resources from the servlet context.
31  * @author Sameer Charles
32  * @version 2.1
33  */

34 public class Spool extends HttpServlet JavaDoc {
35
36     /**
37      * Stable serialVersionUID.
38      */

39     private static final long serialVersionUID = 222L;
40
41     /**
42      * Logger.
43      */

44     private static Logger log = Logger.getLogger(Spool.class);
45
46     /**
47      * This makes browser and proxy caches work more effectively, reducing the load on server and network resources.
48      * @param request HttpServletRequest
49      * @return last modified time in miliseconds since 1st Jan 1970 GMT
50      */

51     public long getLastModified(HttpServletRequest JavaDoc request) {
52         File JavaDoc resource = new File JavaDoc(getServletContext().getRealPath(Path.getURI(request)));
53         if (resource.exists()) {
54             return resource.lastModified();
55         }
56         return -1;
57     }
58
59     /**
60      * All static resource requests are handled here.
61      * @param request HttpServletRequest
62      * @param response HttpServletResponse
63      * @throws IOException for error in accessing the resource or the servlet output stream
64      */

65     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
66         File JavaDoc resource = new File JavaDoc(getServletContext().getRealPath(Path.getURI(request)));
67         if (!resource.exists() || resource.isDirectory()) {
68             response.sendError(HttpServletResponse.SC_NOT_FOUND);
69             return;
70         }
71         this.setResponseHeaders(resource, response);
72         boolean success = this.spool(resource, response);
73         if (!success && !response.isCommitted()) {
74             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
75         }
76     }
77
78     /**
79      * @param resource File to be returned to the client
80      * @param response HttpServletResponse
81      * @return <code>true</code> if the file has been written to the servlet output stream without errors
82      * @throws IOException for error in accessing the resource or the servlet output stream
83      */

84     private boolean spool(File JavaDoc resource, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
85         FileInputStream JavaDoc in = new FileInputStream JavaDoc(resource);
86
87         try {
88             ServletOutputStream JavaDoc os = response.getOutputStream();
89             byte[] buffer = new byte[8192];
90             int read = 0;
91             while ((read = in.read(buffer)) > 0) {
92                 os.write(buffer, 0, read);
93             }
94             os.flush();
95             os.close();
96         }
97         catch (IOException JavaDoc e) {
98             // only log at debug level, tomcat usually throws a ClientAbortException anytime the user stop loading the
99
// page
100
log.debug("Unable to spool resource due to a " + e.getClass().getName() + " exception", e); //$NON-NLS-1$ //$NON-NLS-2$
101
return false;
102         }
103         finally {
104             try {
105                 in.close();
106             }
107             catch (Exception JavaDoc e) {
108                 // ignore
109
}
110         }
111         return true;
112     }
113
114     /**
115      * Set content length. content type is set by the filters as defined in server/MIMEMappings.
116      * @param resource File to be returned to the client
117      * @param response HttpServletResponse
118      */

119     private void setResponseHeaders(File JavaDoc resource, HttpServletResponse JavaDoc response) {
120         response.setContentLength((int) resource.length());
121     }
122
123 }
124
Popular Tags