Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 4ade0d4

Browse files
Marek PotociarGerrit Code Review
authored andcommitted
Merge "J-443: Provide SPI for ValidationResult - added test for property injected validation result - enabled cdi field injected components to get validated - Marek review: New API renames and other minor updates (mostly checkstyle warning fixes)"
2 parents 74f7608 + d0f9cd2 commit 4ade0d4

37 files changed

+2349
-49
lines changed

core-server/src/main/java/org/glassfish/jersey/server/model/ResourceMethodInvoker.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import org.glassfish.jersey.server.model.internal.ResourceMethodDispatcherFactory;
8484
import org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory;
8585
import org.glassfish.jersey.server.monitoring.RequestEvent;
86+
import org.glassfish.jersey.server.spi.ValidationInterceptor;
8687
import org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher;
8788
import org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider;
8889

core-server/src/main/java/org/glassfish/jersey/server/model/internal/AbstractJavaResourceMethodDispatcher.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
*/
4040
package org.glassfish.jersey.server.model.internal;
4141

42-
import java.lang.reflect.Field;
4342
import java.lang.reflect.InvocationHandler;
4443
import java.lang.reflect.InvocationTargetException;
4544
import java.lang.reflect.Method;
@@ -51,7 +50,6 @@
5150
import javax.ws.rs.core.Response;
5251
import javax.ws.rs.core.SecurityContext;
5352

54-
import javax.validation.ConstraintViolationException;
5553
import javax.validation.ValidationException;
5654

