cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1477340 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RuntimeExceptionMapper.java
Date Mon, 29 Apr 2013 21:03:34 GMT
Author: sergeyb
Date: Mon Apr 29 21:03:33 2013
New Revision: 1477340

URL: http://svn.apache.org/r1477340
Log:
[CXF-4988] Using MBW's produces if response content type had a wildcard, checking provider
interfaces during the application setup

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RuntimeExceptionMapper.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java?rev=1477340&r1=1477339&r2=1477340&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
Mon Apr 29 21:03:33 2013
@@ -34,6 +34,7 @@ import java.util.logging.Logger;
 
 import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.InternalServerErrorException;
+import javax.ws.rs.Produces;
 import javax.ws.rs.container.AsyncResponse;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -121,6 +122,11 @@ public class JAXRSOutInterceptor extends
         Response response = null;
         if (responseObj instanceof Response) {
             response = (Response)responseObj;
+            if (response.getStatus() == 500 
+                && message.getExchange().get(JAXRSUtils.EXCEPTION_FROM_MAPPER) !=
null) {
+                message.put(Message.RESPONSE_CODE, 500);
+                return;
+            }
         } else {
             int status = getStatus(message, responseObj != null ? 200 : 204);
             response = Response.status(status).entity(responseObj).build();
@@ -241,7 +247,14 @@ public class JAXRSOutInterceptor extends
         List<WriterInterceptor> writers = providerFactory
             .createMessageBodyWriterInterceptor(targetType, genericType, annotations, responseMediaType,
message);
         
-        responseMediaType = checkFinalContentType(responseMediaType);
+        OutputStream outOriginal = message.getContent(OutputStream.class);
+        if (writers == null || writers.isEmpty()) {
+            message.put(Message.CONTENT_TYPE, "text/plain");
+            message.put(Message.RESPONSE_CODE, 500);
+            writeResponseErrorMessage(outOriginal, "NO_MSG_WRITER", targetType.getSimpleName());
+            return;
+        }
+        responseMediaType = checkFinalContentType(responseMediaType, writers);
         String finalResponseContentType = JAXRSUtils.mediaTypeToString(responseMediaType);
         if (LOG.isLoggable(Level.FINE)) {
             LOG.fine("Response content type is: " + finalResponseContentType);
@@ -249,15 +262,7 @@ public class JAXRSOutInterceptor extends
         responseHeaders.putSingle(HttpHeaders.CONTENT_TYPE, finalResponseContentType);
         message.put(Message.CONTENT_TYPE, finalResponseContentType);
         
-        OutputStream outOriginal = message.getContent(OutputStream.class);
-        if (writers == null) {
-            message.put(Message.CONTENT_TYPE, "text/plain");
-            message.put(Message.RESPONSE_CODE, 500);
-            writeResponseErrorMessage(outOriginal, "NO_MSG_WRITER", targetType.getSimpleName());
-            return;
-        }
         boolean enabled = checkBufferingMode(message, writers, firstTry);
-        
         try {
             
             try {
@@ -390,13 +395,23 @@ public class JAXRSOutInterceptor extends
     }
     
     
-    private MediaType checkFinalContentType(MediaType mt) {
-        if (mt.isWildcardType() || mt.isWildcardSubtype() && mt.getType().equals("application"))
{
-            return MediaType.APPLICATION_OCTET_STREAM_TYPE;
-        } else {
-            return mt;
+    private MediaType checkFinalContentType(MediaType mt, List<WriterInterceptor> writers)
{
+        if (mt.isWildcardType() || mt.isWildcardSubtype()) {
+            int mbwIndex = writers.size() == 1 ? 0 : writers.size() - 1;
+            MessageBodyWriter<Object> writer = ((WriterInterceptorMBW)writers.get(mbwIndex)).getMBW();
+            Produces pm = writer.getClass().getAnnotation(Produces.class);
+            if (pm != null) {
+                List<MediaType> sorted = 
+                    JAXRSUtils.sortMediaTypes(JAXRSUtils.getMediaTypes(pm.value()), JAXRSUtils.MEDIA_TYPE_QS_PARAM);
+                mt = JAXRSUtils.intersectMimeTypes(sorted, mt).get(0);
+                if (mt.isWildcardType() || mt.isWildcardSubtype()) {
+                    return MediaType.APPLICATION_OCTET_STREAM_TYPE;    
+                }
+            } else {
+                return MediaType.APPLICATION_OCTET_STREAM_TYPE;
+            }
         }
-        
+        return mt;
     }
     
     private void setResponseDate(MultivaluedMap<String, Object> headers, boolean firstTry)
{

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RuntimeExceptionMapper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RuntimeExceptionMapper.java?rev=1477340&r1=1477339&r2=1477340&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RuntimeExceptionMapper.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RuntimeExceptionMapper.java
Mon Apr 29 21:03:33 2013
@@ -22,9 +22,7 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 import javax.ws.rs.ext.ExceptionMapper;
-import javax.ws.rs.ext.Provider;
 
-@Provider
 public class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException> {
 
     @Context 



Mime
View raw message