cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1541891 - in /cxf/trunk: core/src/main/java/org/apache/cxf/validation/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/swagger/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ systests/jaxrs/src/test/java/org/apache/c...
Date Thu, 14 Nov 2013 12:39:06 GMT
Author: sergeyb
Date: Thu Nov 14 12:39:06 2013
New Revision: 1541891

URL: http://svn.apache.org/r1541891
Log:
[CXF-5309] Support for custom ParameterNameProviders (patch from Andriy Redko), and adding
more configuration support

Added:
    cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationConfiguration.java  
(with props)
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSParameterNameProvider.java
  (with props)
Removed:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/swagger/
Modified:
    cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java

Added: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationConfiguration.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationConfiguration.java?rev=1541891&view=auto
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationConfiguration.java (added)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationConfiguration.java Thu
Nov 14 12:39:06 2013
@@ -0,0 +1,73 @@
+/**
+ * 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.cxf.validation;
+
+import java.util.Collections;
+import java.util.Map;
+
+import javax.validation.ConstraintValidatorFactory;
+import javax.validation.MessageInterpolator;
+import javax.validation.ParameterNameProvider;
+import javax.validation.TraversableResolver;
+
+public class ValidationConfiguration {
+    private ParameterNameProvider parameterNameProvider;
+    private MessageInterpolator messageInterpolator;
+    private TraversableResolver traversableResolver;
+    private ConstraintValidatorFactory constraintValidatorFactory; 
+    private Map<String, String> properties = Collections.emptyMap();
+    
+    public ValidationConfiguration() {
+        
+    }
+    public ValidationConfiguration(ParameterNameProvider parameterNameProvider) {
+        this.parameterNameProvider = parameterNameProvider;
+    }
+    
+    public ParameterNameProvider getParameterNameProvider() {
+        return parameterNameProvider;
+    }
+    public void setParameterNameProvider(ParameterNameProvider parameterNameProvider) {
+        this.parameterNameProvider = parameterNameProvider;
+    }
+    public Map<String, String> getProperties() {
+        return properties;
+    }
+    public void setProperties(Map<String, String> properties) {
+        this.properties = properties;
+    }
+    public MessageInterpolator getMessageInterpolator() {
+        return messageInterpolator;
+    }
+    public void setMessageInterpolator(MessageInterpolator messageInterpolator) {
+        this.messageInterpolator = messageInterpolator;
+    }
+    public TraversableResolver getTraversableResolver() {
+        return traversableResolver;
+    }
+    public void setTraversableResolver(TraversableResolver traversableResolver) {
+        this.traversableResolver = traversableResolver;
+    }
+    public ConstraintValidatorFactory getConstraintValidatorFactory() {
+        return constraintValidatorFactory;
+    }
+    public void setConstraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory)
{
+        this.constraintValidatorFactory = constraintValidatorFactory;
+    }
+}

Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationConfiguration.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java?rev=1541891&r1=1541890&r2=1541891&view=diff
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java (original)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java Thu Nov
14 12:39:06 2013
@@ -19,12 +19,14 @@
 package org.apache.cxf.validation;
 
 import java.lang.reflect.Method;
+import java.util.Map;
 import java.util.Set;
 import java.util.logging.Logger;
 
 import javax.validation.Configuration;
 import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
+import javax.validation.ParameterNameProvider;
 import javax.validation.Validation;
 import javax.validation.ValidationException;
 import javax.validation.ValidationProviderResolver;
@@ -42,38 +44,69 @@ public class ValidationProvider {
         try {
             factory = Validation.buildDefaultValidatorFactory();
         } catch (final ValidationException ex) {
-            LOG.severe("Bean Validation provider could be found, no validation will be performed");
+            LOG.severe("Bean Validation provider can not be found, no validation will be
performed");
             throw ex;
         }
     }
     
-    public ValidationProvider(ValidationProviderResolver resolver) {
+    public ValidationProvider(ParameterNameProvider parameterNameProvider) {
+        this(new ValidationConfiguration(parameterNameProvider));
+    }
+    
+    public ValidationProvider(ValidationConfiguration cfg) {
         try {
-            Configuration<?> cfg = Validation.byDefaultProvider().providerResolver(resolver).configure();
-            factory = cfg.buildValidatorFactory();
+            Configuration<?> factoryCfg = Validation.byDefaultProvider().configure();
+            initFactoryConfig(factoryCfg, cfg);
+            factory = factoryCfg.buildValidatorFactory();
         } catch (final ValidationException ex) {
-            LOG.severe("Bean Validation provider could be found, no validation will be performed");
+            LOG.severe("Bean Validation provider can not be found, no validation will be
performed");
             throw ex;
         }
     }
     
+    public ValidationProvider(ValidatorFactory factory) {
+        if (factory == null) {
+            throw new NullPointerException("Factory is null");
+        }
+        this.factory = factory;
+    }
+    
+    public ValidationProvider(ValidationProviderResolver resolver) {
+        this(resolver, null);
+    }
+    
+    public <T extends Configuration<T>> ValidationProvider(
+        ValidationProviderResolver resolver,
+        Class<javax.validation.spi.ValidationProvider<T>> providerType) {
+        this(resolver, null, null);
+    }
+    
     public <T extends Configuration<T>> ValidationProvider(
-        Class<javax.validation.spi.ValidationProvider<T>> providerType, 
-        ValidationProviderResolver resolver) {
+        ValidationProviderResolver resolver,
+        Class<javax.validation.spi.ValidationProvider<T>> providerType,
+        ValidationConfiguration cfg) {
         try {
-            Configuration<?> cfg = Validation.byProvider(providerType).providerResolver(resolver).configure();
-            factory = cfg.buildValidatorFactory();
+            Configuration<?> factoryCfg = providerType != null 
+                ? Validation.byProvider(providerType).providerResolver(resolver).configure()
+                : Validation.byDefaultProvider().providerResolver(resolver).configure();
  
+            initFactoryConfig(factoryCfg, cfg);
+            factory = factoryCfg.buildValidatorFactory();
         } catch (final ValidationException ex) {
-            LOG.severe("Bean Validation provider could be found, no validation will be performed");
+            LOG.severe("Bean Validation provider can not be found, no validation will be
performed");
             throw ex;
         }
     }
     
-    public ValidationProvider(ValidatorFactory factory) {
-        if (factory == null) {
-            throw new NullPointerException("Factory is null");
+    private static void initFactoryConfig(Configuration<?> factoryCfg, ValidationConfiguration
cfg) {
+        if (cfg != null) {
+            factoryCfg.parameterNameProvider(cfg.getParameterNameProvider());
+            factoryCfg.messageInterpolator(cfg.getMessageInterpolator());
+            factoryCfg.traversableResolver(cfg.getTraversableResolver());
+            factoryCfg.constraintValidatorFactory(cfg.getConstraintValidatorFactory());
+            for (Map.Entry<String, String> entry : cfg.getProperties().entrySet())
{
+                factoryCfg.addProperty(entry.getKey(), entry.getValue());
+            }
         }
-        this.factory = factory;
     }
     
     public< T > void validateParameters(final T instance, final Method method, final
Object[] arguments) {

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSParameterNameProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSParameterNameProvider.java?rev=1541891&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSParameterNameProvider.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSParameterNameProvider.java
Thu Nov 14 12:39:06 2013
@@ -0,0 +1,70 @@
+/**
+ * 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.cxf.jaxrs.validation;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.validation.ParameterNameProvider;
+
+import org.apache.cxf.jaxrs.model.Parameter;
+import org.apache.cxf.jaxrs.utils.ResourceUtils;
+
+public class JAXRSParameterNameProvider implements ParameterNameProvider {
+    @Override
+    public List<String> getParameterNames(final Constructor< ? > constructor)
{
+        final List< String > parameterNames = new ArrayList< String >();
+        
+        for (int i = 0; i < constructor.getParameterTypes().length; ++i) {
+            parameterNames.add("arg" + i);
+        }
+        
+        return parameterNames;
+    }
+
+    @Override
+    public List<String> getParameterNames(final Method method) {
+        final List< Parameter > parameters = ResourceUtils.getParameters(method);
+        final List< String > parameterNames = new ArrayList< String >();
+
+        for (int i = 0; i < parameters.size(); ++i) {
+            final StringBuilder sb = new StringBuilder();
+            sb.append("arg" + i);
+            sb.append("(");
+            
+            Parameter parameter = parameters.get(i);
+            if (parameter.getName() != null) {
+                sb.append(parameter.getType().toString());
+                sb.append("(\"" + parameter.getName() + "\")");
+                sb.append(" ");
+            }
+            sb.append(method.getParameterTypes()[i].getSimpleName());
+            
+            sb.append(")");
+            parameterNames.add(sb.toString());
+        }
+        
+        return parameterNames;
+    }
+    
+    
+
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSParameterNameProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSParameterNameProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java?rev=1541891&r1=1541890&r2=1541891&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
Thu Nov 14 12:39:06 2013
@@ -23,22 +23,17 @@ import java.util.logging.Logger;
 
 import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
-import javax.validation.Path;
 import javax.validation.ValidationException;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.ExceptionMapper;
 import javax.ws.rs.ext.Provider;
 
 import org.apache.cxf.common.logging.LogUtils;
-import org.apache.cxf.jaxrs.model.OperationResourceInfo;
-import org.apache.cxf.jaxrs.model.Parameter;
-import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.validation.ResponseConstraintViolationException;
 
 @Provider
 public class ValidationExceptionMapper implements ExceptionMapper< ValidationException
> {
     private static final Logger LOG = LogUtils.getL7dLogger(ValidationExceptionMapper.class);
-    private boolean reportParameterInfo;
     
     @Override
     public Response toResponse(ValidationException exception) {
@@ -48,9 +43,9 @@ public class ValidationExceptionMapper i
             final boolean isResponseException = constraint instanceof ResponseConstraintViolationException;
                         
             for (final ConstraintViolation< ? > violation: constraint.getConstraintViolations())
{
-                LOG.log(Level.SEVERE, 
+                LOG.log(Level.WARNING, 
                     violation.getRootBeanClass().getSimpleName() 
-                    + "." + getPropertyPathDescription(violation.getPropertyPath(), isResponseException)

+                    + "." + violation.getPropertyPath() 
                     + ": " + violation.getMessage());
             }
             
@@ -65,60 +60,4 @@ public class ValidationExceptionMapper i
         
         
     }
-    
-    protected String getPropertyPathDescription(Path propertyPath, boolean isResponseException)
{
-        final String path = propertyPath.toString();
-        if (isResponseException || !isReportParameterInfo()) {
-            return path;
-        }
-        int index = path.lastIndexOf(".arg");
-        if (index == -1 || index + 4 >= path.length()) {
-            return path;
-        }
-        
-        boolean compositePath = false;
-        
-        int argPos;
-        try { 
-            String argPath = path.substring(index + 4);
-            int infoIndex = argPath.indexOf('[');
-            if (infoIndex == -1) {
-                infoIndex = argPath.indexOf('.');
-            }
-            if (infoIndex > 0) {
-                argPath = argPath.substring(0, infoIndex);
-                compositePath = true;
-            }
-            argPos = Integer.valueOf(argPath);
-        } catch (NumberFormatException ex) {
-            return path;
-        }
-        final OperationResourceInfo ori = JAXRSUtils.getCurrentMessage().getExchange().get(OperationResourceInfo.class);
-        if (argPos < 0 || argPos > ori.getParameters().size()) {
-            return path;
-        }
-        Parameter param = ori.getParameters().get(argPos);
-        
-        StringBuilder sb = new StringBuilder();
-        sb.append("(");
-        if (compositePath) {
-            sb.append("arg" + argPos + " ");
-        }
-        sb.append("JAXRS param is " + param.getType().toString());
-        if (param.getName() != null) {
-            sb.append("(\"" + param.getName() + "\")");
-        }
-        sb.append(", class: " + ori.getAnnotatedMethod().getParameterTypes()[argPos].getSimpleName());
-        sb.append(")");
-        return path + sb.toString();
-            
-    }
-
-    public boolean isReportParameterInfo() {
-        return reportParameterInfo;
-    }
-
-    public void setReportParameterInfo(boolean reportParameterInfo) {
-        this.reportParameterInfo = reportParameterInfo;
-    }
 }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java?rev=1541891&r1=1541890&r2=1541891&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java
Thu Nov 14 12:39:06 2013
@@ -32,12 +32,14 @@ import org.apache.cxf.jaxrs.client.WebCl
 import org.apache.cxf.jaxrs.interceptor.JAXRSOutExceptionMapperInterceptor;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.jaxrs.validation.JAXRSParameterNameProvider;
 import org.apache.cxf.jaxrs.validation.JAXRSValidationInInterceptor;
 import org.apache.cxf.jaxrs.validation.JAXRSValidationOutInterceptor;
 import org.apache.cxf.jaxrs.validation.ValidationExceptionMapper;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.validation.ValidationProvider;
 
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -59,8 +61,10 @@ public class JAXRSClientServerValidation
             sf.setProvider(new ValidationExceptionMapper());
 
             sf.setAddress("http://localhost:" + PORT + "/");
+            JAXRSValidationInInterceptor in = new JAXRSValidationInInterceptor();
+            in.setProvider(new ValidationProvider(new JAXRSParameterNameProvider()));
             sf.setInInterceptors(Arrays.< Interceptor< ? extends Message > >asList(
-                new JAXRSValidationInInterceptor()));
+                in));
              
             sf.setOutInterceptors(Arrays.< Interceptor< ? extends Message > >asList(
                 new JAXRSOutExceptionMapperInterceptor(),



Mime
View raw message