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/04/16 09:36:48 UTC

[incubator-servicecomb-java-chassis] 01/13: [SCB-292] chassis support standard parameter validation

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 ae70b49730a3bef2c9b86f70b28bfc89c06abae0
Author: acsukesh <su...@huawei.com>
AuthorDate: Thu Mar 29 16:12:49 2018 +0530

    [SCB-292] chassis support standard parameter validation
---
 .../schema/TestProducerSchemaFactory.java          |  8 +++
 swagger/swagger-invocation/invocation-core/pom.xml |  8 +++
 .../swagger/engine/SwaggerProducerOperation.java   | 16 ++++++
 .../extension/AbstractProducerInvokeExtension.java | 20 ++++++++
 .../invocation/extension/ParameterValidator.java   | 59 ++++++++++++++++++++++
 .../extension/ProducerInvokeExtension.java         | 31 ++++++++++++
 ...er.invocation.extension.ProducerInvokeExtension | 18 +++++++
 7 files changed, 160 insertions(+)

diff --git a/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestProducerSchemaFactory.java b/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
index 40e40cf..e0f433e 100644
--- a/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
+++ b/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
@@ -16,6 +16,7 @@
  */
 package org.apache.servicecomb.core.definition.schema;
 
+import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.Executor;
 
@@ -33,6 +34,7 @@ import org.apache.servicecomb.core.executor.ReactiveExecutor;
 import org.apache.servicecomb.core.unittest.UnitTestMeta;
 import org.apache.servicecomb.foundation.common.utils.BeanUtils;
 import org.apache.servicecomb.foundation.common.utils.ReflectUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.ServiceRegistry;
 import org.apache.servicecomb.serviceregistry.registry.ServiceRegistryFactory;
@@ -45,6 +47,7 @@ import org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgu
 import org.apache.servicecomb.swagger.invocation.converter.ConverterMgr;
 import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.apache.servicecomb.swagger.invocation.extension.ProducerInvokeExtension;
 import org.hamcrest.Matchers;
 import org.junit.AfterClass;
 import org.junit.Assert;
