You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/03/14 08:56:21 UTC

[incubator-servicecomb-java-chassis] 01/03: SCB-373 change Rest args code/encoding to HttpClientFilter/HttpServerFilter

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 170473fdf00a9bfea147e5ef914720cbe9cfc1a1
Author: jeho0815 <je...@email.com>
AuthorDate: Thu Mar 8 10:18:21 2018 +0800

    SCB-373 change Rest args code/encoding to HttpClientFilter/HttpServerFilter
---
 .../common/rest/AbstractRestInvocation.java        | 26 +++-----
 .../apache/servicecomb/common/rest/RestConst.java  | 12 ++++
 .../rest/filter/inner/ClientRestArgsFilter.java    | 58 ++++++++++++++++++
 .../rest/filter/inner/ServerRestArgsFilter.java    | 70 ++++++++++++++++++++++
 ...ervicecomb.common.rest.filter.HttpClientFilter} |  2 +-
 ...servicecomb.common.rest.filter.HttpServerFilter |  3 +-
 .../common/rest/TestAbstractRestInvocation.java    | 23 ++++---
 .../rest/client/http/VertxHttpMethod.java          |  3 +-
 8 files changed, 165 insertions(+), 32 deletions(-)

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 3db3879..7d8ab2c 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.HttpHeaders;
 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.event.EventBus;
 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 @@ public abstract class AbstractRestInvocation {
 
     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 @@ public abstract class AbstractRestInvocation {
   }
 
   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 @@ public abstract class AbstractRestInvocation {
     }
     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 2568a82..aa23137 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 @@ public final class 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 0000000..db5aed6
--- /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 (Exception 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 0000000..000defc
--- /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 (Exception e) {
+      throw ExceptionFactory.convertProducerException(e);
+    }
+  }
+
+}
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.HttpClientFilter
similarity index 92%
copy from common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpServerFilter
copy to common/common-rest/src/main/resources/META-INF/services/org.apache.servicecomb.common.rest.filter.HttpClientFilter
index 2e20d17..6fccbe3 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.HttpClientFilter
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.servicecomb.common.rest.filter.tracing.TracingFilter
+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 2e20d17..91e25af 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 16d81fb..aeac2ec 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 java.util.concurrent.Executor;
 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.executor.ReactiveExecutor;
 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 class TestAbstractRestInvocation {
     invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
 
     initRestInvocation();
+    List<HttpServerFilter> httpServerFilters = SPIServiceUtils.getSortedService(HttpServerFilter.class);
+    restInvocation.setHttpServerFilters(httpServerFilters);
   }
 
   private void initRestInvocation() {
@@ -535,8 +537,16 @@ public class TestAbstractRestInvocation {
   @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 @@ public class TestAbstractRestInvocation {
     }.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 @@ public class TestAbstractRestInvocation {
 
   @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 480ceac..b97c39c 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 @@ package org.apache.servicecomb.transport.rest.client.http;
 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 class VertxHttpMethod {
     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);

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.