You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by st...@apache.org on 2011/08/17 23:56:37 UTC

svn commit: r1158931 - /cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java

Author: stevendolg
Date: Wed Aug 17 21:56:37 2011
New Revision: 1158931

URL: http://svn.apache.org/viewvc?rev=1158931&view=rev
Log:
reworked class to fix warnings

Modified:
    cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java

Modified: cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java?rev=1158931&r1=1158930&r2=1158931&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java (original)
+++ cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java Wed Aug 17 21:56:37 2011
@@ -14,9 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/**
- *
- */
 package org.apache.cocoon.servlet;
 
 import java.io.ByteArrayOutputStream;
@@ -26,6 +23,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Properties;
 
@@ -52,11 +50,10 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.BeanFactory;
 
 public class RequestProcessor {
-    /**
-     * Logger.
-     */
-    private static final Logger LOG =
-            LoggerFactory.getLogger(RequestProcessor.class);
+
+    private static final int RESPONSE_BUFFER_SIZE = 8192;
+
+    private static final Logger LOG = LoggerFactory.getLogger(RequestProcessor.class);
     
     private URL baseURL;
     private BeanFactory beanFactory;
@@ -69,16 +66,17 @@ public class RequestProcessor {
     public RequestProcessor(ServletContext servletContext, String sitemapPath, BeanFactory beanFactory)
             throws SitemapNotFoundException, InvalidBaseUrlException, SitemapInitializationException {
         if (servletContext == null) {
-            throw new NullPointerException("A 'ServletContext' has to be passed.");
+            throw new IllegalArgumentException("A 'ServletContext' has to be passed.");
         }
         if (beanFactory == null) {
-            throw new NullPointerException("A Spring 'BeanFactory' has to be passed.");
+            throw new IllegalArgumentException("A Spring 'BeanFactory' has to be passed.");
         }
 
         this.servletContext = servletContext;
-        this.sitemapPath = sitemapPath;
         this.beanFactory = beanFactory;
 
+        this.setSitemapPath(sitemapPath);
+
         this.initializeInServletServiceFramework();
         this.initializeBaseURL();
         this.initializeVersionNumber();
@@ -105,8 +103,13 @@ public class RequestProcessor {
             }
         } finally {
             if (LOG.isInfoEnabled()) {
-                LOG.info("Sitemap execution for " + request.getRequestURI() + " took "
-                        + (System.nanoTime() - start) / 1000000f + " ms.");
+                StringBuilder stringBuilder = new StringBuilder();
+                stringBuilder.append("Sitemap execution for ");
+                stringBuilder.append(request.getRequestURI());
+                stringBuilder.append(" took ");
+                stringBuilder.append((System.nanoTime() - start) / 1000000f);
+                stringBuilder.append(" ms.");
+                LOG.info(stringBuilder.toString());
             }
         }
     }
@@ -141,25 +144,28 @@ public class RequestProcessor {
         return invocationParameters;
     }
 
-    private String getSitemapPath() {
-        String sitemapPath = this.sitemapPath;
-        if (sitemapPath == null) {
-            sitemapPath = "/sitemap.xmap";
+    private void setSitemapPath(String sitemapPath) {
+        this.sitemapPath = sitemapPath;
+
+        if (this.sitemapPath == null) {
+            this.sitemapPath = "/sitemap.xmap";
         }
 
-        if (!sitemapPath.startsWith("/")) {
-            sitemapPath = "/" + sitemapPath;
+        if (!this.sitemapPath.startsWith("/")) {
+            this.sitemapPath = "/" + this.sitemapPath;
         }
+    }
 
-        return sitemapPath;
+    private String getSitemapPath() {
+        return this.sitemapPath;
     }
 
     private void initializeBaseURL() throws InvalidBaseUrlException {
         try {
-            String baseURL = this.getSitemapPath();
-            baseURL = baseURL.substring(0, baseURL.lastIndexOf('/') + 1);
+            String basePath = this.getSitemapPath();
+            basePath = basePath.substring(0, basePath.lastIndexOf('/') + 1);
 
-            this.baseURL = this.servletContext.getResource(baseURL);
+            this.baseURL = this.servletContext.getResource(basePath);
 
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Setting the baseURL to " + this.baseURL);
@@ -178,7 +184,7 @@ public class RequestProcessor {
         URL url = null;
         SitemapBuilder sitemapBuilder = null;
         try {
-            sitemapBuilder = (SitemapBuilder) this.beanFactory.getBean(SitemapBuilder.class.getName());
+            sitemapBuilder = this.beanFactory.getBean(SitemapBuilder.class.getName(), SitemapBuilder.class);
             url = this.servletContext.getResource(this.getSitemapPath());
         } catch (Exception e) {
             throw new SitemapInitializationException("Can't initialize sitemap.", e);
@@ -187,11 +193,11 @@ public class RequestProcessor {
         // if the sitemap URL can't be resolved by the ServletContext, null is returned
         if (url == null) {
             // prepare a meaningful exception
-            String baseURL = this.getBaseURL().toExternalForm();
-            if (baseURL.endsWith("/")) {
-                baseURL = baseURL.substring(0, baseURL.length() - 1);
+            String basePath = this.getBaseURL().toExternalForm();
+            if (basePath.endsWith("/")) {
+                basePath = basePath.substring(0, basePath.length() - 1);
             }
-            throw new SitemapNotFoundException("Can't find sitemap at " + baseURL + this.getSitemapPath());
+            throw new SitemapNotFoundException("Can't find sitemap at " + basePath + this.getSitemapPath());
         }
 
         this.sitemapNode = sitemapBuilder.build(url);
@@ -241,24 +247,33 @@ public class RequestProcessor {
 
     private void logRequest(HttpServletRequest request) {
         if (LOG.isInfoEnabled()) {
-            LOG.info("Performing " + request.getMethod().toUpperCase() + " request at "
-                    + request.getRequestURI());
+            StringBuilder stringBuilder = new StringBuilder();
+            stringBuilder.append("Performing ");
+            stringBuilder.append(this.getRequestMethod(request));
+            stringBuilder.append(" request at ");
+            stringBuilder.append(request.getRequestURI());
+            LOG.info(stringBuilder.toString());
         }
+
         if (LOG.isDebugEnabled()) {
             LOG.debug("The base URL for this request is " + this.getBaseURL());
         }
     }
 
+    private String getRequestMethod(HttpServletRequest request) {
+        return request.getMethod().toUpperCase(Locale.ENGLISH);
+    }
+
     public static Map<String, Object> prepareParameters(HttpServletRequest request, HttpServletResponse response,
             Settings settings, ServletContext servletContext) {
         if (request == null) {
-            throw new NullPointerException("Request mustn't be null.");
+            throw new IllegalArgumentException("Request mustn't be null.");
         }
         if (response == null) {
-            throw new NullPointerException("Response mustn't be null.");
+            throw new IllegalArgumentException("Response mustn't be null.");
         }
         if (servletContext == null) {
-            throw new NullPointerException("ServletContext mustn't be null.");
+            throw new IllegalArgumentException("ServletContext mustn't be null.");
         }
 
         Map<String, Object> parameters = getInvocationParameters(request);
@@ -275,6 +290,8 @@ public class RequestProcessor {
     }
 
     private void sendSitemapResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        final String requestMethod = this.getRequestMethod(request);
+
         Settings settings = (Settings) this.beanFactory.getBean(Settings.class.getName());
 
         // provide conditional GET relevant data and request method
@@ -288,11 +305,11 @@ public class RequestProcessor {
         ResponseHeaderCollector.setIfNoneMatch(ifNoneMatch);
 
         // request method
-        ResponseHeaderCollector.setRequestMethod(request.getMethod());
+        ResponseHeaderCollector.setRequestMethod(requestMethod);
 
         // invoke the sitemap engine
         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-        ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(RESPONSE_BUFFER_SIZE);
         this.invoke(this.calcSitemapRequestURI(request), prepareParameters(request, response, settings,
                 this.servletContext), baos);
 
@@ -336,9 +353,12 @@ public class RequestProcessor {
         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         if (!ResponseHeaderCollector.isModifiedResponse()) {
             if (LOG.isInfoEnabled()) {
-                LOG.info("Going to send NOT MODIFIED response: statusCode="
-                        + HttpServletResponse.SC_NOT_MODIFIED + ", lastModified="
-                        + lastModified);
+                StringBuilder stringBuilder = new StringBuilder();
+                stringBuilder.append("Going to send NOT MODIFIED response: statusCode=");
+                stringBuilder.append(HttpServletResponse.SC_NOT_MODIFIED);
+                stringBuilder.append(", lastModified=");
+                stringBuilder.append(lastModified);
+                LOG.info(stringBuilder.toString());
             }
 
             response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
@@ -349,27 +369,36 @@ public class RequestProcessor {
         // it is not a HEAD request. The Content-Length has to be sent also in the case of
         // a HEAD request (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-        int contentLengh = baos.size();
-        response.setContentLength(contentLengh);
+        int contentLength = baos.size();
+        response.setContentLength(contentLength);
 
         // Status code handling
         response.setStatus(statusCode);
 
         // logging
         if (LOG.isInfoEnabled()) {
-            LOG.info("Going to send " + request.getMethod().toUpperCase() + " response: mimeType=" + mimeType
-                    + ", contentLength=" + contentLengh + ", statusCode=" + statusCode + ", lastModified="
-                    + lastModified);
+            StringBuilder stringBuilder = new StringBuilder();
+            stringBuilder.append("Going to send ");
+            stringBuilder.append(requestMethod);
+            stringBuilder.append(" response: mimeType=");
+            stringBuilder.append(mimeType);
+            stringBuilder.append(", contentLength=");
+            stringBuilder.append(contentLength);
+            stringBuilder.append(", statusCode=");
+            stringBuilder.append(statusCode);
+            stringBuilder.append(", lastModified=");
+            stringBuilder.append(lastModified);
+            LOG.info(stringBuilder.toString());
         }
 
         // in the case of a HEAD request stop processing here (i.e. don't send any content)
-        if ("HEAD".equalsIgnoreCase(request.getMethod())) {
+        if ("HEAD".equals(requestMethod)) {
             return;
         }
 
         // send content
-        if (contentLengh > 0) {
-            response.getOutputStream().write(baos.toByteArray());
+        if (contentLength > 0) {
+            baos.writeTo(response.getOutputStream());
         }
     }