1 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 ; 20 import java.io.InputStream ; 21 import java.util.zip.GZIPOutputStream ; 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 ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.commons.lang.StringUtils; 33 import org.apache.log4j.Logger; 34 35 36 42 public class ResourceDispatcher extends HttpServlet { 43 44 47 private static final long serialVersionUID = 222L; 48 49 52 private static Logger log = Logger.getLogger(ResourceDispatcher.class); 53 54 59 public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { 60 handleResourceRequest(req, res); 61 } 62 63 69 private void handleResourceRequest(HttpServletRequest req, HttpServletResponse res) throws IOException { 70 71 String resourceHandle = (String ) req.getAttribute(Aggregator.HANDLE); 72 if (log.isDebugEnabled()) { 73 log.debug("handleResourceRequest, resourceHandle=\"" + resourceHandle + "\""); } 75 if (StringUtils.isNotEmpty(resourceHandle)) { 76 try { 77 HierarchyManager hm = (HierarchyManager) req.getAttribute(Aggregator.HIERARCHY_MANAGER); 78 InputStream is = getNodedataAstream(resourceHandle, hm, res); 79 if (is != null) { 80 sendUnCompressed(is, res); 82 is.close(); 83 return; 84 } 85 } 86 catch (IOException e) { 87 log.debug("Exception while dispatching resource " + e.getClass().getName() + ": " + e.getMessage(), e); } 91 catch (Exception e) { 92 log.error("Exception while dispatching resource " + e.getClass().getName() + ": " + e.getMessage(), e); } 94 } 95 if (log.isDebugEnabled()) { 96 log.debug("Resource not found, redirecting request for [" + req.getRequestURI() + "] to 404 URI"); } 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"); } 105 106 } 107 108 113 private boolean canCompress(HttpServletRequest request) { 114 String encoding = request.getHeader("Accept-Encoding"); if (encoding != null) { 116 return (encoding.toLowerCase().indexOf("gzip") > -1); } 118 return false; 119 } 120 121 127 private void sendCompressed(InputStream is, HttpServletResponse res) throws IOException { 128 res.setHeader("Content-Encoding", "gzip"); GZIPOutputStream gzos = new GZIPOutputStream (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 148 private void sendUnCompressed(InputStream is, HttpServletResponse res) throws IOException { 149 ServletOutputStream 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 165 private InputStream getNodedataAstream(String path, HierarchyManager hm, HttpServletResponse res) { 166 if (log.isDebugEnabled()) { 167 log.debug("getAtomAsStream for path \"" + path + "\""); } 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"); int sizeInBytes = (new Long (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 + "]"); 186 } 187 catch (PathNotFoundException e) { 188 log.warn("Resource not found: [" + path + "]"); } 190 catch (RepositoryException e) { 191 log.error("RepositoryException while reading Resource [" + path + "]", e); } 193 return null; 194 } 195 } 196 | Popular Tags |