You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/07/23 12:15:54 UTC

svn commit: r1505957 - in /cxf/trunk: rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ systests/jaxrs/src/test/resources/servlet_as_filter/ systests/jaxrs/src/test/resources/s...

Author: sergeyb
Date: Tue Jul 23 10:15:53 2013
New Revision: 1505957

URL: http://svn.apache.org/r1505957
Log:
[CXF-4377] Some more work to do with running CXFServlet as Filter

Added:
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerServletFilter.java   (with props)
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSServletFilterTest.java   (with props)
    cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/
    cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/
    cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/beans.xml   (with props)
    cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/web.xml   (with props)
Modified:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SpringServletConfigStore.java

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java?rev=1505957&r1=1505956&r2=1505957&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java (original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java Tue Jul 23 10:15:53 2013
@@ -21,12 +21,14 @@ package org.apache.cxf.transport.servlet
 import java.io.IOException;
 
 import javax.servlet.FilterChain;
+import javax.servlet.FilterRegistration;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.cxf.Bus;
@@ -131,7 +133,10 @@ public class CXFNonSpringServlet extends
                 if (bus != null) {
                     origBus = BusFactory.getAndSetThreadDefaultBus(bus);
                 }
-                if (controller.filter((HttpServletRequest)request, (HttpServletResponse)response)) {
+                HttpServletRequest httpRequest = (HttpServletRequest)request;
+                if (controller.filter(new HttpServletRequestFilter(httpRequest,
+                                                                   super.getServletName()),
+                                      (HttpServletResponse)response)) {
                     return;
                 }
             } finally {
@@ -189,4 +194,37 @@ public class CXFNonSpringServlet extends
             bus = null;
         }
     }
