cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6577] MessageContextImpl updates
Date Thu, 03 Sep 2015 12:33:22 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 5ec1ddb10 -> a8fa70b8a


[CXF-6577] MessageContextImpl updates


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/a8fa70b8
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/a8fa70b8
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/a8fa70b8

Branch: refs/heads/master
Commit: a8fa70b8a5b0047837f0f7b4bce34024ca6a9a05
Parents: 5ec1ddb
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Thu Sep 3 13:33:05 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Thu Sep 3 13:33:05 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/ext/MessageContextImpl.java       | 36 ++++++++++------
 .../jaxrs/interceptor/JAXRSOutInterceptor.java  | 45 ++++++++++++--------
 .../cxf/jaxrs/ext/MessageContextImplTest.java   | 23 ++++++++++
 .../org/apache/cxf/systest/jaxrs/BookStore.java |  9 ++--
 .../jaxrs/JAXRSClientServerBookTest.java        |  2 +-
 5 files changed, 80 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/a8fa70b8/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
index 78496ac..872bda0 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
@@ -56,6 +56,7 @@ import org.apache.cxf.jaxrs.impl.ProvidersImpl;
 import org.apache.cxf.jaxrs.interceptor.AttachmentInputInterceptor;
 import org.apache.cxf.jaxrs.interceptor.AttachmentOutputInterceptor;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.ExchangeImpl;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
