[ 
https://issues.apache.org/jira/browse/SCB-828?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16576127#comment-16576127
 ] 

ASF GitHub Bot commented on SCB-828:
------------------------------------

liubao68 closed pull request #866: [SCB-828]In some tomcat implementation 
inputstream available is null
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/866
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestCodec.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestCodec.java
index 7c80dfd25..5642642d5 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestCodec.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestCodec.java
@@ -20,10 +20,10 @@
 import java.util.List;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.core.Response.Status;
 
 import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
 import org.apache.servicecomb.common.rest.definition.RestParam;
-import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -67,7 +67,8 @@ public static void argsToRest(Object[] args, 
RestOperationMeta restOperation,
       LOG.error("Parameter is not valid for operation {}. ",
           restOperation.getOperationMeta().getMicroserviceQualifiedName(),
           e);
-      throw ExceptionFactory.convertProducerException(e, "Parameter is not 
valid.");
+      // give standard http error code for invalid parameter
+      throw new InvocationException(Status.BAD_REQUEST, "Parameter is not 
valid.");
     }
   }
 }
diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
index c21f527d9..61f0ea959 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
@@ -31,14 +31,18 @@
 import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
 import org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream;
 import org.apache.servicecomb.swagger.generator.core.utils.ClassUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
 import com.fasterxml.jackson.databind.type.TypeFactory;
 
 import io.swagger.models.parameters.Parameter;
 import io.vertx.core.buffer.Buffer;
 
 public class BodyProcessorCreator implements ParamValueProcessorCreator {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(BodyProcessorCreator.class);
 
   public static final String PARAMTYPE = "body";
 
@@ -75,15 +79,21 @@ public Object getValue(HttpServletRequest request) throws 
Exception {
         return null;
       }
 
-      if (isRequired == false && inputStream.available() == 0) {
-        return null;
-      }
-
       if (!contentType.isEmpty() && 
!contentType.startsWith(MediaType.APPLICATION_JSON)) {
         // TODO: we should consider body encoding
         return IOUtils.toString(inputStream, "UTF-8");
       }
-      return 
RestObjectMapperFactory.getRestObjectMapper().readValue(inputStream, 
targetType);
+
+      try {
+        return 
RestObjectMapperFactory.getRestObjectMapper().readValue(inputStream, 
targetType);
+      } catch (MismatchedInputException e) {
+        // there is no way to detect InputStream is empty, so have to catch 
the exception
+        if (!isRequired) {
+          LOGGER.warn("Mismatched content and required is false, taken as 
null. Msg=" + e.getMessage());
+          return null;
+        }
+        throw e;
+      }
     }
 
     @Override
diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/produce/ProduceProcessor.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/produce/ProduceProcessor.java
index 127250ff7..4172cb56f 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/produce/ProduceProcessor.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/produce/ProduceProcessor.java
@@ -54,10 +54,6 @@ default Buffer encodeResponse(Object result) throws 
Exception {
   }
 
   default Object decodeResponse(InputStream input, JavaType type) throws 
Exception {
-    if (input.available() == 0) {
-      return null;
-    }
-
     return doDecodeResponse(input, type);
   }
 
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java
index d3817a1c3..90245d7a3 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java
@@ -133,7 +133,7 @@ public void testRestToArgsExcetpion(@Mocked 
HttpServletRequest request,
       RestCodec.restToArgs(request, restOperation);
       success = true;
     } catch (InvocationException e) {
-      Assert.assertEquals(590, e.getStatusCode());
+      Assert.assertEquals(400, e.getStatusCode());
       Assert.assertEquals("Parameter is not valid.", ((CommonExceptionData) 
e.getErrorData()).getMessage());
     }
     Assert.assertEquals(success, false);
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestObjectMapper.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestObjectMapper.java
index 1a9980ecf..af840e124 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestObjectMapper.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestObjectMapper.java
@@ -17,11 +17,15 @@
 
 package org.apache.servicecomb.common.rest.codec;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
 import org.junit.Assert;
 import org.junit.Test;
 
 import com.fasterxml.jackson.core.JsonParser.Feature;
 import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
 import com.fasterxml.jackson.databind.type.TypeFactory;
 
 import io.vertx.core.json.JsonObject;
@@ -54,5 +58,15 @@ public void testJsonObjectWork() {
         .convertValue(obj, 
TypeFactory.defaultInstance().constructType(PojoModel.class));
     Assert.assertEquals("a", model.getName());
     Assert.assertEquals("b", model.getDesc());
+
+    InputStream inputStream = new ByteArrayInputStream(new byte[0]);
+    try {
+      RestObjectMapperFactory.getRestObjectMapper().readValue(inputStream, 
PojoModel.class);
+      Assert.fail();
+    } catch (MismatchedInputException e) {
+      // right place, nothing to do.
+    } catch (Exception e) {
+      Assert.fail();
+    }
   }
 }
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/produce/TestProduceJsonProcessor.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/produce/TestProduceJsonProcessor.java
index b186bdaa1..98a83d953 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/produce/TestProduceJsonProcessor.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/produce/TestProduceJsonProcessor.java
@@ -25,6 +25,7 @@
 import org.junit.Test;
 
 import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
 import com.fasterxml.jackson.databind.type.TypeFactory;
 
 import io.vertx.core.buffer.Buffer;
@@ -51,8 +52,13 @@ public void testdecodeResponseNull() throws Exception {
     Assert.assertNull(result);
 
     ByteArrayInputStream is = new ByteArrayInputStream(new byte[] {});
-    result = pp.decodeResponse(is, resultType);
-    Assert.assertNull(result);
+    try {
+      pp.decodeResponse(is, resultType);
+      Assert.fail();
+    } catch (Exception e) {
+      Assert.assertTrue(e instanceof MismatchedInputException);
+    }
+
   }
 
   @Test
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/produce/TestProduceTextPlainProcessor.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/produce/TestProduceTextPlainProcessor.java
index b1df57595..905f70f15 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/produce/TestProduceTextPlainProcessor.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/produce/TestProduceTextPlainProcessor.java
@@ -52,7 +52,7 @@ public void testdecodeResponseNull() throws Exception {
 
     ByteArrayInputStream is = new ByteArrayInputStream(new byte[] {});
     result = pp.decodeResponse(is, resultType);
-    Assert.assertNull(result);
+    Assert.assertEquals(result, "");
   }
 
   @Test


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> In some tomcat implementation inputstream available is null
> -----------------------------------------------------------
>
>                 Key: SCB-828
>                 URL: https://issues.apache.org/jira/browse/SCB-828
>             Project: Apache ServiceComb
>          Issue Type: Bug
>          Components: Java-Chassis
>    Affects Versions: java-chassis-1.0.0
>            Reporter: liubao
>            Priority: Major
>             Fix For: java-chassis-1.1.0
>
>
> inputstream available is not always return a positive value and can always be 
> null. And the api says it's so. So we must not use it to check if stream is 
> empty.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to