1 20 package org.enhydra.barracuda.core.helper.servlet; 21 22 import java.io.*; 23 import java.lang.ref.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 27 import org.apache.log4j.*; 28 29 import org.enhydra.barracuda.plankton.data.*; 30 import org.enhydra.barracuda.plankton.http.*; 31 32 33 37 public class ResourceGateway extends HttpServlet { 38 39 protected static final Logger logger = Logger.getLogger(ResourceGateway.class.getName()); 41 42 public static String EXT_RESOURCE_ID = "xlib"; 45 public static String RESOURCE_NOT_FOUND = "ResourceNotFound"; 46 47 48 57 public void handleDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 58 String resourceName = EXT_RESOURCE_ID + req.getPathInfo(); 62 if (logger.isDebugEnabled()) logger.debug("Looking for static resource: "+resourceName); 63 64 Object resource = ContextServices.getObjectFromCache(this.getServletContext(), resourceName, new LocalReferenceFactory(resourceName)); 66 67 if (resource instanceof LocalResource) { 70 71 resp.setHeader("Cache-Control","public"); 74 resp.setDateHeader("Last-Modified", System.currentTimeMillis()); 75 76 if (logger.isDebugEnabled()) logger.debug("Got resource from cache"); 78 LocalResource lr = (LocalResource)resource; 79 resp.setContentType(lr.contentType); 80 if (logger.isDebugEnabled()) logger.debug("Set content-type: "+lr.contentType); 81 82 OutputStream out = resp.getOutputStream(); 84 out.write(lr.contentData); 85 resp.flushBuffer(); if (logger.isDebugEnabled()) logger.debug("Wrote data: "+lr.contentData.length+" bytes"); 88 89 } else { 91 if (logger.isDebugEnabled()) logger.debug("Resource not found"); 92 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource Not Found: "+resourceName); 93 } 94 } 95 96 99 class LocalReferenceFactory implements ReferenceFactory { 100 String resourceName = null; 101 102 public LocalReferenceFactory(String iresourceName) { 103 resourceName = iresourceName; 104 } 105 106 public Reference getObjectReference() { 107 if (logger.isDebugEnabled()) logger.debug("Resource not in cache...will load from classpath"); 108 109 String contentType = ResourceGateway.this.getServletContext().getMimeType(resourceName); 111 if (contentType==null) contentType = "text/plain"; 112 113 if (logger.isDebugEnabled()) logger.debug("Attempting to read: "+resourceName); 115 116 Object result = null; 117 byte[] dstbytes = new byte[10240]; 118 try { 119 BufferedInputStream in = new BufferedInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName)); 123 int offset = 0; 124 int len = 1024; 125 byte[] inbytes = new byte[len]; 126 while (true) { 127 int cnt = in.read(inbytes, 0, len); 128 if (cnt==-1) break; 129 if (offset+cnt>=dstbytes.length) { 130 byte[] newbytes = new byte[(int) (dstbytes.length+(len*5))]; 131 System.arraycopy(dstbytes, 0, newbytes, 0, dstbytes.length); 132 dstbytes = newbytes; 133 } 134 System.arraycopy(inbytes, 0, dstbytes, offset, cnt); 135 offset+=cnt; 136 } 137 in.close(); 141 142 byte[] newbytes = new byte[offset]; 144 System.arraycopy(dstbytes, 0, newbytes, 0, offset); 145 dstbytes = newbytes; 146 147 if (logger.isDebugEnabled()) logger.debug("Success!"); 149 LocalResource lr = new LocalResource(); 150 lr.contentType = contentType; 151 lr.contentData = dstbytes; 152 result = lr; 153 } catch (IOException e) { 154 if (logger.isDebugEnabled()) logger.debug("Failure: "+e); 155 result = RESOURCE_NOT_FOUND; 156 } 157 return new SoftReference(result); 158 } 159 160 } 161 162 165 class LocalResource { 166 public String contentType = null; 167 public byte[] contentData = null; 168 } 169 170 171 180 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 181 handleDefault(req, resp); 182 } 183 184 192 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 193 handleDefault(req, resp); 194 } 195 196 200 public void init() throws ServletException { 201 logger.info("initializing servlet"); 202 } 203 204 } 205 | Popular Tags |