cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1521072 - in /cxf/branches/2.7.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxr...
Date Mon, 09 Sep 2013 12:29:08 GMT
Author: sergeyb
Date: Mon Sep  9 12:29:08 2013
New Revision: 1521072

URL: http://svn.apache.org/r1521072
Log:
Merged revisions 1521065-1521066 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1521065 | sergeyb | 2013-09-09 13:10:39 +0100 (Mon, 09 Sep 2013) | 1 line
  
  Minimiaing a need to use servlet api on the client side when reading form response
........
  r1521066 | sergeyb | 2013-09-09 13:15:34 +0100 (Mon, 09 Sep 2013) | 1 line
  
  [CXF-5135] Updating Response.readEntity to support reading entities of diff types if IS
has been buffered
........

Modified:
    cxf/branches/2.7.x-fixes/   (props changed)
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1521065-1521066

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

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java?rev=1521072&r1=1521071&r2=1521072&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
Mon Sep  9 12:29:08 2013
@@ -153,7 +153,11 @@ public class MessageContextImpl implemen
     }
     
     public HttpServletRequest getHttpServletRequest() {
-        return getContext(HttpServletRequest.class);
+        try {
+            return getContext(HttpServletRequest.class);
+        } catch (Throwable t) {
+            return null;
+        }
     }
 
     public HttpServletResponse getHttpServletResponse() {

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java?rev=1521072&r1=1521071&r2=1521072&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
Mon Sep  9 12:29:08 2013
@@ -62,6 +62,7 @@ public final class ResponseImpl extends 
     private Message responseMessage;
     private boolean entityClosed;    
     private boolean entityBufferred;
+    private Object lastEntity;
     
     ResponseImpl(int s) {
         this.status = s;
@@ -125,7 +126,7 @@ public final class ResponseImpl extends 
     }
     
     public Object getEntity() {
-        return entity;
+        return lastEntity != null ? lastEntity : entity;
     }
 
     public boolean hasEntity() {
@@ -297,20 +298,13 @@ public final class ResponseImpl extends 
         
         checkEntityIsClosed();
         
-        if (!hasEntity()) {
-            throw new MessageProcessingException("Null entity");
-        }
+        if (lastEntity != null && cls.isAssignableFrom(lastEntity.getClass())
+            && !(lastEntity instanceof InputStream)) {
+            return cls.cast(lastEntity);
+        } 
         
-        if (cls.isAssignableFrom(entity.getClass())) {
-            return cls.cast(entity);
-        }
         if (entity instanceof InputStream) {
             
-            if (responseMessage == null) {
-                // won't happen, just in case
-                throw new RuntimeException();    
-            }
-            
             MediaType mediaType = getMediaType();
             if (mediaType == null) {
                 mediaType = MediaType.WILDCARD_TYPE;
@@ -322,27 +316,34 @@ public final class ResponseImpl extends 
             if (readers != null) {
                 try {
                     responseMessage.put(Message.PROTOCOL_HEADERS, this.getMetadata());
-                    Object newEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls,
t, 
+                    lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, 
                                                                            anns, 
                                                                            InputStream.class.cast(entity),

                                                                            mediaType, 
                                                                            responseMessage);
-                    if (responseStreamCanBeClosed(cls)) {
-                        InputStream.class.cast(entity).close();
+                    if (!entityBufferred) {
+                        if (responseStreamCanBeClosed(cls)) {
+                            InputStream.class.cast(entity).close();
+                            entity = null;
+                        }
+                    } else {
+                        InputStream.class.cast(entity).reset();
                     }
-                    entity = newEntity;
-                    entityBufferred = true;
                     
-                    return cls.cast(entity);
+                    return cls.cast(lastEntity);
                 } catch (Exception ex) {
                     throw new MessageProcessingException(ex);
                 }
             } else {
                 throw new MessageProcessingException("No message body reader for class: "
+ cls);
             }
+        } else if (entity != null && cls.isAssignableFrom(entity.getClass())) {
+            lastEntity = entity;
+            return cls.cast(lastEntity);
         }
+        
         throw new IllegalStateException("The entity is not backed by an input stream, entity
class is : "
-            + entity.getClass().getName());
+            + entity != null ? entity.getClass().getName() : null);
         
     }
     

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java?rev=1521072&r1=1521071&r2=1521072&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
Mon Sep  9 12:29:08 2013
@@ -169,12 +169,22 @@ public class FormEncodingProvider<T> imp
                                                decode);
         } else {
             String enc = HttpUtils.getEncoding(mt, "UTF-8");
-            FormUtils.populateMapFromString(params,
-                                            PhaseInterceptorChain.getCurrentMessage(),
-                                            FormUtils.readBody(is, enc),
-                                            enc,
-                                            decode,
-                                            mc != null ? mc.getHttpServletRequest() : null);
+            
+            Object servletRequest = mc != null ? mc.getHttpServletRequest() : null;
+            if (servletRequest == null) {
+                FormUtils.populateMapFromString(params,
+                                                PhaseInterceptorChain.getCurrentMessage(),
+                                                FormUtils.readBody(is, enc),
+                                                enc,
+                                                decode);
+            } else {
+                FormUtils.populateMapFromString(params,
+                                                PhaseInterceptorChain.getCurrentMessage(),
+                                                FormUtils.readBody(is, enc),
+                                                enc,
+                                                decode,
+                                                (javax.servlet.http.HttpServletRequest)servletRequest);
+            }
         }
     }
     

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java?rev=1521072&r1=1521071&r2=1521072&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
Mon Sep  9 12:29:08 2013
@@ -31,7 +31,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
 
