cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r833119 [2/2] - in /cxf/branches/2.2.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/ rt/frontend/jaxrs/...
Date Thu, 05 Nov 2009 18:41:21 GMT
Modified: cxf/branches/2.2.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java?rev=833119&r1=833118&r2=833119&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
Thu Nov  5 18:41:19 2009
@@ -19,16 +19,25 @@
 package org.apache.cxf.transport.servlet;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Arrays;
+import java.util.LinkedList;
 import java.util.List;
 
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
 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.helpers.IOUtils;
+
 
 
 public abstract class AbstractHTTPServlet extends HttpServlet {
@@ -39,39 +48,75 @@
     private static final List<String> KNOWN_HTTP_VERBS = 
         Arrays.asList(new String[]{"POST", "GET", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE"});
     
+    private static final String STATIC_RESOURCES_PARAMETER = "static-resources-list";
+    
+    private static final String REDIRECTS_PARAMETER = "redirects-list";
+    private static final String REDIRECT_SERVLET_NAME_PARAMETER = "redirect-servlet-name";
+    private static final String REDIRECT_SERVLET_PATH_PARAMETER = "redirect-servlet-path";
+    
+    private List<String> staticResourcesList;
+    private List<String> redirectList; 
+    private String dispatcherServletPath;
+    private String dispatcherServletName;
+    
+    public void init(ServletConfig servletConfig) throws ServletException {
+        super.init(servletConfig);
+
+        staticResourcesList = parseListSequence(servletConfig.getInitParameter(STATIC_RESOURCES_PARAMETER));
+        
+        redirectList = parseListSequence(servletConfig.getInitParameter(REDIRECTS_PARAMETER));
+        dispatcherServletName = servletConfig.getInitParameter(REDIRECT_SERVLET_NAME_PARAMETER);
+        dispatcherServletPath = servletConfig.getInitParameter(REDIRECT_SERVLET_PATH_PARAMETER);
+    }
+    
+    private static List<String> parseListSequence(String values) {
+        if (values != null) {
+            List<String> list = new LinkedList<String>();
+            String[] pathValues = values.split(" ");
+            for (String value : pathValues) {
+                String theValue = value.trim();
+                if (theValue.length() > 0) {
+                    list.add(theValue);
+                }
+            }
+            return list;
+        } else {
+            return null;
+        }
+    }
     
     protected void doPost(HttpServletRequest request, HttpServletResponse response) 
         throws ServletException {
-        invoke(request, response);
+        handleRequest(request, response);
     }
 
     protected void doGet(HttpServletRequest request, HttpServletResponse response) 
         throws ServletException {
-        invoke(request, response);
+        handleRequest(request, response);
     }
 
     @Override
     protected void doDelete(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
-        invoke(request, response);
+        handleRequest(request, response);
     }
 
     @Override
     protected void doPut(HttpServletRequest request, HttpServletResponse response) 
         throws ServletException, IOException {
-        invoke(request, response);
+        handleRequest(request, response);
     }
     
     @Override
     protected void doHead(HttpServletRequest request, HttpServletResponse response) 
         throws ServletException, IOException {
-        invoke(request, response);
+        handleRequest(request, response);
     }
     
     @Override
     protected void doOptions(HttpServletRequest request, HttpServletResponse response) 
         throws ServletException, IOException {
-        invoke(request, response);
+        handleRequest(request, response);
     }
     
     /**
@@ -100,11 +145,105 @@
         if (KNOWN_HTTP_VERBS.contains(method)) {
             super.service(request, response);
         } else {
-            invoke(request, response);
+            handleRequest(request, response);
+        }
+    }
+    
+    protected void handleRequest(HttpServletRequest request, HttpServletResponse response)

+        throws ServletException {
+        if (staticResourcesList != null 
+            && matchPath(staticResourcesList, request.getPathInfo())) {
+            serveStaticContent(request, response, request.getPathInfo());
+            return;
         }
+        if (redirectList != null 
+            && matchPath(redirectList, request.getPathInfo())) {
+            redirect(request, response, request.getPathInfo());
+            return;
+        }
+        invoke(request, response);
+    }
+    
+    private static boolean matchPath(List<String> values, String pathInfo) {
+        for (String value : values) {
+            if (pathInfo.matches(value)) {
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    protected void serveStaticContent(HttpServletRequest request, 
+                                      HttpServletResponse response,
+                                      String pathInfo) throws ServletException {
+        InputStream is = super.getServletContext().getResourceAsStream(pathInfo);
+        if (is == null) {
+            throw new ServletException("Static resource " + pathInfo + " is not available");
+        }
+        try {
+            ServletOutputStream os = response.getOutputStream();
+            IOUtils.copy(is, os);
+            os.flush();
+        } catch (IOException ex) {
+            throw new ServletException("Static resource " + pathInfo 
+                                       + " can not be written to the output stream");
+        }
+        
+    }
+    
+    protected void redirect(HttpServletRequest request, HttpServletResponse response, String
pathInfo) 
+        throws ServletException {
+        
+        String theServletPath = dispatcherServletPath == null ? "/" : dispatcherServletPath;
+        
+        ServletContext sc = super.getServletContext();
+        RequestDispatcher rd = dispatcherServletName != null 
+            ? sc.getNamedDispatcher(dispatcherServletName) 
+            : sc.getRequestDispatcher(theServletPath + pathInfo);
+        if (rd == null) {
+            throw new ServletException("No RequestDispatcher can be created for path " +
pathInfo);
+        }
+        try {
+            HttpServletRequestFilter servletRequest = 
+                new HttpServletRequestFilter(request, pathInfo, theServletPath);
+            rd.forward(servletRequest, response);
+        } catch (Throwable ex) {
+            throw new ServletException("RequestDispatcher for path " + pathInfo + " has failed");
+        }   
     }
     
     protected abstract void invoke(HttpServletRequest request, HttpServletResponse response)

         throws ServletException;
+    
+    private static class HttpServletRequestFilter extends HttpServletRequestWrapper {
+        
+        private String pathInfo;
+        private String servletPath;
+        
+        public HttpServletRequestFilter(HttpServletRequest request, 
+                                        String pathInfo,
+                                        String servletPath) {
+            super(request);
+            this.pathInfo = pathInfo;
+            this.servletPath = servletPath;
+        }
+        
+        @Override
+        public String getServletPath() {
+            return servletPath;
+        }
+        
+        @Override
+        public String getPathInfo() {
+            return pathInfo; 
+        }
+        
+        @Override
+        public String getRequestURI() {
+            String query = super.getQueryString();
+            return query != null ? pathInfo + "?" + query : pathInfo; 
+        }
+        
+    }
 
 }

Modified: cxf/branches/2.2.x-fixes/systests/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/systests/jaxrs/pom.xml?rev=833119&r1=833118&r2=833119&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/systests/jaxrs/pom.xml (original)
+++ cxf/branches/2.2.x-fixes/systests/jaxrs/pom.xml Thu Nov  5 18:41:19 2009
@@ -65,6 +65,21 @@
     </profiles>
     <dependencies>
         <dependency>
+          <groupId>org.apache.ant</groupId>
+          <artifactId>ant</artifactId>
+          <version>1.7.0</version>
+        </dependency>
+        <dependency>  
+           <groupId>jetty</groupId>  
+           <artifactId>jsp-api</artifactId> 
+           <version>2.1-6.0.2</version>
+       </dependency>
+        <dependency>  
+           <groupId>jetty</groupId>  
+           <artifactId>jsp</artifactId> 
+           <version>2.1-6.0.2</version>
+       </dependency> 
+        <dependency>
             <groupId>org.apache.geronimo.specs</groupId>
             <artifactId>geronimo-servlet_2.5_spec</artifactId>
         </dependency>

Modified: cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java?rev=833119&r1=833118&r2=833119&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
(original)
+++ cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
Thu Nov  5 18:41:19 2009
@@ -44,10 +44,14 @@
         
         providers.add(p);
         providers.add(new GenericHandlerWriter());
+        providers.add(new FaultyRequestHandler());
         sf.setProviders(providers);
-        List<Interceptor> ints = new ArrayList<Interceptor>();
-        ints.add(new CustomOutInterceptor());
-        sf.setOutInterceptors(ints);
+        List<Interceptor> outInts = new ArrayList<Interceptor>();
+        outInts.add(new CustomOutInterceptor());
+        sf.setOutInterceptors(outInts);
+        List<Interceptor> outFaultInts = new ArrayList<Interceptor>();
+        outFaultInts.add(new CustomOutFaultInterceptor());
+        sf.setOutFaultInterceptors(outFaultInts);
         sf.setResourceProvider(BookStore.class,
                                new SingletonResourceProvider(new BookStore(), true));
         sf.setAddress("http://localhost:9080/");

Modified: cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=833119&r1=833118&r2=833119&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++ cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Thu Nov  5 18:41:19 2009
@@ -33,6 +33,7 @@
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
+import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.DefaultValue;
@@ -116,6 +117,14 @@
     }
     
     @GET
+    @Path("propogateexception3")
+    public Book propogateException3() throws BookNotFoundFault {
+        PhaseInterceptorChain.getCurrentMessage().getExchange()
+            .put("org.apache.cxf.systest.for-out-fault-interceptor", Boolean.TRUE);
+        throw new BookNotFoundFault("Book Exception");
+    }
+    
+    @GET
     @Path("books/check/{id}")
     @Produces("text/plain")
     public boolean checkBook(@PathParam("id") Long id) {
@@ -447,6 +456,15 @@
     }
     
     @POST
+    @Path("/books/customstatus")
+    @Produces("text/xml")
+    @Consumes("text/xml")
+    public Response addBookCustomFailure(Book book, @Context HttpServletResponse response)
{
+        response.setStatus(333);
+        return null;
+    }
+    
+    @POST
     @Path("/booksinfo")
     @Produces("text/xml")
     @Consumes("application/xml")

Modified: cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=833119&r1=833118&r2=833119&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Thu Nov  5 18:41:19 2009
@@ -76,6 +76,20 @@
     }
     
     @Test
+    public void testPropogateException3() throws Exception {
+        String data = "<nobook/>";
+        getAndCompare("http://localhost:9080/bookstore/propogateexception3",
+                      data, "application/xml", 500);
+    }
+    
+    @Test
+    public void testPropogateException4() throws Exception {
+        String data = "<nobook/>";
+        getAndCompare("http://localhost:9080/bookstore/propogateexception4",
+                      data, "application/xml", 500);
+    }
+    
+    @Test
     public void testWebApplicationException() throws Exception {
         getAndCompare("http://localhost:9080/bookstore/webappexception",
                       "This is a WebApplicationException",
@@ -288,8 +302,8 @@
     
     @Test
     public void testNoMessageWriterFound() throws Exception {
-        String msg1 = ".No message body writer found for response class : GregorianCalendar.";
-        String msg2 = ".No message body writer found for response class : Calendar.";
+        String msg1 = "No message body writer has been found for response class GregorianCalendar.";
+        String msg2 = "No message body writer has been found for response class Calendar.";
         
         getAndCompareStrings("http://localhost:9080/bookstore/timetable", 
                              new String[]{msg1, msg2}, "*/*", 500);
@@ -585,6 +599,21 @@
     }
     
     @Test
+    public void testAddBookNoBody() throws Exception {
+        PostMethod post = new PostMethod("http://localhost:9080/bookstore/books");
+        post.setRequestHeader("Content-Type", "application/xml");
+        HttpClient httpclient = new HttpClient();
+        
+        try {
+            int result = httpclient.executeMethod(post);
+            assertEquals(400, result);
+        } finally {
+            // Release current connection to the connection pool once you are done
+            post.releaseConnection();
+        }
+    }
+    
+    @Test
     public void testAddBook() throws Exception {
         doAddBook("http://localhost:9080/bookstore/books");               
     }
@@ -615,6 +644,27 @@
         }
     }
     
