cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1294611 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Tue, 28 Feb 2012 12:12:14 GMT
Author: sergeyb
Date: Tue Feb 28 12:12:13 2012
New Revision: 1294611

URL: http://svn.apache.org/viewvc?rev=1294611&view=rev
Log:
[CXF-4141] Setting 500 directly on HttpServletResponse in case of write failures

Modified:
    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/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java

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=1294611&r1=1294610&r2=1294611&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
Feb 28 12:12:13 2012
@@ -299,7 +299,7 @@ public class WebClient extends AbstractC
      * @return JAXRS Response
      */
     public Response form(Map<String, List<Object>> values) {
-        type(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
+        type(MediaType.APPLICATION_FORM_URLENCODED);
         return doInvoke("POST", values, null, InputStream.class, InputStream.class);
     }
     
@@ -309,7 +309,7 @@ public class WebClient extends AbstractC
      * @return JAXRS Response
      */
     public Response form(Form form) {
-        type(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
+        type(MediaType.APPLICATION_FORM_URLENCODED);
         return doInvoke("POST", form.getData(), null, InputStream.class, InputStream.class);
     }
     
@@ -668,7 +668,11 @@ public class WebClient extends AbstractC
         MultivaluedMap<String, String> headers = getHeaders();
         boolean contentTypeNotSet = headers.getFirst(HttpHeaders.CONTENT_TYPE) == null;
         if (contentTypeNotSet) {
-            String ct = body != null ? MediaType.APPLICATION_XML_TYPE.toString() : "*/*";
+            String ct = "*/*";
+            if (body != null) { 
+                ct = body instanceof Form ? MediaType.APPLICATION_FORM_URLENCODED 
+                                          : MediaType.APPLICATION_XML;
+            }
             headers.putSingle(HttpHeaders.CONTENT_TYPE, ct);
         }
         if (responseClass != null && headers.getFirst(HttpHeaders.ACCEPT) == null)
{

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=1294611&r1=1294610&r2=1294611&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 Feb 28 12:12:13 2012
@@ -167,8 +167,7 @@ public class JAXRSOutInterceptor extends
             status = isResponseNull(responseObj) ? 204 : 200;
         }
         
-        boolean responseHeadersCopied = isResponseHeadersCopied(message);
-        setResponseStatus(message, status, responseHeadersCopied);
+        setResponseStatus(message, status);
         
         Map<String, List<Object>> theHeaders = 
             (Map<String, List<Object>>)message.get(Message.PROTOCOL_HEADERS);
@@ -385,7 +384,7 @@ public class JAXRSOutInterceptor extends
                 ex.printStackTrace();
             }
         }
-        message.put(Message.RESPONSE_CODE, 500);
+        setResponseStatus(message, 500);
         writeResponseErrorMessage(out, "SERIALIZE_ERROR", 
                                   responseObj.getClass().getSimpleName()); 
             
@@ -497,8 +496,9 @@ public class JAXRSOutInterceptor extends
         }
     }
    
-    private void setResponseStatus(Message message, int status, boolean responseHeadersCopied)
{
+    private void setResponseStatus(Message message, int status) {
         message.put(Message.RESPONSE_CODE, status);   
+        boolean responseHeadersCopied = isResponseHeadersCopied(message);
         if (responseHeadersCopied) {
             HttpServletResponse response = 
                 (HttpServletResponse)message.get(AbstractHTTPDestination.HTTP_RESPONSE);

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java?rev=1294611&r1=1294610&r2=1294611&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java
Tue Feb 28 12:12:13 2012
@@ -19,8 +19,11 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -28,6 +31,8 @@ import java.util.List;
 import java.util.Map;
 
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.stream.XMLStreamWriter;
@@ -35,6 +40,7 @@ import javax.xml.stream.XMLStreamWriter;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.staxutils.CachingXmlEventWriter;
@@ -101,6 +107,15 @@ public class JAXRSClientServerStreamingT
     }
     
     @Test
+    public void testGetBook123Fail() throws Exception {
+        WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/books/123");
+        wc.accept("text/xml");
+        wc.header("fail-write", "");
+        Response r = wc.get();
+        assertEquals(500, r.getStatus());
+    }
+    
+    @Test
     public void testGetBookUsingStaxWriter() throws Exception {
         getAndCompare("http://localhost:" + PORT + "/bookstore/books/123",
                       "text/xml", 200);
@@ -139,5 +154,16 @@ public class JAXRSClientServerStreamingT
                 throw new RuntimeException();
             }
         }
+        @Override
+        public void writeTo(Object obj, Class<?> cls, Type genericType, Annotation[]
anns,  
+            MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) throws
IOException {
+            List<String> failHeaders = getContext().getHttpHeaders().getRequestHeader("fail-write");
+            if (failHeaders.size() > 0) {
+                os.write("fail".getBytes());
+                throw new IOException();
+            } else {
+                super.writeTo(obj, cls, genericType, anns, m, headers, os);
+            }
+        }
     }
 }



Mime
View raw message