+    
+    private static class HttpServletRequestFilter extends HttpServletRequestWrapper {
+        private String filterName;
+        public HttpServletRequestFilter(HttpServletRequest request, String filterName) {
+            super(request);
+            this.filterName = filterName;
+        }
+        
+        @Override
+        public String getServletPath() {
+            FilterRegistration fr = super.getServletContext().getFilterRegistration(filterName);
+            if (fr != null && !fr.getUrlPatternMappings().isEmpty()) {
+                String mapping = fr.getUrlPatternMappings().iterator().next();
+                if (mapping.endsWith("/*")) {
+                    return mapping.substring(0, mapping.length() - 2);
+                }
+            }
+            return "";
+        }
+        
+        @Override
+        public String getPathInfo() {
+            String pathInfo = super.getPathInfo();
+            if (pathInfo == null) {
+                pathInfo = getRequestURI();
+            }
+            String prefix = super.getContextPath() + this.getServletPath();
+            if (pathInfo.startsWith(prefix)) {
+                pathInfo = pathInfo.substring(prefix.length());
+            }
+            return pathInfo;
+        }
+    }
 }

Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerServletFilter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerServletFilter.java?rev=1505957&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerServletFilter.java (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerServletFilter.java Tue Jul 23 10:15:53 2013
@@ -0,0 +1,89 @@
+/**
+ * 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.cxf.systest.jaxrs;
+
+import java.net.URISyntaxException;
+
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.Handler;
+import org.eclipse.jetty.server.handler.DefaultHandler;
+import org.eclipse.jetty.server.handler.HandlerCollection;
+import org.eclipse.jetty.server.nio.SelectChannelConnector;
+import org.eclipse.jetty.webapp.WebAppContext;
+
+
+public class BookServerServletFilter extends AbstractBusTestServerBase {
+    public static final String PORT = allocatePort(BookServerServletFilter.class);
+
+    private org.eclipse.jetty.server.Server server;
+    
+    protected void run() {
+        server = new org.eclipse.jetty.server.Server();
+
+        SelectChannelConnector connector = new SelectChannelConnector();
+        connector.setPort(Integer.parseInt(PORT));
+        server.setConnectors(new Connector[] {connector});
+
+        WebAppContext webappcontext = new WebAppContext();
+        String contextPath = null;
+        try {
+            contextPath = getClass().getResource("/servlet_as_filter").toURI().getPath();
+        } catch (URISyntaxException e1) {
+            e1.printStackTrace();
+        }
+        webappcontext.setContextPath("/webapp");
+
+        webappcontext.setWar(contextPath);
+
+        HandlerCollection handlers = new HandlerCollection();
+        handlers.setHandlers(new Handler[] {webappcontext, new DefaultHandler()});
+
+        server.setHandler(handlers);
+        try {
+            server.start();
+                       
+        } catch (Exception e) {
+            e.printStackTrace();
+        }     
+    }
+
+    public void tearDown() throws Exception {
+        super.tearDown();
+        if (server != null) {
+            server.stop();
+            server.destroy();
+            server = null;
+        }
+    }    
+    
+    public static void main(String args[]) {
+        try {
+            BookServerServletFilter s = new BookServerServletFilter();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+
+}

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerServletFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerServletFilter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSServletFilterTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSServletFilterTest.java?rev=1505957&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSServletFilterTest.java (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSServletFilterTest.java Tue Jul 23 10:15:53 2013
@@ -0,0 +1,52 @@
+/**
+ * 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.cxf.systest.jaxrs;
+
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JAXRSServletFilterTest extends AbstractBusClientServerTestBase {
+    public static final String PORT = BookServerServletFilter.PORT;
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        AbstractResourceInfo.clearAllMaps();
+        assertTrue("server did not launch correctly",
+                   launchServer(BookServerServletFilter.class, true));
+        createStaticBus();
+    }
+    
+    
+    @Test
+    public void testServletConfigInitParam() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:" + PORT + "/webapp/filter/resources/servlet/config/query?name=a"; 
+        WebClient wc = WebClient.create(endpointAddress);
+        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+        wc.accept("text/plain");
+
+        assertEquals("avalue", wc.get(String.class));
+    }
+}

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSServletFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSServletFilterTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SpringServletConfigStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SpringServletConfigStore.java?rev=1505957&r1=1505956&r2=1505957&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SpringServletConfigStore.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SpringServletConfigStore.java Tue Jul 23 10:15:53 2013
@@ -23,6 +23,7 @@ import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 
 import org.springframework.web.context.ServletConfigAware;
 
@@ -39,4 +40,11 @@ public class SpringServletConfigStore im
     public String getServletConfigInitParam(@PathParam("name") String name) {
         return servletConfig.getInitParameter(name);
     }
+    
+    @GET
+    @Produces("text/plain")
+    @Path("config/query")
+    public String getServletConfigInitParamQuery(@QueryParam("name") String name) {
+        return servletConfig.getInitParameter(name);
+    }
 }

Added: cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/beans.xml?rev=1505957&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/beans.xml (added)
+++ cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/beans.xml Tue Jul 23 10:15:53 2013
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
+  xsi:schemaLocation="
+http://www.springframework.org/schema/beans 
+http://www.springframework.org/schema/beans/spring-beans.xsd
+http://cxf.apache.org/jaxrs
+http://cxf.apache.org/schemas/jaxrs.xsd">
+	<import resource="classpath:/META-INF/cxf/cxf.xml"/>
+
+    <jaxrs:server id="bookservice"
+                  address="/resources">
+        <jaxrs:serviceBeans>
+            <ref bean="servletconfigstore"/>
+        </jaxrs:serviceBeans>
+    </jaxrs:server>
+    <bean id="servletconfigstore" class="org.apache.cxf.systest.jaxrs.SpringServletConfigStore"/>
+
+    
+</beans>

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/beans.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/beans.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/beans.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/web.xml?rev=1505957&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/web.xml (added)
+++ cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/web.xml Tue Jul 23 10:15:53 2013
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
+<!DOCTYPE web-app
+    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+    "http://java.sun.com/dtd/web-app_2_3.dtd">
+
+<!--
+	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.
+-->
+<!-- START SNIPPET: webxml -->
+<web-app>
+
+	<filter>
+		<filter-name>CXFServlet</filter-name>
+		<filter-class>
+			org.apache.cxf.transport.servlet.CXFServlet
+		</filter-class>
+		<init-param>
+		      <param-name>a</param-name>
+		      <param-value>avalue</param-value>    
+		</init-param>
+		<init-param>
+		      <param-name>config-location</param-name>
+		      <param-value>/WEB-INF/beans.xml</param-value>    
+		</init-param>
+		<load-on-startup>1</load-on-startup>
+	</filter>
+
+	<filter-mapping>
+		<filter-name>CXFServlet</filter-name>
+		<url-pattern>/filter/*</url-pattern>
+	</filter-mapping>
+</web-app>
+<!-- END SNIPPET: webxml -->
\ No newline at end of file

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/web.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/web.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/servlet_as_filter/WEB-INF/web.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml