We currently have a problem with CXF 2.7.3 on Jboss EAP 6.2 with a custom SoapFault exception.
The Subcode and its value is not displayed when we send a custom SoapFault:
Here is what we want from cxf:
<?xml version="1.0" encoding="UTF-8"?>
<tns:Fault
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://www.w3.org/2003/05/soap-envelope">
<tns:Code>
<tns:Value>tns:Sender</tns:Value>
<tns:Subcode>
<tns:Value>value</tns:Value>
</tns:Subcode>
</tns:Code>
<tns:Reason>
<tns:Text xml:lang="fr">
****
</tns:Text>
</tns:Reason>
<tns:Detail>
**Custom fault***
</tns:Detail>
</tns:Fault>
Here is what we have so far:
<?xml version="1.0" encoding="UTF-8"?>
<tns:Fault
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://www.w3.org/2003/05/soap-envelope">
<tns:Code>
<tns:Value>tns:Sender</tns:Value>
</tns:Code>
<tns:Reason>
<tns:Text xml:lang="fr">
****
</tns:Text>
</tns:Reason>
<tns:Detail>
**Custom fault***
</tns:Detail>
</tns:Fault>
The subcode is completely missing.
We tried to use a custom interceptor (extending from LoggingOutInterceptor or AbstractInterceptor) from CXF like this to intercept the custom fault:
public class SoapRequestInterceptor extends LoggingOutInterceptor {
private static Logger log = Logger.getLogger(SoapRequestInterceptor.class);
public SoapRequestInterceptor() {
super(Phase.MARSHAL);
}
public void handleMessage(SoapMessage message) throws Fault{
SoapMessage soapMessage = message.getContent(SoapMessage.class);
if (soapMessage != null) {
log.info( "request intercepted:" + soapMessage.toString());
}
}
}
The interceptor is not even called when we add him either to CXF bus or to jaxws interceptor (it’s added at the start of the application though since it gets through the constructor).
How can we intercept a custom soap Fault message and edit it in CXF?
Thanks a lot!
As asked here's the way we declare the interceptor in spring applicationContext.xml :
<cxf:bus>
<cxf:outFaultInterceptors>
<ref bean="soapRequestInterceptor" />
</cxf:outFaultInterceptors>
</cxf:bus>
<bean id="soapRequestInterceptor" class="fr.test.SoapRequestInterceptor" />
<jaxws:server serviceClass="fr.test.PriseEnChargeB2ServiceSP"
address="" serviceBean="#service">
<jaxws:binding>
<soap:soapBinding version="1.2" mtomEnabled="true" />
</jaxws:binding>
</jaxws:server>
Note : the interceptor is well instancied, but not called after a soap fault throw from our WS
The exception thrown at the end of our WS is this one :
public class PecSoapFaultException extends SoapFault {
private static final long serialVersionUID = 1L;
public TypeErreur erreur;
public PecSoapFaultException(String message, TypeErreur structure) {
super(message, new QName(""));
this.erreur = structure;
}
public PecSoapFaultException(String message, TypeErreur structure, QName faultcode) {
super(message, faultcode);
this.erreur = structure;
}
public PecSoapFaultException(String message, TypeErreur structure, QName faultcode,
QName subcode) {
super(message, faultcode);
this.setSubCode(subcode);
this.erreur = structure;
}
public TypeErreur getFaultInfo() {
return erreur;
}
The problem that your interceptor is not called is that you do not override the correct method. You should have your code like this:
#Override
public void handleMessage(Message message) throws Fault {
SoapMessage soapMessage = message.getContent(SoapMessage.class);
if (soapMessage != null) {
log.info( "request intercepted:" + soapMessage.toString());
}
}
Then your interceptor will be called. But as I said in my comment: in case of a fault the soapmessage is null. So you won't get any output in your log.
Related
I have task where I need to transform custom SOAP message. At the beginning I have to get this custom SOAP message, then I need to transform this message using XSLT, then process this message in my WebService. The reverse process is repeated.
I have XSLT file and method for transforming SOAP message, but I don't know where I need to call this method for transforming. How do I intercept SOAP message and where I have to do it? Because I have only class with one method (example below) and i don't understand how I can transform this message before processing in webservice.
#WebService
public class Calculator {
public String showCard(final CreditCard creditCard) {
return creditCard.toString();
}
}
Here is the technical mapping what I need to do.
How do I intercept SOAP message and where I have to do it?
You need a SoapHandler to capture the soap request before the execution of the bussiness logic of the endpoint, and other SoapHandler to transform the outbound response.
Define a SoapHandler class (example extracted from mkyong)
public class CalculatorSoapHandler implements SOAPHandler<SOAPMessageContext>{
#Override
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
//for response message only, true for outbound messages, false for inbound
if(!isRequest){
try{
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
//process....
}catch(SOAPException e){
System.err.println(e);
}catch(IOException e){
System.err.println(e);
}
}
//continue other handler chain
return true;
}
#Override
public boolean handleFault(SOAPMessageContext context) {
}
#Override
public void close(MessageContext context) {
}
#Override
public Set<QName> getHeaders() {
}
}
Create a soap handler XML file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<javaee:handler-chain>
<javaee:handler>
<javaee:handler-class>com.CalculatorSoapHandler</javaee:handler-class>
</javaee:handler>
</javaee:handler-chain>
</javaee:handler-chains>
Attach SOAP Handler to Web Service using #HandlerChain
#WebService
#HandlerChain(file="handler-chain.xml")
public class Calculator {
If you use a jax-ws framework like CXF or spring-WS, check the documentation to see specific configuration
I'm trying to build an endpoint that will receive SOAP messages from a client. The message I'm receiving contains a username and password inside the soap header ...
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://www.company.com/Application">
<soapenv:Header xmlns:wsse="http://__________.xsd">
<wsse:Security >
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
I'm using Spring WS - the obvious solution is to create a filter inside web.xml that will bypass Spring WS completely, parse the SOAP message, extract the username and password and then continue to Spring WS which will parse the SOAP again.
Is there a way to get the content of the header without circumventing Spring WS?
I've tried adding a bean inside sws:interceptors:
<sws:interceptors>
<!-- extract Security details from Header -->
<bean class="com.company.application.service.SecurityInterceptorService" />
<!-- log full Body of request -->
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
<!-- validate Request against XSD to make sure it's a valid request -->
<bean id="CompanyApplication" class="com.company.application.interceptor.ValidatingInterceptor">
<property name="schema" value="/WEB-INF/_______________.xsd" />
<property name="validateRequest" value="true" />
<property name="validateResponse" value="true" />
</bean>
</sws:interceptors>
and then implementing that class:
public class SecurityInterceptorService implements EndpointInterceptor {
#Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
System.out.println("---------------");
System.out.println("handleRequest") ;
System.out.println("---------------");
return true;
}
#Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
System.out.println("---------------");
System.out.println("handleResponse");
System.out.println("---------------");
return true;
}
#Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
System.out.println("---------------");
System.out.println("handleFault");
System.out.println("---------------");
return true;
}
#Override
public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {
System.out.println("---------------");
System.out.println("afterCompletion");
System.out.println("---------------");
}
}
endpoint only contains data about the endpoint inside handleRequest and after traversing through many layers and layers inside messageContext while in debug mode, I can't seem to spot the content of the header.
Is the content I'm looking for inside messageContext and if so, how do I access it?
From the messageContext object, you can retrieve either the request or the response (In your case, I guess you need the request).
The request/response is basically a WebServiceMessage. If you examine the webServiceMessage, you will see that the object can be casted to a SoapMessage. From the soap message, you can now get the soap header.
WebServiceMessage webServiceMessageRequest = messageContext_.getRequest();
SoapMessage soapMessage = (SoapMessage) webServiceMessageRequest;
SoapHeader soapHeader = soapMessage.getSoapHeader()
Afterwards, You might want to get the source object and convert it to a DOMSource object and then get the Node object which make the information retrieval much easier.
Source bodySource = soapHeader .getSource();
DOMSource bodyDomSource = (DOMSource) bodySource;
Node bodyNode = _bodyDomSource.getNode();
If you are using spring-boot you can use this kind of configuration:
#EnableWs
#Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
#Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingInterceptor();
validatingInterceptor.setValidateRequest(true);
validatingInterceptor.setValidateResponse(true);
validatingInterceptor.setXsdSchema(resourceSchema());
interceptors.add(validatingInterceptor);
}
#Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/api/*");
}
#Bean(name = "registros")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("ResourcePort");
wsdl11Definition.setLocationUri("/api");
wsdl11Definition.setTargetNamespace("http://resource.com/schema");
wsdl11Definition.setSchema(resourceSchema());
return wsdl11Definition;
}
#Bean
public XsdSchema resourceSchema() {
return new SimpleXsdSchema(new ClassPathResource("registro.xsd"));
}
}
In this example the addInterceptors method is the important one, the others 3 are basic to expose a WSDL API.
Maybe it'll be useful for someone else.
There is no easy way to unmarshall Soap headers with Spring-ws (it's currently not supported)
However, you can access the SoapHeaderElement in your #PayloadRoot annotated method, and do the process of unmarshalling with JAXB.
#Endpoint
public class SubmitEndpoint implements EndpointInterface {
private static final String NAMESPACE_URI = "http://www.example.com/namespace";
private Security unmarshallSecurityFromSoapHeader(SoapHeaderElement header) {
Security security = null;
try {
JAXBContext context = JAXBContext.newInstance(Security.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
security = (Security) unmarshaller.unmarshal(header.getSource());
} catch (JAXBException e) {
e.printStackTrace();
}
return security;
}
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "submit")
#ResponsePayload
public SubmitResponse submit(#RequestPayload Submit submit, #SoapHeader(
value = "{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Security") SoapHeaderElement wsseSecurityHeader) throws JAXBException {
Security security = unmarshallSecurityFromSoapHeader(wsseSecurityHeader);
}
}
Security.java
#Getter
#Setter
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(namespace = Security.SECURITY_NS, name = "Security")
public class Security {
public static final String SECURITY_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
#XmlElement(namespace = SECURITY_NS, name = "UsernameToken")
private UsernameToken usernameToken;
}
UsernameToken.java
#Getter
#Setter
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(namespace = Security.SECURITY_NS, name = "UsernameToken")
public class UsernameToken {
#XmlElement(name = "Username", namespace = Security.SECURITY_NS)
private String username;
#XmlElement(name = "Password", namespace = Security.SECURITY_NS)
private String password;
}
Our system consumes SOAP Web Service, using JAX-WS client stubs generated based on service's WSDL. In case of error server returns SOAP faults like this:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<s:Fault>
<faultcode>SomeErrorCode</faultcode>
<faultstring xml:lang="en-US">Some error message</faultstring>
<detail>
<ApiFault xmlns="http://somenamespace.com/v1.0" xmlns:a="http://somenamespace.com/v1.0" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:RequestId>123456789</a:RequestId>
<a:CanRetry>true</a:CanRetry>
</ApiFault>
</detail>
</s:Fault>
</s:Body>
Based on WSDL SomeCustomFault exception class is generated and all service methods are declared to throw this (see below) exception.
#WebFault(name = "ApiFault", targetNamespace = "http://services.altasoft.ge/orders/v1.0")
public class SomeCustomFault
extends Exception
{
private ApiFault faultInfo;
public SomeCustomFault(String message, ApiFault faultInfo) {
super(message);
this.faultInfo = faultInfo;
}
public SomeCustomFault(String message, ApiFault faultInfo, Throwable cause) {
super(message, cause);
this.faultInfo = faultInfo;
}
public ApiFault getFaultInfo() {
return faultInfo;
}
}
As you can see this custom fault exception extends Exception and not SOAPFaultException. Hovewer I need to get SOAP fault's faultcode which could be retrieved only from SOAPFaultException using getFaultCode method. Could you tell me how can I reach SOAPFaultException or SOAP fault's faultcode in place where I catch above mentioned custom fault exception?
You could implement a JAX-WS handler and add it to your client web service reference. This will be given opportunity to handle the request message and response message OR notified of a fault.
Create a SOAPHandler<SOAPMessageContext> and your handleFault() method will be passed the SOAPMessageContext. From that you can getMessage().getSOAPBody().getFault() to get the SOAPFault, which contains getFaultCode() and getDetail().
Assign your new fault handler to your web service ref. One way is via #HandlerChain. It will be invoked prior to your catch clause.
I used the Netbeans Web Service wizard to generate Java code given a WSDL. If I drag the web service method into a class, then it creates some Java code to call that web service (ex: SubmitApplication). I can see how to populate objects to send info to that web service, but the service also requires a security header with username/password.
There is a generated class called SecurityHeader that contains the username/password attributes. I can create this object with a valid username/password, but I cannot see how to pass that object or add it to the SubmitApplication call. How can the SecurityHeader be added to the SubmitApplication call?
Here is an example of what the SOAP request should look like:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<SecurityHeader xmlns="http://schemas.turss.com/BDS/1.0/">
<CreateTime>6/8/2012 8:32:59 PM</CreateTime>
<Owner>Sample_Owner</Owner>
<HashKey>Sample_Hash_Key</HashKey>
</SecurityHeader>
</soap:Header>
<soap:Body>
<SubmitApplication xmlns="http://schemas.turss.com/BDS/1.0/">
<newSearch>
<CurrentApplicant xmlns="http://schemas.turss.com/BDS/1.0/proxy">
<FirstName>Bob</FirstName>
<MiddleName />
<LastName>Smith</LastName>
<Suffix />
<BirthDate>1970-10-20T00:00:00</BirthDate>
<SSN />
<Address />
<City />
<State />
<PostalCode />
</CurrentApplicant>
<PermissiblePurpose xmlns="http://schemas.turss.com/BDS/1.0/proxy">TenantScreening</PermissiblePurpose>
</newSearch>
</SubmitApplication>
</soap:Body>
</soap:Envelope>
I found a sample code that add a token string to a soap security header. Here is the header form of the code below :
<TicketHeader>
<Ticket>OD01096347CCA</Ticket>
</TicketHeader>
The method to add the header to the message :
// Security token
String token;
// MyService and MySoapService are stubs generated from WSDL
MyService service = new MyService();
MyServiceSoap ep = service.getMyServiceSoap();
Binding binding = ((BindingProvider) ep).getBinding();
List handlers = binding.getHandlerChain();
handlers.add(new MySOAPHandler(token));
binding.setHandlerChain(handlers);
code of MySoapHandler :
public class MySOAPHandler implements SOAPHandler {
private String token;
public DHSOAPHandler(String token) {
this.token = token;
}
public boolean handleMessage(SOAPMessageContext messageContext) {
SOAPMessage
msg = messageContext.getMessage();
if ((Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)){
try {
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
SOAPElement el = header.addHeaderElement(envelope.createName("TicketHeader",
"", "http://ws.service.com/"));
el = el.addChildElement(envelope.createName("Ticket", "", "http://ws.service.com/"));
el.setValue(token);
msg.saveChanges();
}
catch (SOAPException e) {
return false;
}
}
return true;
}
public boolean handleFault(SOAPMessageContext messageContext) {
return true;
}
public void close(MessageContext messageContext){
}
// I'm not quite sure about what should this function do, but I guess something like this...
public Set getHeaders(){
Set headers = new HashSet();
headers.add(new QName("https://ws.service.com/", "TicketHeader"));
return headers;
}
}
I am using Apache CXF for my webservices. I've created an instance of AbstractSoapInterceptor. In its public void handleMessage(SoapMessage message) throws Fault method I would like to print the XML content of the intercepted message to the console. How can I achieve that?
Check this out and search for INBOUND INTERCEPTOR. Will place it here for reference...
public class InterceptorMensajeSOAPIn extends AbstractSoapInterceptor {
private static Logger log =
Logger.getLogger(InterceptorMensajeSOAPIn.class);
private SAAJInInterceptor saajIn = new SAAJInInterceptor();
public InterceptorMensajeSOAPIn(){
super(Phase.PRE_PROTOCOL);
getAfter().add(SAAJInInterceptor.class.getName());
}
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage soapMessage = getSOAPMessage(message);
try {
soapMessage.writeTo(System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
private SOAPMessage getSOAPMessage(SoapMessage smsg){
SOAPMessage soapMessage = smsg.getContent(SOAPMessage.class);
if (soapMessage == null) {
saajIn.handleMessage(smsg);
soapMessage = smsg.getContent(SOAPMessage.class);
}
return soapMessage;
}
}
Any reason you cannot just use the LoggingInInterceptor that is shipped with CXF? You could just grab the code for that and use that as a basis, but in 2.3, the LoggingInInterceptor was enhanced to allow specifying a printstream and such to use, so it might "just work".
You can also use a feature for this: org.apache.cxf.feature.LoggingFeature:
<jaxws:endpoint ...>
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature"/>
</jaxws:features>
</jaxws:endpoint>