You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2011/03/21 12:40:58 UTC

svn commit: r1083742 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/

Author: sergeyb
Date: Mon Mar 21 11:40:58 2011
New Revision: 1083742

URL: http://svn.apache.org/viewvc?rev=1083742&view=rev
Log:
[CXF-3207] Adding the utility method to ResponseReader for reading error streams

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java?rev=1083742&r1=1083741&r2=1083742&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java Mon Mar 21 11:40:58 2011
@@ -34,8 +34,13 @@ import javax.ws.rs.core.Response.Respons
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.Providers;
 
+import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
 
 public class ResponseReader implements MessageBodyReader<Response> {
 
@@ -102,4 +107,59 @@ public class ResponseReader implements M
     protected MessageContext getContext() {
         return context;
     }
+    
+    /**
+     * 
+     * @param client the client
+     * @param response {@link Response} object with the response input stream
+     * @param cls the entity class
+     * @return the typed entity
+     */
+    @SuppressWarnings("unchecked")
+    public <T> T readEntity(Client client, Response response, Class<T> cls) {
+        Class<?> oldClass = entityCls;
+        setEntityClass(cls);
+        try {
+            MultivaluedMap headers = response.getMetadata();
+            Object contentType = headers.getFirst("Content-Type");
+            InputStream inputStream = (InputStream)response.getEntity();
+            if (contentType == null || inputStream == null) {
+                return null;
+            }
+            Annotation[] annotations = new Annotation[]{};
+            MediaType mt = MediaType.valueOf(contentType.toString());
+            
+            Endpoint ep = WebClient.getConfig(client).getConduitSelector().getEndpoint();
+            Exchange exchange = new ExchangeImpl();
+            Message inMessage = new MessageImpl();
+            inMessage.setExchange(exchange);
+            exchange.put(Endpoint.class, ep);
+            exchange.setOutMessage(new MessageImpl());
+            exchange.setInMessage(inMessage);
+            inMessage.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
+            inMessage.put(Message.PROTOCOL_HEADERS, headers);
+            
+            ProviderFactory pf = (ProviderFactory)ep.get(ProviderFactory.class.getName());
+            
+            MessageBodyReader reader = pf.createMessageBodyReader(entityCls, 
+                                                             entityCls, 
+                                                             annotations, 
+                                                             mt, 
+                                                             inMessage);
+            
+            
+            
+            if (reader == null) {
+                return null;
+            }
+            
+            return (T)reader.readFrom(entityCls, entityCls, annotations, mt, 
+                                      (MultivaluedMap<String, String>)headers, 
+                                      inputStream);
+        } catch (Exception ex) {
+            throw new ClientWebApplicationException(ex);
+        } finally {
+            entityCls = oldClass;
+        }
+    }
 }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1083742&r1=1083741&r2=1083742&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Mon Mar 21 11:40:58 2011
@@ -136,6 +136,16 @@ public class BookStore {
     }
     
     @GET
+    @Path("webappexceptionXML")
+    public Book throwExceptionXML() {
+        
+        Response response = Response.status(406).type("application/xml")
+                            .entity("<Book><name>Exception</name><id>999</id></Book>")
+                            .build();
+        throw new WebApplicationException(response);
+    }
+    
+    @GET
     @Path("tempredirect")
     public Response tempRedirectAndSetCookies() {
         URI uri = UriBuilder.fromPath("whatever/redirection")

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1083742&r1=1083741&r2=1083742&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Mon Mar 21 11:40:58 2011
@@ -256,6 +256,24 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testServerWebApplicationExceptionXML() throws Exception {
+        ResponseReader reader = new ResponseReader();
+        reader.setEntityClass(Book.class);
+        WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/webappexceptionXML",
+                                        Collections.singletonList(reader));
+        wc.accept("application/xml");
+        try {
+            wc.get(Book.class);
+            fail("Exception expected");
+        } catch (ServerWebApplicationException ex) {
+            assertEquals(406, ex.getStatus());
+            Book exBook = reader.readEntity(wc, ex.getResponse(), Book.class);
+            assertEquals("Exception", exBook.getName());
+            assertEquals(999L, exBook.getId());
+        }
+    }
+    
+    @Test
     public void testServerWebApplicationExceptionWithProxy() throws Exception {
         BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
         try {