@@ -82,15 +83,19 @@ public class MessageContextImpl implements MessageContext {
         if (keyValue.equals("WRITE-" + Message.ATTACHMENTS)) {
             return m.getExchange().getOutMessage().get(Message.ATTACHMENTS);
         }
-        Object value = m.get(key);
+        
+        Message currentMessage = getCurrentMessage();
+        Object value = currentMessage.get(key);
         if (value == null) {
             if (Message.class.getName().equals(key)) {
-                return m;
+                return currentMessage;
             }
-            if (m.getExchange() != null) {
-                Message inMessage = m.getExchange().getInMessage();
-                if (inMessage != null && inMessage != m) {
-                    value = inMessage.get(key);
+            Exchange exchange = currentMessage.getExchange();
+            if (exchange != null) {
+                Message otherMessage = exchange.getInMessage() == currentMessage 
+                    ? exchange.getOutMessage() : exchange.getInMessage();
+                if (otherMessage != null) {
+                    value = otherMessage.get(key);
                 }
                 if (value == null) {
                     value = m.getExchange().get(key);
@@ -100,6 +105,14 @@ public class MessageContextImpl implements MessageContext {
         return value;
     }
     
+    private Message getCurrentMessage() {
+        Message currentMessage = JAXRSUtils.getCurrentMessage();
+        if (currentMessage == null) {
+            currentMessage = m;
+        }
+        return currentMessage;
+    }
+
     public <T> T getContent(Class<T> format) {
         if (MessageUtils.isRequestor(m) && m.getExchange().getInMessage() != null)
{
             Message inMessage = m.getExchange().getInMessage();
@@ -176,14 +189,9 @@ public class MessageContextImpl implements MessageContext {
         if (MultipartBody.OUTBOUND_MESSAGE_ATTACHMENTS.equals(key.toString())) {
             convertToAttachments(value);
         }
-        m.put(key.toString(), value);
-        if (!MessageUtils.isRequestor(m)) {
-            m.getExchange().put(key.toString(), value);
-            Message outMessage = m.getExchange().getOutMessage();
-            if (outMessage != null && outMessage != m) {
-                outMessage.put(key.toString(), value);
-            }
-        }
+        Message currentMessage = getCurrentMessage();
+        currentMessage.put(key.toString(), value);
+        currentMessage.getExchange().put(key.toString(), value);
             
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/a8fa70b8/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
index bf0f7ec..5cafd8c 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
@@ -125,7 +125,6 @@ public class JAXRSOutInterceptor extends AbstractOutDatabindingInterceptor
{
         return customStatus == null ? defaultValue : (Integer)customStatus;
     }
     
-    @SuppressWarnings("unchecked")
     private void serializeMessage(ServerProviderFactory providerFactory,
                                   Message message, 
                                   Response theResponse, 
@@ -163,22 +162,8 @@ public class JAXRSOutInterceptor extends AbstractOutDatabindingInterceptor
{
         response.setEntity(entity, annotations);
         
         // Prepare the headers
-        MultivaluedMap<String, Object> responseHeaders = response.getMetadata();
-        Map<String, List<Object>> userHeaders = 
-            (Map<String, List<Object>>)message.get(Message.PROTOCOL_HEADERS);
-        if (firstTry && userHeaders != null) {
-            responseHeaders.putAll(userHeaders);
-        }
-        if (entity != null) {
-            String initialResponseContentType = (String)message.get(Message.CONTENT_TYPE);
-            if (initialResponseContentType != null && !responseHeaders.containsKey(HttpHeaders.CONTENT_TYPE))
{
-                responseHeaders.putSingle(HttpHeaders.CONTENT_TYPE, initialResponseContentType);
-            }
-        }
-        
-        message.put(Message.PROTOCOL_HEADERS, responseHeaders);
-        
-        setResponseDate(responseHeaders, firstTry);
+        MultivaluedMap<String, Object> responseHeaders = 
+            prepareResponseHeaders(message, response, entity, firstTry);
                
         // Run the filters
         try {
@@ -273,6 +258,32 @@ public class JAXRSOutInterceptor extends AbstractOutDatabindingInterceptor
{
         }
     }
     
+    private MultivaluedMap<String, Object> prepareResponseHeaders(Message message,

+                                                                  ResponseImpl response,
+                                                                  Object entity,
+                                                                  boolean firstTry) {
+        MultivaluedMap<String, Object> responseHeaders = response.getMetadata();
+        @SuppressWarnings("unchecked")
+        Map<String, List<Object>> userHeaders = (Map<String, List<Object>>)message.get(Message.PROTOCOL_HEADERS);
+        if (firstTry && userHeaders != null) {
+            responseHeaders.putAll(userHeaders);
+        }
+        if (entity != null) {
+            Object customContentType = responseHeaders.getFirst(HttpHeaders.CONTENT_TYPE);
+            if (customContentType == null) {
+                String initialResponseContentType = (String)message.get(Message.CONTENT_TYPE);
+                if (initialResponseContentType != null) {
+                    responseHeaders.putSingle(HttpHeaders.CONTENT_TYPE, initialResponseContentType);
+                }
+            } else {
+                message.put(Message.CONTENT_TYPE, customContentType.toString());
+            }
+        }
+        message.put(Message.PROTOCOL_HEADERS, responseHeaders);
+        setResponseDate(responseHeaders, firstTry);
+        return responseHeaders;
+    }
+
     private MediaType getResponseMediaType(Object mediaTypeHeader) {
         MediaType responseMediaType;
         if (mediaTypeHeader instanceof MediaType) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/a8fa70b8/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/MessageContextImplTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/MessageContextImplTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/MessageContextImplTest.java
index 0865964..b40c669 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/MessageContextImplTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/MessageContextImplTest.java
@@ -61,6 +61,29 @@ public class MessageContextImplTest extends Assert {
         assertEquals("b", mc.get("a"));
         assertNull(mc.get("b"));
     }
+    @Test
+    public void testGetPropertyFromExchange() {
+        Message m = new MessageImpl();
+        Exchange ex = new ExchangeImpl();
+        ex.put("a", "b");
+        ex.setInMessage(m);
+        MessageContext mc = new MessageContextImpl(m);
+        assertEquals("b", mc.get("a"));
+        assertNull(mc.get("b"));
+    }
+    @Test
+    public void testGetPropertyFromOtherMessage() {
+        Message m1 = new MessageImpl();
+        Message m2 = new MessageImpl();
+        m2.put("a", "b");
+        
+        Exchange ex = new ExchangeImpl();
+        ex.setInMessage(m1);
+        ex.setOutMessage(m2);
+        MessageContext mc = new MessageContextImpl(m1);
+        assertEquals("b", mc.get("a"));
+        assertNull(mc.get("b"));
+    }
     
     @Test
     public void testGetUriInfo() {

http://git-wip-us.apache.org/repos/asf/cxf/blob/a8fa70b8/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index 90564a8..06ced26 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -1218,8 +1218,8 @@ public class BookStore {
     @GET
     @Path("/books/statusFromStream")
     @Produces("text/xml")
-    public StreamingOutput statusFromStream() {
-        return new ResponseStreamingOutputImpl();
+    public Response statusFromStream() {
+        return Response.ok(new ResponseStreamingOutputImpl()).type("text/plain").build();
     }
     
     @SuppressWarnings("rawtypes")
@@ -1712,9 +1712,12 @@ public class BookStore {
     }
     private class ResponseStreamingOutputImpl implements StreamingOutput {
         public void write(OutputStream output) throws IOException, WebApplicationException
{
+            if (!"text/plain".equals(BookStore.this.messageContext.get("Content-Type")))
{
+                throw new RuntimeException();
+            }
             BookStore.this.messageContext.put(Message.RESPONSE_CODE, 503);
             MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
-            headers.putSingle("Content-Type", "text/plain");
+            headers.putSingle("Content-Type", "text/custom+plain");
             headers.putSingle("CustomHeader", "CustomValue");
             BookStore.this.messageContext.put(Message.PROTOCOL_HEADERS, headers);
             

http://git-wip-us.apache.org/repos/asf/cxf/blob/a8fa70b8/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index a9c6e96..07a93f6 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -1432,7 +1432,7 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         wc.accept("text/xml");
         Response r = wc.get();
         assertEquals(503, r.getStatus());
-        assertEquals("text/plain", r.getMediaType().toString());
+        assertEquals("text/custom+plain", r.getMediaType().toString());
         assertEquals("CustomValue", r.getHeaderString("CustomHeader"));
         assertEquals("Response is not available", r.readEntity(String.class));
         


Mime
View raw message