incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r581676 - in /incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl: ComponentContextImpl.java ComponentRequestHandlerImpl.java ComponentServletRequestWrapper.java ComponentServletResponseWrapper.java RequestData.java
Date Wed, 03 Oct 2007 18:18:02 GMT
Author: fmeschbe
Date: Wed Oct  3 11:18:01 2007
New Revision: 581676

URL: http://svn.apache.org/viewvc?rev=581676&view=rev
Log:
SLING-32 <jsp:include> does not work for Sling based JSPs

Added:
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletRequestWrapper.java
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletResponseWrapper.java
Modified:
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestHandlerImpl.java
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java

Modified: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java?rev=581676&r1=581675&r2=581676&view=diff
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java
(original)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java
Wed Oct  3 11:18:01 2007
@@ -59,49 +59,49 @@
      * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
      */
     public Object getAttribute(String name) {
-        return this.getServletContext().getAttribute(name);
+        return getServletContext().getAttribute(name);
     }
 
     /**
      * @see javax.servlet.ServletContext#getAttributeNames()
      */
     public Enumeration getAttributeNames() {
-        return this.getServletContext().getAttributeNames();
+        return getServletContext().getAttributeNames();
     }
 
     /**
      * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
      */
     public String getInitParameter(String name) {
-        return this.getServletContext().getInitParameter(name);
+        return getServletContext().getInitParameter(name);
     }
 
     /**
      * @see javax.servlet.ServletContext#getInitParameterNames()
      */
     public Enumeration getInitParameterNames() {
-        return this.getServletContext().getInitParameterNames();
+        return getServletContext().getInitParameterNames();
     }
 
     /**
      * @see javax.servlet.ServletContext#getMajorVersion()
      */
     public int getMajorVersion() {
-        return this.getServletContext().getMajorVersion();
+        return getServletContext().getMajorVersion();
     }
 
     /**
      * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
      */
     public String getMimeType(String file) {
-        return this.getServletContext().getMimeType(file);
+        return getServletContext().getMimeType(file);
     }
 
     /**
      * @see javax.servlet.ServletContext#getMinorVersion()
      */
     public int getMinorVersion() {
-        return this.getServletContext().getMinorVersion();
+        return getServletContext().getMinorVersion();
     }
 
     /**
@@ -147,7 +147,7 @@
      * @see javax.servlet.ServletContext#getServerInfo()
      */
     public String getServerInfo() {
-        return this.requestHandler.getServerInfo();
+        return requestHandler.getServerInfo();
     }
 
     public void log(String message, Throwable throwable) {
@@ -159,54 +159,54 @@
     }
 
     public void log(Exception exception, String msg) {
-        this.log(msg, exception);
+        log(msg, exception);
     }
 
     /**
      * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
      */
     public void removeAttribute(String name) {
-        this.getServletContext().removeAttribute(name);
+        getServletContext().removeAttribute(name);
     }
 
     /**
      * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
      */
     public void setAttribute(String name, Object object) {
-        this.getServletContext().removeAttribute(name);
+        getServletContext().removeAttribute(name);
     }
 
     private ServletContext getServletContext() {
-        return this.requestHandler.getServletContext();
+        return requestHandler.getServletContext();
     }
 
     public ServletContext getContext(String uripath) {
         // check whether to return ComponentContext ??
-        return this.getServletContext().getContext(uripath);
+        return getServletContext().getContext(uripath);
     }
 
     public RequestDispatcher getNamedDispatcher(String name) {
-        return this.getServletContext().getNamedDispatcher(name);
+        return getServletContext().getNamedDispatcher(name);
     }
 
     public String getRealPath(String path) {
-        return this.getServletContext().getRealPath(path);
+        return getServletContext().getRealPath(path);
     }
 
     public Servlet getServlet(String name) throws ServletException {
-        return this.getServletContext().getServlet(name);
+        return getServletContext().getServlet(name);
     }
 
     public String getServletContextName() {
-        return this.getServletContext().getServletContextName();
+        return getServletContext().getServletContextName();
     }
 
     public Enumeration getServletNames() {
-        return this.getServletContext().getServletNames();
+        return getServletContext().getServletNames();
     }
 
     public Enumeration getServlets() {
-        return this.getServletContext().getServlets();
+        return getServletContext().getServlets();
     }
 
     //---------- Inner class --------------------------------------------------
