When Running the validation from within the REST Application, the Validation is not being executed trustworthy, sometimes resulting in positive validation however the given parameters should result in ConstraintViolation. REST Request was triggered by SOAP-UI and/or Postman. Request has been successfully parsed and validation of input Parameters has been triggered. This is how we trigger validation:
public final class ValidationHelper {
private static MethodValidator validator = null;
public static void validateInput(MessageContext context) {
org.apache.wink.server.internal.handlers.SearchResult searchResult = loadFromContext(context);
Method javaMethod = getMethodProxyFromContext(searchResult);
Object[] parameters = getInvocationParameters(searchResult);
Object instance = getMethodInstanceFromContext(context, searchResult);
Set<MethodConstraintViolation<Object>> violations =
getValidatorInstance().validateAllParameters(instance, javaMethod, parameters);
if (!violations.isEmpty()) {
throw new RestInputParameterValidationException(violations);
}
}
private static MethodValidator getValidatorInstance() {
synchronized (ValidationHelper.class) {
if (validator == null) {
MethodValidator validatorTmp = Validation.byProvider(HibernateValidator.class).configure()
.buildValidatorFactory().getValidator().unwrap(MethodValidator.class);
validator = validatorTmp;
}
}
return validator;
}
}
Previosly Validatoor instance was loaded on each execution like:
public final class ValidationHelper {
public static void validateInput(MessageContext context) {
org.apache.wink.server.internal.handlers.SearchResult searchResult = loadFromContext(context);
Method javaMethod = getMethodProxyFromContext(searchResult);
Object[] parameters = getInvocationParameters(searchResult);
Object instance = getMethodInstanceFromContext(context, searchResult);
MethodValidator validatorTmp = Validation.byProvider(HibernateValidator.class).configure()
.buildValidatorFactory().getValidator().unwrap(MethodValidator.class);
Set<MethodConstraintViolation<Object>> violations =
validatorTmp.validateAllParameters(instance, javaMethod, parameters);
if (!violations.isEmpty()) {
throw new RestInputParameterValidationException(violations);
}
}
}
The static Validator has been implemented to make the results more stable. However if now the validator fails to load constraints it stays so until application restart... After some debugging with validator loaded on demand and the same request being validated I have found out following:
- Method Instance given for Validation and Proxy-Bean Instance is same.
- Parameter instances changes, but always contain same value.
- Sometimes ParameterMetaData does not contain constraints However Method Instance as mentioned does not change and contains all required.
I was not able to reproduce it in jUnit on same JVM. Not sure it it is somehow connected with running on WebSphere. WebSphere Version Log
************ Start Display Current Environment ************
WebSphere Platform 8.0.0.9 [BASE 8.0.0.9 cf091421.02] running with process name iap\iapnode0\server and process id 15048
Host Operating System is Windows 7, version 6.1
Java version = 1.6.0, Java Compiler = j9jit26, Java VM name = IBM J9 VM
was.install.root = c:\opt\IBM\WebSphere\AppServer
user.install.root = c:\opt\IBM\WebSphere\Profiles\base
Java Home = c:\opt\IBM\WebSphere\AppServer\java\jre
ws.ext.dirs = c:\opt\IBM\WebSphere\AppServer/java/lib;c:\opt\IBM\WebSphere\Profiles\base/classes;c:\opt\IBM\WebSphere\AppServer/classes;c:\opt\IBM\WebSphere\AppServer/lib;c:\opt\IBM\WebSphere\AppServer/installedChannels;c:\opt\IBM\WebSphere\AppServer/lib/ext;c:\opt\IBM\WebSphere\AppServer/web/help;c:\opt\IBM\WebSphere\AppServer/deploytool/itp/plugins/com.ibm.etools.ejbdeploy/runtime
Classpath = c:\opt\IBM\WebSphere\Profiles\base/properties;c:\opt\IBM\WebSphere\AppServer/properties;c:\opt\IBM\WebSphere\AppServer/lib/startup.jar;c:\opt\IBM\WebSphere\AppServer/lib/bootstrap.jar;c:\opt\IBM\WebSphere\AppServer/lib/jsf-nls.jar;c:\opt\IBM\WebSphere\AppServer/lib/lmproxy.jar;c:\opt\IBM\WebSphere\AppServer/lib/urlprotocols.jar;c:\opt\IBM\WebSphere\AppServer/deploytool/itp/batchboot.jar;c:\opt\IBM\WebSphere\AppServer/deploytool/itp/batch2.jar;c:\opt\IBM\WebSphere\AppServer/java/lib/tools.jar
Java Library path = c:\opt\IBM\WebSphere\AppServer/lib/native/win/x86_32/;C:\opt\IBM\WebSphere\AppServer\java\jre\bin\default;C:\opt\IBM\WebSphere\AppServer\java\jre\bin;C:\WINDOWS\SysWOW64;C:\WINDOWS;C:\opt\IBM\WebSphere\AppServer\lib\native\win\x86_32;C:\opt\IBM\WebSphere\AppServer\bin;C:\opt\IBM\WebSphere\AppServer\java\bin;C:\opt\IBM\WebSphere\AppServer\java\jre\bin;C:\app\software\python\Scripts;\bin;\usr\bin;\cmd;c:\app\tools\apache-maven-3.0.4\bin;c:\app\tools\apache-ant-1.9.6\bin;C:\app\software\python\Scripts;C:\app\software\Git\usr\bin;C:\app\software\Git\bin;C:\app\software\python\Scripts;C:\app\software\Git\usr\bin;C:\app\software\Git\bin;C:\ProgramData\Oracle\Java\javapath;C:\app\software\python\Scripts\;C:\app\software\python\;C:\Program Files (x86)\Apache-ANT-1.9.4\bin;c:\app\tools\apache-maven-3.0.4\bin;c:\program files (x86)\intel\icls client\;c:\program files\intel\icls client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\wbem;C:\WINDOWS\system32\windowspowershell\v1.0\;c:\program files\intel\intel(r) management engine components\dal;c:\program files\intel\intel(r) management engine components\ipt;c:\program files (x86)\intel\intel(r) management engine components\dal;c:\program files (x86)\intel\intel(r) management engine components\ipt;c:\program files\tortoisesvn\bin;c:\program files (x86)\git\cmd;c:\program files\ibm\gsk8\lib64;c:\program files (x8;c:\app\tools\apache-ant-1.9.6\bin\;c:\program files (x86)\ibm\gs;.;
Orb Version = IBM Java ORB build orb626ifx-20140404.00 (IX90144)
************* End Display Current Environment *************
In current situation the validator is no use at it can't guarantee the results Please let me know, if I can provide any further details. |