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:52 UTC
[incubator-servicecomb-java-chassis] 05/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 a9388f0bf6cd044d404480e7bd7d10ff5c6996e6
Author: acsukesh <su...@huawei.com>
AuthorDate: Mon Apr 9 14:05:47 2018 +0530
[SCB-292] chassis support standard parameter validation
---
.../schema/TestProducerSchemaFactory.java | 10 +-
demo/demo-validator/pom.xml | 35 +++++++
demo/demo-validator/validator-client/pom.xml | 58 +++++++++++
.../client/CodeFirstRestTemplateValidator.java | 22 ++--
.../client/CodeFirstValidatorRestTemplate.java | 115 +++++++++++++++++++++
.../demo/validator/client/ValidatorClient.java | 48 +++++++++
.../src/main/resources/microservice.yaml | 29 ++++++
demo/demo-validator/validator-server/pom.xml | 88 ++++++++++++++++
.../demo/validator/server/CodeFirstJaxrs.java | 63 +++++++++++
.../demo/validator/server/ValidatorServer.java | 17 ++-
.../src/main/resources/microservice.yaml | 33 ++++++
demo/pom.xml | 2 +-
.../swagger/engine/SwaggerProducerOperation.java | 4 -
.../exception/ExceptionToResponseConverters.java | 2 +-
.../extension/ProducerInvokeExtension.java | 5 +-
...raintViolationExceptionToResponseConverter.java | 7 +-
.../invocation/validator/ParameterValidator.java | 2 +-
17 files changed, 506 insertions(+), 34 deletions(-)
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 e0f433e..5aa29c2 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
@@ -1,5 +1,5 @@
/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
+ * 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
@@ -16,7 +16,6 @@
*/
package org.apache.servicecomb.core.definition.schema;
-import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@@ -34,7 +33,6 @@ 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;
@@ -47,7 +45,6 @@ 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;
@@ -137,11 +134,6 @@ 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/demo/demo-validator/pom.xml b/demo/demo-validator/pom.xml
new file mode 100644
index 0000000..1525262
--- /dev/null
+++ b/demo/demo-validator/pom.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-parent</artifactId>
+ <version>1.0.0-m2-SNAPSHOT</version>
+ </parent>
+ <artifactId>demo-validator</artifactId>
+ <name>Java Chassis::Demo::Validator</name>
+ <packaging>pom</packaging>
+ <modules>
+ <module>validator-server</module>
+ <module>validator-client</module>
+ </modules>
+
+</project>
diff --git a/demo/demo-validator/validator-client/pom.xml b/demo/demo-validator/validator-client/pom.xml
new file mode 100644
index 0000000..c9e044b
--- /dev/null
+++ b/demo/demo-validator/validator-client/pom.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0"?>
+<!--
+ ~ 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.
+ -->
+
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-validator</artifactId>
+ <version>1.0.0-m2-SNAPSHOT</version>
+ </parent>
+ <artifactId>validator-client</artifactId>
+ <name>Java Chassis::Demo::Validator::Client</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-schema</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>provider-jaxrs</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>provider-springmvc</artifactId>
+ </dependency>
+ </dependencies>
+
+ <properties>
+ <demo.main>org.apache.servicecomb.demo.jaxrs.client.JaxrsClient</demo.main>
+ </properties>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java b/demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/CodeFirstRestTemplateValidator.java
similarity index 51%
copy from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java
copy to demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/CodeFirstRestTemplateValidator.java
index e559124..2f4ff80 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java
+++ b/demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/CodeFirstRestTemplateValidator.java
@@ -14,16 +14,24 @@
* 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;
+package org.apache.servicecomb.demo.validator.client;
+import org.apache.servicecomb.demo.CodeFirstRestTemplate;
+import org.apache.servicecomb.demo.TestMgr;
+import org.springframework.web.client.RestTemplate;
-public interface ProducerInvokeExtension {
+public class CodeFirstRestTemplateValidator extends CodeFirstRestTemplate {
+ @Override
+ protected void testAllTransport(String microserviceName, RestTemplate template, String cseUrlPrefix) {
+ testDefaultPath(template, cseUrlPrefix);
- public int getOrder();
+ super.testAllTransport(microserviceName, template, cseUrlPrefix);
+ }
- <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
- Object[] args) throws Exception;
+ private void testDefaultPath(RestTemplate template, String cseUrlPrefix) {
+ int result =
+ template.getForObject(cseUrlPrefix.substring(0, cseUrlPrefix.length() - 1), Integer.class);
+ TestMgr.check(100, result);
+ }
}
diff --git a/demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/CodeFirstValidatorRestTemplate.java b/demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/CodeFirstValidatorRestTemplate.java
new file mode 100644
index 0000000..9c4c971
--- /dev/null
+++ b/demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/CodeFirstValidatorRestTemplate.java
@@ -0,0 +1,115 @@
+/*
+ * 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.validator.client;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.servicecomb.core.Const;
+import org.apache.servicecomb.core.CseContext;
+import org.apache.servicecomb.demo.DemoConst;
+import org.apache.servicecomb.demo.TestMgr;
+import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
+import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
+public class CodeFirstValidatorRestTemplate {
+ protected void changeTransport(String microserviceName, String transport) {
+ CseContext.getInstance().getConsumerProviderManager().setTransport(microserviceName, transport);
+ TestMgr.setMsg(microserviceName, transport);
+ }
+
+ public void testCodeFirst(RestTemplate template, String microserviceName, String basePath) {
+ String cseUrlPrefix = "cse://" + microserviceName + basePath;
+ for (String transport : DemoConst.transports) {
+ changeTransport(microserviceName, transport);
+ testAllTransport(microserviceName, template, cseUrlPrefix);
+ }
+ }
+
+ protected void testAllTransport(String microserviceName, RestTemplate template, String cseUrlPrefix) {
+ testCodeFirstAdd(template, cseUrlPrefix);
+ testCodeFirstAddForException(template, cseUrlPrefix);
+ testCodeFirstSayHi(template, cseUrlPrefix);
+ testCodeFirstSayHiForException(template, cseUrlPrefix);
+ testTraceIdOnContextContainsTraceId(template, cseUrlPrefix);
+ }
+
+ protected void checkStatusCode(String microserviceName, int expectStatusCode, HttpStatus httpStatus) {
+ TestMgr.check(expectStatusCode, httpStatus.value());
+ }
+
+ protected void testCodeFirstSayHi(RestTemplate template, String cseUrlPrefix) {
+ ResponseEntity<String> responseEntity =
+ template.exchange(cseUrlPrefix + "sayhi/{name}", HttpMethod.PUT, null, String.class, "world");
+ TestMgr.check(202, responseEntity.getStatusCode());
+ TestMgr.check("world sayhi", responseEntity.getBody());
+ }
+
+ protected void testCodeFirstSayHiForException(RestTemplate template, String cseUrlPrefix) {
+ boolean isExcep = false;
+ try {
+ template.exchange(cseUrlPrefix + "sayhi/{name}", HttpMethod.PUT, null, String.class, "te");
+ } catch (Exception e) {
+ isExcep = true;
+ }
+ TestMgr.check(true, isExcep);
+ }
+
+ protected void testCodeFirstAdd(RestTemplate template, String cseUrlPrefix) {
+ Map<String, String> params = new HashMap<>();
+ params.put("a", "5");
+ params.put("b", "20");
+ int result =
+ template.postForObject(cseUrlPrefix + "add", params, Integer.class);
+ TestMgr.check(25, result);
+ }
+
+ protected void testCodeFirstAddForException(RestTemplate template, String cseUrlPrefix) {
+ Map<String, String> params = new HashMap<>();
+ params.put("a", "5");
+ params.put("b", "3");
+ boolean isExcep = false;
+ try {
+ template.postForObject(cseUrlPrefix + "add", params, Integer.class);
+ } catch (Exception e) {
+ isExcep = true;
+ }
+
+ TestMgr.check(true, isExcep);
+ }
+
+ protected void testTraceIdOnNotSetBefore(RestTemplate template, String cseUrlPrefix) {
+ String traceIdUrl = cseUrlPrefix + "traceId";
+ String result = template.getForObject(traceIdUrl, String.class);
+ TestMgr.checkNotEmpty(result);
+ }
+
+ protected void testTraceIdOnContextContainsTraceId(RestTemplate template, String cseUrlPrefix) {
+ String traceIdUrl = cseUrlPrefix + "traceId";
+ InvocationContext invocationContext = new InvocationContext();
+ invocationContext.addContext(Const.TRACE_ID_NAME, String.valueOf(Long.MIN_VALUE));
+ ContextUtils.setInvocationContext(invocationContext);
+ String result = template.getForObject(traceIdUrl, String.class);
+ TestMgr.check(String.valueOf(Long.MIN_VALUE), result);
+ ContextUtils.removeInvocationContext();
+ }
+}
diff --git a/demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/ValidatorClient.java b/demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/ValidatorClient.java
new file mode 100644
index 0000000..2f257a1
--- /dev/null
+++ b/demo/demo-validator/validator-client/src/main/java/org/apache/servicecomb/demo/validator/client/ValidatorClient.java
@@ -0,0 +1,48 @@
+/*
+ * 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.validator.client;
+
+import org.apache.servicecomb.demo.TestMgr;
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
+import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
+import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
+import org.springframework.web.client.RestTemplate;
+
+public class ValidatorClient {
+ private static RestTemplate templateNew = RestTemplateBuilder.create();
+
+ public static void main(String[] args) throws Exception {
+ init();
+
+ run();
+
+ TestMgr.summary();
+ }
+
+ public static void init() throws Exception {
+ Log4jUtils.init();
+ BeanUtils.init();
+ }
+
+ public static void run() throws Exception {
+ CodeFirstValidatorRestTemplate codeFirstClient = new CodeFirstValidatorRestTemplate();
+ codeFirstClient.testCodeFirst(templateNew, "validator", "/codeFirstJaxrs/");
+ }
+
+
+}
diff --git a/demo/demo-validator/validator-client/src/main/resources/microservice.yaml b/demo/demo-validator/validator-client/src/main/resources/microservice.yaml
new file mode 100644
index 0000000..8055ff5
--- /dev/null
+++ b/demo/demo-validator/validator-client/src/main/resources/microservice.yaml
@@ -0,0 +1,29 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+APPLICATION_ID: validatortest
+service_description:
+ name: validatorClient
+ version: 0.0.1
+cse:
+ service:
+ registry:
+ address: http://127.0.0.1:30100
+ handler:
+ chain:
+ Consumer:
+ default: bizkeeper-consumer,loadbalance
diff --git a/demo/demo-validator/validator-server/pom.xml b/demo/demo-validator/validator-server/pom.xml
new file mode 100644
index 0000000..7959ae4
--- /dev/null
+++ b/demo/demo-validator/validator-server/pom.xml
@@ -0,0 +1,88 @@
+<?xml version="1.0"?>
+<!--
+ ~ 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.
+ -->
+
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-validator</artifactId>
+ <version>1.0.0-m2-SNAPSHOT</version>
+ </parent>
+ <artifactId>validator-server</artifactId>
+ <name>Java Chassis::Demo::Validator::Server</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-schema</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>provider-jaxrs</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>swagger-invocation-validator</artifactId>
+ </dependency>
+ </dependencies>
+
+ <properties>
+ <demo.main>org.apache.servicecomb.demo.jaxrs.server.JaxrsServer</demo.main>
+ </properties>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>com.github.odavid.maven.plugins</groupId>
+ <artifactId>mixin-maven-plugin</artifactId>
+ <configuration>
+ <mixins>
+ <mixin>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>docker-build-config</artifactId>
+ <version>1.0.0-m2-SNAPSHOT</version>
+ </mixin>
+ </mixins>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <profiles>
+ <profile>
+ <id>docker</id>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>io.fabric8</groupId>
+ <artifactId>docker-maven-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.commonjava.maven.plugins</groupId>
+ <artifactId>directory-maven-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
+</project>
diff --git a/demo/demo-validator/validator-server/src/main/java/org/apache/servicecomb/demo/validator/server/CodeFirstJaxrs.java b/demo/demo-validator/validator-server/src/main/java/org/apache/servicecomb/demo/validator/server/CodeFirstJaxrs.java
new file mode 100644
index 0000000..a9fc320
--- /dev/null
+++ b/demo/demo-validator/validator-server/src/main/java/org/apache/servicecomb/demo/validator/server/CodeFirstJaxrs.java
@@ -0,0 +1,63 @@
+/*
+ * 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.validator.server;
+
+import javax.validation.constraints.Min;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.servicecomb.core.Const;
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
+import org.hibernate.validator.constraints.Length;
+
+@RestSchema(schemaId = "codeFirst")
+@Path("/codeFirstJaxrs")
+@Produces(MediaType.APPLICATION_JSON)
+public class CodeFirstJaxrs {
+
+ @GET
+ public int defaultPath() {
+ return 100;
+ }
+
+ @Path("/add")
+ @POST
+ public int add(@FormParam("a") int a, @Min(20) @FormParam("b") int b) {
+ return a + b;
+ }
+
+ @Path("/sayhi/{name}")
+ @PUT
+ public String sayHi(@Length(min = 3) @PathParam("name") String name) {
+ ContextUtils.getInvocationContext().setStatus(202);
+ return name + " sayhi";
+ }
+
+ @Path("/traceId")
+ @GET
+ public String getTraceId() {
+ return ContextUtils.getInvocationContext().getContext(Const.TRACE_ID_NAME);
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java b/demo/demo-validator/validator-server/src/main/java/org/apache/servicecomb/demo/validator/server/ValidatorServer.java
similarity index 66%
copy from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java
copy to demo/demo-validator/validator-server/src/main/java/org/apache/servicecomb/demo/validator/server/ValidatorServer.java
index e559124..5ce0ed0 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ProducerInvokeExtension.java
+++ b/demo/demo-validator/validator-server/src/main/java/org/apache/servicecomb/demo/validator/server/ValidatorServer.java
@@ -14,16 +14,15 @@
* 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;
+package org.apache.servicecomb.demo.validator.server;
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
+import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
-public interface ProducerInvokeExtension {
-
- public int getOrder();
-
- <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
- Object[] args) throws Exception;
+public class ValidatorServer {
+ public static void main(String[] args) throws Exception {
+ Log4jUtils.init();
+ BeanUtils.init();
+ }
}
diff --git a/demo/demo-validator/validator-server/src/main/resources/microservice.yaml b/demo/demo-validator/validator-server/src/main/resources/microservice.yaml
new file mode 100644
index 0000000..b2711fd
--- /dev/null
+++ b/demo/demo-validator/validator-server/src/main/resources/microservice.yaml
@@ -0,0 +1,33 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+APPLICATION_ID: validatortest
+service_description:
+ name: validator
+ version: 0.0.2
+cse:
+ service:
+ registry:
+ address: http://127.0.0.1:30100
+ rest:
+ address: 0.0.0.0:8080
+ highway:
+ address: 0.0.0.0:7070
+ handler:
+ chain:
+ Provider:
+ default: bizkeeper-provider
diff --git a/demo/pom.xml b/demo/pom.xml
index 2cf4603..465ec97 100644
--- a/demo/pom.xml
+++ b/demo/pom.xml
@@ -47,7 +47,7 @@
<module>demo-multiple</module>
<module>demo-signature</module>
<module>demo-edge</module>
-
+ <module>demo-validator</module>
<module>perf</module>
</modules>
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 150bce1..5c3caf0 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
@@ -50,10 +50,6 @@ public class SwaggerProducerOperation {
private List<ProducerInvokeExtension> producerInvokeExtenstionList =
SPIServiceUtils.getSortedService(ProducerInvokeExtension.class);
- public void setProducerInvokeExtenstionList(List<ProducerInvokeExtension> producerInvokeExtenstionList) {
- this.producerInvokeExtenstionList = producerInvokeExtenstionList;
- }
-
public String getName() {
return name;
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java
index bc2ea31..60706a9 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java
@@ -31,7 +31,7 @@ public class ExceptionToResponseConverters {
@SuppressWarnings("unchecked")
public ExceptionToResponseConverters() {
- SPIServiceUtils.getAllService(ExceptionToResponseConverter.class).forEach(converter -> {
+ SPIServiceUtils.getSortedService(ExceptionToResponseConverter.class).forEach(converter -> {
if (converter.getExceptionClass() == null) {
defaultConverter = converter;
return;
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
index e559124..370b3d1 100644
--- 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
@@ -19,11 +19,14 @@ package org.apache.servicecomb.swagger.invocation.extension;
import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation;
import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
-
+/**
+ * Producer method invocation extension to handle the required validations/checks before invoking the actual method.
+ */
public interface ProducerInvokeExtension {
public int getOrder();
+ ///Invoked before the method execution to handle the required checks before method invocation.
<T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
Object[] args) throws Exception;
}
diff --git a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToResponseConverter.java
index 61c2c30..5ade951 100644
--- a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToResponseConverter.java
+++ b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToResponseConverter.java
@@ -33,6 +33,11 @@ public class ConstraintViolationExceptionToResponseConverter
@Override
public Response convert(SwaggerInvocation swaggerInvocation, ConstraintViolationException e) {
- return Response.createFail(new InvocationException(Status.BAD_REQUEST, e.getMessage()));
+ return Response.createFail(new InvocationException(Status.BAD_REQUEST, e.getConstraintViolations().toString()));
+ }
+
+ @Override
+ public int getOrder() {
+ return -100;
}
}
diff --git a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
index a2ed0c6..0cb42ed 100644
--- a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
+++ b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
@@ -53,7 +53,7 @@ public class ParameterValidator implements ProducerInvokeExtension {
Default.class);
if (violations.size() > 0) {
LOGGER.warn("Parameter validation failed : " + violations.toString());
- throw new ConstraintViolationException(violations.toString(), violations);
+ throw new ConstraintViolationException(violations);
}
}
--
To stop receiving notification emails like this one, please contact
liubao@apache.org.