cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1349967 - in /cxf/trunk: rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ systests/transports/src/test/java/org/apache/cxf/systest/servlet/
Date Wed, 13 Jun 2012 17:37:01 GMT
Author: dkulp
Date: Wed Jun 13 17:37:00 2012
New Revision: 1349967

URL: http://svn.apache.org/viewvc?rev=1349967&view=rev
Log:
[CXF-4377] Update CXF servlets to also be Filters

Added:
    cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFFilterTest.java
    cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/TestServlet.java
    cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-filter.xml
      - copied, changed from r1349926, cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-restful.xml
Removed:
    cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-restful.xml
Modified:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java?rev=1349967&r1=1349966&r2=1349967&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
Wed Jun 13 17:37:00 2012
@@ -21,11 +21,14 @@ package org.apache.cxf.transport.servlet
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
+import javax.servlet.Filter;
+import javax.servlet.FilterConfig;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
@@ -42,8 +45,7 @@ import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 
 
-
-public abstract class AbstractHTTPServlet extends HttpServlet {
+public abstract class AbstractHTTPServlet extends HttpServlet implements Filter {
     
     private static final long serialVersionUID = -8357252743467075117L;
 
@@ -88,7 +90,23 @@ public abstract class AbstractHTTPServle
         dispatcherServletName = servletConfig.getInitParameter(REDIRECT_SERVLET_NAME_PARAMETER);
         dispatcherServletPath = servletConfig.getInitParameter(REDIRECT_SERVLET_PATH_PARAMETER);
     }
-    
+    public final void init(final FilterConfig filterConfig) throws ServletException {
+        init(new ServletConfig() {
+            public String getServletName() {
+                return filterConfig.getFilterName();
+            }
+            public ServletContext getServletContext() {
+                return filterConfig.getServletContext();
+            }
+            public String getInitParameter(String name) {
+                return filterConfig.getInitParameter(name);
+            }
+            public Enumeration<String> getInitParameterNames() {
+                return filterConfig.getInitParameterNames();
+            }
+        });
+    }
+
     private static List<String> parseListSequence(String values) {
         if (values != null) {
             List<String> list = new LinkedList<String>();
@@ -257,6 +275,7 @@ public abstract class AbstractHTTPServle
         }   
     }
     
+    
     protected abstract void invoke(HttpServletRequest request, HttpServletResponse response)

         throws ServletException;
     

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=1349967&r1=1349966&r2=1349967&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
Wed Jun 13 17:37:00 2012
@@ -18,8 +18,13 @@
  */
 package org.apache.cxf.transport.servlet;
 
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
 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.HttpServletResponse;
@@ -59,7 +64,6 @@ public class CXFNonSpringServlet extends
         this.globalRegistry = destinationRegistry != null;
         this.loadBus = loadBus;
     }
