1 16 package org.outerj.daisy.frontend.components.reading; 17 18 import org.apache.cocoon.reading.Reader; 19 import org.apache.cocoon.ProcessingException; 20 import org.apache.cocoon.caching.Cache; 21 import org.apache.cocoon.caching.CachedResponse; 22 import org.apache.cocoon.environment.SourceResolver; 23 import org.apache.cocoon.environment.Request; 24 import org.apache.cocoon.environment.ObjectModelHelper; 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.avalon.framework.parameters.ParameterException; 27 import org.apache.avalon.framework.service.Serviceable; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.service.ServiceException; 30 import org.apache.excalibur.source.Source; 31 import org.apache.excalibur.source.SourceValidity; 32 import org.apache.excalibur.source.SourceParameters; 33 import org.apache.excalibur.source.impl.validity.ExpiresValidity; 34 import org.xml.sax.SAXException ; 35 36 import java.io.IOException ; 37 import java.io.OutputStream ; 38 import java.io.InputStream ; 39 import java.io.ByteArrayOutputStream ; 40 import java.util.Map ; 41 42 46 public class CachingReader implements Reader, Serviceable { 47 private ServiceManager serviceManager; 48 private MyCachedResponse cachedResponse; 49 private OutputStream outputStream; 50 51 public void service(ServiceManager serviceManager) throws ServiceException { 52 this.serviceManager = serviceManager; 53 } 54 55 public void generate() throws IOException , SAXException , ProcessingException { 56 outputStream.write(cachedResponse.getResponse()); 57 outputStream.flush(); 58 } 59 60 public long getLastModified() { 61 return System.currentTimeMillis(); 62 } 63 64 public void setup(SourceResolver sourceResolver, Map objectModel, String src, Parameters parameters) throws ProcessingException, SAXException , IOException { 65 Request request = ObjectModelHelper.getRequest(objectModel); 66 SourceParameters sourceParameters = new SourceParameters(request.getQueryString()); 67 long expires; 68 try { 69 expires = parameters.getParameterAsLong("expires") * 1000; 70 } catch (ParameterException e) { 71 throw new ProcessingException(e); 72 } 73 74 String cacheKey = "----DAISY-CACHING-READER----" + request.getRequestURI() + "?" + sourceParameters.getQueryString() + "~~" + src; 76 77 Cache cache; 78 try { 79 cache = (Cache) this.serviceManager.lookup(Cache.ROLE); 80 } catch (ServiceException e) { 81 throw new ProcessingException(e); 82 } 83 try { 84 cachedResponse = (MyCachedResponse)cache.get(cacheKey); 85 86 if (cachedResponse != null) { 87 SourceValidity[] validities = cachedResponse.getValidityObjects(); 88 ExpiresValidity validity = (ExpiresValidity)validities[0]; 89 if (validity.isValid() != SourceValidity.VALID) 90 cachedResponse = null; 91 } 92 93 if (cachedResponse == null) { 94 ExpiresValidity validity = new ExpiresValidity(expires); 95 Source source = sourceResolver.resolveURI(src); 96 byte[] data = readSource(source); 97 cachedResponse = new MyCachedResponse(new SourceValidity[] { validity }, data, source.getMimeType()); 98 cache.store(cacheKey, cachedResponse); 99 } 100 } finally { 101 serviceManager.release(cache); 102 } 103 } 104 105 private byte[] readSource(Source source) throws IOException { 106 ByteArrayOutputStream os = new ByteArrayOutputStream (); 107 byte[] buffer = new byte[4096]; 108 InputStream inputStream = source.getInputStream(); 109 int length; 110 while ((length = inputStream.read(buffer)) > -1) { 111 os.write(buffer, 0, length); 112 } 113 inputStream.close(); 114 return os.toByteArray(); 115 } 116 117 118 public void setOutputStream(OutputStream outputStream) throws IOException { 119 this.outputStream = outputStream; 120 } 121 122 public String getMimeType() { 123 return cachedResponse.getMimeType(); 124 } 125 126 public boolean shouldSetContentLength() { 127 return false; 128 } 129 130 static class MyCachedResponse extends CachedResponse { 131 private String mimeType; 132 public MyCachedResponse(SourceValidity[] sourceValidities, byte[] bytes, String mimeType) { 133 super(sourceValidities, bytes); 134 this.mimeType = mimeType; 135 } 136 137 public String getMimeType() { 138 return mimeType; 139 } 140 } 141 } 142 | Popular Tags |