+    
+    @Test
+    public void testAddBookCustomFailureStatus() throws Exception {
+        String endpointAddress = "http://localhost:9080/bookstore/books/customstatus";
+
+        File input = new File(getClass().getResource("resources/update_book.txt").toURI());
+        PostMethod put = new PostMethod(endpointAddress);
+        RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
+        put.setRequestEntity(entity);
+        HttpClient httpclient = new HttpClient();
+
+        try {
+            int result = httpclient.executeMethod(put);
+            assertEquals(333, result);
+        } finally {
+            // Release current connection to the connection pool once you are
+            // done
+            put.releaseConnection();
+        }
+    }
+    
     @Test
     public void testUpdateBook() throws Exception {
         String endpointAddress = "http://localhost:9080/bookstore/books";

Modified: cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java?rev=833119&r1=833118&r2=833119&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
(original)
+++ cxf/branches/2.2.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
Thu Nov  5 18:41:19 2009
@@ -355,6 +355,22 @@
         assertEquals("java.jpg", cd2.getParameter("filename"));
     }
     
+    @Test
+    public void testMultipartRequestNoBody() throws Exception { 
+        PostMethod post = new PostMethod("http://localhost:9085/bookstore/books/image");
+        String ct = "multipart/mixed";
+        post.setRequestHeader("Content-Type", ct);
+        HttpClient httpclient = new HttpClient();
+        
+        try {
+            int result = httpclient.executeMethod(post);
+            assertEquals(400, result);
+        } finally {
+            // Release current connection to the connection pool once you are done
+            post.releaseConnection();
+        }
+    }
+    
     private void doAddBook(String address, String resourceName, int status) throws Exception
{
         doAddBook("multipart/related", address, resourceName, status);
     }



Mime
View raw message