You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/03/14 08:56:20 UTC

[GitHub] liubao68 closed pull request #578: SCB-373 change Rest args code/encoding to HttpClientFilter/HttpServer���

liubao68 closed pull request #578: SCB-373 change Rest args code/encoding to HttpClientFilter/HttpServer…
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/578
 
 
   

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 3db387932..7d8ab2c76 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
@@ -27,7 +27,6 @@
 import javax.ws.rs.core.Response.Status;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.servicecomb.common.rest.codec.RestCodec;
 import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor;
 import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
 import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
@@ -43,15 +42,12 @@
 import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
-import org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream;
 import org.apache.servicecomb.swagger.invocation.InvocationType;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import io.netty.buffer.Unpooled;
-
 public abstract class AbstractRestInvocation {
   private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRestInvocation.class);
 
@@ -80,6 +76,7 @@ protected void findRestOperation(MicroserviceMeta microserviceMeta) {
 
     OperationLocator locator = locateOperation(servicePathManager);
     requestEx.setAttribute(RestConst.PATH_PARAMETERS, locator.getPathVarMap());
+    requestEx.setAttribute(RestConst.OPERATION_PARAMETERS, locator.getOperation());
     this.restOperationMeta = locator.getOperation();
   }
 
@@ -140,8 +137,7 @@ protected void scheduleInvocation() {
   }
 
   protected void runOnExecutor(InvocationStartedEvent startedEvent) {
-    Object[] args = RestCodec.restToArgs(requestEx, restOperationMeta);
-    createInvocation(args);
+    createInvocation(null);
 
     //立刻设置开始时间,否则Finished时无法计算TotalTime
     invocation.setStartTime(startedEvent.getStartedTime());
@@ -227,21 +223,13 @@ protected void sendResponse(Response response) throws Exception {
     }
     responseEx.setStatus(response.getStatusCode(), response.getReasonPhrase());
     responseEx.setContentType(produceProcessor.getName() + "; charset=utf-8");
+    invocation.getHandlerContext().put(RestConst.INVOCATION_HANDLER_RESPONSE, response);
+    invocation.getHandlerContext().put(RestConst.INVOCATION_HANDLER_PROCESSOR, produceProcessor);
 
-    Object body = response.getResult();
-    if (response.isFailed()) {
-      body = ((InvocationException) body).getErrorData();
+    for (HttpServerFilter filter : httpServerFilters) {
+      filter.beforeSendResponse(invocation, responseEx);
     }
 
-    try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
-      produceProcessor.encodeResponse(output, body);
-
-      responseEx.setBodyBuffer(output.getBuffer());
-      for (HttpServerFilter filter : httpServerFilters) {
-        filter.beforeSendResponse(invocation, responseEx);
-      }
-
-      responseEx.flushBuffer();
-    }
+    responseEx.flushBuffer();
   }
 }
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
index 2568a8294..aa23137ca 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
@@ -34,12 +34,24 @@ private RestConst() {
   // in HttpServletRequest attribute
   public static final String PATH_PARAMETERS = "servicecomb-paths";
 
+  //in HttpServletRequest attribute
+  public static final String OPERATION_PARAMETERS = "servicecomb-operations";
+
   // in HttpServletRequest attribute
   public static final String BODY_PARAMETER = "servicecomb-body";
 
   // in HttpServletRequest attribute
   public static final String FORM_PARAMETERS = "servicecomb-forms";
 
+  //in invocation response
+  public static final String INVOCATION_HANDLER_RESPONSE = "servicecomb-invocation-hanlder-response";
+
+  //in invocation response
+  public static final String INVOCATION_HANDLER_PROCESSOR = "servicecomb-invocation-hanlder-processor";
+
+  //in invocation response
+  public static final String INVOCATION_HANDLER_REQUESTCLIENT = "servicecomb-invocation-hanlder-requestclient";
+
   public static final String REST_PRODUCER_INVOCATION = "servicecomb-rest-producer-invocation";
 
   public static final String REST_REQUEST = "servicecomb-rest-request";
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ClientRestArgsFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ClientRestArgsFilter.java
new file mode 100644
index 000000000..099d313a5
--- /dev/null
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ClientRestArgsFilter.java
@@ -0,0 +1,58 @@
+/*
+ * 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.common.rest.filter.inner;
+
+import org.apache.servicecomb.common.rest.RestConst;
+import org.apache.servicecomb.common.rest.codec.RestCodec;
+import org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl;
+import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
+import org.apache.servicecomb.common.rest.filter.HttpClientFilter;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.definition.OperationMeta;
+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.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+
+public class ClientRestArgsFilter implements HttpClientFilter {
+
+  @Override
+  public int getOrder() {
+    return -100;
+  }
+
+  @Override
+  public void beforeSendRequest(Invocation invocation, HttpServletRequestEx requestEx) {
+    RestClientRequestImpl restClientRequest = (RestClientRequestImpl) invocation.getHandlerContext()
+        .get(RestConst.INVOCATION_HANDLER_REQUESTCLIENT);
+    OperationMeta operationMeta = invocation.getOperationMeta();
+    RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
+    try {
+      RestCodec.argsToRest(invocation.getArgs(), swaggerRestOperation, restClientRequest);
+      requestEx.setBodyBuffer(restClientRequest.getBodyBuffer());
+    } catch (Throwable e) {
+      throw ExceptionFactory.convertConsumerException(e);
+    }
+  }
+
+  @Override
+  public Response afterReceiveResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+    return null;
+  }
+
+}
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
new file mode 100644
index 000000000..830e33b3e
--- /dev/null
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
@@ -0,0 +1,70 @@
+/*
+ * 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.common.rest.filter.inner;
+
+import org.apache.servicecomb.common.rest.RestConst;
+import org.apache.servicecomb.common.rest.codec.RestCodec;
+import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor;
+import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
+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.foundation.vertx.stream.BufferOutputStream;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+
+import io.netty.buffer.Unpooled;
+
+public class ServerRestArgsFilter implements HttpServerFilter {
+
+  @Override
+  public int getOrder() {
+    return -100;
+  }
+
+  @Override
+  public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx) {
+    RestOperationMeta restOperationMeta = (RestOperationMeta) requestEx
+        .getAttribute(RestConst.OPERATION_PARAMETERS);
+    Object[] args = RestCodec.restToArgs(requestEx, restOperationMeta);
+    invocation.setSwaggerArguments(args);
+    return null;
+  }
+
+  @Override
+  public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+    Response response = (Response) invocation.getHandlerContext().get(RestConst.INVOCATION_HANDLER_RESPONSE);
+    ProduceProcessor produceProcessor = (ProduceProcessor) invocation.getHandlerContext()
+        .get(RestConst.INVOCATION_HANDLER_PROCESSOR);
+    Object body = response.getResult();
+    if (response.isFailed()) {
+      body = ((InvocationException) body).getErrorData();
+    }
+
+    try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
+      produceProcessor.encodeResponse(output, body);
+
+      responseEx.setBodyBuffer(output.getBuffer());
+    } catch (Throwable e) {
+      throw ExceptionFactory.convertProducerException(e);
+    }
+  }
+
+}
diff --git a/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpClientFilter b/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpClientFilter
new file mode 100644
index 000000000..6fccbe3e6
--- /dev/null
+++ b/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpClientFilter
@@ -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.common.rest.filter.inner.ClientRestArgsFilter
\ No newline at end of file
diff --git a/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter b/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
index 2e20d1747..91e25afe4 100644
--- a/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
+++ b/common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
@@ -15,4 +15,5 @@
 # limitations under the License.
 #
 
-org.apache.servicecomb.common.rest.filter.tracing.TracingFilter
+org.apache.servicecomb.common.rest.filter.inner.ServerRestArgsFilter
+org.apache.servicecomb.common.rest.filter.tracing.TracingFilter
\ No newline at end of file
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 16d81fb39..aeac2ec36 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
@@ -26,7 +26,6 @@
 import javax.ws.rs.core.Response.Status;
 import javax.xml.ws.Holder;
 
-import org.apache.servicecomb.common.rest.codec.RestCodec;
 import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
 import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
 import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
@@ -44,6 +43,7 @@
 import org.apache.servicecomb.core.metrics.InvocationStartedEvent;
 import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
 import org.apache.servicecomb.foundation.common.utils.JsonUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
@@ -116,6 +116,8 @@ public void setup() {
     invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
 
     initRestInvocation();
+    List<HttpServerFilter> httpServerFilters = SPIServiceUtils.getSortedService(HttpServerFilter.class);
+    restInvocation.setHttpServerFilters(httpServerFilters);
   }
 
   private void initRestInvocation() {
@@ -535,8 +537,16 @@ void setBodyBuffer(Buffer bodyBuffer) {
   @Test
   public void testDoSendResponseResultOKFilter(@Mocked Response response)
       throws Exception {
+    Headers headers = new Headers();
+    headers.addHeader("Content-Type", "application/json");
     new Expectations() {
       {
+        response.getHeaders();
+        result = headers;
+        response.getStatusCode();
+        result = 123;
+        response.getReasonPhrase();
+        result = "reason";
         response.getResult();
         result = "ok";
       }
@@ -558,7 +568,9 @@ void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx)
     }.getMockInstance();
 
     initRestInvocation();
-    restInvocation.setHttpServerFilters(Arrays.asList(filter));
+    List<HttpServerFilter> httpServerFilters = SPIServiceUtils.getSortedService(HttpServerFilter.class);
+    httpServerFilters.add(filter);
+    restInvocation.setHttpServerFilters(httpServerFilters);
 
     restInvocation.sendResponse(response);
     Assert.assertEquals("\"ok\"-filter", buffer.toString());
@@ -720,13 +732,6 @@ protected void runOnExecutor(InvocationStartedEvent startedEvent) {
 
   @Test
   public void runOnExecutor() {
-    new Expectations(RestCodec.class) {
-      {
-        RestCodec.restToArgs(requestEx, restOperation);
-        result = null;
-      }
-    };
-
     Holder<Boolean> result = new Holder<>();
     restInvocation = new AbstractRestInvocationForTest() {
       @Override
diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/VertxHttpMethod.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/VertxHttpMethod.java
index 480ceac86..b97c39c62 100644
--- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/VertxHttpMethod.java
+++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/VertxHttpMethod.java
@@ -20,7 +20,6 @@
 import java.util.List;
 
 import org.apache.servicecomb.common.rest.RestConst;
-import org.apache.servicecomb.common.rest.codec.RestCodec;
 import org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl;
 import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
 import org.apache.servicecomb.common.rest.filter.HttpClientFilter;
@@ -80,7 +79,7 @@ public void doMethod(HttpClientWithContext httpClientWithContext, Invocation inv
     clientRequest.putHeader(org.apache.servicecomb.core.Const.TARGET_MICROSERVICE, invocation.getMicroserviceName());
     RestClientRequestImpl restClientRequest =
         new RestClientRequestImpl(clientRequest, httpClientWithContext.context().owner(), asyncResp);
-    RestCodec.argsToRest(invocation.getArgs(), swaggerRestOperation, restClientRequest);
+    invocation.getHandlerContext().put(RestConst.INVOCATION_HANDLER_REQUESTCLIENT, restClientRequest);
 
     Buffer requestBodyBuffer = restClientRequest.getBodyBuffer();
     HttpServletRequestEx requestEx = new VertxClientRequestToHttpServletRequest(clientRequest, requestBodyBuffer);
diff --git a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestVertxHttpMethod.java b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestVertxHttpMethod.java
index 5547b0f74..1c9dcfe9c 100644
--- a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestVertxHttpMethod.java
+++ b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestVertxHttpMethod.java
@@ -110,6 +110,8 @@ public void runOnContext(Handler<Void> action) {
     when(endpoint.getAddress()).thenReturn(address);
 
     when(request.exceptionHandler(Mockito.any())).then(answer -> null);
+    Map<String, Object> map = new HashMap<>();
+    when(invocation.getHandlerContext()).then(answer -> map);;
 
     this.doMethod(httpClientWithContext, invocation, asyncResp);
     Assert.assertTrue(true);


 

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


With regards,
Apache Git Services