cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1458893 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ systests/jaxrs/src/test/java/org/apac...
Date Wed, 20 Mar 2013 15:41:34 GMT
Author: sergeyb
Date: Wed Mar 20 15:41:34 2013
New Revision: 1458893

URL: http://svn.apache.org/r1458893
Log:
Adding few more ContainerResponseFilter tests with minor follow-up updates to do with setting
the content type

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=1458893&r1=1458892&r2=1458893&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java Wed Mar
20 15:41:34 2013
@@ -129,6 +129,7 @@ public class JAXRSInvoker extends Abstra
                 return new MessageContentsList(Response.serverError().build());
             }
         } else {
+            setResponseContentTypeIfNeeded(exchange.getInMessage(), asyncObj);
             return new MessageContentsList(asyncObj);
         }
     }
@@ -259,27 +260,33 @@ public class JAXRSInvoker extends Abstra
 
                 return this.invoke(exchange, newParams, result);
             } catch (IOException ex) {
-                Response resp = JAXRSUtils.convertFaultToResponse(ex, exchange.getInMessage());
+                Response resp = JAXRSUtils.convertFaultToResponse(ex, inMessage);
                 if (resp == null) {
-                    resp = JAXRSUtils.convertFaultToResponse(ex, exchange.getInMessage());
+                    resp = JAXRSUtils.convertFaultToResponse(ex, inMessage);
                 }
                 return new MessageContentsList(resp);
             } catch (WebApplicationException ex) {
                 Response excResponse;
                 if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), 
-                        (String)exchange.getInMessage().get(Message.HTTP_REQUEST_METHOD)))
{
+                        (String)inMessage.get(Message.HTTP_REQUEST_METHOD))) {
                     excResponse = JAXRSUtils.createResponse(Collections.singletonList(subCri),

                                                             null, null, 200, true);
                 } else {
-                    excResponse = JAXRSUtils.convertFaultToResponse(ex, exchange.getInMessage());
+                    excResponse = JAXRSUtils.convertFaultToResponse(ex, inMessage);
                 }
                 return new MessageContentsList(excResponse);
             }
         }
-
+        setResponseContentTypeIfNeeded(inMessage, result);
         return result;
     }
     
