You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/10/31 18:05:46 UTC
svn commit: r1537558 - in /cxf/trunk: core/
core/src/main/java/org/apache/cxf/validation/ parent/ rt/frontend/jaxrs/
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/
systests/jaxrs/ systests/jaxrs/src/test/java/org/apache/cxf/systest/ja...
Author: sergeyb
Date: Thu Oct 31 17:05:46 2013
New Revision: 1537558
URL: http://svn.apache.org/r1537558
Log:
[CXF-5309] Initial support for JAX-RS BeanValidation, applying a patch from Andriy Redko with minor modifications
Added:
cxf/trunk/core/src/main/java/org/apache/cxf/validation/
cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java (with props)
cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInterceptor.java (with props)
cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java (with props)
cxf/trunk/core/src/main/java/org/apache/cxf/validation/ResponseConstraintViolationException.java (with props)
cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java (with props)
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ConstraintViolationExceptionMapper.java (with props)
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationFeature.java (with props)
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInInterceptor.java (with props)
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java (with props)
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java (with props)
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java (with props)
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java (with props)
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java (with props)
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java (with props)
Modified:
cxf/trunk/core/pom.xml
cxf/trunk/parent/pom.xml
cxf/trunk/rt/frontend/jaxrs/pom.xml
cxf/trunk/systests/jaxrs/pom.xml
Modified: cxf/trunk/core/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/core/pom.xml?rev=1537558&r1=1537557&r2=1537558&view=diff
==============================================================================
--- cxf/trunk/core/pom.xml (original)
+++ cxf/trunk/core/pom.xml Thu Oct 31 17:05:46 2013
@@ -74,7 +74,10 @@
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
-
+ <dependency>
+ <groupId>javax.validation</groupId>
+ <artifactId>validation-api</artifactId>
+ </dependency>
<dependency>
<groupId>${cxf.asm.groupId}</groupId>
<artifactId>${cxf.asm.artifactId}</artifactId>
Added: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java (added)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,42 @@
+/**
+ * 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.lang.reflect.Method;
+import java.util.List;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.Phase;
+
+public abstract class AbstractValidationInInterceptor extends AbstractValidationInterceptor {
+ public AbstractValidationInInterceptor() {
+ super(Phase.PRE_INVOKE);
+ }
+ public AbstractValidationInInterceptor(String phase) {
+ super(phase);
+ }
+
+ @Override
+ protected void handleValidation(final Message message, final Object resourceInstance,
+ final Method method, final List<Object> arguments) {
+ ValidationProvider provider = getProvider(message);
+ provider.validate(resourceInstance, method, arguments.toArray());
+ message.getExchange().put(ValidationProvider.class, provider);
+ }
+}
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInterceptor.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInterceptor.java (added)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInterceptor.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,106 @@
+/**
+ * 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.lang.reflect.Method;
+import java.util.List;
+import java.util.logging.Logger;
+
+import javax.validation.ValidationException;
+
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageContentsList;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.service.Service;
+import org.apache.cxf.service.invoker.MethodDispatcher;
+import org.apache.cxf.service.model.BindingOperationInfo;
+
+public abstract class AbstractValidationInterceptor extends AbstractPhaseInterceptor< Message > {
+ private static final Logger LOG = LogUtils.getL7dLogger(AbstractValidationInterceptor.class);
+
+ private volatile ValidationProvider provider;
+
+ public AbstractValidationInterceptor(String phase) {
+ super(phase);
+ }
+
+ public void setProvider(ValidationProvider provider) {
+ this.provider = provider;
+ }
+
+ @Override
+ public void handleMessage(Message message) throws Fault {
+ final Object resourceInstance = getResourceInstance(message);
+ if (resourceInstance == null) {
+ String error = "Resource instance is not available";
+ LOG.severe(error);
+ throw new ValidationException(error);
+ }
+
+ final Method method = getResourceMethod(message);
+ if (method == null) {
+ String error = "Resource method is not available";
+ LOG.severe(error);
+ throw new ValidationException(error);
+ }
+
+
+ final List< Object > arguments = MessageContentsList.getContentsList(message);
+
+ handleValidation(message, resourceInstance, method, arguments);
+
+ }
+
+ protected abstract Object getResourceInstance(Message message);
+
+ protected Method getResourceMethod(Message message) {
+ Message inMessage = message.getExchange().getInMessage();
+ Method method = (Method)inMessage.get("org.apache.cxf.resource.method");
+ if (method == null) {
+ BindingOperationInfo bop = inMessage.getExchange().get(BindingOperationInfo.class);
+ if (bop != null) {
+ MethodDispatcher md = (MethodDispatcher)
+ inMessage.getExchange().get(Service.class).get(MethodDispatcher.class.getName());
+ method = md.getMethod(bop);
+ }
+ }
+ return method;
+ }
+
+ protected abstract void handleValidation(final Message message, final Object resourceInstance,
+ final Method method, final List<Object> arguments);
+
+
+ protected ValidationProvider getProvider(Message message) {
+ if (provider == null) {
+ Object prop = message.getContextualProperty(ValidationProvider.class.getName());
+ if (prop != null) {
+ provider = (ValidationProvider)prop;
+ } else {
+ provider = new ValidationProvider();
+ }
+ }
+ return provider;
+ }
+
+
+}
+
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInterceptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInterceptor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java (added)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,56 @@
+/**
+ * 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.lang.reflect.Method;
+import java.util.List;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.Phase;
+
+public abstract class AbstractValidationOutInterceptor extends AbstractValidationInterceptor {
+ public AbstractValidationOutInterceptor() {
+ super(Phase.PRE_MARSHAL);
+ }
+ public AbstractValidationOutInterceptor(String phase) {
+ super(phase);
+ }
+
+ @Override
+ protected void handleValidation(final Message message, final Object resourceInstance,
+ final Method method, final List<Object> arguments) {
+ if (arguments.size() == 1) {
+ Object responseObject = getResponseObject(arguments.get(0));
+ if (responseObject != null) {
+ getOutProvider(message).validate(resourceInstance, method, responseObject);
+ }
+ }
+ }
+
+ protected Object getResponseObject(Object object) {
+ return object;
+ }
+
+ protected ValidationProvider getOutProvider(Message message) {
+ ValidationProvider provider = message.getExchange().get(ValidationProvider.class);
+ return provider == null ? getProvider(message) : provider;
+ }
+
+
+}
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ResponseConstraintViolationException.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/ResponseConstraintViolationException.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/ResponseConstraintViolationException.java (added)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/ResponseConstraintViolationException.java Thu Oct 31 17:05:46 2013
@@ -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.cxf.validation;
+
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+
+@SuppressWarnings("serial")
+public class ResponseConstraintViolationException extends ConstraintViolationException {
+ public ResponseConstraintViolationException(Set<? extends ConstraintViolation<?>> constraintViolations) {
+ super(constraintViolations);
+ }
+}
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ResponseConstraintViolationException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ResponseConstraintViolationException.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: 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=1537558&view=auto
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java (added)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,99 @@
+/**
+ * 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.lang.reflect.Method;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.validation.Configuration;
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Validation;
+import javax.validation.ValidationException;
+import javax.validation.ValidationProviderResolver;
+import javax.validation.ValidatorFactory;
+import javax.validation.executable.ExecutableValidator;
+
+import org.apache.cxf.common.logging.LogUtils;
+
+public class ValidationProvider {
+ private static final Logger LOG = LogUtils.getL7dLogger(ValidationProvider.class);
+
+ private final ValidatorFactory factory;
+
+ public ValidationProvider() {
+ try {
+ factory = Validation.buildDefaultValidatorFactory();
+ } catch (final ValidationException ex) {
+ LOG.severe("Bean Validation provider could be found, no validation will be performed");
+ throw ex;
+ }
+ }
+
+ public ValidationProvider(ValidationProviderResolver resolver) {
+ try {
+ Configuration<?> cfg = Validation.byDefaultProvider().providerResolver(resolver).configure();
+ factory = cfg.buildValidatorFactory();
+ } catch (final ValidationException ex) {
+ LOG.severe("Bean Validation provider could be found, no validation will be performed");
+ throw ex;
+ }
+ }
+
+ public <T extends Configuration<T>> ValidationProvider(
+ Class<javax.validation.spi.ValidationProvider<T>> providerType,
+ ValidationProviderResolver resolver) {
+ try {
+ Configuration<?> cfg = Validation.byProvider(providerType).providerResolver(resolver).configure();
+ factory = cfg.buildValidatorFactory();
+ } catch (final ValidationException ex) {
+ LOG.severe("Bean Validation provider could 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< T > void validate(final T instance, final Method method, final Object[] arguments) {
+
+ final ExecutableValidator methodValidator = factory.getValidator().forExecutables();
+ final Set< ConstraintViolation< T > > violations = methodValidator.validateParameters(instance,
+ method, arguments);
+
+ if (!violations.isEmpty()) {
+ throw new ConstraintViolationException(violations);
+ }
+ }
+
+ public< T > void validate(final T instance, final Method method, final Object returnValue) {
+ final ExecutableValidator methodValidator = factory.getValidator().forExecutables();
+ final Set<ConstraintViolation< T > > violations = methodValidator.validateReturnValue(instance,
+ method, returnValue);
+
+ if (!violations.isEmpty()) {
+ throw new ResponseConstraintViolationException(violations);
+ }
+ }
+}
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: cxf/trunk/parent/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/parent/pom.xml?rev=1537558&r1=1537557&r2=1537558&view=diff
==============================================================================
--- cxf/trunk/parent/pom.xml (original)
+++ cxf/trunk/parent/pom.xml Thu Oct 31 17:05:46 2013
@@ -159,8 +159,7 @@
<cxf.xmlschema.version>2.0.3</cxf.xmlschema.version>
<cxf.xpp3.bundle.version>1.1.4c_6</cxf.xpp3.bundle.version>
-
-
+ <cxf.validation.api.version>1.1.0.Final</cxf.validation.api.version>
<!-- various OSGi related versions -->
<cxf.aries.version>1.0.0</cxf.aries.version>
<cxf.aries.version.range>[1.0,2)</cxf.aries.version.range>
@@ -771,6 +770,13 @@
</dependency>
<dependency>
+ <groupId>javax.validation</groupId>
+ <artifactId>validation-api</artifactId>
+ <version>${cxf.validation.api.version}</version>
+ <optional>true</optional>
+ </dependency>
+
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${cxf.slf4j.version}</version>
Modified: cxf/trunk/rt/frontend/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/pom.xml?rev=1537558&r1=1537557&r2=1537558&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/pom.xml (original)
+++ cxf/trunk/rt/frontend/jaxrs/pom.xml Thu Oct 31 17:05:46 2013
@@ -46,6 +46,10 @@
<dependencies>
<dependency>
+ <groupId>javax.validation</groupId>
+ <artifactId>validation-api</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.core</artifactId>
<optional>true</optional>
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ConstraintViolationExceptionMapper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ConstraintViolationExceptionMapper.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ConstraintViolationExceptionMapper.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ConstraintViolationExceptionMapper.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,52 @@
+/**
+ * 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.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+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.validation.ResponseConstraintViolationException;
+
+@Provider
+public class ConstraintViolationExceptionMapper implements ExceptionMapper< ConstraintViolationException > {
+ private static final Logger LOG = LogUtils.getL7dLogger(ConstraintViolationExceptionMapper.class);
+
+ @Override
+ public Response toResponse(ConstraintViolationException exception) {
+ for (final ConstraintViolation< ? > violation: exception.getConstraintViolations()) {
+ LOG.log(Level.SEVERE,
+ violation.getRootBeanClass().getSimpleName()
+ + "." + violation.getPropertyPath()
+ + ": " + violation.getMessage());
+ }
+
+ if (exception instanceof ResponseConstraintViolationException) {
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
+ }
+
+ return Response.status(Response.Status.BAD_REQUEST).build();
+ }
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ConstraintViolationExceptionMapper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ConstraintViolationExceptionMapper.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationFeature.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationFeature.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationFeature.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationFeature.java Thu Oct 31 17:05:46 2013
@@ -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.
+ */
+package org.apache.cxf.jaxrs.validation;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.feature.AbstractFeature;
+import org.apache.cxf.interceptor.InterceptorProvider;
+
+
+public class JAXRSValidationFeature extends AbstractFeature {
+
+ @Override
+ protected void initializeProvider(InterceptorProvider provider, Bus bus) {
+ provider.getInInterceptors().add(new JAXRSValidationInInterceptor());
+ provider.getOutInterceptors().add(new JAXRSValidationInInterceptor());
+ }
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationFeature.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationFeature.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInInterceptor.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInInterceptor.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInInterceptor.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,47 @@
+/**
+ * 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.Method;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.validation.AbstractValidationInInterceptor;
+
+
+public class JAXRSValidationInInterceptor extends AbstractValidationInInterceptor {
+ public JAXRSValidationInInterceptor() {
+ }
+ public JAXRSValidationInInterceptor(String phase) {
+ super(phase);
+ }
+
+ @Override
+ protected Object getResourceInstance(Message message) {
+ return ValidationUtils.getResourceInstance(message);
+ }
+
+ @Override
+ protected Method getResourceMethod(Message message) {
+ if (!ValidationUtils.isAnnotatedMethodAvailable(message)) {
+ return null;
+ } else {
+ return super.getResourceMethod(message);
+ }
+ }
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInInterceptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInInterceptor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,78 @@
+/**
+ * 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.Method;
+import java.util.List;
+import java.util.logging.Logger;
+
+import javax.validation.ValidationException;
+
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.jaxrs.JAXRSInvoker;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.validation.ValidationProvider;
+
+
+public class JAXRSValidationInvoker extends JAXRSInvoker {
+ private static final Logger LOG = LogUtils.getL7dLogger(JAXRSValidationInvoker.class);
+ private volatile ValidationProvider provider;
+
+ @Override
+ public Object invoke(Exchange exchange, final Object serviceObject, Method m, List<Object> params) {
+ Message message = JAXRSUtils.getCurrentMessage();
+
+ if (!ValidationUtils.isAnnotatedMethodAvailable(message)) {
+ String error = "Resource method is not available";
+ LOG.severe(error);
+ throw new ValidationException(error);
+ }
+
+ ValidationProvider theProvider = getProvider(message);
+
+ theProvider.validate(serviceObject, m, params.toArray());
+
+ Object response = super.invoke(exchange, serviceObject, m, params);
+
+ Object responseToValidate = ValidationUtils.getResponseObject(response);
+ if (responseToValidate != null) {
+ theProvider.validate(serviceObject, m, responseToValidate);
+ }
+
+ return response;
+ }
+
+ protected ValidationProvider getProvider(Message message) {
+ if (provider == null) {
+ Object prop = message.getContextualProperty(ValidationProvider.class.getName());
+ if (prop != null) {
+ provider = (ValidationProvider)prop;
+ } else {
+ provider = new ValidationProvider();
+ }
+ }
+ return provider;
+ }
+
+ public void setProvider(ValidationProvider provider) {
+ this.provider = provider;
+ }
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,52 @@
+/**
+ * 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.Method;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.validation.AbstractValidationOutInterceptor;
+
+
+public class JAXRSValidationOutInterceptor extends AbstractValidationOutInterceptor {
+ public JAXRSValidationOutInterceptor() {
+ }
+ public JAXRSValidationOutInterceptor(String phase) {
+ super(phase);
+ }
+
+ @Override
+ protected Object getResourceInstance(Message message) {
+ return ValidationUtils.getResourceInstance(message);
+ }
+
+ @Override
+ protected Method getResourceMethod(Message message) {
+ if (!ValidationUtils.isAnnotatedMethodAvailable(message)) {
+ return null;
+ } else {
+ return super.getResourceMethod(message);
+ }
+ }
+
+ @Override
+ protected Object getResponseObject(Object o) {
+ return ValidationUtils.getResponseObject(o);
+ }
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,58 @@
+/**
+ * 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.util.logging.Logger;
+
+import javax.ws.rs.core.Response;
+
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
+import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.message.Message;
+
+
+public final class ValidationUtils {
+ private static final Logger LOG = LogUtils.getL7dLogger(ValidationUtils.class);
+ private ValidationUtils() {
+ }
+ public static Object getResourceInstance(Message message) {
+ final OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
+ final ResourceProvider resourceProvider = ori.getClassResourceInfo().getResourceProvider();
+ if (!resourceProvider.isSingleton()) {
+ String error = "Service object is not a singleton, use a custom invoker to validate";
+ LOG.warning(error);
+ return null;
+ } else {
+ return resourceProvider.getInstance(message);
+ }
+ }
+
+ public static boolean isAnnotatedMethodAvailable(Message message) {
+ final OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
+
+ // If this is a user-model resource then no validation is possible
+ return ori.getAnnotatedMethod() != null;
+
+ }
+
+ public static Object getResponseObject(Object o) {
+ return o instanceof Response ? ((Response)o).getEntity() : o;
+ }
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: cxf/trunk/systests/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/pom.xml?rev=1537558&r1=1537557&r2=1537558&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/pom.xml (original)
+++ cxf/trunk/systests/jaxrs/pom.xml Thu Oct 31 17:05:46 2013
@@ -31,8 +31,33 @@
<description>Apache CXF JAX-RS System Tests</description>
<url>http://cxf.apache.org</url>
+ <properties>
+ <cxf.el.api.version>3.0-b02</cxf.el.api.version>
+ <cxf.glassfish.el.version>3.0-b01</cxf.glassfish.el.version>
+ <cxf.hibernate.validator.version>5.0.1.Final</cxf.hibernate.validator.version>
+ </properties>
+
<dependencies>
<dependency>
+ <groupId>javax.validation</groupId>
+ <artifactId>validation-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-validator</artifactId>
+ <version>${cxf.hibernate.validator.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.el</groupId>
+ <artifactId>javax.el-api</artifactId>
+ <version>${cxf.el.api.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish</groupId>
+ <artifactId>javax.el</artifactId>
+ <version>${cxf.glassfish.el.version}</version>
+ </dependency>
+ <dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
</dependency>
Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,78 @@
+/**
+ * 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.systest.jaxrs.validation;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.validation.Valid;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+@Path("/bookstore/")
+public class BookStoreWithValidation {
+ private Map< String, BookWithValidation > books = new HashMap< String, BookWithValidation >();
+
+ public BookStoreWithValidation() {
+ }
+
+ @GET
+ @Path("/books/{bookId}")
+ @Valid
+ public BookWithValidation getBook(@Pattern(regexp = "\\d+") @PathParam("bookId") String id) {
+ return books.get(id);
+ }
+
+ @POST
+ @Path("/books")
+ public Response addBook(@Context final UriInfo uriInfo,
+ @NotNull @Size(min = 1, max = 50) @FormParam("id") String id,
+ @FormParam("name") String name) {
+ books.put(id, new BookWithValidation(name, id));
+ return Response.created(uriInfo.getRequestUriBuilder().path(id).build()).build();
+ }
+
+ @GET
+ @Path("/books")
+ @Valid public Collection< BookWithValidation > list(
+ @Min(1) @DefaultValue("1") @QueryParam("page") int page) {
+ return books.values();
+ }
+
+ @DELETE
+ @Path("/books")
+ public Response clear() {
+ books.clear();
+ return Response.ok().build();
+ }
+}
Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java?rev=1537558&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,61 @@
+/**
+ * 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.systest.jaxrs.validation;
+
+import javax.validation.constraints.NotNull;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
+import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
+
+@JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY, property = "class")
+@XmlRootElement(name = "BookWithValidation")
+public class BookWithValidation {
+ @NotNull private String name;
+ private String id;
+
+ public BookWithValidation() {
+ }
+
+ public BookWithValidation(String id) {
+ this.id = id;
+ }
+
+ public BookWithValidation(String name, String id) {
+ this.name = name;
+ this.id = id;
+ }
+
+ public void setName(String n) {
+ name = n;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setId(String i) {
+ id = i;
+ }
+
+ public String getId() {
+ return id;
+ }
+}
Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: 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=1537558&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java Thu Oct 31 17:05:46 2013
@@ -0,0 +1,160 @@
+/**
+ * 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.systest.jaxrs.validation;
+
+import java.util.Arrays;
+
+import javax.ws.rs.core.Form;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
+import org.apache.cxf.interceptor.Interceptor;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.WebClient;
+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.ConstraintViolationExceptionMapper;
+import org.apache.cxf.jaxrs.validation.JAXRSValidationInInterceptor;
+import org.apache.cxf.jaxrs.validation.JAXRSValidationOutInterceptor;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class JAXRSClientServerValidationTest extends AbstractBusClientServerTestBase {
+ public static final String PORT = allocatePort(Server.class);
+
+ @Ignore
+ public static class Server extends AbstractBusTestServerBase {
+ @SuppressWarnings("unchecked")
+ protected void run() {
+ JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+
+ sf.setResourceClasses(BookStoreWithValidation.class);
+ sf.setResourceProvider(BookStoreWithValidation.class,
+ new SingletonResourceProvider(new BookStoreWithValidation()));
+ sf.setProvider(new ConstraintViolationExceptionMapper());
+
+ sf.setAddress("http://localhost:" + PORT + "/");
+ sf.setInInterceptors(Arrays.< Interceptor< ? extends Message > >asList(
+ new JAXRSValidationInInterceptor()));
+
+ sf.setOutInterceptors(Arrays.< Interceptor< ? extends Message > >asList(
+ new JAXRSOutExceptionMapperInterceptor(),
+ new JAXRSValidationOutInterceptor()));
+
+ sf.create();
+ }
+
+ public static void main(String[] args) {
+ try {
+ Server s = new Server();
+ s.start();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ System.exit(-1);
+ } finally {
+ System.out.println("done!");
+ }
+ }
+ }
+
+ @BeforeClass
+ public static void startServers() throws Exception {
+ AbstractResourceInfo.clearAllMaps();
+ //keep out of process due to stack traces testing failures
+ assertTrue("server did not launch correctly", launchServer(Server.class, true));
+ createStaticBus();
+ }
+
+ @Before
+ public void setUp() {
+ final Response r = createWebClient("/bookstore/books").delete();
+ assertEquals(Status.OK.getStatusCode(), r.getStatus());
+ }
+
+ @Test
+ public void testThatPatternValidationFails() throws Exception {
+ final Response r = createWebClient("/bookstore/books/blabla").get();
+ assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus());
+ }
+
+ @Test
+ public void testThatNotNullValidationFails() {
+ final Response r = createWebClient("/bookstore/books").post(new Form());
+ assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus());
+ }
+
+ @Test
+ public void testThatSizeValidationFails() {
+ final Response r = createWebClient("/bookstore/books").post(new Form().param("id", ""));
+ assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus());
+ }
+
+ @Test
+ public void testThatMinValidationFails() {
+ final Response r = createWebClient("/bookstore/books").query("page", "0").get();
+ assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus());
+ }
+
+ @Test
+ public void testThatNoValidationConstraintsAreViolated() {
+ final Response r = createWebClient("/bookstore/books").query("page", "2").get();
+ assertEquals(Status.OK.getStatusCode(), r.getStatus());
+ }
+
+ @Test
+ public void testThatNoValidationConstraintsAreViolatedWithDefaultValue() {
+ final Response r = createWebClient("/bookstore/books").get();
+ assertEquals(Status.OK.getStatusCode(), r.getStatus());
+ }
+
+ @Test
+ public void testThatResponseValidationForOneBookFails() {
+ Response r = createWebClient("/bookstore/books").post(new Form().param("id", "1234"));
+ assertEquals(Status.CREATED.getStatusCode(), r.getStatus());
+
+ r = createWebClient("/bookstore/books/1234").get();
+ assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
+ }
+
+ @Test
+ public void testThatResponseValidationForAllBooksFails() {
+ Response r = createWebClient("/bookstore/books").post(new Form().param("id", "1234"));
+ assertEquals(Status.CREATED.getStatusCode(), r.getStatus());
+
+ r = createWebClient("/bookstore/books").get();
+ assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
+ }
+
+ private WebClient createWebClient(final String url) {
+ WebClient wc = WebClient
+ .create("http://localhost:" + PORT + url)
+ .accept(MediaType.APPLICATION_JSON);
+ WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
+ return wc;
+ }
+}
+
Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date