You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2008/06/08 19:36:49 UTC

svn commit: r664531 - in /cocoon/whiteboard/corona/trunk/corona-servlet/src/main: java/org/apache/cocoon/corona/servlet/ java/org/apache/cocoon/corona/servlet/node/ resources/META-INF/cocoon/spring/

Author: reinhard
Date: Sun Jun  8 10:36:49 2008
New Revision: 664531

URL: http://svn.apache.org/viewvc?rev=664531&view=rev
Log:
. the SitemapServlet sets the the Last-Modified header
. the SitemapServlet supports conditional GET operations
. improve logging and exception handling of SitemapServlet
. remove Corona specific ServletURLConnection 

Added:
    cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/LastModifiedCollector.java   (with props)
Removed:
    cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/ServletURLConnection.java
    cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/ServletURLStreamHandler.java
    cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/ServletURLStreamHandlerFactory.java
Modified:
    cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java
    cocoon/whiteboard/corona/trunk/corona-servlet/src/main/resources/META-INF/cocoon/spring/corona-servlet-node.xml

Modified: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java?rev=664531&r1=664530&r2=664531&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java (original)
+++ cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java Sun Jun  8 10:36:49 2008
@@ -33,6 +33,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.cocoon.corona.servlet.node.LastModifiedCollector;
 import org.apache.cocoon.corona.servlet.node.MimeTypeCollector;
 import org.apache.cocoon.corona.servlet.node.StatusCodeCollector;
 import org.apache.cocoon.corona.servlet.util.HttpContextHelper;
@@ -84,29 +85,64 @@
         long start = System.nanoTime();
         this.logRequest(request);
 
+        long ifLastModifiedSince = request.getDateHeader("If-Modified-Since");
+
         this.lazyInitialize(this.servletConfig);
 
         try {
             SitemapDelegator.setSitemapServlet(this);
             MimeTypeCollector.clearMimeType();
             StatusCodeCollector.clearStatusCode();
+            LastModifiedCollector.clearLastModified();
 
             // assemble parameters
             Map<String, Object> parameters = this.getInvocationParameters(request);
             HttpContextHelper.storeRequest(request, parameters);
             HttpContextHelper.storeResponse(response, parameters);
 
+            // invoke the sitemap engine
             ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
             this.invoke(this.calcSitemapRequestURI(request), parameters, baos);
 
-            response.setContentType(MimeTypeCollector.getMimeType());
-            response.setContentLength(baos.size());
+            // collect meta information from the previous run of the sitemap
+            long lastModified = LastModifiedCollector.getLastModified();
+            String mimeType = MimeTypeCollector.getMimeType();
+            int contentLengh = baos.size();
+            int statusCode = StatusCodeCollector.getStatusCode();
+
+            // send the Last-Modified header in every case
+            if (lastModified > -1) {
+                response.setDateHeader("Last-Modified", lastModified);
+            }
+
+            // conditional request support
+            if (ifLastModifiedSince > 0 && ifLastModifiedSince / 1000 >= lastModified / 1000) {
+                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+                if (this.logger.isInfoEnabled()) {
+                    this.logger.info("The requested resource " + request.getRequestURI()
+                            + " hasn't changed: ifLastModifiedSince=" + ifLastModifiedSince + ", lastModified="
+                            + lastModified + ". Hence 304 (NOT_MODIFIED) was sent as status code.");
+                }
+
+                return;
+            }
+
+            // write the sitemap result to the output stream
+            response.setContentType(mimeType);
+            response.setContentLength(contentLengh);
+            response.setStatus(statusCode);
+            if (this.logger.isInfoEnabled()) {
+                this.logger.info("Going to send response: mimeType=" + mimeType + ", contentLength=" + contentLengh
+                        + ", statusCode=" + statusCode + ", lastModified=" + lastModified);
+            }
+
             response.getOutputStream().write(baos.toByteArray());
-            response.setStatus(StatusCodeCollector.getStatusCode());
         } catch (Exception e) {
             String msg = "An exception occurred while executing the sitemap.";
             this.logger.error(msg, e);
-            throw new ServletException(msg, e);
+            ServletException servletException = new ServletException(msg, e);
+            servletException.initCause(e);
+            throw servletException;
         } finally {
             SitemapDelegator.removeSitemapServlet();
             this.logger.info("Sitemap execution took " + (System.nanoTime() - start) / 1000000f + " ms.");
@@ -141,7 +177,9 @@
         } catch (MalformedURLException e) {
             String msg = "An exception occurred while retrieving the base URL from the servlet context.";
             this.logger.error(msg, e);
-            throw new ServletException(msg, e);
+            ServletException servletException = new ServletException(msg, e);
+            servletException.initCause(e);
+            throw servletException;
         }
     }
 
@@ -161,7 +199,9 @@
             } catch (Exception e) {
                 String msg = "An exception occurred while building the sitemap.";
                 this.logger.error(msg, e);
-                throw new ServletException(msg, e);
+                ServletException servletException = new ServletException(msg, e);
+                servletException.initCause(e);
+                throw servletException;
             }
             this.initialized = true;
         }

Added: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/LastModifiedCollector.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/LastModifiedCollector.java?rev=664531&view=auto
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/LastModifiedCollector.java (added)
+++ cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/LastModifiedCollector.java Sun Jun  8 10:36:49 2008
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.corona.servlet.node;
+
+import org.apache.cocoon.corona.pipeline.Pipeline;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+
+@Aspect
+public class LastModifiedCollector {
+
+    private static final ThreadLocal<Long> THREAD_LOCAL = new ThreadLocal<Long>();
+
+    public static long getLastModified() {
+        return THREAD_LOCAL.get();
+    }
+
+    public static void clearLastModified() {
+        THREAD_LOCAL.set(null);
+    }
+
+    @Around("execution(* org.apache.cocoon.corona.pipeline.Pipeline.execute(..))")
+    public Object interceptInvoke(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
+        Object result = proceedingJoinPoint.proceed();
+
+        Pipeline pipeline = (Pipeline) proceedingJoinPoint.getTarget();
+        long lastModified = pipeline.getLastModified();
+        THREAD_LOCAL.set(lastModified);
+
+        return result;
+    }
+}

Propchange: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/LastModifiedCollector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/LastModifiedCollector.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/LastModifiedCollector.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/resources/META-INF/cocoon/spring/corona-servlet-node.xml
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-servlet/src/main/resources/META-INF/cocoon/spring/corona-servlet-node.xml?rev=664531&r1=664530&r2=664531&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-servlet/src/main/resources/META-INF/cocoon/spring/corona-servlet-node.xml (original)
+++ cocoon/whiteboard/corona/trunk/corona-servlet/src/main/resources/META-INF/cocoon/spring/corona-servlet-node.xml Sun Jun  8 10:36:49 2008
@@ -29,5 +29,7 @@
   <bean id="org.apache.cocoon.corona.servlet.node.StatusCodeCollector" class="org.apache.cocoon.corona.servlet.node.StatusCodeCollector" />
 
   <bean id="org.apache.cocoon.corona.servlet.node.MimeTypeCollector" class="org.apache.cocoon.corona.servlet.node.MimeTypeCollector" />
+  
+  <bean id="org.apache.cocoon.corona.servlet.node.LastModifiedCollector" class="org.apache.cocoon.corona.servlet.node.LastModifiedCollector" />
 
 </beans>