[seam-commits] Seam SVN: r8495 - trunk/src/resteasy/org/jboss/seam/resteasy.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed Jul 23 02:55:53 EDT 2008


Author: christian.bauer at jboss.com
Date: 2008-07-23 02:55:53 -0400 (Wed, 23 Jul 2008)
New Revision: 8495

Modified:
   trunk/src/resteasy/org/jboss/seam/resteasy/ApplicationConfig.java
   trunk/src/resteasy/org/jboss/seam/resteasy/ResteasyResourceAdapter.java
   trunk/src/resteasy/org/jboss/seam/resteasy/reasteasy-2.1.xsd
Log:
Added UrlInfo path filtering for RESTEasy

Modified: trunk/src/resteasy/org/jboss/seam/resteasy/ApplicationConfig.java
===================================================================
--- trunk/src/resteasy/org/jboss/seam/resteasy/ApplicationConfig.java	2008-07-23 06:30:53 UTC (rev 8494)
+++ trunk/src/resteasy/org/jboss/seam/resteasy/ApplicationConfig.java	2008-07-23 06:55:53 UTC (rev 8495)
@@ -36,6 +36,9 @@
     private boolean scanResources = true;
     private boolean useBuiltinProviders = true;
 
+    private String resourcePathPrefix = "/rest";
+    private boolean stripSeamResourcePath = true;
+
     public ApplicationConfig()
     {
         super();
@@ -156,4 +159,24 @@
     {
         this.useBuiltinProviders = useBuiltinProviders;
     }
+
+    public String getResourcePathPrefix()
+    {
+        return resourcePathPrefix;
+    }
+
+    public void setResourcePathPrefix(String resourcePathPrefix)
+    {
+        this.resourcePathPrefix = resourcePathPrefix;
+    }
+
+    public boolean isStripSeamResourcePath()
+    {
+        return stripSeamResourcePath;
+    }
+
+    public void setStripSeamResourcePath(boolean stripSeamResourcePath)
+    {
+        this.stripSeamResourcePath = stripSeamResourcePath;
+    }
 }

Modified: trunk/src/resteasy/org/jboss/seam/resteasy/ResteasyResourceAdapter.java
===================================================================
--- trunk/src/resteasy/org/jboss/seam/resteasy/ResteasyResourceAdapter.java	2008-07-23 06:30:53 UTC (rev 8494)
+++ trunk/src/resteasy/org/jboss/seam/resteasy/ResteasyResourceAdapter.java	2008-07-23 06:55:53 UTC (rev 8495)
@@ -2,25 +2,40 @@
 
 import org.jboss.seam.Component;
 import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Logger;
 import org.jboss.seam.annotations.Name;
 import org.jboss.seam.annotations.Scope;
 import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.log.Log;
 import org.jboss.seam.servlet.ContextualHttpServletRequest;
 import org.jboss.seam.web.AbstractResource;
+import org.resteasy.plugins.server.servlet.HttpServletDispatcher;
+import org.resteasy.plugins.server.servlet.HttpServletInputMessage;
+import org.resteasy.plugins.server.servlet.HttpServletResponseWrapper;
+import org.resteasy.plugins.server.servlet.ServletSecurityContext;
+import org.resteasy.specimpl.PathSegmentImpl;
+import org.resteasy.specimpl.UriBuilderImpl;
+import org.resteasy.specimpl.UriInfoImpl;
+import org.resteasy.spi.HttpRequest;
+import org.resteasy.spi.HttpResponse;
+import org.resteasy.spi.ResteasyProviderFactory;
+import org.resteasy.util.PathHelper;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.PathSegment;
+import javax.ws.rs.core.SecurityContext;
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.util.List;
 
 /**
- * Accepts incoming HTTP request under the Seam resource-servlet URL mapping and
+ * Accepts incoming HTTP request throug the SeamResourceServlet and
  * dispatches the call to RESTEasy. Wraps the call in Seam contexts.
- * <p>
- * Hardcoded URL path is <tt>/rest</tt>. Subclass and override the
- * <tt>getResourcePath()</tt> method to customize this.
- * </p>
  *
  * @author Christian Bauer
  */
