cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r649981 - in /cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet: SitemapServlet.java node/MimeTypeCollector.java node/StatusCodeCollector.java
Date Sun, 20 Apr 2008 21:20:54 GMT
Author: reinhard
Date: Sun Apr 20 14:20:53 2008
New Revision: 649981

URL: http://svn.apache.org/viewvc?rev=649981&view=rev
Log:
COCOON-2201
Set additional header fields in the HttpServletResponse in the SitemapServlet. (patch by Steven
Dolg)

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/java/org/apache/cocoon/corona/servlet/node/MimeTypeCollector.java
    cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/StatusCodeCollector.java

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=649981&r1=649980&r2=649981&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 Apr 20 14:20:53 2008
@@ -18,6 +18,7 @@
  */
 package org.apache.cocoon.corona.servlet;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintWriter;
@@ -48,10 +49,9 @@
     private static final long serialVersionUID = 1L;
 
     private BeanFactory beanFactory;
-    private Sitemap sitemap;
     private boolean initialized;
-
     private ServletConfig servletConfig;
+    private Sitemap sitemap;
 
     @Override
     public void init(ServletConfig servletConfig) throws ServletException {
@@ -59,24 +59,9 @@
         super.init(this.servletConfig);
     }
 
-    private void lazyInitialize(ServletConfig servletConfig) throws ServletException {
-        if (!this.initialized) {
-            try {
-                SitemapBuilder sitemapBuilder = (SitemapBuilder) this.beanFactory.getBean(SitemapBuilder.class
-                        .getName());
-                URL url = servletConfig.getServletContext().getResource("/sitemap.xmap");
-                this.sitemap = sitemapBuilder.build(url);
-            } catch (Exception e) {
-                throw new ServletException(e);
-            }
-            this.initialized = true;
-        }
-    }
-
     public void invoke(String requestURI, Map<String, Object> parameters, OutputStream
outputStream) {
         InvocationImpl invocation = (InvocationImpl) this.beanFactory.getBean(Invocation.class.getName());
 
-        System.out.println("Starting invocation for RequestURI " + requestURI);
         invocation.setRequestURI(requestURI);
         invocation.setParameters(parameters);
         invocation.setOutputStream(outputStream);
@@ -90,20 +75,25 @@
 
     @Override
     protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
-        this.lazyInitialize(servletConfig);
+        this.lazyInitialize(this.servletConfig);
+
         try {
             SitemapDelegator.setSitemapServlet(this);
+            MimeTypeCollector.clearMimeType();
+            StatusCodeCollector.clearStatusCode();
 
             // assemble parameters
             Map<String, Object> parameters = this.getInvocationParameters(request);
             HttpContextHelper.storeRequest(request, parameters);
             HttpContextHelper.storeResponse(response, parameters);
 
-            this.invoke(request.getRequestURI(), parameters, response.getOutputStream());
+            ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
+            this.invoke(request.getRequestURI(), parameters, baos);
 
-            response.setStatus(StatusCodeCollector.getStatusCode());
             response.setContentType(MimeTypeCollector.getMimeType());
-            response.setContentType("text/html;charset=UTF-8");
+            response.setContentLength(baos.size());
+            response.getOutputStream().write(baos.toByteArray());
+            response.setStatus(StatusCodeCollector.getStatusCode());
         } catch (Exception e) {
             PrintWriter writer = new PrintWriter(response.getOutputStream());
             e.printStackTrace(writer);
@@ -123,5 +113,18 @@
         }
 
         return invocationParameters;
+    }
+
+    private synchronized void lazyInitialize(ServletConfig servletConfig) throws ServletException
{
+        if (!this.initialized) {
+            try {
+                SitemapBuilder sitemapBuilder = (SitemapBuilder) this.beanFactory.getBean(SitemapBuilder.class.getName());
+                URL url = servletConfig.getServletContext().getResource("/sitemap.xmap");
+                this.sitemap = sitemapBuilder.build(url);
+            } catch (Exception e) {
+                throw new ServletException(e);
+            }
+            this.initialized = true;
+        }
     }
 }