+    private void setResponseContentTypeIfNeeded(Message inMessage, Object response) {
+        if (response instanceof Response) {
+            JAXRSUtils.setMessageContentType(inMessage, (Response)response);
+        }
+    }
+    
     private Object handleFault(Fault ex, Message inMessage, 
                                ClassResourceInfo cri, Method methodToInvoke) {
         String errorMessage = ex.getCause().getMessage();

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java?rev=1458893&r1=1458892&r2=1458893&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
Wed Mar 20 15:41:34 2013
@@ -168,15 +168,18 @@ public final class ServerProviderFactory
         for (Map.Entry<NameKey, ProviderInfo<T>> entry : boundFilters.entrySet())
{
             String entryName = entry.getKey().getName();
             if (entryName.equals(DEFAULT_FILTER_NAME_BINDING)) {
-                map.put(entry.getValue(), Collections.<String>emptyList());
+                ProviderInfo<T> provider = entry.getValue(); 
+                map.put(provider, Collections.<String>emptyList());
             } else {
                 map.add(entry.getValue(), entryName);
             }
         }
         List<ProviderInfo<T>> list = new LinkedList<ProviderInfo<T>>();
         for (Map.Entry<ProviderInfo<T>, List<String>> entry : map.entrySet())
{
-            if (names.containsAll(entry.getValue())) {
-                list.add(entry.getKey());
+            List<String> values = entry.getValue();
+            if (names.containsAll(values)) {
+                ProviderInfo<T> provider = entry.getKey();
+                list.add(provider);
             }
         }
         return list;

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=1458893&r1=1458892&r2=1458893&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Wed
Mar 20 15:41:34 2013
@@ -138,6 +138,7 @@ import org.apache.cxf.jaxrs.provider.Abs
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
 import org.apache.cxf.jaxrs.utils.multipart.AttachmentUtils;
+import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.phase.PhaseInterceptorChain;
@@ -437,7 +438,7 @@ public final class JAXRSUtils {
                                 MediaType pMediaType = matchProduceTypes(acceptType, ori);
                                 if (mMatched && cMatched && pMediaType !=
null) {
                                     subresourcesOnly = false;
-                                    map.putSingle(MediaType.class.getName(), pMediaType.toString());
+                                    map.putSingle(Message.CONTENT_TYPE, pMediaType.toString());
                                     candidateList.put(ori, map);
                                     added = true;
                                 } else {
@@ -481,7 +482,7 @@ public final class JAXRSUtils {
                                ori.getClassResourceInfo().getServiceClass().getName()).toString());
             }
             if (!ori.isSubResourceLocator()) {
-                List<String> responseContentType = matchedValues.remove(MediaType.class.getName());
+                List<String> responseContentType = matchedValues.remove(Message.CONTENT_TYPE);
                 if (responseContentType != null) {
                     message.getExchange().put(Message.CONTENT_TYPE, responseContentType.get(0));
                 }
@@ -1461,28 +1462,46 @@ public final class JAXRSUtils {
         return cls == null ? defaultExceptionType : cls;
     }
     
-    public static <T extends Throwable> Response convertFaultToResponse(T ex, Message
inMessage) {
-        
+    public static <T extends Throwable> Response convertFaultToResponse(T ex, Message
currentMessage) {
+        Message inMessage = currentMessage.getExchange().getInMessage();
+        Response response = null;
         if (ex.getClass() == WebApplicationException.class) {
             WebApplicationException webEx = (WebApplicationException)ex;
             if (webEx.getResponse().hasEntity() 
                 && webEx.getCause() == null
                 && MessageUtils.isTrue(inMessage.getContextualProperty(SUPPORT_WAE_SPEC_OPTIMIZATION)))
{
-                return webEx.getResponse();
+                response = webEx.getResponse();
+                
             }
         }
-        
-        ExceptionMapper<T>  mapper =
-            ServerProviderFactory.getInstance(inMessage).createExceptionMapper(ex.getClass(),
inMessage);
-        if (mapper != null) {
-            try {
-                return mapper.toResponse(ex);
-            } catch (Exception mapperEx) {
-                mapperEx.printStackTrace();
-                return Response.serverError().build();
+        if (response == null) {
+            ExceptionMapper<T>  mapper =
+                ServerProviderFactory.getInstance(inMessage).createExceptionMapper(ex.getClass(),
inMessage);
+            if (mapper != null) {
+                try {
+                    response = mapper.toResponse(ex);
+                } catch (Exception mapperEx) {
+                    mapperEx.printStackTrace();
+                    return Response.serverError().build();
+                }
+            }
+        }
+        setMessageContentType(currentMessage, response);
+        return response;
+    }
+    
+    public static void setMessageContentType(Message message, Response response) {
+        if (response != null) {
+            Object ct = response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
+            if (ct != null) {
+                Exchange ex = message.getExchange();
+                if (ex.getInMessage() == message) {
+                    ex.put(Message.CONTENT_TYPE, ct.toString());
+                } else {
+                    message.put(Message.CONTENT_TYPE, ct.toString());
+                }
             }
         }
-        return null;
         
     }
     
@@ -1542,7 +1561,9 @@ public final class JAXRSUtils {
                 } catch (IOException ex) {
                     throw new InternalServerErrorException(ex);
                 }
-                if (m.getExchange().get(Response.class) != null) {
+                Response response = m.getExchange().get(Response.class);
+                if (response != null) {
+                    setMessageContentType(m, response);
                     return true;
                 }
             }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java?rev=1458893&r1=1458892&r2=1458893&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
Wed Mar 20 15:41:34 2013
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import javax.annotation.Priority;
+import javax.ws.rs.InternalServerErrorException;
 import javax.ws.rs.NameBinding;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.container.ContainerRequestContext;
@@ -42,6 +43,7 @@ import javax.ws.rs.container.PreMatching
 import javax.ws.rs.container.ResourceInfo;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.FeatureContext;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 import javax.ws.rs.ext.ReaderInterceptor;
 import javax.ws.rs.ext.ReaderInterceptorContext;
@@ -112,12 +114,20 @@ public class BookServer20 extends Abstra
 
         @Override
         public void filter(ContainerRequestContext context) throws IOException {
+            if ("true".equals(context.getProperty("DynamicPrematchingFilter"))) {
+                throw new RuntimeException();
+            }
             context.setProperty("FirstPrematchingFilter", "true");
             
             UriInfo ui = context.getUriInfo();
             String path = ui.getPath(false);
             if ("wrongpath".equals(path)) {
                 context.setRequestUri(URI.create("/bookstore/bookheaders/simple"));
+            } else if ("throwException".equals(path)) {
+                context.setProperty("filterexception", "prematch");
+                throw new InternalServerErrorException(
+                    Response.status(500).type("text/plain")
+                        .entity("Prematch filter error").build());
             }
         }
         
@@ -176,9 +186,16 @@ public class BookServer20 extends Abstra
     
     @CustomHeaderAdded
     private static class PostMatchContainerRequestFilter implements ContainerRequestFilter
{
-
+        @Context
+        private UriInfo ui;
         @Override
         public void filter(ContainerRequestContext context) throws IOException {
+            if (ui.getQueryParameters().getFirst("throwException") != null) {
+                context.setProperty("filterexception", "postmatch");
+                throw new InternalServerErrorException(
+                    Response.status(500).type("text/plain")
+                        .entity("Postmatch filter error").build());
+            }
             String value = context.getHeaders().getFirst("Book");
             if (value != null) {
                 context.getHeaders().addFirst("Book", value + "3");
@@ -205,6 +222,13 @@ public class BookServer20 extends Abstra
         public void filter(ContainerRequestContext requestContext,
                            ContainerResponseContext responseContext) throws IOException {
             String ct = responseContext.getMediaType().toString();
+            if (requestContext.getProperty("filterexception") != null) {
+                if (!"text/plain".equals(ct)) {
+                    throw new RuntimeException();
+                }
+                responseContext.getHeaders().putSingle("FilterException", 
+                                                       requestContext.getProperty("filterexception"));
+            }
             ct += ";charset=";
             responseContext.getHeaders().putSingle("Content-Type", ct);
             responseContext.getHeaders().add("Response", "OK");
@@ -218,8 +242,11 @@ public class BookServer20 extends Abstra
         @Override
         public void filter(ContainerRequestContext requestContext,
                            ContainerResponseContext responseContext) throws IOException {
-            if (!responseContext.getHeaders().containsKey("Response")
-                || !responseContext.getHeaders().containsKey("DynamicResponse")) {
+            if (!responseContext.getHeaders().containsKey("Response")) {
+                throw new RuntimeException();
+            }
+            if (!responseContext.getHeaders().containsKey("DynamicResponse")
+                && !"Prematch filter error".equals(responseContext.getEntity()))
{
                 throw new RuntimeException();
             }
             responseContext.getHeaders().add("Response2", "OK2");
@@ -236,8 +263,10 @@ public class BookServer20 extends Abstra
         public void filter(ContainerRequestContext requestContext,
                            ContainerResponseContext responseContext) throws IOException {
             responseContext.getHeaders().add("Custom", "custom");
-            Book book = (Book)responseContext.getEntity();
-            responseContext.setEntity(new Book(book.getName(), 1 + book.getId()), null, null);
+            if (!responseContext.getEntity().equals("Postmatch filter error")) {
+                Book book = (Book)responseContext.getEntity();
+                responseContext.setEntity(new Book(book.getName(), 1 + book.getId()), null,
null);
+            }
         }
         
     }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java?rev=1458893&r1=1458892&r2=1458893&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Wed Mar 20 15:41:34 2013
@@ -93,6 +93,39 @@ public class JAXRS20ClientServerBookTest
     }
     
     @Test
+    public void testPreMatchContainerFilterThrowsException() {
+        String address = "http://localhost:" + PORT + "/throwException";
+        WebClient wc = WebClient.create(address);
+        Response response = wc.get();
+        assertEquals(500, response.getStatus());
+        assertEquals("Prematch filter error", response.readEntity(String.class));
+        assertEquals("prematch", response.getHeaderString("FilterException"));
+        assertEquals("OK", response.getHeaderString("Response"));
+        assertEquals("OK2", response.getHeaderString("Response2"));
+        assertNull(response.getHeaderString("DynamicResponse"));
+        assertNull(response.getHeaderString("Custom"));
+        assertEquals("serverWrite", response.getHeaderString("ServerWriterInterceptor"));
+        assertEquals("text/plain;charset=us-ascii", response.getMediaType().toString());
+    }
+    
+    @Test
+    public void testPostMatchContainerFilterThrowsException() {
+        String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple?throwException";
+        WebClient wc = WebClient.create(address);
+        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+        Response response = wc.get();
+        assertEquals(500, response.getStatus());
+        assertEquals("Postmatch filter error", response.readEntity(String.class));
+        assertEquals("postmatch", response.getHeaderString("FilterException"));
+        assertEquals("OK", response.getHeaderString("Response"));
+        assertEquals("OK2", response.getHeaderString("Response2"));
+        assertEquals("Dynamic", response.getHeaderString("DynamicResponse"));
+        assertEquals("custom", response.getHeaderString("Custom"));
+        assertEquals("serverWrite", response.getHeaderString("ServerWriterInterceptor"));
+        assertEquals("text/plain;charset=us-ascii", response.getMediaType().toString());
+    }
+    
+    @Test
     public void testGetBookWrongPath() {
         String address = "http://localhost:" + PORT + "/wrongpath";
         doTestGetBook(address);



Mime
View raw message