-import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.BadRequestException;
 import javax.ws.rs.InternalServerErrorException;
 import javax.ws.rs.WebApplicationException;
@@ -115,28 +114,37 @@ public final class FormUtils {
                                              Message m,
                                              String postBody, 
                                              String enc,
+                                             boolean decode) {
+        List<String> parts = Arrays.asList(StringUtils.split(postBody, "&"));
+        checkNumberOfParts(m, parts.size());
+        for (String part : parts) {
+            String[] keyValue = new String[2];
+            int index = part.indexOf("=");
+            if (index != -1) {
+                keyValue[0] = part.substring(0, index);
+                keyValue[1] = index + 1 < part.length() ? part.substring(index + 1) :
"";
+            } else {
+                keyValue[0] = part;
+                keyValue[1] = "";
+            }
+            String name = HttpUtils.urlDecode(keyValue[0], enc);
+            if (decode) {
+                params.add(name, HttpUtils.urlDecode(keyValue[1], enc));
+            } else {
+                params.add(name, keyValue[1]);
+            }
+        }
+        
+    }
+    
+    public static void populateMapFromString(MultivaluedMap<String, String> params,
+                                             Message m,
+                                             String postBody, 
+                                             String enc,
                                              boolean decode,
-                                             HttpServletRequest request) {
+                                             javax.servlet.http.HttpServletRequest request)
{
         if (!StringUtils.isEmpty(postBody)) {
-            List<String> parts = Arrays.asList(StringUtils.split(postBody, "&"));
-            checkNumberOfParts(m, parts.size());
-            for (String part : parts) {
-                String[] keyValue = new String[2];
-                int index = part.indexOf("=");
-                if (index != -1) {
-                    keyValue[0] = part.substring(0, index);
-                    keyValue[1] = index + 1 < part.length() ? part.substring(index + 1)
: "";
-                } else {
-                    keyValue[0] = part;
-                    keyValue[1] = "";
-                }
-                String name = HttpUtils.urlDecode(keyValue[0], enc);
-                if (decode) {
-                    params.add(name, HttpUtils.urlDecode(keyValue[1], enc));
-                } else {
-                    params.add(name, keyValue[1]);
-                }
-            }
+            populateMapFromString(params, m, postBody, enc, decode);
         } else if (request != null) {
             for (Enumeration<String> en = request.getParameterNames(); en.hasMoreElements();)
{
                 String paramName = en.nextElement();

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1521072&r1=1521071&r2=1521072&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Mon Sep  9 12:29:08 2013
@@ -186,6 +186,23 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testGetCustomBookBufferedResponse() {
+        String address = "http://localhost:" + PORT + "/bookstore/customresponse";
+        WebClient wc = WebClient.create(address);
+        Response r = wc.accept("application/xml").get(Response.class);
+        
+        r.bufferEntity();
+        
+        String bookStr = r.readEntity(String.class);
+        assertTrue(bookStr.endsWith("</Book>"));
+        
+        Book book = r.readEntity(Book.class);
+        assertEquals(222L, book.getId());
+        assertEquals("OK", r.getHeaderString("customresponse"));
+    }
+    
+    
+    @Test
     public void testGetCustomBookText() {
         String address = "http://localhost:" + PORT + "/bookstore/customtext";
         WebClient wc = WebClient.create(address);



Mime
View raw message