5755
import org.glassfish.jersey.message.internal.TracingLogger;
@@ -85,9 +83,9 @@ abstract class AbstractJavaResourceMethodDispatcher implements ResourceMethodDis
8583
* @param methodHandler method invocation handler.
8684
* @param validator input/output parameter validator.
8785
*/
88-
AbstractJavaResourceMethodDispatcher(Invocable resourceMethod,
89-
InvocationHandler methodHandler,
90-
ConfiguredValidator validator) {
86+
AbstractJavaResourceMethodDispatcher(final Invocable resourceMethod,
87+
final InvocationHandler methodHandler,
88+
final ConfiguredValidator validator) {
9189
this.method = resourceMethod.getDefinitionMethod();
9290
this.methodHandler = methodHandler;
9391
this.resourceMethod = resourceMethod;
@@ -133,25 +131,7 @@ final Object invoke(final ContainerRequest containerRequest, final Object resour
133131
try {
134132
// Validate resource class & method input parameters.
135133
if (validator != null) {
136-
try {
137-
validator.validateResourceAndInputParams(resource, resourceMethod, args);
138-
} catch (ConstraintViolationException e) {
139-
// First check for a property
140-
if (ValidationResultUtil.hasValidationResultProperty(resource)) {
141-
final Method validationResultGetter = ValidationResultUtil.getValidationResultGetter(resource);
142-
ValidationResultUtil.updateValidationResultProperty(resource, validationResultGetter,
143-
e.getConstraintViolations());
144-
} else {
145-
// Then check for a field
146-
final Field validationResult = ValidationResultUtil.getValidationResultField(resource);
147-
if (validationResult != null) {
148-
ValidationResultUtil.updateValidationResultField(resource, validationResult,
149-
e.getConstraintViolations());
150-
} else {
151-
throw e;
152-
}
153-
}
154-
}
134+
validator.validateResourceAndInputParams(resource, resourceMethod, args);
155135
}
156136

157137
final PrivilegedAction invokeMethodAction = new PrivilegedAction() {

core-server/src/main/java/org/glassfish/jersey/server/model/internal/JavaResourceMethodDispatcherProvider.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
import org.glassfish.jersey.server.ContainerRequest;
5353
import org.glassfish.jersey.server.model.Invocable;
54+
import org.glassfish.jersey.server.spi.ValidationInterceptor;
5455
import org.glassfish.jersey.server.spi.internal.ParameterValueHelper;
5556
import org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher;
5657

@@ -79,18 +80,19 @@ public ResourceMethodDispatcher create(final Invocable resourceMethod,
7980

8081
ResourceMethodDispatcher resourceMethodDispatcher;
8182
if (Response.class.isAssignableFrom(returnType)) {
82-
resourceMethodDispatcher = new ResponseOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
83-
// TODO should we support JResponse?
84-
// } else if (JResponse.class.isAssignableFrom(returnType)) {
85-
// return new JResponseOutInvoker(resourceMethod, pp, invocationHandler);
83+
resourceMethodDispatcher =
84+
new ResponseOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
8685
} else if (returnType != void.class) {
8786
if (returnType == Object.class || GenericEntity.class.isAssignableFrom(returnType)) {
88-
resourceMethodDispatcher = new ObjectOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
87+
resourceMethodDispatcher =
88+
new ObjectOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
8989
} else {
90-
resourceMethodDispatcher = new TypeOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
90+
resourceMethodDispatcher =
91+
new TypeOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
9192
}
9293
} else {
93-
resourceMethodDispatcher = new VoidOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
94+
resourceMethodDispatcher
95+
= new VoidOutInvoker(resourceMethod, invocationHandler, valueProviders, validator);
9496
}
9597

9698
// Inject validator.

core-server/src/main/java/org/glassfish/jersey/server/model/internal/ResourceMethodDispatcherFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.glassfish.jersey.server.internal.LocalizationMessages;
5252
import org.glassfish.jersey.server.internal.inject.ConfiguredValidator;
5353
import org.glassfish.jersey.server.model.Invocable;
54+
import org.glassfish.jersey.server.spi.ValidationInterceptor;
5455
import org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher;
5556

5657
import org.glassfish.hk2.api.ServiceLocator;

core-server/src/main/java/org/glassfish/jersey/server/model/internal/VoidVoidDispatcherProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.glassfish.jersey.server.ContainerRequest;
5252
import org.glassfish.jersey.server.internal.inject.ConfiguredValidator;
5353
import org.glassfish.jersey.server.model.Invocable;
54+
import org.glassfish.jersey.server.spi.ValidationInterceptor;
5455
import org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher;
5556

5657
/**
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.server.spi;
41+
42+
import javax.validation.ConstraintViolationException;
43+
import javax.validation.ValidationException;
44+
45+
import org.glassfish.jersey.spi.Contract;
46+
47+
/**
48+
* Interceptor for method validation processing.
49+
*
50+
* Allows to override the default Jersey behaviour. By default, the Jersey runtime throws a
51+
* {@link javax.validation.ValidationException} or one of its subclasses,
52+
* that gets mapped to a HTTP 400 response, if any validation issues occur. In such case
53+
* the actual resource method does not get invoked at all.
54+
* <p>
55+
* Validation interceptor implementation allows to e.g. swallow the {@link ConstraintViolationException}
56+
* and handle the validation issues in the resource method. It is also possible to tweak
57+
* validated components. This could be utilized in case of proxied resources,
58+
* when field validation is not possible on a dynamic proxy, and the validator requires
59+
* the original delegated instance.
60+
* </p>
61+
* <p>
62+
* Each validation interceptor implementation must invoke proceed
63+
* method on provided interceptor context as part of interception processing.
64+
* </p>
65+
*
66+
* @author Jakub Podlesak (jakub.podleak at oracle.com)
67+
* @since 2.18
68+
*/
69+
@Contract
70+
public interface ValidationInterceptor {
71+
72+
/**
73+
* Used to intercept resource method validation processing.
74+
* <p/>
75+
* To allow further validation processing, every and each implementation
76+
* must invoke {@link ValidationInterceptorContext#proceed()} method.
77+
*
78+
* @param context method validation context.
79+
* @throws ValidationException in case the validation exception should be thrown as a result of the validation processing.
80+
*/
81+
public void onValidate(ValidationInterceptorContext context) throws ValidationException;
82+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.server.spi;
41+
42+
import javax.validation.ValidationException;
43+
44+
import org.glassfish.jersey.server.model.Invocable;
45+
46+
/**
47+
* Context for resource method validation interception processing (see {@link ValidationInterceptor}).
48+
* The context gives access to key validation data.
49+
* It also allows interceptor implementation to tweak resource and/or parameters that are going to be validated.
50+
*
51+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
52+
* @see ValidationInterceptor
53+
* @since 2.18
54+
*/
55+
public interface ValidationInterceptorContext {
56+
57+
/**
58+
* Provide actual resource instance that will get validated.
59+
*
60+
* @return current resource instance.
61+
*/
62+
public Object getResource();
63+
64+
/**
65+
* Setter for resource instance that should get validated.
66+
*
67+
* @param resource instance to validate
68+
*/
69+
public void setResource(Object resource);
70+
71+
/**
72+
* Provide invocable for which validation will been done.
73+
*
74+
* @return actual invocable instance.
75+
*/
76+
public Invocable getInvocable();
77+
78+
/**
79+
* Provide method parameters for which validation will be done.
80+
*
81+
* @return actual method parameters.
82+
*/
83+
public Object[] getArgs();
84+
85+
/**
86+
* Method parameters setter.
87+
*
88+
* @param args method parameters to be used for validation.
89+
*/
90+
public void setArgs(Object[] args);
91+
92+
/**
93+
* Proceed with validation.
94+
*
95+
* This method must be invoked by a validation interceptor implementation.
96+
*
97+
* @throws javax.validation.ValidationException in case the further validation processing failed with a validation error.
98+
*/
99+
public void proceed() throws ValidationException;
100+
}

core-server/src/main/java/org/glassfish/jersey/server/spi/internal/ResourceMethodDispatcher.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
package org.glassfish.jersey.server.spi.internal;
4141

4242
import java.lang.reflect.InvocationHandler;
43+
import java.util.List;
4344

4445
import javax.ws.rs.ProcessingException;
4546
import javax.ws.rs.core.Request;
@@ -49,6 +50,7 @@
4950
import org.glassfish.jersey.server.internal.inject.ConfiguredValidator;
5051
import org.glassfish.jersey.server.internal.process.MappableException;
5152
import org.glassfish.jersey.server.model.Invocable;
53+
import org.glassfish.jersey.server.spi.ValidationInterceptor;
5254

5355
/**
5456
* A resource method dispatcher responsible for consuming a JAX-RS {@link Request request}

0 commit comments

Comments
 (0)