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 29f68197d699ce367704fd9d571ead875ae3e5cf Author: acsukesh <sukes...@huawei.com> AuthorDate: Fri Mar 30 12:37:56 2018 +0530 [SCB-292] chassis support standard parameter validation --- .../exception/ExceptionToResponseConverter.java | 4 ++++ .../extension/AbstractProducerInvokeExtension.java | 20 -------------------- ...traintViolationExceptionToResponseConverter.java} | 20 ++++++++++++++++---- .../invocation/extension/ParameterValidator.java | 4 ++-- ...invocation.exception.ExceptionToResponseConverter | 3 ++- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java index 3b36f5a..4c14ad8 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java @@ -22,5 +22,9 @@ import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; public interface ExceptionToResponseConverter<T extends Throwable> { Class<T> getExceptionClass(); + default int getOrder() { + return 0; + } + Response convert(SwaggerInvocation swaggerInvocation, T 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 deleted file mode 100644 index 7080bf7..0000000 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/AbstractProducerInvokeExtension.java +++ /dev/null @@ -1,20 +0,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. - */ -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/exception/ExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ConstraintViolationExceptionToResponseConverter.java similarity index 53% copy from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java copy to swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ConstraintViolationExceptionToResponseConverter.java index 3b36f5a..98a1412 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/extension/ConstraintViolationExceptionToResponseConverter.java @@ -14,13 +14,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.servicecomb.swagger.invocation.exception; +package org.apache.servicecomb.swagger.invocation.extension; + +import javax.validation.ConstraintViolationException; +import javax.ws.rs.core.Response.Status; import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; +import org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter; +import org.apache.servicecomb.swagger.invocation.exception.InvocationException; -public interface ExceptionToResponseConverter<T extends Throwable> { - Class<T> getExceptionClass(); +public class ConstraintViolationExceptionToResponseConverter + implements ExceptionToResponseConverter<ConstraintViolationException> { + @Override + public Class<ConstraintViolationException> getExceptionClass() { + return ConstraintViolationException.class; + } - Response convert(SwaggerInvocation swaggerInvocation, T e); + @Override + public Response convert(SwaggerInvocation swaggerInvocation, ConstraintViolationException e) { + return Response.createFail(new InvocationException(Status.BAD_REQUEST, e.getMessage())); + } } 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 index 510ed75..7372377 100644 --- 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 @@ -30,7 +30,7 @@ import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ParameterValidator extends AbstractProducerInvokeExtension { +public class ParameterValidator implements ProducerInvokeExtension { private static final Logger LOGGER = LoggerFactory.getLogger(ParameterValidator.class); @@ -52,7 +52,7 @@ public class ParameterValidator extends AbstractProducerInvokeExtension { Default.class); if (violations.size() > 0) { LOGGER.warn("Parameter validation failed : " + violations.toString()); - throw new ConstraintViolationException(violations); + throw new ConstraintViolationException(violations.toString(), violations); } } diff --git a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter index 2fc781e..1d0e0bc 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter +++ b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter @@ -16,4 +16,5 @@ # org.apache.servicecomb.swagger.invocation.exception.InvocationExceptionToResponseConverter -org.apache.servicecomb.swagger.invocation.exception.DefaultExceptionToResponseConverter \ No newline at end of file +org.apache.servicecomb.swagger.invocation.exception.DefaultExceptionToResponseConverter +org.apache.servicecomb.swagger.invocation.extension.ConstraintViolationExceptionToResponseConverter \ No newline at end of file -- To stop receiving notification emails like this one, please contact liu...@apache.org.