| 1 |
/** |
| 2 |
* Copyright (c) 2012-2017, jcabi.com |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: 1) Redistributions of source code must retain the above |
| 8 |
* copyright notice, this list of conditions and the following |
| 9 |
* disclaimer. 2) Redistributions in binary form must reproduce the above |
| 10 |
* copyright notice, this list of conditions and the following |
| 11 |
* disclaimer in the documentation and/or other materials provided |
| 12 |
* with the distribution. 3) Neither the name of the jcabi.com nor |
| 13 |
* the names of its contributors may be used to endorse or promote |
| 14 |
* products derived from this software without specific prior written |
| 15 |
* permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 19 |
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 21 |
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 22 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 28 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
package com.jcabi.aspects.aj; |
| 31 |
|
| 32 |
import java.lang.reflect.Constructor; |
| 33 |
import java.lang.reflect.Method; |
| 34 |
import java.util.Collection; |
| 35 |
import java.util.Set; |
| 36 |
import javax.validation.ConstraintViolation; |
| 37 |
import javax.validation.ConstraintViolationException; |
| 38 |
import javax.validation.Validation; |
| 39 |
import javax.validation.Validator; |
| 40 |
import org.aspectj.lang.JoinPoint; |
| 41 |
import org.aspectj.lang.annotation.AfterReturning; |
| 42 |
import org.aspectj.lang.annotation.Aspect; |
| 43 |
import org.aspectj.lang.annotation.Before; |
| 44 |
import org.aspectj.lang.reflect.ConstructorSignature; |
| 45 |
import org.aspectj.lang.reflect.MethodSignature; |
| 46 |
|
| 47 |
/** |
| 48 |
* Validates method calls. |
| 49 |
* |
| 50 |
* <p>We do this manual processing of {@code javax.validation.constraints.*} |
| 51 |
* annotations only because |
| 52 |
* JSR-303 in its current version doesn't support method level validation |
| 53 |
* (see its Appendix C). At the moment we don't support anything expect these |
| 54 |
* two annotations. We think that it's better to wait for JSR-303. |
| 55 |
* |
| 56 |
* <p>The class is thread-safe. |
| 57 |
* |
| 58 |
* @author Yegor Bugayenko ([email protected]) |
| 59 |
* @version $Id: 9ad797469151a785e3924ccbeacf650711a96798 $ |
| 60 |
* @since 0.1.10 |
| 61 |
* @see <a href="http://beanvalidation.org/1.0/spec/#appendix-methodlevelvalidation">Appendix C</a> |
| 62 |
* @see <a href="http://aspects.jcabi.com/jsr-303.html">How it works</a> |
| 63 |
*/ |
| 64 |
@Aspect |
| 65 |
@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.TooManyMethods" }) |
| 66 |
public final class MethodValidator { |
| 67 |
|
| 68 |
/** |
| 69 |
* JSR-303 Validator. |
| 70 |
*/ |
| 71 |
private final transient Validator validator = Validation |
| 72 |
.buildDefaultValidatorFactory() |
| 73 |
.getValidator(); |
| 74 |
|
| 75 |
/** |
| 76 |
* Validate arguments of a method. |
| 77 |
* |
| 78 |
* <p>Try NOT to change the signature of this method, in order to keep |
| 79 |
* it backward compatible. |
| 80 |
* |
| 81 |
* @param point Join point |
| 82 |
* @checkstyle LineLength (3 lines) |
| 83 |
*/ |
| 84 |
@Before("execution(* *(.., @(javax.validation.* || javax.validation.constraints.*) (*), ..))") |
| 85 |
public void beforeMethod(final JoinPoint point) { |
| 86 |
if (this.validator != null) { |
| 87 |
this.validateMethod( |
| 88 |
point.getThis(), |
| 89 |
MethodSignature.class.cast(point.getSignature()).getMethod(), |
| 90 |
point.getArgs() |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Validate arguments of constructor. |
| 97 |
* |
| 98 |
* <p>Try NOT to change the signature of this method, in order to keep |
| 99 |
* it backward compatible. |
| 100 |
* |
| 101 |
* @param point Join point |
| 102 |
* @checkstyle LineLength (3 lines) |
| 103 |
*/ |
| 104 |
@Before("preinitialization(*.new(.., @(javax.validation.* || javax.validation.constraints.*) (*), ..))") |
| 105 |
public void beforeCtor(final JoinPoint point) { |
| 106 |
if (this.validator != null) { |
| 107 |
@SuppressWarnings("unchecked") |
| 108 |
final Constructor<Object> constructor = (Constructor<Object>) |
| 109 |
ConstructorSignature.class.cast(point.getSignature()) |
| 110 |
.getConstructor(); |
| 111 |
this.validateConstructor( |
| 112 |
constructor, |
| 113 |
point.getArgs() |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Validate method response. |
| 120 |
* |
| 121 |
* <p>Try NOT to change the signature of this method, in order to keep |
| 122 |
* it backward compatible. |
| 123 |
* |
| 124 |
* @param point Join point |
| 125 |
* @param result Result of the method |
| 126 |
* @since 0.7.11 |
| 127 |
*/ |
| 128 |
@AfterReturning |
| 129 |
( |
| 130 |
// @checkstyle LineLength (1 line) |
| 131 |
pointcut = "execution(@(javax.validation.* || javax.validation.constraints.*) * *(..))", |
| 132 |
returning = "result" |
| 133 |
) |
| 134 |
public void after(final JoinPoint point, final Object result) { |
| 135 |
this.checkForViolations( |
| 136 |
this.validator |
| 137 |
.forExecutables() |
| 138 |
.validateReturnValue( |
| 139 |
point.getThis(), |
| 140 |
new ImprovedJoinPoint(point).currentMethod(), |
| 141 |
result |
| 142 |
) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Validates method parameters. |
| 148 |
* @param object Object at pointcut |
| 149 |
* @param method Method at pointcut |
| 150 |
* @param args Parameters of the method |
| 151 |
*/ |
| 152 |
private void validateMethod(final Object object, final Method method, |
| 153 |
final Object... args) { |
| 154 |
this.checkForViolations(this.validator |
| 155 |
.forExecutables() |
| 156 |
.validateParameters( |
| 157 |
object, |
| 158 |
method, |
| 159 |
args |
| 160 |
) |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Validates constructor parameters. |
| 166 |
* @param ctr Constructor at pointcut |
| 167 |
* @param args Parameters of the method |
| 168 |
*/ |
| 169 |
private void validateConstructor(final Constructor<Object> ctr, |
| 170 |
final Object... args) { |
| 171 |
this.checkForViolations( |
| 172 |
this.validator |
| 173 |
.forExecutables() |
| 174 |
.validateConstructorParameters( |
| 175 |
ctr, |
| 176 |
args |
| 177 |
) |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Checks if violations set is empty and throws a |
| 183 |
* {@link ConstraintViolationException} if it isn't. |
| 184 |
* @param violations JSR303 violations. |
| 185 |
*/ |
| 186 |
private void checkForViolations( |
| 187 |
final Set<ConstraintViolation<Object>> violations) { |
| 188 |
if (!violations.isEmpty()) { |
| 189 |
throw new ConstraintViolationException( |
| 190 |
MethodValidator.pack(violations), |
| 191 |
violations |
| 192 |
); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Pack violations into string. |
| 198 |
* @param errs All violations |
| 199 |
* @return The full text |
| 200 |
*/ |
| 201 |
private static String pack( |
| 202 |
final Collection<ConstraintViolation<Object>> errs) { |
| 203 |
final StringBuilder text = new StringBuilder(0); |
| 204 |
for (final ConstraintViolation<?> violation : errs) { |
| 205 |
if (text.length() > 0) { |
| 206 |
text.append("; "); |
| 207 |
} |
| 208 |
text.append(violation.getMessage()); |
| 209 |
} |
| 210 |
return text.toString(); |
| 211 |
} |
| 212 |
} |