cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1387250 - in /cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs: client/ interceptor/ provider/ utils/
Date Tue, 18 Sep 2012 16:48:27 GMT
Author: sergeyb
Date: Tue Sep 18 16:48:27 2012
New Revision: 1387250

URL: http://svn.apache.org/viewvc?rev=1387250&view=rev
Log:
[CXF-4455] Wiring reader/writer interceptors into the existing client runtime, tests to follow

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=1387250&r1=1387249&r2=1387250&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
Tue Sep 18 16:48:27 2012
@@ -48,8 +48,8 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.UriBuilder;
-import javax.ws.rs.ext.MessageBodyReader;
-import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.ReaderInterceptor;
+import javax.ws.rs.ext.WriterInterceptor;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
@@ -393,24 +393,37 @@ public abstract class AbstractClient imp
         return rb;
     }
     
-    protected <T> void writeBody(T o, Message outMessage, Class<?> cls, Type
type, Annotation[] anns, 
-        MultivaluedMap<String, Object> headers, OutputStream os) {
+    protected <T> void writeBody(T o, Message outMessage, Class<?> cls, Type
type, Annotation[] anns,
+                                 OutputStream os) {
         
         if (o == null) {
             return;
         }
         @SuppressWarnings("unchecked")
+        MultivaluedMap<String, Object> headers = 
+            (MultivaluedMap<String, Object>)outMessage.get(Message.PROTOCOL_HEADERS);
+        
+        @SuppressWarnings("unchecked")
         Class<T> theClass = (Class<T>)cls;
         
         MediaType contentType = MediaType.valueOf(headers.getFirst("Content-Type").toString());

         
-        MessageBodyWriter<T> mbw = ProviderFactory.getInstance(outMessage)
-            .createMessageBodyWriter(theClass, type, anns, contentType, outMessage);
-        if (mbw != null) {
+        List<WriterInterceptor> writers = ProviderFactory.getInstance(outMessage)
+            .createMessageBodyWriterInterceptor(theClass, type, anns, contentType, outMessage);
+        if (writers != null) {
             try {
-                mbw.writeTo(o, theClass, type, anns, contentType, headers, os);
-                if (os != null) {
-                    os.flush();
+                JAXRSUtils.writeMessageBody(writers, 
+                                            o, 
+                                            theClass, 
+                                            type, 
+                                            anns, 
+                                            contentType,
+                                            headers,
+                                            outMessage);
+                
+                OutputStream realOs = outMessage.get(OutputStream.class);
+                if (realOs != null) {
+                    realOs.flush();
                 }
             } catch (Exception ex) {
                 reportMessageHandlerProblem("MSG_WRITER_PROBLEM", cls, contentType, ex, null);
@@ -436,9 +449,10 @@ public abstract class AbstractClient imp
     @SuppressWarnings("unchecked")
     protected <T> T readBody(Response r, Message outMessage, Class<T> cls, 
                              Type type, Annotation[] anns) {
+        Message responseMessage = outMessage.getExchange().getInMessage();
+        
         InputStream inputStream = (InputStream)r.getEntity();
         if (inputStream == null) {
-            Message responseMessage = outMessage.getExchange().getInMessage();
             if (responseMessage == null) {
                 responseMessage = outMessage.getExchange().getInFaultMessage();    
             }    
@@ -464,16 +478,15 @@ public abstract class AbstractClient imp
         
         MediaType contentType = getResponseContentType(r);
         
-        MessageBodyReader<T> mbr 
-            = ProviderFactory.getInstance(outMessage).createMessageBodyReader(
+        List<ReaderInterceptor> readers 
+            = ProviderFactory.getInstance(outMessage).createMessageBodyReaderInterceptor(
                 cls, type, anns, contentType, outMessage);
-        if (mbr != null) {
+        if (readers != null) {
             try {
-                MultivaluedMap<String, String> m 
-                    = (MultivaluedMap<String, String>)
-                        ((MultivaluedMap<?, ?>)new MetadataMap<String, Object>(r.getMetadata(),
-                            true, true));
-                return mbr.readFrom(cls, type, anns, contentType, m, inputStream);
+                responseMessage.put(Message.PROTOCOL_HEADERS, r.getMetadata());
+                return (T)JAXRSUtils.readFromMessageBodyReader(readers, cls, type, 
+                                                            anns, inputStream, contentType,

+                                                            responseMessage);
             } catch (Exception ex) {
                 reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, contentType, ex, r);
             }
@@ -908,7 +921,6 @@ public abstract class AbstractClient imp
             super(Phase.WRITE);
         }
         
-        @SuppressWarnings("unchecked")
         public void handleMessage(Message outMessage) throws Fault {
             
             MessageContentsList objs = MessageContentsList.getContentsList(outMessage);
@@ -924,15 +936,11 @@ public abstract class AbstractClient imp
                 }
             }
             
-            MultivaluedMap<String, Object> headers = 
-                (MultivaluedMap<String, Object>)outMessage.get(Message.PROTOCOL_HEADERS);
-            
             Object body = objs.get(0);
             
-            doWriteBody(outMessage, body, headers, os);
+            doWriteBody(outMessage, body, os);
         }
         
-        protected abstract void doWriteBody(Message outMessage, Object body,
-                                 MultivaluedMap<String, Object> headers, OutputStream
os) throws Fault;
+        protected abstract void doWriteBody(Message outMessage, Object body, OutputStream
os) throws Fault;
     }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=1387250&r1=1387249&r2=1387250&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Tue Sep 18 16:48:27 2012
@@ -599,8 +599,7 @@ public class ClientProxyImpl extends Abs
     
     private class BodyWriter extends AbstractBodyWriter {
         
-        protected void doWriteBody(Message outMessage, Object body,
-                                 MultivaluedMap<String, Object> headers, 
+        protected void doWriteBody(Message outMessage, Object body, 
                                  OutputStream os) throws Fault {
             
             
@@ -623,10 +622,10 @@ public class ClientProxyImpl extends Abs
                     writeBody(body, outMessage,
                               isAssignable ? paramClass : body.getClass(),
                               isAssignable ? paramType : body.getClass(),
-                              anns, headers, os);
+                              anns, os);
                 } else {
                     writeBody(body, outMessage, body.getClass(), body.getClass(), 
-                              anns, headers, os);
+                              anns, os);
                 }
             } catch (Exception ex) {
                 throw new Fault(ex);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1387250&r1=1387249&r2=1387250&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java Tue
Sep 18 16:48:27 2012
@@ -852,8 +852,7 @@ public class WebClient extends AbstractC
     
     private class BodyWriter extends AbstractBodyWriter {
 
-        protected void doWriteBody(Message outMessage, Object body,
-                                   MultivaluedMap<String, Object> headers, 
+        protected void doWriteBody(Message outMessage, Object body, 
                                    OutputStream os) throws Fault {    
             
             Map<String, Object> requestContext = WebClient.this.getRequestContext(outMessage);
@@ -868,10 +867,7 @@ public class WebClient extends AbstractC
                 writeBody(body, outMessage, 
                           requestClass == null || !isAssignable ? body.getClass() : requestClass,
                           requestType == null || !isAssignable ? body.getClass() : requestType,

-                          new Annotation[]{}, headers, os);
-                if (os != null) {
-                    os.flush();
-                }
+                          new Annotation[]{}, os);
             } catch (Exception ex) {
                 throw new Fault(ex);
             }

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=1387250&r1=1387249&r2=1387250&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
Tue Sep 18 16:48:27 2012
@@ -35,7 +35,6 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.servlet.http.HttpServletResponse;
-import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.GenericEntity;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -43,7 +42,6 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.WriterInterceptor;
-import javax.ws.rs.ext.WriterInterceptorContext;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.stream.events.XMLEvent;
 
@@ -54,7 +52,6 @@ import org.apache.cxf.interceptor.Abstra
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.ext.ResponseHandler;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
-import org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl;
 import org.apache.cxf.jaxrs.impl.WriterInterceptorMBW;
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
@@ -257,7 +254,7 @@ public class JAXRSOutInterceptor extends
             message.put(Message.CONTENT_TYPE, responseType.toString());
             
             try {
-                writeTo(writers, 
+                JAXRSUtils.writeMessageBody(writers, 
                         entity, 
                         targetType, 
                         genericType, 
@@ -289,36 +286,6 @@ public class JAXRSOutInterceptor extends
         }
     }
     
-    //CHECKSTYLE:OFF
-    private static void writeTo(List<WriterInterceptor> writers, 
-                                Object entity,
-                                Class<?> type, Type genericType,
-                                Annotation[] annotations, 
-                                MediaType mediaType,
-                                MultivaluedMap<String, Object> httpHeaders,
-                                Message message) 
-        throws WebApplicationException, IOException {
-        
-        OutputStream entityStream = message.getContent(OutputStream.class);
-        if (writers.size() > 1) {
-            WriterInterceptor first = writers.remove(0);
-            WriterInterceptorContext context = new WriterInterceptorContextImpl(entity,
-                                                                                type, 
-                                                                            genericType,

-                                                                            annotations,

-                                                                            mediaType,
-                                                                            entityStream,
-                                                                            message,
-                                                                            writers);
-            
-            first.aroundWriteTo(context);
-        } else {
-            MessageBodyWriter<Object> writer = ((WriterInterceptorMBW)writers.get(0)).getMBW();
-            writer.writeTo(entity, type, genericType, annotations, mediaType,
-                           httpHeaders, entityStream);
-        }
-    }
-    //CHECKSTYLE:ON
 
     private boolean isResponseNull(Object o) {
         return o == null || GenericEntity.class.isAssignableFrom(o.getClass()) 

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=1387250&r1=1387249&r2=1387250&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
Tue Sep 18 16:48:27 2012
@@ -456,7 +456,7 @@ public final class ProviderFactory {
                                                       mediaType,
                                                       m);
         if (mr != null) {
-            ReaderInterceptor mbrReader = new ReaderInterceptorMBR(mr, m);
+            ReaderInterceptor mbrReader = new ReaderInterceptorMBR(mr, m.getExchange().getInMessage());
             
             int size = readerInterceptors.size();
             List<ReaderInterceptor> interceptors = null;

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=1387250&r1=1387249&r2=1387250&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 Tue
Sep 18 16:48:27 2012
@@ -21,6 +21,7 @@ package org.apache.cxf.jaxrs.utils;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
@@ -84,6 +85,8 @@ import javax.ws.rs.ext.MessageBodyWriter
 import javax.ws.rs.ext.Providers;
 import javax.ws.rs.ext.ReaderInterceptor;
 import javax.ws.rs.ext.ReaderInterceptorContext;
+import javax.ws.rs.ext.WriterInterceptor;
+import javax.ws.rs.ext.WriterInterceptorContext;
 import javax.xml.namespace.QName;
 
 import org.apache.cxf.common.i18n.BundleUtils;
@@ -110,6 +113,8 @@ import org.apache.cxf.jaxrs.impl.Request
 import org.apache.cxf.jaxrs.impl.SecurityContextImpl;
 import org.apache.cxf.jaxrs.impl.UriInfoImpl;
 import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
+import org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl;
+import org.apache.cxf.jaxrs.impl.WriterInterceptorMBW;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.ClassResourceInfoComparator;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
@@ -1130,6 +1135,37 @@ public final class JAXRSUtils {
     }
 
     
+    //CHECKSTYLE:OFF
+    public static void writeMessageBody(List<WriterInterceptor> writers, 
+                                Object entity,
+                                Class<?> type, Type genericType,
+                                Annotation[] annotations, 
+                                MediaType mediaType,
+                                MultivaluedMap<String, Object> httpHeaders,
+                                Message message) 
+        throws WebApplicationException, IOException {
+        
+        OutputStream entityStream = message.getContent(OutputStream.class);
+        if (writers.size() > 1) {
+            WriterInterceptor first = writers.remove(0);
+            WriterInterceptorContext context = new WriterInterceptorContextImpl(entity,
+                                                                                type, 
+                                                                            genericType,

+                                                                            annotations,

+                                                                            mediaType,
+                                                                            entityStream,
+                                                                            message,
+                                                                            writers);
+            
+            first.aroundWriteTo(context);
+        } else {
+            MessageBodyWriter<Object> writer = ((WriterInterceptorMBW)writers.get(0)).getMBW();
+            writer.writeTo(entity, type, genericType, annotations, mediaType,
+                           httpHeaders, entityStream);
+        }
+    }
+    //CHECKSTYLE:ON
+    
 
     public static boolean matchConsumeTypes(MediaType requestContentType, 
                                             OperationResourceInfo ori) {



Mime
View raw message