@@ -134,6 +137,11 @@ public class TestProducerSchemaFactory {
 
     SwaggerProducerOperation producerOperation = operationMeta.getExtData(Const.PRODUCER_OPERATION);
 
+    List<ProducerInvokeExtension> producerInvokeExtenstionList =
+        SPIServiceUtils.getSortedService(ProducerInvokeExtension.class);
+
+    producerOperation.setProducerInvokeExtenstionList(producerInvokeExtenstionList);
+
     Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
     ReflectUtils.setField(addBody, "x", 1);
     ReflectUtils.setField(addBody, "y", 2);
diff --git a/swagger/swagger-invocation/invocation-core/pom.xml b/swagger/swagger-invocation/invocation-core/pom.xml
index d60e954..492be2e 100644
--- a/swagger/swagger-invocation/invocation-core/pom.xml
+++ b/swagger/swagger-invocation/invocation-core/pom.xml
@@ -38,6 +38,14 @@
 			<scope>test</scope>
 		</dependency>
 		<dependency>
+      	    <groupId>org.hibernate</groupId>
+      		<artifactId>hibernate-validator</artifactId>
+    	</dependency>
+    	<dependency>
+      		<groupId>org.glassfish</groupId>
+      		<artifactId>javax.el</artifactId>
+	    </dependency>
+		<dependency>
 			<groupId>org.slf4j</groupId>
 			<artifactId>slf4j-log4j12</artifactId>
 			<scope>test</scope>
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerProducerOperation.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerProducerOperation.java
index 4827a76..150bce1 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerProducerOperation.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerProducerOperation.java
@@ -18,14 +18,17 @@ package org.apache.servicecomb.swagger.engine;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.List;
 import java.util.concurrent.CompletableFuture;
 
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.swagger.invocation.AsyncResponse;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
 import org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper;
 import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
 import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.extension.ProducerInvokeExtension;
 import org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper;
 
 public class SwaggerProducerOperation {
@@ -44,6 +47,13 @@ public class SwaggerProducerOperation {
 
   private ProducerResponseMapper responseMapper;
 
+  private List<ProducerInvokeExtension> producerInvokeExtenstionList =
+      SPIServiceUtils.getSortedService(ProducerInvokeExtension.class);
+
+  public void setProducerInvokeExtenstionList(List<ProducerInvokeExtension> producerInvokeExtenstionList) {
+    this.producerInvokeExtenstionList = producerInvokeExtenstionList;
+  }
+
   public String getName() {
     return name;
   }
@@ -119,6 +129,9 @@ public class SwaggerProducerOperation {
   public void doCompletableFutureInvoke(SwaggerInvocation invocation, AsyncResponse asyncResp) {
     try {
       Object[] args = argumentsMapper.toProducerArgs(invocation);
+      for (ProducerInvokeExtension producerInvokeExtension : producerInvokeExtenstionList) {
+        producerInvokeExtension.beforeMethodInvoke(invocation, this, args);
+      }
       Object result = producerMethod.invoke(producerInstance, args);
 
       ((CompletableFuture<Object>) result).whenComplete((realResult, ex) -> {
@@ -145,6 +158,9 @@ public class SwaggerProducerOperation {
     Response response = null;
     try {
       Object[] args = argumentsMapper.toProducerArgs(invocation);
+      for (ProducerInvokeExtension producerInvokeExtension : producerInvokeExtenstionList) {
+        producerInvokeExtension.beforeMethodInvoke(invocation, this, args);
+      }
       Object result = producerMethod.invoke(producerInstance, args);
       response = responseMapper.mapResponse(invocation.getStatus(), result);
     } catch (Throwable e) {
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/AbstractProducerInvokeExtension.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/AbstractProducerInvokeExtension.java
new file mode 100644
index 0000000..7080bf7
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/AbstractProducerInvokeExtension.java
@@ -0,0 +1,20 @@
+/*
+ * 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.swagger.invocation.extension;
+
+public abstract class AbstractProducerInvokeExtension implements ProducerInvokeExtension {
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ParameterValidator.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ParameterValidator.java
new file mode 100644
index 0000000..510ed75
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ParameterValidator.java
@@ -0,0 +1,59 @@
+/*
+ * 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.swagger.invocation.extension;
+
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Validation;
+import javax.validation.ValidatorFactory;
+import javax.validation.executable.ExecutableValidator;
+import javax.validation.groups.Default;
+
+import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation;
+import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ParameterValidator extends AbstractProducerInvokeExtension {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ParameterValidator.class);
+
+  private static ExecutableValidator executableValidator;
+
+  @Override
+  public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
+      Object[] args)
+      throws ConstraintViolationException {
+
+    if (null == executableValidator) {
+      ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+      executableValidator = factory.getValidator().forExecutables();
+    }
+    Set<ConstraintViolation<Object>> violations =
+        executableValidator.validateParameters(producerOperation.getProducerInstance(),
+            producerOperation.getProducerMethod(),
+            args,
+            Default.class);
+    if (violations.size() > 0) {
+      LOGGER.warn("Parameter validation failed : " + violations.toString());
+      throw new ConstraintViolationException(violations);
+    }
+  }
+
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java
new file mode 100644
index 0000000..efa1b9c
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java
@@ -0,0 +1,31 @@
+/*
+ * 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.swagger.invocation.extension;
+
+import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation;
+import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
+
+
+public interface ProducerInvokeExtension {
+
+  default int getOrder() {
+    return 0;
+  }
+
+  <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
+      Object[] args) throws Exception;
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.extension.ProducerInvokeExtension b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.extension.ProducerInvokeExtension
new file mode 100644
index 0000000..a532fe7
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.extension.ProducerInvokeExtension
@@ -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.swagger.invocation.extension.ParameterValidator

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