Modified: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/MimeTypeCollector.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/MimeTypeCollector.java?rev=649981&r1=649980&r2=649981&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/MimeTypeCollector.java
(original)
+++ cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/MimeTypeCollector.java
Sun Apr 20 14:20:53 2008
@@ -26,24 +26,24 @@
 
     private static final ThreadLocal<String> THREAD_LOCAL = new ThreadLocal<String>();
 
+    public static void clearMimeType() {
+        THREAD_LOCAL.set(null);
+    }
+
+    public static String getMimeType() {
+        return THREAD_LOCAL.get();
+    }
+
     @Around("execution(* org.apache.cocoon.corona.pipeline.Pipeline.execute(..))")
     public Object interceptInvoke(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
{
-        System.out.println("Executing MimeTypeCollector");
         Object result = proceedingJoinPoint.proceed();
 
         Pipeline pipeline = (Pipeline) proceedingJoinPoint.getTarget();
-        THREAD_LOCAL.set(pipeline.getContentType());
+        String newValue = pipeline.getContentType();
+        if (newValue != null) {
+            THREAD_LOCAL.set(newValue);
+        }
 
         return result;
-    }
-
-    public static String getMimeType() {
-        String mimeType = THREAD_LOCAL.get();
-
-        return mimeType;
-    }
-
-    public static void setMimeType(String mimeType) {
-        THREAD_LOCAL.set(mimeType);
     }
 }

Modified: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/StatusCodeCollector.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/StatusCodeCollector.java?rev=649981&r1=649980&r2=649981&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/StatusCodeCollector.java
(original)
+++ cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/node/StatusCodeCollector.java
Sun Apr 20 14:20:53 2008
@@ -29,24 +29,14 @@
 @Aspect
 public class StatusCodeCollector {
 
-    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
+    private static final ThreadLocal<Integer> THREAD_LOCAL = new ThreadLocal<Integer>();
 
-    @Around("execution(org.apache.cocoon.corona.sitemap.node.InvocationResult org.apache.cocoon.corona.sitemap.node.SerializeNode.invoke(org.apache.cocoon.corona.sitemap.Invocation))")
-    public Object interceptInvoke(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
{
-        SerializeNode target = (SerializeNode) proceedingJoinPoint.getTarget();
-        String statusCode = target.getParameters().get("status-code");
-
-        InvocationResult invocationResult = (InvocationResult) proceedingJoinPoint.proceed();
-
-        if (invocationResult.isProcessed() && statusCode != null) {
-            threadLocal.set(Integer.valueOf(statusCode));
-        }
-
-        return invocationResult;
+    public static void clearStatusCode() {
+        THREAD_LOCAL.set(null);
     }
 
     public static int getStatusCode() {
-        Integer integer = threadLocal.get();
+        Integer integer = THREAD_LOCAL.get();
 
         if (integer == null) {
             return HttpServletResponse.SC_OK;
@@ -56,6 +46,20 @@
     }
 
     public static void setStatusCode(int statusCode) {
-        threadLocal.set(statusCode);
+        THREAD_LOCAL.set(statusCode);
+    }
+
+    @Around("execution(org.apache.cocoon.corona.sitemap.node.InvocationResult org.apache.cocoon.corona.sitemap.node.SerializeNode.invoke(org.apache.cocoon.corona.sitemap.Invocation))")
+    public Object interceptInvoke(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
{
+        SerializeNode target = (SerializeNode) proceedingJoinPoint.getTarget();
+        String statusCode = target.getParameters().get("status-code");
+
+        InvocationResult invocationResult = (InvocationResult) proceedingJoinPoint.proceed();
+
+        if (invocationResult.isProcessed() && statusCode != null) {
+            THREAD_LOCAL.set(Integer.valueOf(statusCode));
+        }
+
+        return invocationResult;
     }
 }



Mime
View raw message