cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1084169 - in /cxf/branches/2.3.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/ws/security/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Tue, 22 Mar 2011 13:27:02 GMT
Author: sergeyb
Date: Tue Mar 22 13:27:02 2011
New Revision: 1084169

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

........
  r1084168 | sergeyb | 2011-03-22 13:23:33 +0000 (Tue, 22 Mar 2011) | 1 line
  
  [CXF-3207] Moving the code for reading the error streams to ServerWebApplicationException
........

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/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
    cxf/branches/2.3.x-fixes/rt/ws/security/   (props changed)
    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 Tue Mar 22 13:27:02 2011
@@ -1 +1 @@
-/cxf/trunk:1083736,1083742,1083826
+/cxf/trunk:1083736,1083742,1083826,1084168

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=1084169&r1=1084168&r2=1084169&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
Tue Mar 22 13:27:02 2011
@@ -34,13 +34,8 @@ 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> {
 
@@ -107,59 +102,4 @@ 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/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java?rev=1084169&r1=1084168&r2=1084169&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ServerWebApplicationException.java
Tue Mar 22 13:27:02 2011
@@ -19,17 +19,28 @@
 
 package org.apache.cxf.jaxrs.client;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.annotation.Annotation;
 import java.util.Iterator;
+import java.util.List;
 
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.ext.MessageBodyReader;
 
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.helpers.IOUtils;
-import org.apache.cxf.jaxrs.impl.MetadataMap;
+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;
 
 /**
  * Utility Exception class which makes it easier to get the response status,
@@ -51,19 +62,29 @@ public class ServerWebApplicationExcepti
         super(cause, response);
     }
     
+    @Override
+    public Response getResponse() {
+        Response response = super.getResponse();
+        
+        ResponseBuilder rb = Response.status(response.getStatus());
+        MultivaluedMap<String, Object> headers = response.getMetadata();
+        for (String header : headers.keySet()) {
+            List<Object> values = headers.get(header);
+            for (Object value : values) {
+                rb.header(header, value);
+            }
+        }
+        rb.entity(new ByteArrayInputStream(getMessage().getBytes()));
+        return rb.build();
+    }
+    
     public int getStatus() {
-        return getResponse().getStatus();    
+        return super.getResponse().getStatus();    
     }
     
+    @SuppressWarnings("unchecked")
     public MultivaluedMap<String, String> getHeaders() {
-        MultivaluedMap<String, Object> metadata = getResponse().getMetadata();
-        MultivaluedMap<String, String> headers = new MetadataMap<String, String>(metadata.size());
-        for (String key : metadata.keySet()) {
-            for (Object strObject : metadata.get(key)) {
-                headers.add(key, strObject.toString());
-            }
-        }
-        return headers;
+        return (MultivaluedMap<String, String>)((MultivaluedMap)super.getResponse().getMetadata());
     }
     
     @Override
@@ -75,7 +96,7 @@ public class ServerWebApplicationExcepti
     }
     
     private String readErrorMessage() {
-        Object entity = getResponse().getEntity();
+        Object entity = super.getResponse().getEntity();
         try {
             return entity == null ? "" : entity instanceof InputStream 
                 ? IOUtils.readStringFromStream((InputStream)entity) : entity.toString();
@@ -111,4 +132,55 @@ public class ServerWebApplicationExcepti
         }
         return sb.toString();
     }
+    
+    /**
+     * Returns the typed error message 
+     * @param client the client
+     * @param cls the entity class
+     * @return the typed entity
+     */
+    @SuppressWarnings("unchecked")
+    public <T> T toErrorObject(Client client, Class<T> entityCls) {
+        Response response = getResponse();
+        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);
+        }
+    }
 }

Propchange: cxf/branches/2.3.x-fixes/rt/ws/security/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Mar 22 13:27:02 2011
@@ -1 +1 @@
-/cxf/trunk/rt/ws/security:1083736,1083742,1083826,1084160
+/cxf/trunk/rt/ws/security:1083736,1083742,1083826,1084160,1084168

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=1084169&r1=1084168&r2=1084169&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
Tue Mar 22 13:27:02 2011
@@ -293,7 +293,24 @@ public class JAXRSClientServerBookTest e
             fail("Exception expected");
         } catch (ServerWebApplicationException ex) {
             assertEquals(406, ex.getStatus());
-            Book exBook = reader.readEntity(wc, ex.getResponse(), Book.class);
+            Book exBook = ex.toErrorObject(wc, Book.class);
+            assertEquals("Exception", exBook.getName());
+            assertEquals(999L, exBook.getId());
+        }
+    }
+    
+    @Test
+    public void testServerWebApplicationExceptionXMLWithProxy() throws Exception {
+        ResponseReader reader = new ResponseReader();
+        reader.setEntityClass(Book.class);
+        BookStore proxy = JAXRSClientFactory.create("http://localhost:" + PORT,
+                                                    BookStore.class, Collections.singletonList(reader));
+        try {
+            proxy.throwExceptionXML();
+            fail("Exception expected");
+        } catch (ServerWebApplicationException ex) {
+            assertEquals(406, ex.getStatus());
+            Book exBook = ex.toErrorObject(WebClient.client(proxy), Book.class);
             assertEquals("Exception", exBook.getName());
             assertEquals(999L, exBook.getId());
         }



Mime
View raw message