-
     @Override
     public void init(ServletConfig sc) throws ServletException {
         super.init(sc);
@@ -69,8 +73,7 @@ public class CXFNonSpringServlet extends
         if (this.bus != null) {
             loader = bus.getExtension(ClassLoader.class);
             ResourceManager resourceManager = bus.getExtension(ResourceManager.class);
-            resourceManager.addResourceResolver(new ServletContextResourceResolver(
-                                                   sc.getServletContext()));
+            resourceManager.addResourceResolver(new ServletContextResourceResolver(sc.getServletContext()));
             if (destinationRegistry == null) {
                 this.destinationRegistry = getDestinationRegistryFromBus(this.bus);
             }
@@ -97,7 +100,7 @@ public class CXFNonSpringServlet extends
     protected void loadBus(ServletConfig sc) {
         this.bus = BusFactory.newInstance().createBus();
     }
-
+    
     private ServletController createServletController(ServletConfig servletConfig) {
         HttpServlet serviceListGeneratorServlet = 
             new ServiceListGeneratorServlet(destinationRegistry, bus);
@@ -115,7 +118,29 @@ public class CXFNonSpringServlet extends
     public void setBus(Bus bus) {
         this.bus = bus;
     }
-
+    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+        throws IOException, ServletException {
+        ClassLoaderHolder origLoader = null;
+        if (request instanceof HttpServletRequest && response instanceof HttpServletResponse)
{
+            try {
+                if (loader != null) {
+                    origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
+                }
+                if (bus != null) {
+                    BusFactory.setThreadDefaultBus(bus);
+                }
+                if (controller.filter((HttpServletRequest)request, (HttpServletResponse)response))
{
+                    return;
+                }
+            } finally {
+                BusFactory.setThreadDefaultBus(null);
+                if (origLoader != null) {
+                    origLoader.reset();
+                }
+            }
+        }
+        chain.doFilter(request, response);
+    }
     @Override
     protected void invoke(HttpServletRequest request, HttpServletResponse response) throws
ServletException {
         ClassLoaderHolder origLoader = null;

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java?rev=1349967&r1=1349966&r2=1349967&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFServlet.java
Wed Jun 13 17:37:00 2012
@@ -22,8 +22,10 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Method;
 import java.util.Collection;
+import java.util.Enumeration;
 
 import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 
 import org.apache.cxf.Bus;
@@ -69,7 +71,7 @@ public class CXFServlet extends CXFNonSp
             }
         }
         if (configLocation != null) {
-            wac = createSpringContext(wac, sc, configLocation);
+            wac = createSpringContext(wac, sc.getServletContext(), configLocation);
         }
         if (wac != null) {
             setBus((Bus)wac.getBean("cxf", Bus.class));
@@ -102,12 +104,24 @@ public class CXFServlet extends CXFNonSp
      * @return
      */
     private ApplicationContext createSpringContext(ApplicationContext ctx,
-                                                   ServletConfig sc,
+                                                   final ServletContext sc,
                                                    String location) {
         XmlWebApplicationContext ctx2 = new XmlWebApplicationContext();
         createdContext = ctx2;
-        ctx2.setServletConfig(sc);
-
+        ctx2.setServletConfig(new ServletConfig() {
+            public String getServletName() {
+                return "CXF";
+            }
+            public ServletContext getServletContext() {
+                return sc;
+            }
+            public String getInitParameter(String name) {
+                return sc.getInitParameter(name);
+            }
+            public Enumeration<String> getInitParameterNames() {
+                return sc.getInitParameterNames();
+            }
+        });
         Resource r = ctx2.getResource(location);
         try {
             InputStream in = r.getInputStream();

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java?rev=1349967&r1=1349966&r2=1349967&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java
Wed Jun 13 17:37:00 2012
@@ -130,8 +130,14 @@ public class ServletController {
             this.serviceListRelativePath = serviceListPath;
         }
     }
-    
+    public boolean filter(HttpServletRequest request, HttpServletResponse res) throws ServletException
{
+        return invoke(request, res, false);
+    }    
     public void invoke(HttpServletRequest request, HttpServletResponse res) throws ServletException
{
+        invoke(request, res, true);
+    }
+    public boolean invoke(HttpServletRequest request, HttpServletResponse res, boolean returnErrors)
+        throws ServletException {
         try {
             String pathInfo = request.getPathInfo() == null ? "" : request.getPathInfo();
             AbstractHTTPDestination d = destinationRegistry.getDestinationForPath(pathInfo,
true);
@@ -146,9 +152,12 @@ public class ServletController {
                 } else {
                     d = destinationRegistry.checkRestfulRequest(pathInfo);
                     if (d == null || d.getMessageObserver() == null) {
-                        LOG.warning("Can't find the the request for "
-                                    + request.getRequestURL() + "'s Observer ");
-                        generateNotFound(request, res);
+                        if (returnErrors) {
+                            LOG.warning("Can't find the the request for "
+                                + request.getRequestURL() + "'s Observer ");
+                            generateNotFound(request, res);
+                        }
+                        return false;
                     }  else { // the request should be a restful service request
                         updateDestination(request, d);
                         invokeDestination(request, res, d);
@@ -185,7 +194,7 @@ public class ServletController {
                         
                         if (selectedHandler != null) {
                             respondUsingQueryHandler(selectedHandler, res, ei, ctxUri, baseUri);
-                            return;
+                            return true;
                         }
                     } else {
                         updateDestination(request, d);
@@ -196,11 +205,11 @@ public class ServletController {
                         orig.reset();
                     }
                 }
-                
             }
         } catch (IOException e) {
             throw new ServletException(e);
         }
+        return true;
     }
 
     public void invokeDestination(final HttpServletRequest request, HttpServletResponse response,

Added: cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFFilterTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFFilterTest.java?rev=1349967&view=auto
==============================================================================
--- cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFFilterTest.java
(added)
+++ cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFFilterTest.java
Wed Jun 13 17:37:00 2012
@@ -0,0 +1,86 @@
+/**
+ * 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.servlet;
+
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.w3c.dom.Document;
+
+
+import com.meterware.httpunit.PostMethodWebRequest;
+import com.meterware.httpunit.WebLink;
+import com.meterware.httpunit.WebRequest;
+import com.meterware.httpunit.WebResponse;
+import com.meterware.servletunit.ServletUnitClient;
+
+import org.apache.cxf.helpers.DOMUtils;
+
+import org.junit.Test;
+
+public class CXFFilterTest extends AbstractServletTest {
+
+
+    @Override
+    protected String getConfiguration() {
+        return "/org/apache/cxf/systest/servlet/web-filter.xml";
+    }
+    
+    @Test
+    public void testGetServiceList() throws Exception {
+        
+        ServletUnitClient client = newClient();
+        client.setExceptionsThrownOnErrorStatus(false);
+
+        //test the '/' context get service list
+        WebResponse  res = client.getResponse(CONTEXT_URL + "/");
+        WebLink[] links = res.getLinks();
+        assertEquals("Wrong number of service links", 3, links.length);
+        
+        Set<String> links2 = new HashSet<String>();
+        for (WebLink l : links) {
+            links2.add(l.getURLString());
+        }
+
+        assertEquals("text/html", res.getContentType());
+    }
+
+    @Test
+    public void testPostInvokeServices() throws Exception {
+        newClient();
+        
+        WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/Greeter",
+                getClass().getResourceAsStream("GreeterMessage.xml"),
+                "text/xml; charset=UTF-8");
+        
+        WebResponse response = newClient().getResponse(req);
+
+        assertEquals("text/xml", response.getContentType());
+        assertEquals("UTF-8", response.getCharacterSet());
+
+        Document doc = DOMUtils.readXml(response.getInputStream());
+        assertNotNull(doc);
+        
+        addNamespace("h", "http://apache.org/hello_world_soap_http/types");
+        
+        assertValid("/s:Envelope/s:Body", doc);
+        assertValid("//h:sayHiResponse", doc);
+    }
+}

Added: cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/TestServlet.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/TestServlet.java?rev=1349967&view=auto
==============================================================================
--- cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/TestServlet.java
(added)
+++ cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/TestServlet.java
Wed Jun 13 17:37:00 2012
@@ -0,0 +1,30 @@
+/**
+ * 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.servlet;
+
+import javax.servlet.http.HttpServlet;
+
+/**
+ * 
+ */
+public class TestServlet extends HttpServlet {
+    private static final long serialVersionUID = 1L;
+
+}

Copied: cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-filter.xml
(from r1349926, cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-restful.xml)
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-filter.xml?p2=cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-filter.xml&p1=cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-restful.xml&r1=1349926&r2=1349967&rev=1349967&view=diff
==============================================================================
--- cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-restful.xml
(original)
+++ cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/servlet/web-filter.xml
Wed Jun 13 17:37:00 2012
@@ -27,7 +27,7 @@
 	<context-param>
 		<param-name>contextConfigLocation</param-name>
 		<param-value>
-			classpath:org/apache/cxf/systest/servlet/restful_service.xml
+			classpath:org/apache/cxf/systest/servlet/spring.xml
 		</param-value>
 	</context-param>
 
@@ -37,19 +37,27 @@
 		</listener-class>
 	</listener>
 	
-	<servlet>
-		<servlet-name>CXFServlet</servlet-name>
-		<display-name>CXF Servlet</display-name>
-		<servlet-class>
-			org.apache.cxf.transport.servlet.CXFServlet
-		</servlet-class>
-		<load-on-startup>1</load-on-startup>
-	</servlet>
+	<filter>
+		<filter-name>CXFFilter</filter-name>
+		<display-name>CXFFilter</display-name>
+		<filter-class>org.apache.cxf.transport.servlet.CXFServlet</filter-class>
+	</filter>
 
-	<servlet-mapping>
-		<servlet-name>CXFServlet</servlet-name>
+	<filter-mapping>
+		<filter-name>CXFFilter</filter-name>
 		<url-pattern>/*</url-pattern>
-	</servlet-mapping>
+	</filter-mapping>
 
 
+  <servlet>
+    <servlet-name>Default</servlet-name>
+    <display-name>Default</display-name>
+    <servlet-class>org.apache.cxf.systest.servlet.TestServlet</servlet-class>
+  </servlet>
+
+  <servlet-mapping>
+    <servlet-name>Default</servlet-name>
+    <url-pattern>/*</url-pattern>
+  </servlet-mapping>
+
 </web-app>
\ No newline at end of file



Mime
View raw message