cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1083746 - in /cxf/branches/2.3.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Mon, 21 Mar 2011 11:53:10 GMT
Author: sergeyb
Date: Mon Mar 21 11:53:09 2011
New Revision: 1083746

URL: http://svn.apache.org/viewvc?rev=1083746&view=rev
Log:
Merged revisions 1083742 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1083742 | sergeyb | 2011-03-21 11:40:58 +0000 (Mon, 21 Mar 2011) | 1 line
  
  [CXF-3207] Adding the utility method to ResponseReader for reading error streams
........

Modified:
    cxf/branches/2.3.x-fixes/   (props changed)
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
    cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Mar 21 11:53:09 2011
@@ -1 +1 @@
-/cxf/trunk:1083736
+/cxf/trunk:1083736,1083742

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

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java?rev=1083746&r1=1083745&r2=1083746&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ResponseReader.java
Mon Mar 21 11:53:09 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/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1083746&r1=1083745&r2=1083746&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++ cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Mon Mar 21 11:53:09 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/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1083746&r1=1083745&r2=1083746&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Mon Mar 21 11:53:09 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 {



Mime
View raw message