I have a SOAP message that I'll validate using commonValidationFeature in CXF 3.0.3. The validator works, but it's return a soap fault without meaningful error message.
I want to catch the ValidationException and convert it to a SOAP message with the error in a status error code. I'll describe the output message later.
Here the soap fault returned :
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Fault occurred while processing.</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
The validation error should be :
The field [xyz] is invalid, must be geater than 0. (something like that)
Here it the field is the XSD
<xs:element minOccurs="0" name="merchantId">
<xs:simpleType>
<xs:restriction base="xs:long">
<xs:minInclusive value="1" />
</xs:restriction>
</xs:simpleType>
</xs:element>
The java code generated @XmlElement(type = String.class) @XmlJavaTypeAdapter(Adapter2 .class) @DecimalMin("1") protected Long merchantId;
Here the validation used in my endpoint
<jaxws:features>
<ref bean="commonValidationFeature" />
</jaxws:features>
<bean id="commonValidationFeature" class="org.apache.cxf.validation.BeanValidationFeature"/>
I tried to create my interceptor to catch the error, but when I do that, I get a schema validation error for unmarshalling. A message similar to my previous question : CXF SOAP JAX-WS WSS4JInInterceptor change namespace and cause Unmarshalling error
Here my code (a test code) for my interceptor
public class ExceptionInterceptor extends AbstractSoapInterceptor {
ConstraintViolationExceptionMapper mapper = new ConstraintViolationExceptionMapper();
public ExceptionInterceptor() {
super(Phase.PRE_LOGICAL);
}
public void handleMessage(SoapMessage message) throws Fault {
Fault fault = (Fault) message.getContent(Exception.class);
Throwable ex = fault.getCause();
if (ex instanceof ConstraintViolationException) {
ConstraintViolationException e = (ConstraintViolationException) ex;
Response response = mapper.toResponse(e);
generateSoapFault(fault, e, response);
} else {
generateSoapFault(fault, null, null);
}
}
private void generateSoapFault(Fault fault, Exception e, Response response) {
fault.setFaultCode(createQName(response.getEntity().toString()));
}
private static QName createQName(String errorCode) {
return new QName("qname.com", errorCode);
}
}
What I would like to get as output, is something like that
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns6:newOutputResponse xmlns:ns6="http://demo.test" >
<asset>
...
</asset>
<status>
<type>ERROR</type>
<description>[merchantId] is invalid, must be geater than 0.</description>
</....
</ns6...>
....
the tags will be added only when there is a error. How can I do that ?