@@ -228,43 +228,39 @@
         }
 
         public void include(ServletRequest request, ServletResponse response) throws ServletException,
IOException {
-            // TODO Auto-generated method stub
-            if (request instanceof ComponentRequest && response instanceof ComponentResponse)
{
-                this.include((ComponentRequest) request,
-                    (ComponentResponse) response);
-            } else {
-                // TODO, use servlet container dispatcher !!
-            }
-        }
-
-        public void forward(ServletRequest request, ServletResponse response) throws ServletException,
IOException {
-            // TODO Auto-generated method stub
-            // TODO, use servlet container dispatcher !!
-        }
-
-        /**
-         * @see org.apache.sling.core.component.ComponentRequestDispatcher#include(org.apache.sling.core.component.ComponentRequest,
org.apache.sling.core.component.ComponentResponse)
-         */
-        public void include(ComponentRequest request, ComponentResponse response)
-                throws ComponentException, IOException {
-
             // if content is null, try to resolve it using the path
             Content target;
-            if (this.content != null) {
-                target = this.content;
+            if (content != null) {
+                target = content;
             } else {
                 // this may throw an exception in case loading fails, which is
                 // ok here, if no content is available at that path null is
                 // return, which results in using the servlet container
-                String absPath = this.getAbsolutePath(request, this.path);
-                target = request.getContent(absPath);
+                ComponentRequest cRequest = RequestData.unwrap(request);
+
+                String absPath = getAbsolutePath(cRequest, path);
+                target = cRequest.getContent(absPath);
             }
 
             if (target != null) {
-                ComponentContextImpl.this.requestHandler.includeContent(request, response,
target);
+                requestHandler.includeContent(request, response, target);
             } else {
-                ComponentContextImpl.this.requestHandler.includeServlet(request, response,
this.path);
+                requestHandler.includeServlet(request, response, path);
+            }
+        }
+
+        public void include(ComponentRequest request, ComponentResponse response)
+                throws ComponentException, IOException {
+            try {
+                include((ServletRequest) request, (ServletResponse) response);
+            } catch (ServletException se) {
+                throw new ComponentException(se);
             }
+        }
+
+        public void forward(ServletRequest request, ServletResponse response) throws ServletException,
IOException {
+            // TODO Auto-generated method stub
+            // TODO, use servlet container dispatcher !!
         }
 
         private String getAbsolutePath(ComponentRequest request, String path) {

Modified: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestHandlerImpl.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestHandlerImpl.java?rev=581676&r1=581675&r2=581676&view=diff
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestHandlerImpl.java
(original)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestHandlerImpl.java
Wed Oct  3 11:18:01 2007
@@ -159,9 +159,8 @@
 
     // ---------- Generic Content Request processor ----------------------------
 
-    /* package */void includeServlet(ComponentRequest request,
-            ComponentResponse response, String path) throws IOException,
-            ComponentException {
+    void includeServlet(ServletRequest request, ServletResponse response,
+            String path) throws IOException, ComponentException {
 
         // check type of response, don't care actually for the response itself
         RequestData.unwrap(response);
@@ -183,19 +182,19 @@
         }
     }
 
-    /* package */void includeContent(ComponentRequest request,
-            ComponentResponse response, Content content) throws IOException,
-            ComponentException {
+    void includeContent(ServletRequest request, ServletResponse response,
+            Content content) throws IOException, ComponentException {
 
-        // check type of response, don't care actually for the response itself
-        RequestData.unwrap(response);
-
-        // get the request data (and btw check the correct type
-        RequestData requestData = RequestData.getRequestData(request);
+        // we need a ComponentRequest/ComponentResponse tupel to continue
+        ComponentRequest cRequest = RequestData.toComponentRequest(request);
+        ComponentResponse cResponse = RequestData.toComponentResponse(response);
 
+        // get the request data (and btw check the correct type)
+        RequestData requestData = RequestData.getRequestData(cRequest);
         requestData.pushContent(content);
+
         try {
-            this.processRequest(request, response);
+            this.processRequest(cRequest, cResponse);
         } catch (ServletException se) {
             throw new ComponentException(se.getMessage(), se);
         } finally {

Added: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletRequestWrapper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletRequestWrapper.java?rev=581676&view=auto
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletRequestWrapper.java
(added)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletRequestWrapper.java
Wed Oct  3 11:18:01 2007
@@ -0,0 +1,329 @@
+/*
+ * $Url: $
+ * $Id: $
+ *
+ * Copyright 1997-2005 Day Management AG
+ * Barfuesserplatz 6, 4001 Basel, Switzerland
+ * All Rights Reserved.
+ *
+ * This software is the confidential and proprietary information of
+ * Day Management AG, ("Confidential Information"). You shall not
+ * disclose such Confidential Information and shall use it only in
+ * accordance with the terms of the license agreement you entered into
+ * with Day.
+ */
+package org.apache.sling.core.impl;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.apache.sling.component.ComponentRequest;
+import org.apache.sling.component.ComponentRequestWrapper;
+
+/**
+ * The <code>ComponentServletRequestWrapper</code> class is a
+ * <code>ComponentRequestWrapper</code> which does not delegate to a wrapped
+ * <code>ComponentRequest</code> but to a wrapped
+ * <code>HttpServletRequest</code>. This is required if any user of the
+ * <code>RequestDispatcher.include</code> method uses a
+ * <code>HttpServletRequestWrapper</code> instead of a
+ * <code>ComponentRequestWrapper</code>.
+ */
+public class ComponentServletRequestWrapper extends ComponentRequestWrapper {
+
+    private final HttpServletRequest request;
+
+    public ComponentServletRequestWrapper(ComponentRequest delegatee,
+            HttpServletRequest request) {
+        super(delegatee);
+        this.request = request;
+    }
+
+    @Override
+    public String getLocalAddr() {
+        return request.getLocalAddr();
+    }
+
+    @Override
+    public String getLocalName() {
+        return request.getLocalName();
+    }
+
+    @Override
+    public int getLocalPort() {
+        return request.getLocalPort();
+    }
+
+    @Override
+    public int getRemotePort() {
+        return request.getRemotePort();
+    }
+
+    @Override
+    public String getAuthType() {
+        return request.getAuthType();
+    }
+
+    @Override
+    public String getContextPath() {
+        return request.getContextPath();
+    }
+
+    @Override
+    public Cookie[] getCookies() {
+        return request.getCookies();
+    }
+
+    @Override
+    public long getDateHeader(String name) {
+        return request.getDateHeader(name);
+    }
+
+    @Override
+    public String getHeader(String name) {
+        return request.getHeader(name);
+    }
+
+    @Override
+    public Enumeration getHeaderNames() {
+        return request.getHeaderNames();
+    }
+
+    @Override
+    public Enumeration getHeaders(String name) {
+        return request.getHeaders(name);
+    }
+
+    @Override
+    public int getIntHeader(String name) {
+        return request.getIntHeader(name);
+    }
+
+    @Override
+    public String getMethod() {
+        return request.getMethod();
+    }
+
+    @Override
+    public String getPathInfo() {
+        return request.getPathInfo();
+    }
+
+    @Override
+    public String getPathTranslated() {
+        return request.getPathTranslated();
+    }
+
+    @Override
+    public String getQueryString() {
+        return request.getQueryString();
+    }
+
+    @Override
+    public String getRemoteUser() {
+        return request.getRemoteUser();
+    }
+
+    @Override
+    public String getRequestURI() {
+        return request.getRequestURI();
+    }
+
+    @Override
+    public StringBuffer getRequestURL() {
+        return request.getRequestURL();
+    }
+
+    @Override
+    public String getRequestedSessionId() {
+        return request.getRequestedSessionId();
+    }
+
+    @Override
+    public String getServletPath() {
+        return request.getServletPath();
+    }
+
+    @Override
+    public HttpSession getSession() {
+        return request.getSession();
+    }
+
+    @Override
+    public HttpSession getSession(boolean create) {
+        return request.getSession(create);
+    }
+
+    @Override
+    public Principal getUserPrincipal() {
+        return request.getUserPrincipal();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdFromCookie() {
+        return request.isRequestedSessionIdFromCookie();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdFromURL() {
+        return request.isRequestedSessionIdFromURL();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdFromUrl() {
+        return request.isRequestedSessionIdFromUrl();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdValid() {
+        return request.isRequestedSessionIdValid();
+    }
+
+    @Override
+    public boolean isUserInRole(String role) {
+        return request.isUserInRole(role);
+    }
+
+    @Override
+    public Object getAttribute(String name) {
+        return request.getAttribute(name);
+    }
+
+    @Override
+    public Enumeration getAttributeNames() {
+        return request.getAttributeNames();
+    }
+
+    @Override
+    public String getCharacterEncoding() {
+        return request.getCharacterEncoding();
+    }
+
+    @Override
+    public int getContentLength() {
+        return request.getContentLength();
+    }
+
+    @Override
+    public String getContentType() {
+        return request.getContentType();
+    }
+
+    @Override
+    public ServletInputStream getInputStream() throws IOException {
+        return request.getInputStream();
+    }
+
+    @Override
+    public Locale getLocale() {
+        return request.getLocale();
+    }
+
+    @Override
+    public Enumeration getLocales() {
+        return request.getLocales();
+    }
+
+    @Override
+    public String getParameter(String name) {
+        return request.getParameter(name);
+    }
+
+    @Override
+    public Map getParameterMap() {
+        return request.getParameterMap();
+    }
+
+    @Override
+    public Enumeration getParameterNames() {
+        return request.getParameterNames();
+    }
+
+    @Override
+    public String[] getParameterValues(String name) {
+        return request.getParameterValues(name);
+    }
+
+    @Override
+    public String getProtocol() {
+        return request.getProtocol();
+    }
+
+    @Override
+    public BufferedReader getReader() throws IOException {
+        return request.getReader();
+    }
+
+    @Override
+    public String getRealPath(String path) {
+        return request.getRealPath(path);
+    }
+
+    @Override
+    public String getRemoteAddr() {
+        return request.getRemoteAddr();
+    }
+
+    @Override
+    public String getRemoteHost() {
+        return request.getRemoteHost();
+    }
+
+    @Override
+    public ServletRequest getRequest() {
+        return request;
+    }
+
+    @Override
+    public RequestDispatcher getRequestDispatcher(String path) {
+        return request.getRequestDispatcher(path);
+    }
+
+    @Override
+    public String getScheme() {
+        return request.getScheme();
+    }
+
+    @Override
+    public String getServerName() {
+        return request.getServerName();
+    }
+
+    @Override
+    public int getServerPort() {
+        return request.getServerPort();
+    }
+
+    @Override
+    public boolean isSecure() {
+        return request.isSecure();
+    }
+
+    @Override
+    public void removeAttribute(String name) {
+        request.removeAttribute(name);
+    }
+
+    @Override
+    public void setAttribute(String name, Object o) {
+        request.setAttribute(name, o);
+    }
+
+    @Override
+    public void setCharacterEncoding(String enc)
+            throws UnsupportedEncodingException {
+        request.setCharacterEncoding(enc);
+    }
+}

Added: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletResponseWrapper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletResponseWrapper.java?rev=581676&view=auto
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletResponseWrapper.java
(added)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentServletResponseWrapper.java
Wed Oct  3 11:18:01 2007
@@ -0,0 +1,213 @@
+/*
+ * $Url: $
+ * $Id: $
+ *
+ * Copyright 1997-2005 Day Management AG
+ * Barfuesserplatz 6, 4001 Basel, Switzerland
+ * All Rights Reserved.
+ *
+ * This software is the confidential and proprietary information of
+ * Day Management AG, ("Confidential Information"). You shall not
+ * disclose such Confidential Information and shall use it only in
+ * accordance with the terms of the license agreement you entered into
+ * with Day.
+ */
+package org.apache.sling.core.impl;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Locale;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.component.ComponentResponse;
+import org.apache.sling.component.ComponentResponseWrapper;
+
+/**
+ * The <code>ComponentServletResponseWrapper</code> class is a
+ * <code>ComponentResponseWrapper</code> which does not delegate to a wrapped
+ * <code>ComponentResponse</code> but to a wrapped
+ * <code>HttpServletResponse</code>. This is required if any user of the
+ * <code>RequestDispatcher.include</code> method uses a
+ * <code>HttpServletResponseWrapper</code> instead of a
+ * <code>ComponentResponseWrapper</code>. One such case is the Jasper runtime
+ * which does this.
+ */
+public class ComponentServletResponseWrapper extends ComponentResponseWrapper {
+
+    private final HttpServletResponse response;
+
+    public ComponentServletResponseWrapper(ComponentResponse delegatee,
+            HttpServletResponse response) {
+        super(delegatee);
+        this.response = response;
+    }
+
+    @Override
+    public String getContentType() {
+        return response.getContentType();
+    }
+
+    @Override
+    public void setCharacterEncoding(String charset) {
+        response.setCharacterEncoding(charset);
+    }
+
+    @Override
+    public void addCookie(Cookie cookie) {
+        response.addCookie(cookie);
+    }
+
+    @Override
+    public void addDateHeader(String name, long date) {
+        response.addDateHeader(name, date);
+    }
+
+    @Override
+    public void addHeader(String name, String value) {
+        response.addHeader(name, value);
+    }
+
+    @Override
+    public void addIntHeader(String name, int value) {
+        response.addIntHeader(name, value);
+    }
+
+    @Override
+    public boolean containsHeader(String name) {
+        return response.containsHeader(name);
+    }
+
+    @Override
+    public String encodeRedirectUrl(String url) {
+        return response.encodeRedirectUrl(url);
+    }
+
+    @Override
+    public String encodeRedirectURL(String url) {
+        return response.encodeRedirectURL(url);
+    }
+
+    @Override
+    public String encodeUrl(String url) {
+        return response.encodeUrl(url);
+    }
+
+    @Override
+    public String encodeURL(String url) {
+        return response.encodeURL(url);
+    }
+
+    @Override
+    public void sendError(int sc, String msg) throws IOException {
+        response.sendError(sc, msg);
+    }
+
+    @Override
+    public void sendError(int sc) throws IOException {
+        response.sendError(sc);
+    }
+
+    @Override
+    public void sendRedirect(String location) throws IOException {
+        response.sendRedirect(location);
+    }
+
+    @Override
+    public void setDateHeader(String name, long date) {
+        response.setDateHeader(name, date);
+    }
+
+    @Override
+    public void setHeader(String name, String value) {
+        response.setHeader(name, value);
+    }
+
+    @Override
+    public void setIntHeader(String name, int value) {
+        response.setIntHeader(name, value);
+    }
+
+    @Override
+    public void setStatus(int sc, String sm) {
+        response.setStatus(sc, sm);
+    }
+
+    @Override
+    public void setStatus(int sc) {
+        response.setStatus(sc);
+    }
+
+    @Override
+    public void flushBuffer() throws IOException {
+        response.flushBuffer();
+    }
+
+    @Override
+    public int getBufferSize() {
+        return response.getBufferSize();
+    }
+
+    @Override
+    public String getCharacterEncoding() {
+        return response.getCharacterEncoding();
+    }
+
+    @Override
+    public Locale getLocale() {
+        return response.getLocale();
+    }
+
+    @Override
+    public ServletOutputStream getOutputStream() throws IOException {
+        return response.getOutputStream();
+    }
+
+    @Override
+    public ServletResponse getResponse() {
+        return response;
+    }
+
+    @Override
+    public PrintWriter getWriter() throws IOException {
+        return response.getWriter();
+    }
+
+    @Override
+    public boolean isCommitted() {
+        return response.isCommitted();
+    }
+
+    @Override
+    public void reset() {
+        response.reset();
+    }
+
+    @Override
+    public void resetBuffer() {
+        response.resetBuffer();
+    }
+
+    @Override
+    public void setBufferSize(int size) {
+        response.setBufferSize(size);
+    }
+
+    @Override
+    public void setContentLength(int len) {
+        response.setContentLength(len);
+    }
+
+    @Override
+    public void setContentType(String type) {
+        response.setContentType(type);
+    }
+
+    @Override
+    public void setLocale(Locale loc) {
+        response.setLocale(loc);
+    }
+}

Modified: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java?rev=581676&r1=581675&r2=581676&view=diff
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java (original)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java Wed
Oct  3 11:18:01 2007
@@ -28,6 +28,10 @@
 import javax.jcr.Session;
 import javax.servlet.ServletInputStream;
 import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletRequestWrapper;
+import javax.servlet.ServletResponse;
+import javax.servlet.ServletResponseWrapper;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -176,6 +180,36 @@
 
     //---------- Request Helper
 
+    /**
+     * Unwraps the ServletRequest to a ComponentRequest.
+     */
+    public static ComponentRequest unwrap(ServletRequest request) throws ComponentException
{
+
+        // early check for most cases
+        if (request instanceof ComponentRequest) {
+            return (ComponentRequest) request;
+        }
+
+        // unwrap wrappers
+        while (request instanceof ServletRequestWrapper) {
+            request = ((ServletRequestWrapper) request).getRequest();
+
+            // immediate termination if we found one
+            if (request instanceof ComponentRequest) {
+                return (ComponentRequest) request;
+            }
+        }
+
+        // if we unwrapped everything and did not find a ComponentRequest, we lost
+        throw new ComponentException("ServletRequest not wrapping ComponentRequest");
+    }
+
+    /**
+     * Unwraps the ComponentRequest to a ComponentRequestImpl
+     * @param request
+     * @return
+     * @throws ComponentException
+     */
     public static ComponentRequestImpl unwrap(ComponentRequest request) throws ComponentException
{
         while (request instanceof ComponentRequestWrapper) {
             request = ((ComponentRequestWrapper) request).getComponentRequest();
@@ -185,9 +219,39 @@
             return (ComponentRequestImpl) request;
         }
 
-        throw new ComponentException("RenderRequest not of correct type");
+        throw new ComponentException("ComponentRequest not of correct type");
+    }
+
+    /**
+     * Unwraps the ServletRequest to a ComponentRequest.
+     */
+    public static ComponentResponse unwrap(ServletResponse response) throws ComponentException
{
+
+        // early check for most cases
+        if (response instanceof ComponentResponse) {
+            return (ComponentResponse) response;
+        }
+
+        // unwrap wrappers
+        while (response instanceof ServletResponseWrapper) {
+            response = ((ServletResponseWrapper) response).getResponse();
+
+            // immediate termination if we found one
+            if (response instanceof ComponentResponse) {
+                return (ComponentResponse) response;
+            }
+        }
+
+        // if we unwrapped everything and did not find a ComponentResponse, we lost
+        throw new ComponentException("ServletResponse not wrapping ComponentResponse");
     }
 
+    /**
+     * Unwraps a ComponentResponse to a ComponentResponseImpl
+     * @param response
+     * @return
+     * @throws ComponentException
+     */
     public static ComponentResponseImpl unwrap(ComponentResponse response) throws ComponentException
{
         while (response instanceof ComponentResponseWrapper) {
             response = ((ComponentResponseWrapper) response).getComponentResponse();
@@ -202,6 +266,54 @@
 
     public static RequestData getRequestData(ComponentRequest request) throws ComponentException
{
         return unwrap(request).getRequestData();
+    }
+
+    public static RequestData getRequestData(ServletRequest request) throws ComponentException
{
+        return unwrap(unwrap(request)).getRequestData();
+    }
+
+    public static ComponentRequest toComponentRequest(ServletRequest request) throws ComponentException
{
+        // unwrap to ComponentRequest
+        ComponentRequest cRequest = unwrap(request);
+
+        // check type of response, don't care actually for the response itself
+        RequestData.unwrap(cRequest);
+
+        // if the servlet response is actually the ComponentResponse, we are done
+        if (cRequest == request) {
+            return cRequest;
+        }
+
+        // ensure the request is a HTTP request
+        if (!(request instanceof HttpServletRequest)) {
+            throw new ComponentException("Request is not an HTTP request");
+        }
+
+        // otherwise, we create a new response wrapping the servlet response
+        // and unwrapped component response
+        return new ComponentServletRequestWrapper(cRequest, (HttpServletRequest) request);
+    }
+
+    public static ComponentResponse toComponentResponse(ServletResponse response) throws
ComponentException {
+        // unwrap to ComponentResponse
+        ComponentResponse cResponse = unwrap(response);
+
+        // check type of response, don't care actually for the response itself
+        RequestData.unwrap(cResponse);
+
+        // if the servlet response is actually the ComponentResponse, we are done
+        if (cResponse == response) {
+            return cResponse;
+        }
+
+        // ensure the response is a HTTP response
+        if (!(response instanceof HttpServletResponse)) {
+            throw new ComponentException("Response is not an HTTP response");
+        }
+
+        // otherwise, we create a new response wrapping the servlet response
+        // and unwrapped component response
+        return null;
     }
 
     // ---------- Content inclusion stacking -----------------------------------



Mime
View raw message