KickJava   Java API By Example, From Geeks To Geeks.

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


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.Aggregator;
16 import info.magnolia.cms.core.HierarchyManager;
17 import info.magnolia.cms.core.NodeData;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.zip.GZIPOutputStream JavaDoc;
22
23 import javax.jcr.PathNotFoundException;
24 import javax.jcr.PropertyType;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.Value;
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.lang.StringUtils;
33 import org.apache.log4j.Logger;
34
35
36 /**
37  * Class ResourceDispatcher is responsible to gather data from the <strong>HttpServletRequest </strong> and write back
38  * the requested resource on the <strong>ServletOutputStream </strong>.
39  * @author Sameer Charles
40  * @version 1.0
41  */

42 public class ResourceDispatcher extends HttpServlet JavaDoc {
43
44     /**
45      * Stable serialVersionUID.
46      */

47     private static final long serialVersionUID = 222L;
48
49     /**
50      * Logger.
51      */

52     private static Logger log = Logger.getLogger(ResourceDispatcher.class);
53
54     /**
55      * @param req HttpServletRequest as given by the servlet container
56      * @param res HttpServletResponse as given by the servlet container
57      * @throws IOException standard servlet exception
58      */

59     public void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws IOException JavaDoc {
60         handleResourceRequest(req, res);
61     }
62
63     /**
64      * Get the requested resource and copy it to the ServletOutputStream, bit by bit.
65      * @param req HttpServletRequest as given by the servlet container
66      * @param res HttpServletResponse as given by the servlet container
67      * @throws IOException standard servlet exception
68      */

69     private void handleResourceRequest(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws IOException JavaDoc {
70
71         String JavaDoc resourceHandle = (String JavaDoc) req.getAttribute(Aggregator.HANDLE);
72         if (log.isDebugEnabled()) {
73             log.debug("handleResourceRequest, resourceHandle=\"" + resourceHandle + "\""); //$NON-NLS-1$ //$NON-NLS-2$
74
}
75         if (StringUtils.isNotEmpty(resourceHandle)) {
76             try {
77                 HierarchyManager hm = (HierarchyManager) req.getAttribute(Aggregator.HIERARCHY_MANAGER);
78                 InputStream JavaDoc is = getNodedataAstream(resourceHandle, hm, res);
79                 if (is != null) {
80                     // todo always send as is, find better way to discover if resource could be compressed
81
sendUnCompressed(is, res);
82                     is.close();
83                     return;
84                 }
85             }
86             catch (IOException JavaDoc e) {
87                 // don't log at error level since tomcat tipically throws a
88
// org.apache.catalina.connector.ClientAbortException if the user stops loading the page
89
log.debug("Exception while dispatching resource " + e.getClass().getName() + ": " + e.getMessage(), e); //$NON-NLS-1$ //$NON-NLS-2$
90
}
91             catch (Exception JavaDoc e) {
92                 log.error("Exception while dispatching resource " + e.getClass().getName() + ": " + e.getMessage(), e); //$NON-NLS-1$ //$NON-NLS-2$
93
}
94         }
95         if (log.isDebugEnabled()) {
96             log.debug("Resource not found, redirecting request for [" + req.getRequestURI() + "] to 404 URI"); //$NON-NLS-1$
97
}
98
99         if (!res.isCommitted()) {
100             res.sendError(HttpServletResponse.SC_NOT_FOUND);
101         }
102         else {
103             log.info("Unable to redirect to 404 page, response is already committed"); //$NON-NLS-1$
104
}
105
106     }
107
108     /**
109      * Returns true if the request sender accepts GZIP compressed data.
110      * @param request HttpServletRequest
111      * @return <code>true</code> if the client accepts gzip encoding
112      */

113     private boolean canCompress(HttpServletRequest JavaDoc request) {
114         String JavaDoc encoding = request.getHeader("Accept-Encoding"); //$NON-NLS-1$
115
if (encoding != null) {
116             return (encoding.toLowerCase().indexOf("gzip") > -1); //$NON-NLS-1$
117
}
118         return false;
119     }
120
121     /**
122      * Send data as GZIP output stream ;)
123      * @param is Input stream for the resource
124      * @param res HttpServletResponse as received by the service method
125      * @throws IOException standard servlet exception
126      */

127     private void sendCompressed(InputStream JavaDoc is, HttpServletResponse JavaDoc res) throws IOException JavaDoc {
128         res.setHeader("Content-Encoding", "gzip"); //$NON-NLS-1$ //$NON-NLS-2$
129
GZIPOutputStream JavaDoc gzos = new GZIPOutputStream JavaDoc(res.getOutputStream());
130         try {
131             int bit;
132             while ((bit = is.read()) != -1) {
133                 gzos.write(bit);
134             }
135             gzos.flush();
136         }
137         finally {
138             gzos.close();
139         }
140     }
141
142     /**
143      * Send data as is.
144      * @param is Input stream for the resource
145      * @param res HttpServletResponse as received by the service method
146      * @throws IOException standard servlet exception
147      */

148     private void sendUnCompressed(InputStream JavaDoc is, HttpServletResponse JavaDoc res) throws IOException JavaDoc {
149         ServletOutputStream JavaDoc os = res.getOutputStream();
150         byte[] buffer = new byte[8192];
151         int read = 0;
152         while ((read = is.read(buffer)) > 0) {
153             os.write(buffer, 0, read);
154         }
155         os.flush();
156         os.close();
157     }
158
159     /**
160      * @param path path for nodedata in jcr repository
161      * @param hm Hierarchy manager
162      * @param res HttpServletResponse
163      * @return InputStream or <code>null</code> if nodeData is not found
164      */

165     private InputStream JavaDoc getNodedataAstream(String JavaDoc path, HierarchyManager hm, HttpServletResponse JavaDoc res) {
166         if (log.isDebugEnabled()) {
167             log.debug("getAtomAsStream for path \"" + path + "\""); //$NON-NLS-1$ //$NON-NLS-2$
168
}
169         try {
170             NodeData atom = hm.getNodeData(path);
171             if (atom != null) {
172                 if (atom.getType() == PropertyType.BINARY) {
173                     NodeData size = hm.getNodeData(path + "_properties/size"); //$NON-NLS-1$
174
int sizeInBytes = (new Long JavaDoc(size.getLong())).intValue();
175                     res.setContentLength(sizeInBytes);
176                 }
177
178                 Value value = atom.getValue();
179                 if (value != null) {
180                     return value.getStream();
181                 }
182             }
183
184             log.warn("Resource not found: [" + path + "]"); //$NON-NLS-1$ //$NON-NLS-2$
185

186         }
187         catch (PathNotFoundException e) {
188             log.warn("Resource not found: [" + path + "]"); //$NON-NLS-1$ //$NON-NLS-2$
189
}
190         catch (RepositoryException e) {
191             log.error("RepositoryException while reading Resource [" + path + "]", e); //$NON-NLS-1$ //$NON-NLS-2$
192
}
193         return null;
194     }
195 }
196
Popular Tags