wujimin commented on a change in pull request #607: [SCB-406] Chassis must 
support standard parameter validation handler
URL: 
https://github.com/apache/incubator-servicecomb-java-chassis/pull/607#discussion_r177282866
 
 

 ##########
 File path: 
handlers/handler-validator/src/main/java/org/apache/servicecomb/validator/ParamValidateHandler.java
 ##########
 @@ -0,0 +1,114 @@
+/*
+ * 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.validator;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+
+import org.apache.servicecomb.core.Handler;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.swagger.invocation.AsyncResponse;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * ParamValidateHandler
+ * Handler to validate the input request parameters 
+ *
+ */
+public class ParamValidateHandler implements Handler {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(ParamValidateHandler.class);
+
+  private static Validator validator = 
Validation.buildDefaultValidatorFactory().getValidator();
+
+  @Override
+  public void handle(Invocation invocation, AsyncResponse asyncResp) throws 
Exception {
+    Object[] args = invocation.getArgs();
+    boolean invalid = false;
+    List<Object> errList = new ArrayList<>();
+    if (null != args) {
+      for (Object arg : args) {
+        Set<ConstraintViolation<Object>> violations = validator.validate(arg);
 
 Review comment:
   invocation arguments in  handler, is about contract arguments, not always 
equals producer arguments.
   so validation feature should be inject to SwaggerProducerOperation.
   maybe need to create a extension point before producerMethod.invoke, do not 
named it to be validateXXX, this mechanism just like AOP.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to