You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by cs...@apache.org on 2011/03/23 11:50:22 UTC

svn commit: r1084534 - in /cxf/trunk/rt/transports: http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/ http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/ http/src/main/java/org/apache/cxf/transport/servlet/ http/src/test/java/org/apac...

Author: cschneider
Date: Wed Mar 23 10:50:20 2011
New Revision: 1084534

URL: http://svn.apache.org/viewvc?rev=1084534&view=rev
Log:
Make servlet and osgi transports more alike. There is one failing test in jax-rs. Trying to fix that later

Added:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFSpringServlet.java
Modified:
    cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java
    cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java
    cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java

Modified: cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java?rev=1084534&r1=1084533&r2=1084534&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java (original)
+++ cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java Wed Mar 23 10:50:20 2011
@@ -50,18 +50,18 @@ public class OsgiServletController exten
     
     public void invoke(HttpServletRequest request, HttpServletResponse res) throws ServletException {
         try {
-            String address = request.getPathInfo() == null ? "" : request.getPathInfo();
-            AbstractHTTPDestination d = destinationRegistry.getDestinationForPath(address);
+            String pathInfo = request.getPathInfo() == null ? "" : request.getPathInfo();
+            AbstractHTTPDestination d = destinationRegistry.getDestinationForPath(pathInfo, true);
 
             if (d == null) {
                 if (!isHideServiceList && (request.getRequestURI().endsWith(serviceListRelativePath)
-                    || request.getRequestURI().endsWith(serviceListRelativePath + "/"))
-                    || StringUtils.isEmpty(request.getPathInfo())
-                    || "/".equals(request.getPathInfo())) {
+                    || request.getRequestURI().endsWith(serviceListRelativePath + "/")
+                    || StringUtils.isEmpty(pathInfo)
+                    || "/".equals(pathInfo))) {
                     updateDests(request, true);
                     serviceListGenerator.service(request, res);
                 } else {
-                    d = destinationRegistry.checkRestfulRequest(address);
+                    d = destinationRegistry.checkRestfulRequest(pathInfo);
                     if (d == null || d.getMessageObserver() == null) {
                         LOG.warning("Can't find the the request for "
                                     + request.getRequestURL() + "'s Observer ");
@@ -104,7 +104,7 @@ public class OsgiServletController exten
                             respondUsingQueryHandler(selectedHandler, res, ei, ctxUri, baseUri);
                             return;
                         }
-                    } else if ("/".equals(address) || address.length() == 0) {
+                    } else if ("/".equals(pathInfo) || pathInfo.length() == 0) {
                         updateDests(request);
                     }
                     invokeDestination(request, res, d);

Modified: cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java?rev=1084534&r1=1084533&r2=1084534&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java (original)
+++ cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java Wed Mar 23 10:50:20 2011
@@ -203,9 +203,7 @@ public class OsgiServletTest extends Ass
         EasyMock.expect(request.getContextPath()).andReturn("");
         EasyMock.expect(request.getServletPath()).andReturn("/cxf");
         paths.add(PATH);
-        // TODO How can the registry first return null then destination for the same path?
-        EasyMock.expect(registry.getDestinationForPath(PATH)).andReturn(null);
-        EasyMock.expect(registry.getDestinationForPath(PATH)).andReturn(destination);
+        EasyMock.expect(registry.getDestinationForPath(PATH)).andReturn(destination).anyTimes();
         EasyMock.expect(registry.checkRestfulRequest(EasyMock.isA(String.class))).andReturn(destination);
         EasyMock.expect(destination.getMessageObserver()).andReturn(observer);
         endpoint.addExtensor(extensor);
@@ -233,7 +231,7 @@ public class OsgiServletTest extends Ass
                                                 ? path
                                                 : PATH).anyTimes();
         if (path != null) {
-            EasyMock.expect(registry.getDestinationForPath(path)).andReturn(destination);
+            EasyMock.expect(registry.getDestinationForPath(path, true)).andReturn(destination);
         }
 
         if (destinationCount == -1) {

Added: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFSpringServlet.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFSpringServlet.java?rev=1084534&view=auto
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFSpringServlet.java (added)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFSpringServlet.java Wed Mar 23 10:50:20 2011
@@ -0,0 +1,78 @@
+/**
+ * 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.transport.servlet;
+
+import java.io.IOException;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.transport.servlet.servicelist.ServiceListGeneratorServlet;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.WebApplicationContextUtils;
+
+public class CXFSpringServlet extends HttpServlet {
+
+    private ServletTransportFactory servletTransportFactory;
+    private Bus bus;
+    private ServletController controller;
+    
+    public CXFSpringServlet() {
+    }
+
+    @Override
+    public void init(ServletConfig sc) throws ServletException {
+        super.init(sc);
+        WebApplicationContext wac = WebApplicationContextUtils.
+            getRequiredWebApplicationContext(sc.getServletContext());
+        this.bus = wac.getBean("cxf", Bus.class);
+        this.servletTransportFactory = wac.getBean(ServletTransportFactory.class);
+        this.controller = createServletController(sc);
+    }
+
+    private ServletController createServletController(ServletConfig servletConfig) {
+        HttpServlet serviceListGeneratorServlet = 
+            new ServiceListGeneratorServlet(servletTransportFactory.getRegistry(), bus);
+        ServletController newController =
+            new ServletController(servletTransportFactory.getRegistry(),
+                                  servletConfig,
+                                  serviceListGeneratorServlet);        
+        return newController;
+    }
+
+    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
+        throws ServletException, IOException {
+        try {
+            BusFactory.setThreadDefaultBus(bus);
+            controller.invoke(request, response);
+        } finally {
+            BusFactory.setThreadDefaultBus(null);
+        }
+    }
+
+    public void setBus(Bus bus) {
+        this.bus = bus;
+    }
+
+}

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=1084534&r1=1084533&r2=1084534&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 Mar 23 10:50:20 2011
@@ -19,8 +19,6 @@
 package org.apache.cxf.transport.servlet;
 
 import java.io.IOException;
-import java.io.OutputStream;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.servlet.ServletConfig;
@@ -32,7 +30,7 @@ import javax.servlet.http.HttpServletRes
 import org.apache.cxf.Bus;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
-import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.resource.ResourceManager;
 import org.apache.cxf.service.model.EndpointInfo;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 import org.apache.cxf.transport.http.DestinationRegistry;
@@ -40,24 +38,20 @@ import org.apache.cxf.transports.http.Qu
 import org.apache.cxf.transports.http.QueryHandlerRegistry;
 
 public class ServletController extends AbstractServletController {
-    
-    private static final Logger LOG = LogUtils.getL7dLogger(ServletController.class);  
-    
-    public ServletController(DestinationRegistry destinationRegistry,
-                             ServletConfig config, 
-                             HttpServlet serviceListGeneratorServlet) {
-        super(config, destinationRegistry, serviceListGeneratorServlet);
+    private static final Logger LOG = LogUtils.getL7dLogger(ServletController.class);
+
+    public ServletController(DestinationRegistry destinationRegistry, 
+                                 ServletConfig config,
+                                 HttpServlet serviceListGenerator) {
+        super(config, destinationRegistry, serviceListGenerator);
     }
-        
-    public void invoke(HttpServletRequest request, HttpServletResponse res) 
-        throws ServletException {
+
+    
+    public void invoke(HttpServletRequest request, HttpServletResponse res) throws ServletException {
         try {
-            EndpointInfo ei = new EndpointInfo();
-            
             String pathInfo = request.getPathInfo() == null ? "" : request.getPathInfo();
-            ei.setAddress(pathInfo);
-            
-            AbstractHTTPDestination d = destinationRegistry.getDestinationForPath(ei.getAddress(), true);
+            AbstractHTTPDestination d = destinationRegistry.getDestinationForPath(pathInfo, true);
+
             if (d == null) {
                 if (!isHideServiceList && (request.getRequestURI().endsWith(serviceListRelativePath)
                     || request.getRequestURI().endsWith(serviceListRelativePath + "/")
@@ -67,8 +61,8 @@ public class ServletController extends A
                     serviceListGenerator.service(request, res);
                 } else {
                     d = destinationRegistry.checkRestfulRequest(pathInfo);
-                    if (d == null) {                        
-                        LOG.warning("Can't find the request for " 
+                    if (d == null || d.getMessageObserver() == null) {
+                        LOG.warning("Can't find the the request for "
                                     + request.getRequestURL() + "'s Observer ");
                         generateNotFound(request, res);
                     }  else { // the request should be a restful service request
@@ -77,52 +71,51 @@ public class ServletController extends A
                     }
                 }
             } else {
-                ei = d.getEndpointInfo();
+                EndpointInfo ei = d.getEndpointInfo();
                 Bus bus = d.getBus();
-                if ("GET".equals(request.getMethod())
-                    && null != request.getQueryString() 
-                    && request.getQueryString().length() > 0
-                    && bus.getExtension(QueryHandlerRegistry.class) != null) {                    
-                    
-                    String ctxUri = request.getPathInfo();
-                    String baseUri = request.getRequestURL().toString() 
-                        + "?" + request.getQueryString();
-                    // update the EndPoint Address with request url
-                    updateDests(request);
-                    
-                    for (QueryHandler qh : bus.getExtension(QueryHandlerRegistry.class).getHandlers()) {
-                        if (qh.isRecognizedQuery(baseUri, ctxUri, ei)) {
-                            
-                            res.setContentType(qh.getResponseContentType(baseUri, ctxUri));
-                            OutputStream out = res.getOutputStream();
-                            try {
-                                qh.writeResponse(baseUri, ctxUri, ei, out);
-                            } catch (Exception e) {
-                                LogUtils.log(LOG, Level.WARNING,
-                                             qh.getClass().getName() 
-                                             + " Exception caught writing response.",
-                                             e);
-                                throw new ServletException(e);                                
-                            }
-                            out.flush();
+                ClassLoader orig = Thread.currentThread().getContextClassLoader();
+                try {
+                    ResourceManager manager = bus.getExtension(ResourceManager.class);
+                    if (manager != null) {
+                        ClassLoader loader = manager.resolveResource("", ClassLoader.class);
+                        if (loader != null) {
+                            //need to set the context classloader to the loader of the bundle
+                            Thread.currentThread().setContextClassLoader(loader);
+                        }
+                    }
+                    QueryHandlerRegistry queryHandlerRegistry = bus.getExtension(QueryHandlerRegistry.class);
+
+                    if (!StringUtils.isEmpty(request.getQueryString()) && queryHandlerRegistry != null) {
+                        
+                        // update the EndPoint Address with request url
+                        if ("GET".equals(request.getMethod())) {
+                            updateDests(request);
+                        }
+                        
+                        String ctxUri = request.getPathInfo();
+                        String baseUri = request.getRequestURL().toString()
+                            + "?" + request.getQueryString();
+
+                        QueryHandler selectedHandler = 
+                            findQueryHandler(queryHandlerRegistry, ei, ctxUri, baseUri);
+                        
+                        if (selectedHandler != null) {
+                            respondUsingQueryHandler(selectedHandler, res, ei, ctxUri, baseUri);
                             return;
-                        }   
+                        }
+                    } else if ("/".equals(pathInfo) || pathInfo.length() == 0) {
+                        updateDests(request);
                     }
-                } else {
-                    updateDests(request);
+                    invokeDestination(request, res, d);
+                } finally {
+                    Thread.currentThread().setContextClassLoader(orig);
                 }
                 
-                invokeDestination(request, res, d);
-            }
-        } catch (Fault ex) {
-            if (ex.getCause() instanceof RuntimeException) {
-                throw (RuntimeException)ex.getCause(); 
-            } else {
-                throw new ServletException(ex.getCause());
             }
         } catch (IOException e) {
             throw new ServletException(e);
-        } 
+        }
     }
 
+
 }

Modified: cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java?rev=1084534&r1=1084533&r2=1084534&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java (original)
+++ cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java Wed Mar 23 10:50:20 2011
@@ -26,6 +26,7 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.cxf.transport.MessageObserver;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 import org.apache.cxf.transport.http.DestinationRegistry;
 import org.easymock.classextension.EasyMock;
@@ -130,10 +131,12 @@ public class ServletControllerTest exten
         AbstractHTTPDestination dest = EasyMock.createMock(AbstractHTTPDestination.class);
         registry.checkRestfulRequest("");
         EasyMock.expectLastCall().andReturn(dest).atLeastOnce();
+        dest.getMessageObserver();
+        EasyMock.expectLastCall().andReturn(EasyMock.createMock(MessageObserver.class)).atLeastOnce();
         
         expectServiceListGeneratorNotCalled();
         
-        EasyMock.replay(req, registry, serviceListGenerator);
+        EasyMock.replay(req, registry, serviceListGenerator, dest);
         TestServletController sc = new TestServletController(registry, serviceListGenerator);
         sc.setHideServiceList(true);
         sc.invoke(req, res);