cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [08/12] cxf git commit: [CXF-6891] Avoiding wrapping InputStream into PushBackInputStream in IOUtils.isEmpty
Date Thu, 05 May 2016 01:44:35 GMT
[CXF-6891] Avoiding wrapping InputStream into PushBackInputStream in IOUtils.isEmpty


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/c3395174
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/c3395174
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/c3395174

Branch: refs/heads/master-jaxrs-2.1
Commit: c3395174b4d8bbb3424168b3b8d723a5d1ceef0c
Parents: 447f2da
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Wed May 4 14:18:46 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Wed May 4 14:18:46 2016 +0100

----------------------------------------------------------------------
 .../java/org/apache/cxf/helpers/IOUtils.java     | 19 +++++++++++++------
 .../jaxrs/impl/ContainerRequestContextImpl.java  |  8 ++++++++
 .../provider/AbstractConfigurableProvider.java   | 15 ++-------------
 .../org/apache/cxf/jaxrs/utils/HttpUtils.java    | 16 ++++++++++++++++
 4 files changed, 39 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/c3395174/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
index 194ee25..b14c96c 100644
--- a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
+++ b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
@@ -48,6 +48,15 @@ public final class IOUtils {
         if (is == null) {
             return true;
         }
+        try {
+            // if available is 0 it does not mean it is empty; it can also throw IOException
+            if (is.available() > 0) {
+                return false;
+            }
+        } catch (IOException ex) {
+            // ignore
+        }
+        
         final byte[] bytes = new byte[1];
         try {
             if (is.markSupported()) {
@@ -58,16 +67,14 @@ public final class IOUtils {
                     is.reset();
                 }
             }
-            // if available is 0 it does not mean it is empty; it can also throw IOException
-            if (is.available() > 0) {
-                return false;
-            }
         } catch (IOException ex) {
             // ignore
         }
+        if (!(is instanceof PushbackInputStream)) {
+            return false;
+        }
         // it may be an attachment stream
-        PushbackInputStream pbStream = 
-            is instanceof PushbackInputStream ? (PushbackInputStream)is : new PushbackInputStream(is);
+        PushbackInputStream pbStream = (PushbackInputStream)is;
         boolean isEmpty = isEof(pbStream.read(bytes));
         if (!isEmpty) {
             pbStream.unread(bytes);

http://git-wip-us.apache.org/repos/asf/cxf/blob/c3395174/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
index 039b68c..9110e3e 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
@@ -68,6 +68,14 @@ public class ContainerRequestContextImpl extends AbstractRequestContextImpl
 
     @Override
     public boolean hasEntity() {
+        InputStream is = getEntityStream();
+        if (is == null) {
+            return false;
+        }
+        // Is Content-Length is explicitly set to 0 ?
+        if (HttpUtils.isPayloadEmpty(getHeaders())) {
+            return false;
+        }
         try {
             return !IOUtils.isEmpty(getEntityStream());
         } catch (IOException ex) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/c3395174/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
index ae201a6..90ce45b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
@@ -32,6 +32,7 @@ import org.apache.cxf.BusFactory;
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
+import org.apache.cxf.jaxrs.utils.HttpUtils;
 
 public abstract class AbstractConfigurableProvider {
     protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractJAXBProvider.class);
@@ -163,19 +164,7 @@ public abstract class AbstractConfigurableProvider {
     }
     
     protected boolean isPayloadEmpty(MultivaluedMap<String, String> headers) {
-        if (headers != null) {
-            String value = headers.getFirst(HttpHeaders.CONTENT_LENGTH);
-            if (value != null) {
-                try {
-                    Long len = Long.valueOf(value);
-                    return len <= 0;
-                } catch (NumberFormatException ex) {
-                    // ignore
-                }
-            }
-        }
-        
-        return false;
+        return HttpUtils.isPayloadEmpty(headers);
     }
     protected void reportEmptyContentLength() throws NoContentException {
         String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();

http://git-wip-us.apache.org/repos/asf/cxf/blob/c3395174/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
index e945943..1aa2f6b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
@@ -627,4 +627,20 @@ public final class HttpUtils {
     public static String toHttpLanguage(Locale locale) {
         return Headers.toHttpLanguage(locale);
     }
+    
+    public static boolean isPayloadEmpty(MultivaluedMap<String, String> headers) {
+        if (headers != null) {
+            String value = headers.getFirst(HttpHeaders.CONTENT_LENGTH);
+            if (value != null) {
+                try {
+                    Long len = Long.valueOf(value);
+                    return len <= 0;
+                } catch (NumberFormatException ex) {
+                    // ignore
+                }
+            }
+        }
+        
+        return false;
+    }
 }


Mime
View raw message