@@ -30,12 +45,14 @@
 public class ResteasyResourceAdapter extends AbstractResource
 {
 
-    public static final String RESTEASY_RESOURCE_BASEPATH = "/rest";
+    @Logger
+    Log log;
 
     @Override
     public String getResourcePath()
     {
-        return RESTEASY_RESOURCE_BASEPATH;
+        ApplicationConfig appConfig = (ApplicationConfig)Component.getInstance(ApplicationConfig.class);
+        return appConfig.getResourcePathPrefix();
     }
 
     @Override
@@ -43,31 +60,77 @@
             throws ServletException, IOException
     {
 
-        // Wrap this in Seam contexts
-        new ContextualHttpServletRequest(request)
-        {
-            @Override
-            public void process() throws ServletException, IOException
+        // Wrap in RESTEasy contexts
+        try {
+            log.debug("processing REST request");
+            ResteasyProviderFactory.pushContext(HttpServletRequest.class, request);
+            ResteasyProviderFactory.pushContext(HttpServletResponse.class, response);
+            ResteasyProviderFactory.pushContext(SecurityContext.class, new ServletSecurityContext(request));
+
+            // Wrap in Seam contexts
+            new ContextualHttpServletRequest(request)
             {
+                @Override
+                public void process() throws ServletException, IOException
+                {
 
-                ResteasyDispatcher dispatcher =
-                        (ResteasyDispatcher) Component.getInstance(ResteasyDispatcher.class);
-                if (dispatcher == null)
-                {
-                    throw new IllegalStateException("RESTEasy is not installed, check your classpath");
+                    HttpHeaders headers = HttpServletDispatcher.extractHttpHeaders(request);
+                    String path = PathHelper.getEncodedPathInfo(request.getRequestURI(), request.getContextPath());
+                    URI absolutePath;
+                    try
+                    {
+                        URL absolute = new URL(request.getRequestURL().toString());
+
+                        UriBuilderImpl builder = new UriBuilderImpl();
+                        builder.scheme(absolute.getProtocol());
+                        builder.host(absolute.getHost());
+                        builder.port(absolute.getPort());
+                        builder.path(absolute.getPath());
+                        builder.replaceQueryParams(absolute.getQuery());
+                        absolutePath = builder.build();
+                    }
+                    catch (MalformedURLException e)
+                    {
+                        throw new RuntimeException(e);
+                    }
+
+                    ApplicationConfig appConfig = (ApplicationConfig)Component.getInstance(ApplicationConfig.class);
+                    if (appConfig.isStripSeamResourcePath()) {
+                        log.debug("removing SeamResourceServlet url-pattern and dispatcher prefix from request path");
+                        path = path.substring(path.indexOf(getResourcePath())+getResourcePath().length());
+                    }
+
+                    log.debug("final request path: " + path);
+                    List<PathSegment> pathSegments = PathSegmentImpl.parseSegments(path);
+                    UriInfoImpl uriInfo = new UriInfoImpl(absolutePath, path, request.getQueryString(), pathSegments);
+
+                    HttpRequest in;
+                    try
+                    {
+                        in =
+                            new HttpServletInputMessage(
+                                headers,
+                                request.getInputStream(),
+                                uriInfo,
+                                request.getMethod().toUpperCase()
+                            );
+                    }
+                    catch (IOException e)
+                    {
+                        throw new RuntimeException(e);
+                    }
+
+                    ResteasyDispatcher dispatcher =
+                            (ResteasyDispatcher) Component.getInstance(ResteasyDispatcher.class);
+                    HttpResponse theResponse =
+                            new HttpServletResponseWrapper(response, dispatcher.getDispatcher().getProviderFactory());
+                    dispatcher.getDispatcher().invoke(in, theResponse);
                 }
-                dispatcher.invoke(
-                        new HttpServletRequestWrapper(request)
-                        {
-                            // TODO: Strip out the /seam/resource/rest stuff
-                            public String getPathInfo()
-                            {
-                                return super.getPathInfo();
-                            }
-                        },
-                        response
-                );
-            }
-        }.run();
+            }.run();
+
+        } finally {
+            ResteasyProviderFactory.clearContextData();
+            log.debug("completed processing of REST request");
+        }
     }
 }

Modified: trunk/src/resteasy/org/jboss/seam/resteasy/reasteasy-2.1.xsd
===================================================================
--- trunk/src/resteasy/org/jboss/seam/resteasy/reasteasy-2.1.xsd	2008-07-23 06:30:53 UTC (rev 8494)
+++ trunk/src/resteasy/org/jboss/seam/resteasy/reasteasy-2.1.xsd	2008-07-23 06:55:53 UTC (rev 8495)
@@ -57,6 +57,24 @@
               </xs:documentation>
           </xs:annotation>
       </xs:attribute>
+      <xs:attribute name="resource-path-prefix" type="components:string">
+          <xs:annotation>
+              <xs:documentation>
+                 Append this prefix to any request path, after the SeamResourceServlet
+                 url-pattern prefix (configured in web.xml). Defaults to "/rest".
+              </xs:documentation>
+          </xs:annotation>
+      </xs:attribute>
+      <xs:attribute name="strip-seam-resource-path" type="components:boolean">
+          <xs:annotation>
+              <xs:documentation>
+                 Remove the "/&lt;url-pattern-of-seam-resource-servlet&gt;/&lt;resource-path-prefix&gt;" part
+                 of the  request path before the request is mapped to a @Path resource. If disabled,
+                 all @Path definitions must use the full prefix of the SeamResourceServlet as 
+                 mapped with the url-pattern in  web.xml, plus the resourcePathPrefix.
+              </xs:documentation>
+          </xs:annotation>
+      </xs:attribute>
    </xs:attributeGroup>
 
 </xs:schema>




More information about the seam-commits mailing list