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/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 5f5264f  [SCB-2029] tiny improve to make extend filter features easier
5f5264f is described below

commit 5f5264ff8ed39b9e0671514221c948ff36984ec1
Author: wujimin <[email protected]>
AuthorDate: Thu Jul 2 10:33:28 2020 +0800

    [SCB-2029] tiny improve to make extend filter features easier
---
 .../rest/filter/inner/RestServerCodecFilter.java   |  4 +--
 .../ConstraintViolationExceptionConverter.java     |  2 +-
 .../converter/DefaultExceptionConverter.java       |  4 ++-
 .../IllegalArgumentExceptionConverter.java         |  2 +-
 .../converter/InvocationExceptionConverter.java    |  2 +-
 .../core/filter/impl/ParameterValidatorFilter.java | 31 ++++++++++++--------
 .../core/filter/impl/ProducerOperationFilter.java  |  6 ++--
 .../core/filter/impl/ScheduleFilter.java           |  2 +-
 .../common/utils/LambdaMetafactoryUtils.java       |  2 +-
 .../common/utils/TestLambdaMetafactoryUtils.java   | 33 +++++++++++-----------
 .../apache/servicecomb/provider/pojo/Invoker.java  |  3 +-
 .../highway/HighwayServerCodecFilter.java          |  4 +--
 12 files changed, 52 insertions(+), 43 deletions(-)

diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java
index 7842302..329b61f 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilter.java
@@ -59,7 +59,7 @@ public class RestServerCodecFilter implements Filter {
         .thenCompose(response -> encodeResponse(invocation, response));
   }
 
