cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1458912 - in /cxf/branches/2.7.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Wed, 20 Mar 2013 16:09:14 GMT
Author: sergeyb
Date: Wed Mar 20 16:09:13 2013
New Revision: 1458912

URL: http://svn.apache.org/r1458912
Log:
Merged revisions 1458893 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1458893 | sergeyb | 2013-03-20 15:41:34 +0000 (Wed, 20 Mar 2013) | 1 line
  
  Adding few more ContainerResponseFilter tests with minor follow-up updates to do with setting
the content type
........

Modified:
    cxf/branches/2.7.x-fixes/   (props changed)
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1458893

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=1458912&r1=1458911&r2=1458912&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
Wed Mar 20 16:09:13 2013
@@ -123,6 +123,7 @@ public class JAXRSInvoker extends Abstra
             return handleFault(new Fault((Throwable)asyncObj), 
                                exchange.getInMessage(), null, null);    
         } else {
+            setResponseContentTypeIfNeeded(exchange.getInMessage(), asyncObj);
             return new MessageContentsList(asyncObj);
         }
     }
@@ -255,26 +256,32 @@ 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(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/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=1458912&r1=1458911&r2=1458912&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
Wed Mar 20 16:09:13 2013
@@ -134,6 +134,7 @@ import org.apache.cxf.jaxrs.model.URITem
 import org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider;
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
 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.transport.http.AbstractHTTPDestination;
@@ -407,7 +408,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 {
@@ -450,7 +451,7 @@ public final class JAXRSUtils {
                                resource.getServiceClass().getName()).toString());
             }
             if (!ori.isSubResourceLocator()) {
-                List<String> responseContentType = values.remove(MediaType.class.getName());
+                List<String> responseContentType = values.remove(Message.CONTENT_TYPE);
                 if (responseContentType != null) {
                     message.getExchange().put(Message.CONTENT_TYPE, responseContentType.get(0));
                 }
@@ -1411,28 +1412,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 =
-            ProviderFactory.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 =
+                ProviderFactory.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;
         
     }
     
@@ -1489,7 +1508,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/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java?rev=1458912&r1=1458911&r2=1458912&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
Wed Mar 20 16:09:13 2013
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import javax.ws.rs.BindingPriority;
+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.Configurable;
 import javax.ws.rs.core.Context;
+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/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java?rev=1458912&r1=1458911&r2=1458912&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Wed Mar 20 16:09:13 2013
@@ -94,6 +94,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