liubao68 closed pull request #817: [SCB-754] add null pointer check on 
AbstractRestInvocation.invocation
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/817
 
 
   

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/AbstractRestInvocation.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
index cb4413b47..a467f0bf1 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
@@ -49,8 +49,11 @@
 import org.slf4j.LoggerFactory;
 
 public abstract class AbstractRestInvocation {
+
   private static final Logger LOGGER = 
LoggerFactory.getLogger(AbstractRestInvocation.class);
 
+  public static final String UNKNOWN_OPERATION_ID = "UNKNOWN_OPERATION";
+
   protected RestOperationMeta restOperationMeta;
 
   protected Invocation invocation;
@@ -199,9 +202,8 @@ protected void sendResponseQuietly(Response response) {
     try {
       sendResponse(response);
     } catch (Throwable e) {
-      LOGGER.error("Failed to send rest response, operation:{}.",
-          invocation.getMicroserviceQualifiedName(),
-          e);
+      LOGGER.error("Failed to send rest response, operation:{}, request 
uri:{}",
+          getMicroserviceQualifiedName(), requestEx.getRequestURI(), e);
     }
   }
 
@@ -235,17 +237,15 @@ protected void executeHttpServerFilters(Response 
response) {
 
   protected void onExecuteHttpServerFiltersFinish(Response response, Throwable 
e) {
     if (e != null) {
-      LOGGER.error("Failed to execute HttpServerFilters, operation:{}.",
-          invocation.getMicroserviceQualifiedName(),
-          e);
+      LOGGER.error("Failed to execute HttpServerFilters, operation:{}, request 
uri:{}",
+          getMicroserviceQualifiedName(), requestEx.getRequestURI(), e);
     }
 
     try {
       responseEx.flushBuffer();
     } catch (IOException flushException) {
-      LOGGER.error("Failed to flush rest response, operation:{}.",
-          invocation.getMicroserviceQualifiedName(),
-          flushException);
+      LOGGER.error("Failed to flush rest response, operation:{}, request 
uri:{}",
+          getMicroserviceQualifiedName(), requestEx.getRequestURI(), 
flushException);
     }
 
     requestEx.getAsyncContext().complete();
@@ -255,4 +255,8 @@ protected void onExecuteHttpServerFiltersFinish(Response 
response, Throwable e)
       invocation.onFinish(response);
     }
   }
+
+  private String getMicroserviceQualifiedName() {
+    return null == invocation ? UNKNOWN_OPERATION_ID : 
invocation.getMicroserviceQualifiedName();
+  }
 }
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
index e8d4d7f08..cc0c2c013 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
@@ -440,6 +440,26 @@ protected void sendResponse(Response response) {
     // just log, check nothing
   }
 
+  @Test
+  public void sendResponseQuietlyExceptionOnNullInvocation(@Mocked Response 
response) {
+    restInvocation = new AbstractRestInvocationForTest() {
+      @Override
+      protected void doInvoke() {
+      }
+
+      @Override
+      protected void sendResponse(Response response) {
+        throw new Error("");
+      }
+    };
+    initRestInvocation();
+    restInvocation.invocation = null;
+
+    restInvocation.sendResponseQuietly(response);
+
+    // just log, check nothing, and should not throw NPE
+  }
+
   @Test
   public void sendResponseStatusAndContentTypeAndHeader(@Mocked Response 
response) {
     new Expectations() {
diff --git 
a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/CodeFirstRestTemplateJaxrs.java
 
b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/CodeFirstRestTemplateJaxrs.java
index 429a12ff5..38ae6e963 100644
--- 
a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/CodeFirstRestTemplateJaxrs.java
+++ 
b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/CodeFirstRestTemplateJaxrs.java
@@ -30,12 +30,15 @@
 import org.springframework.core.io.FileSystemResource;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
 
 public class CodeFirstRestTemplateJaxrs extends CodeFirstRestTemplate {
   @Override
   protected void testAllTransport(String microserviceName, RestTemplate 
template, String cseUrlPrefix) {
     testDefaultPath(template, cseUrlPrefix);
+    test404(template);
 
     super.testAllTransport(microserviceName, template, cseUrlPrefix);
   }
@@ -103,4 +106,17 @@ private void testFileAndStringUpload(RestTemplate 
template, String cseUrlPrefix,
     String result = template.postForObject(cseUrlPrefix + "/upload2", new 
HttpEntity<>(map, headers), String.class);
     TestMgr.check(expect, result);
   }
+
+  private void test404(RestTemplate template) {
+    HttpClientErrorException exception = null;
+    try {
+      template.getForEntity("http://127.0.0.1:8080/aPathNotExist";, 
String.class);
+    } catch (RestClientException e) {
+      if (e instanceof HttpClientErrorException) {
+        exception = (HttpClientErrorException) e;
+      }
+    }
+    TestMgr.check(404, exception.getRawStatusCode());
+    TestMgr.check("404 Not Found", exception.getMessage());
+  }
 }
diff --git 
a/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/JaxrsDemoHttpServerFilter.java
 
b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/JaxrsDemoHttpServerFilter.java
new file mode 100644
index 000000000..025847445
--- /dev/null
+++ 
b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/JaxrsDemoHttpServerFilter.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.demo.jaxrs.server;
+
+import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class JaxrsDemoHttpServerFilter implements HttpServerFilter {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(JaxrsDemoHttpServerFilter.class);
+
+  @Override
+  public int getOrder() {
+    return 0;
+  }
+
+  @Override
+  public Response afterReceiveRequest(Invocation invocation, 
HttpServletRequestEx requestEx) {
+    return null;
+  }
+
+  @Override
+  public void beforeSendResponse(Invocation invocation, HttpServletResponseEx 
responseEx) {
+    // in 404 situation, invocation is null and a NPE is thrown
+    LOGGER.info("JaxrsDemoHttpServerFilter is called, operation=[{}]", 
invocation.getOperationName());
+  }
+}
diff --git 
a/demo/demo-jaxrs/jaxrs-server/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
 
b/demo/demo-jaxrs/jaxrs-server/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
new file mode 100644
index 000000000..5e737d86d
--- /dev/null
+++ 
b/demo/demo-jaxrs/jaxrs-server/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.servicecomb.demo.jaxrs.server.JaxrsDemoHttpServerFilter
\ No newline at end of file


 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to