cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r485189 - in /cocoon/trunk/core/cocoon-core/src: main/java/org/apache/cocoon/core/container/spring/ main/java/org/apache/cocoon/servlet/ test/java/org/apache/cocoon/
Date Sun, 10 Dec 2006 14:42:50 GMT
Author: cziegeler
Date: Sun Dec 10 06:42:48 2006
New Revision: 485189

URL: http://svn.apache.org/viewvc?view=rev&rev=485189
Log:
Add new servlet filter to simplify accessing processing information

Added:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestListener.java
  (with props)
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/ServletRequestHolder.java
  (with props)
Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Container.java
    cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/CocoonTestCase.java

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Container.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Container.java?view=diff&rev=485189&r1=485188&r2=485189
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Container.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/Container.java
Sun Dec 10 06:42:48 2006
@@ -21,11 +21,13 @@
 import javax.servlet.ServletContext;
 
 import org.apache.cocoon.configuration.Settings;
+import org.apache.cocoon.servlet.ServletRequestHolder;
 import org.springframework.beans.factory.BeanFactory;
 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.web.context.WebApplicationContext;
 import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.support.WebApplicationContextUtils;
 
 /**
@@ -48,6 +50,12 @@
     }
 
     protected static Container ROOT_CONTAINER;
+
+    public static Container getCurrentContainer() {
+        final ServletContext servletContext = ServletRequestHolder.currentServletContext();
+        final RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
+        return getCurrentContainer(servletContext, attributes);
+    }
 
     /**
      * Return the current web application context.

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestListener.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestListener.java?view=auto&rev=485189
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestListener.java
(added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestListener.java
Sun Dec 10 06:42:48 2006
@@ -0,0 +1,101 @@
+/*
+ * 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.servlet;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.springframework.context.i18n.LocaleContextHolder;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+/**
+ * A servlet filter making some per request information available.
+ * This class is similar to Spring's RequestContextListener with some additional
+ * logic.
+ *
+ * @version $Id
+ * @since 2.2
+ */
+public class RequestListener implements Filter {
+
+	/** Logger available to subclasses. */
+	protected final Log logger = LogFactory.getLog(getClass());
+
+    /** The servlet context. */
+    protected ServletContext servletContext;
+
+    /**
+     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
+     */
+    public void init(FilterConfig config) throws ServletException {
+        this.servletContext = config.getServletContext();
+    }
+
+	/**
+	 * @see javax.servlet.Filter#destroy()
+	 */
+	public void destroy() {
+        this.servletContext = null;
+    }
+
+    /**
+     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
javax.servlet.FilterChain)
+     */
+    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
+    throws IOException, ServletException {
+        if (!(req instanceof HttpServletRequest)) {
+            throw new IllegalArgumentException("Request is not an HttpServletRequest: " +
req);
+        }
+        if (!(res instanceof HttpServletResponse)) {
+            throw new IllegalArgumentException("Response is not an HttpServletResponse: "
+ res);
+        }
+        final HttpServletRequest request = (HttpServletRequest)req;
+        final HttpServletResponse resposne = (HttpServletResponse)res;
+        try {
+            LocaleContextHolder.setLocale(request.getLocale());
+            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
+            ServletRequestHolder.set(this.servletContext, request, resposne);
+            if (logger.isDebugEnabled()) {
+                logger.debug("Bound request information to thread: " + request);
+            }
+        } finally {
+            final ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
RequestContextHolder.getRequestAttributes();
+            if ( requestAttributes != null ) {
+                requestAttributes.requestCompleted();
+            }
+            RequestContextHolder.resetRequestAttributes();
+            LocaleContextHolder.resetLocaleContext();
+            ServletRequestHolder.reset();
+            if (logger.isDebugEnabled()) {
+                logger.debug("Cleared thread-bound request information: " + request);
+            }
+        }
+    }
+}

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestListener.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/ServletRequestHolder.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/ServletRequestHolder.java?view=auto&rev=485189
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/ServletRequestHolder.java
(added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/ServletRequestHolder.java
Sun Dec 10 06:42:48 2006
@@ -0,0 +1,188 @@
+/*
+ * 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.servlet;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Holder class to expose the servlet context, the current request and the current
+ * response in the form of a thread-bound object.
+ *
+ * @version $Id$
+ * @since 2.2
+ */
+public abstract class ServletRequestHolder  {
+	
+	private static final ThreadLocal infoContextHolder = new ThreadLocal();
+
+	private static final ThreadLocal inheritableInfoContextHolder = new InheritableThreadLocal();
+
+
+	/**
+	 * Reset the context for the current thread.
+	 */
+	public static void reset() {
+        infoContextHolder.set(null);
+        inheritableInfoContextHolder.set(null);
+	}
+
+	/**
+	 * Bind the given information to the current thread,
+	 * <i>not</i> exposing it as inheritable for child threads.
+	 * @param context the ServletContext to expose
+     * @param req the HttpServletRequest to expose
+     * @param res the HttpServletResponse to expose
+	 * @see #set(ServletContext, HttpServletRequest, HttpServletResponse, boolean)
+	 */
+	public static void set(ServletContext context, HttpServletRequest req, HttpServletResponse
res) {
+        set(context, req, res, false);
+	}
+
+	/**
+	 * Bind the given information to the current thread.
+     * @param context the ServletContext to expose
+     * @param req the HttpServletRequest to expose
+     * @param res the HttpServletResponse to expose
+	 * @param inheritable whether to expose the information as inheritable
+	 * for child threads (using an {@link java.lang.InheritableThreadLocal})
+	 */
+	public static void set(ServletContext context,
+                           HttpServletRequest req,
+                           HttpServletResponse res,
+                           boolean inheritable) {
+        final RequestInfo info = new RequestInfo(context, req, res);
+		if (inheritable) {
+            inheritableInfoContextHolder.set(info);
+            infoContextHolder.set(null);
+		} else {
+            infoContextHolder.set(info);
+            inheritableInfoContextHolder.set(null);
+		}
+	}
+
+	/**
+	 * Return the ServletContext currently bound to the thread.
+	 * @return the ServletContext currently bound to the thread,
+	 * or <code>null</code>
+	 */
+	public static ServletContext getServletContext() {
+        final RequestInfo info = get();
+        if ( info != null ) {
+            return info.servletContext;
+        }
+		return null;
+	}
+
+    /**
+     * Return the HttpServletRequest currently bound to the thread.
+     * @return the HttpServletRequest currently bound to the thread,
+     * or <code>null</code>
+     */
+    public static HttpServletRequest getRequest() {
+        final RequestInfo info = get();
+        if ( info != null ) {
+            return info.request;
+        }
+        return null;
+    }
+
+    /**
+     * Return the HttpServletResponse currently bound to the thread.
+     * @return the HttpServletResponse currently bound to the thread,
+     * or <code>null</code>
+     */
+    public static HttpServletResponse getResponse() {
+        final RequestInfo info = get();
+        if ( info != null ) {
+            return info.respone;
+        }
+        return null;
+    }
+
+    /**
+	 * Return the ServletContext currently bound to the thread.
+	 * @return the ServletContext currently bound to the thread
+	 * @throws IllegalStateException if no ServletContext object
+	 * is bound to the current thread
+	 */
+	public static ServletContext currentServletContext() throws IllegalStateException {
+        final RequestInfo info = currentInfo();
+        if ( info != null ) {
+            return info.servletContext;
+        }
+        return null;
+	}
+
+    /**
+     * Return the HttpServletRequest currently bound to the thread.
+     * @return the HttpServletRequest currently bound to the thread
+     * @throws IllegalStateException if no HttpServletRequest object
+     * is bound to the current thread
+     */
+    public static HttpServletRequest currentRequest() throws IllegalStateException {
+        final RequestInfo info = currentInfo();
+        if ( info != null ) {
+            return info.request;
+        }
+        return null;
+    }
+
+    /**
+     * Return the HttpServletResponse currently bound to the thread.
+     * @return the HttpServletResponse currently bound to the thread
+     * @throws IllegalStateException if no HttpServletResponse object
+     * is bound to the current thread
+     */
+    public static HttpServletResponse currentResponse() throws IllegalStateException {
+        final RequestInfo info = currentInfo();
+        if ( info != null ) {
+            return info.respone;
+        }
+        return null;
+    }
+
+    private static RequestInfo get() {
+        RequestInfo info = (RequestInfo)infoContextHolder.get();
+        if (info == null) {
+            info = (RequestInfo) inheritableInfoContextHolder.get();
+        }
+        return info;    
+    }
+
+    private static RequestInfo currentInfo() {
+        RequestInfo info = get();
+        if (info == null) {
+            throw new IllegalStateException("No thread-bound information found.");
+        }
+        return info;        
+    }
+
+    private static final class RequestInfo {
+
+        public final ServletContext servletContext;
+        public final HttpServletRequest request;
+        public final HttpServletResponse respone;
+
+        public RequestInfo(ServletContext c, HttpServletRequest req, HttpServletResponse
res) {
+            this.servletContext = c;
+            this.request = req;
+            this.respone = res;
+        }
+    }
+}

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/ServletRequestHolder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/ServletRequestHolder.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/CocoonTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/CocoonTestCase.java?view=diff&rev=485189&r1=485188&r2=485189
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/CocoonTestCase.java (original)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/CocoonTestCase.java Sun
Dec 10 06:42:48 2006
@@ -21,13 +21,13 @@
 import org.apache.cocoon.core.container.ContainerTestCase;
 import org.apache.cocoon.core.container.spring.avalon.ComponentInfo;
 import org.apache.cocoon.core.container.spring.avalon.ConfigurationInfo;
+import org.apache.cocoon.xml.JaxpParser;
 import org.apache.excalibur.source.SourceFactory;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.source.impl.ResourceSourceFactory;
 import org.apache.excalibur.source.impl.SourceResolverImpl;
 import org.apache.excalibur.source.impl.URLSourceFactory;
 import org.apache.excalibur.xml.dom.DOMParser;
-import org.apache.excalibur.xml.impl.JaxpParser;
 import org.apache.excalibur.xml.sax.SAXParser;
 import org.apache.excalibur.xmlizer.DefaultXMLizer;
 import org.apache.excalibur.xmlizer.XMLizer;



Mime
View raw message