Return-Path: Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 3094 invoked by uid 1041); 13 Jan 2001 13:39:19 -0000 Date: 13 Jan 2001 13:39:19 -0000 Message-ID: <20010113133919.3093.qmail@apache.org> From: giacomo@apache.org To: xml-cocoon-cvs@apache.org Subject: cvs commit: xml-cocoon/src/org/apache/cocoon/reading ResourceReader.java giacomo 01/01/13 05:39:19 Modified: src/org/apache/cocoon/reading Tag: xml-cocoon2 ResourceReader.java Log: The ResourceReader conforms now much more to the HTTP protocol concerning the caching headers like if-modified-since. This will increase performance of this Component Revision Changes Path No revision No revision 1.1.2.11 +34 -9 xml-cocoon/src/org/apache/cocoon/reading/Attic/ResourceReader.java Index: ResourceReader.java =================================================================== RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/reading/Attic/ResourceReader.java,v retrieving revision 1.1.2.10 retrieving revision 1.1.2.11 diff -u -r1.1.2.10 -r1.1.2.11 --- ResourceReader.java 2001/01/12 09:53:09 1.1.2.10 +++ ResourceReader.java 2001/01/13 13:39:19 1.1.2.11 @@ -15,9 +15,11 @@ import java.net.URL; import java.net.MalformedURLException; import java.net.URLConnection; +import java.util.Enumeration; import java.util.Hashtable; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletRequest; import javax.servlet.ServletContext; import org.apache.cocoon.Cocoon; @@ -28,17 +30,19 @@ /** * * @author Giacomo Pati - * @version CVS $Revision: 1.1.2.10 $ $Date: 2001/01/12 09:53:09 $ + * @version CVS $Revision: 1.1.2.11 $ $Date: 2001/01/13 13:39:19 $ * * The ResourceReader component is used to serve binary data - * in a sitemap pipeline. + * in a sitemap pipeline. It makes use of HTTP Headers to determine if + * the requested resource should be written to the OutputStream + * or if it can signal that it hasn't changed. * * Parameters: *
*
<expires>
*
This parameter is optional. When specified it determines how long - * in miliseconds the resources can be cached by any proxy between - * Cocoon2 and the requesting browser. + * in miliseconds the resources can be cached by any proxy or browser + * between Cocoon2 and the requesting visitor. *
*
*/ @@ -48,32 +52,40 @@ * Generates the requested resource. */ public void generate() throws IOException, ProcessingException { + HttpServletRequest req = (HttpServletRequest) objectModel.get(Cocoon.REQUEST_OBJECT); HttpServletResponse res = (HttpServletResponse) objectModel.get(Cocoon.RESPONSE_OBJECT); + if (res == null) { throw new ProcessingException ("Missing a Response object in the objectModel"); } + if (req == null) { + throw new ProcessingException ("Missing a Request object in the objectModel"); + } String src = null; File file = null; URL url = null; URLConnection conn = null; InputStream is = null; long len = 0; - long lastModified = 0; try { - if(this.source.indexOf(":/")!=-1) { + if(this.source.indexOf(":/") != -1) { src = this.source; url = new URL (src); conn = url.openConnection(); + if (!modified (conn.getLastModified(), req, res)) { + return; + } len = conn.getContentLength(); is = conn.getInputStream(); - lastModified = conn.getLastModified(); } else { src = this.resolver.resolveEntity (null,this.source).getSystemId(); url = new URL (src); file = new File (url.getFile()); + if (!modified (file.lastModified(), req, res)) { + return; + } len = file.length(); is = new FileInputStream (file); - lastModified = file.lastModified(); } } catch (SAXException se) { log.error("ResourceReader: error resolving source \"" + source + "\"", se); @@ -88,7 +100,6 @@ is.read(buffer); is.close(); res.setContentLength(buffer.length); - res.setDateHeader("Last-Modified", lastModified); long expires = parameters.getParameterAsInteger("expires", -1); if (expires > 0) { res.setDateHeader("Expires", System.currentTimeMillis()+expires); @@ -96,6 +107,20 @@ res.setHeader("Accept-Ranges", "bytes"); out.write ( buffer ); } + + /** + * Checks if the file has been modified + */ + private boolean modified (long lastModified, HttpServletRequest req, HttpServletResponse res) { + res.setDateHeader("Last-Modified", lastModified); + long if_modified_since = req.getDateHeader("if-modified-since"); + if (if_modified_since >= lastModified) { + res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + } + log.debug("ResourceReader: resource has " + ((if_modified_since < lastModified) ? "" : "not ") + "been modified"); + return (if_modified_since < lastModified); + } + /** * Returns the mime-type of the resource in process. */