-  private CompletableFuture<Invocation> decodeRequest(Invocation invocation) {
+  protected CompletableFuture<Invocation> decodeRequest(Invocation invocation) 
{
     HttpTransportContext transportContext = invocation.getTransportContext();
     HttpServletRequestEx requestEx = transportContext.getRequestEx();
 
@@ -71,7 +71,7 @@ public class RestServerCodecFilter implements Filter {
     return CompletableFuture.completedFuture(invocation);
   }
 
-  private CompletableFuture<Response> encodeResponse(Invocation invocation, 
Response response) {
+  protected CompletableFuture<Response> encodeResponse(Invocation invocation, 
Response response) {
     HttpTransportContext transportContext = invocation.getTransportContext();
     ProduceProcessor produceProcessor = transportContext.getProduceProcessor();
     HttpServletResponseEx responseEx = transportContext.getResponseEx();
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/exception/converter/ConstraintViolationExceptionConverter.java
 
b/core/src/main/java/org/apache/servicecomb/core/exception/converter/ConstraintViolationExceptionConverter.java
index 5006b22..c2e0834 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/exception/converter/ConstraintViolationExceptionConverter.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/exception/converter/ConstraintViolationExceptionConverter.java
@@ -35,7 +35,7 @@ import com.netflix.config.DynamicPropertyFactory;
 import com.netflix.config.DynamicStringProperty;
 
 public class ConstraintViolationExceptionConverter implements 
ExceptionConverter<ConstraintViolationException> {
-  public static final short ORDER = Short.MAX_VALUE;
+  public static final int ORDER = Short.MAX_VALUE;
 
   public static final String KEY_CODE = "servicecomb.filters.validate.code";
 
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/exception/converter/DefaultExceptionConverter.java
 
b/core/src/main/java/org/apache/servicecomb/core/exception/converter/DefaultExceptionConverter.java
index d90e88a..ef1777f 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/exception/converter/DefaultExceptionConverter.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/exception/converter/DefaultExceptionConverter.java
@@ -40,9 +40,11 @@ import org.slf4j.LoggerFactory;
 public class DefaultExceptionConverter implements 
ExceptionConverter<Throwable> {
   private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultExceptionConverter.class);
 
+  public static final int ORDER = Integer.MAX_VALUE;
+
   @Override
   public int getOrder() {
-    return Integer.MAX_VALUE;
+    return ORDER;
   }
 
   @Override
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/exception/converter/IllegalArgumentExceptionConverter.java
 
b/core/src/main/java/org/apache/servicecomb/core/exception/converter/IllegalArgumentExceptionConverter.java
index 9d17d46..8725ce1 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/exception/converter/IllegalArgumentExceptionConverter.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/exception/converter/IllegalArgumentExceptionConverter.java
@@ -24,7 +24,7 @@ import 
org.apache.servicecomb.core.exception.ExceptionConverter;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 
 public class IllegalArgumentExceptionConverter implements 
ExceptionConverter<IllegalArgumentException> {
-  public static final short ORDER = Short.MAX_VALUE;
+  public static final int ORDER = Short.MAX_VALUE;
 
   @Override
   public int getOrder() {
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/exception/converter/InvocationExceptionConverter.java
 
b/core/src/main/java/org/apache/servicecomb/core/exception/converter/InvocationExceptionConverter.java
index 55ab688..0a7d271 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/exception/converter/InvocationExceptionConverter.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/exception/converter/InvocationExceptionConverter.java
@@ -24,7 +24,7 @@ import 
org.apache.servicecomb.core.exception.ExceptionConverter;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 
 public class InvocationExceptionConverter implements 
ExceptionConverter<InvocationException> {
-  public static final byte ORDER = Byte.MAX_VALUE;
+  public static final int ORDER = Byte.MAX_VALUE;
 
   @Override
   public int getOrder() {
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ParameterValidatorFilter.java
 
b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ParameterValidatorFilter.java
index c2793ba..b4517f7 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ParameterValidatorFilter.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ParameterValidatorFilter.java
@@ -54,13 +54,16 @@ public class ParameterValidatorFilter implements Filter {
   private final ExecutableValidator validator;
 
   public ParameterValidatorFilter() {
-    ValidatorFactory factory =
-        Validation.byProvider(HibernateValidator.class)
-            .configure()
-            .propertyNodeNameProvider(new JacksonPropertyNodeNameProvider())
-            .messageInterpolator(messageInterpolator())
-            .buildValidatorFactory();
-    validator = factory.getValidator().forExecutables();
+    validator = createValidatorFactory()
+        .getValidator().forExecutables();
+  }
+
+  protected ValidatorFactory createValidatorFactory() {
+    return Validation.byProvider(HibernateValidator.class)
+        .configure()
+        .propertyNodeNameProvider(new JacksonPropertyNodeNameProvider())
+        .messageInterpolator(messageInterpolator())
+        .buildValidatorFactory();
   }
 
   private AbstractMessageInterpolator messageInterpolator() {
@@ -76,11 +79,7 @@ public class ParameterValidatorFilter implements Filter {
 
   @Override
   public CompletableFuture<Response> onFilter(Invocation invocation, 
FilterNode nextNode) {
-    SwaggerProducerOperation producerOperation = 
invocation.getOperationMeta().getSwaggerProducerOperation();
-    Object instance = producerOperation.getProducerInstance();
-    Method method = producerOperation.getProducerMethod();
-    Object[] args = invocation.toProducerArguments();
-    Set<ConstraintViolation<Object>> violations = 
validator.validateParameters(instance, method, args, Default.class);
+    Set<ConstraintViolation<Object>> violations = doValidate(invocation);
     if (violations.size() > 0) {
       LOGGER.error("Parameter validation failed : " + violations.toString());
       return AsyncUtils.completeExceptionally(new 
ConstraintViolationException(violations));
@@ -88,4 +87,12 @@ public class ParameterValidatorFilter implements Filter {
 
     return nextNode.onFilter(invocation);
   }
+
+  protected Set<ConstraintViolation<Object>> doValidate(Invocation invocation) 
{
+    SwaggerProducerOperation producerOperation = 
invocation.getOperationMeta().getSwaggerProducerOperation();
+    Object instance = producerOperation.getProducerInstance();
+    Method method = producerOperation.getProducerMethod();
+    Object[] args = invocation.toProducerArguments();
+    return validator.validateParameters(instance, method, args, Default.class);
+  }
 }
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ProducerOperationFilter.java
 
b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ProducerOperationFilter.java
index 09c6238..6b58df4 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ProducerOperationFilter.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ProducerOperationFilter.java
@@ -52,7 +52,7 @@ public class ProducerOperationFilter implements Filter {
   }
 
   @SuppressWarnings("unchecked")
-  private CompletableFuture<Object> invoke(Invocation invocation, Object 
instance, Method method, Object[] args) {
+  protected CompletableFuture<Object> invoke(Invocation invocation, Object 
instance, Method method, Object[] args) {
     ContextUtils.setInvocationContext(invocation);
 
     try {
@@ -69,12 +69,12 @@ public class ProducerOperationFilter implements Filter {
     }
   }
 
-  private Response convertResultToResponse(Invocation invocation, 
SwaggerProducerOperation producerOperation,
+  protected Response convertResultToResponse(Invocation invocation, 
SwaggerProducerOperation producerOperation,
       Object result) {
     return 
producerOperation.getResponseMapper().mapResponse(invocation.getStatus(), 
result);
   }
 
-  private void whenComplete(Invocation invocation, Throwable throwable) {
+  protected void whenComplete(Invocation invocation, Throwable throwable) {
     if (throwable != null) {
       Throwable unwrapped = Exceptions.unwrap(throwable);
       if (shouldPrintErrorLog(unwrapped)) {
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ScheduleFilter.java
 
b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ScheduleFilter.java
index a74bddf..cd4a3b6 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/filter/impl/ScheduleFilter.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/filter/impl/ScheduleFilter.java
@@ -39,7 +39,7 @@ public class ScheduleFilter implements Filter {
         .thenComposeAsync(response -> runInExecutor(invocation, next), 
executor);
   }
 
-  private CompletableFuture<Response> runInExecutor(Invocation invocation, 
FilterNode next) {
+  protected CompletableFuture<Response> runInExecutor(Invocation invocation, 
FilterNode next) {
     invocation.onExecuteStart();
     InvocationStageTrace trace = invocation.getInvocationStageTrace();
     trace.startServerFiltersRequest();
diff --git 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/LambdaMetafactoryUtils.java
 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/LambdaMetafactoryUtils.java
index c500d16..b392f6b 100644
--- 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/LambdaMetafactoryUtils.java
+++ 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/LambdaMetafactoryUtils.java
@@ -165,7 +165,7 @@ public final class LambdaMetafactoryUtils {
     // This check is not accurate. Most of time package visible and protected 
access can be ignored, so simply do this.
     if (!Modifier.isPublic(field.getModifiers()) || 
!Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
       throw new IllegalStateException(
-          String.format("Can not access field, a public field or and accessor 
is required."
+          String.format("Can not access field, a public field or accessor is 
required."
                   + "Declaring class is %s, field is %s",
               field.getDeclaringClass().getName(),
               field.getName()));
diff --git 
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaMetafactoryUtils.java
 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaMetafactoryUtils.java
index 1adf6c1..0af9d27 100644
--- 
a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaMetafactoryUtils.java
+++ 
b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestLambdaMetafactoryUtils.java
@@ -16,6 +16,9 @@
  */
 package org.apache.servicecomb.foundation.common.utils;
 
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.catchThrowable;
+
 import java.lang.reflect.Field;
 import java.util.Arrays;
 import java.util.List;
@@ -31,8 +34,10 @@ import org.junit.Assert;
 import org.junit.Test;
 
 public class TestLambdaMetafactoryUtils {
-  static class Model {
-    private int f1;
+  public static class Model {
+    public int f1;
+
+    private int f2;
 
     public int getF1() {
       return f1;
@@ -91,19 +96,15 @@ public class TestLambdaMetafactoryUtils {
   }
 
   @Test
-  public void createGetterSetterByField() throws Throwable {
-    Field f1 = Model.class.getDeclaredField("f1");
-    try {
-      LambdaMetafactoryUtils.createGetter(f1);
-      Assert.fail();
-    } catch (IllegalStateException e) {
-      Assert.assertTrue(true);
-    }
-    try {
-      LambdaMetafactoryUtils.createSetter(f1);
-      Assert.fail();
-    } catch (IllegalStateException e) {
-      Assert.assertTrue(true);
-    }
+  public void 
should_failed_when_createGetterSetterByField_and_field_is_not_public() throws 
Throwable {
+    Field field = Model.class.getDeclaredField("f2");
+    assertThat(catchThrowable(() -> 
LambdaMetafactoryUtils.createGetter(field)))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessage(
+            "Can not access field, a public field or accessor is 
required.Declaring class is 
org.apache.servicecomb.foundation.common.utils.TestLambdaMetafactoryUtils$Model,
 field is f2");
+    assertThat(catchThrowable(() -> 
LambdaMetafactoryUtils.createSetter(field)))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessage(
+            "Can not access field, a public field or accessor is 
required.Declaring class is 
org.apache.servicecomb.foundation.common.utils.TestLambdaMetafactoryUtils$Model,
 field is f2");
   }
 }
diff --git 
a/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
 
b/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
index 8e6fa1a..751e5a8 100644
--- 
a/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
+++ 
b/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
@@ -116,7 +116,7 @@ public class Invoker implements InvocationHandler {
     return microserviceMeta.findSchemaMeta(consumerIntf.getName());
   }
 
-  private PojoConsumerMeta refreshMeta() {
+  protected PojoConsumerMeta refreshMeta() {
     MicroserviceReferenceConfig microserviceReferenceConfig = scbEngine
         .createMicroserviceReferenceConfig(microserviceName);
     MicroserviceMeta microserviceMeta = 
microserviceReferenceConfig.getLatestMicroserviceMeta();
@@ -185,7 +185,6 @@ public class Invoker implements InvocationHandler {
     return syncInvoke(invocation, consumerOperation);
   }
 
-
   public Map<String, Object> toArguments(Method method, Object[] args) {
     Map<String, Object> arguments = new HashMap<>();
     for (int i = 0; i < method.getParameterCount(); i++) {
diff --git 
a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilter.java
 
b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilter.java
index 7f8cc8a..d2faa2e 100644
--- 
a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilter.java
+++ 
b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilter.java
@@ -45,7 +45,7 @@ public class HighwayServerCodecFilter implements Filter {
         .thenCompose(response -> encodeResponse(invocation, response));
   }
 
-  private CompletableFuture<Invocation> decodeRequest(Invocation invocation) {
+  protected CompletableFuture<Invocation> decodeRequest(Invocation invocation) 
{
     HighwayTransportContext transportContext = 
invocation.getTransportContext();
     try {
       HighwayCodec.decodeRequest(invocation,
@@ -58,7 +58,7 @@ public class HighwayServerCodecFilter implements Filter {
     }
   }
 
-  private CompletableFuture<Response> encodeResponse(Invocation invocation, 
Response response) {
+  protected CompletableFuture<Response> encodeResponse(Invocation invocation, 
Response response) {
     ResponseHeader header = new ResponseHeader();
     header.setStatusCode(response.getStatusCode());
     header.setReasonPhrase(response.getReasonPhrase());

Reply via email to