CXF log SOAP output - soap

I am having trouble logging an outgoing SOAP message from the server. The handleMessage method does not overwrite the message content as expected. How would I store the outgoing SOAP to the message?
public class OutgoingSoapInterceptor extends AbstractPhaseInterceptor<Message> {
private static final Logger logger = LoggerFactory.getLogger(OutgoingSoapInterceptor.class.getName());
public OutgoingSoapInterceptor ()
{
super(Phase.PRE_STREAM);
}
#Override
public void handleMessage(Message message) throws Fault {
logger.debug("outbound soap handleMessage");
OutputStream os = message.getContent ( OutputStream.class );
CacheAndWriteOutputStream cwos = new CacheAndWriteOutputStream ( os);
message.setContent ( OutputStream.class, cwos );
cwos.registerCallback ( new LoggingOutCallBack ( ) );
}
}

There is a simpler way to log the SOAP messages using CXF LoggingInInterceptor and LoggingOutInterceptor
LogUtils.setLoggerClass(org.apache.cxf.common.logging.Log4jLogger.class);
yourService = new YourService(wsdlURL, SERVICE_NAME);
port = yourService.getServicePort();
Client client = ClientProxy.getClient(port);
client.getInInterceptors().add(new LoggingInInterceptor());
client.getOutInterceptors().add(new LoggingOutInterceptor());
Or configuring interceptors in <cxf:bus> with spring
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<cxf:bus>
<cxf:features>
<cxf:logging />
</cxf:features>
</cxf:bus>
<jaxws:endpoint ... />
</beans>
See more examples in How to log Apache CXF Soap Request and Soap Response using Log4j

Related

How to handle soap:mustUnderstand header in Spring WS

An external system is sending my service a SOAP message and I have a listener in place
#Endpoint
public class NotificationListener {
private static final String NAMESPACE_URI = "http://test.com/test";
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "Notification")
#ResponsePayload
public void getSOAPMessage(#RequestPayload HistoryMessage request) {
// calls to methods in other classes which handle the business logic
}
The request body contains the following headers:
<soap:Header>
<wsa:To soap:mustUnderstand="1"
xmlns:wsa="http://www.w3.org/2005/08/addressing">{destination endpoint}
</wsa:To>
<wsa:From
xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
</wsa:From>
<wsa:ReplyTo
xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:FaultTo
xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
</wsa:FaultTo>
<wsa:Action soap:mustUnderstand="1"
xmlns:wsa="http://www.w3.org/2005/08/addressing">
</wsa:Action>
<wsa:MessageID
xmlns:wsa="http://www.w3.org/2005/08/addressing">urn:uuid:fa163e6e-ef55-1eec-b9ac-5e80af1d126a
</wsa:MessageID>
I get the following error on calling my endpoint:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:MustUnderstand</faultcode>
<faultstring xml:lang="en">One or more mandatory SOAP header blocks not understood</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I understand that the mustUnderstand attribute in the header is set to 1 which means true and that header must be handled. But how do I handle it?
I'm using Spring WS to build the listener service.
TIA:)

Apache camel cxfrs—Can't find the request for <URL> Observer

I tried to develop a rest service and expose the same via Apache Camel's CXFRS. I followed all the steps given in http://camel.apache.org/cxfrs.html and also referred to many samples given. I already referred to the question Can't find the the request for url Observer, but in my case it is a simple rest request. Below are the Service class, Route class, and cxf context used:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:cxf="http://camel.apache.org/schema/cxf" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<context:annotation-config />
<!-- enable Spring #Component scan -->
<context:component-scan base-package="org.camelsample.rest" />
<cxf:rsServer id="rsServer" address="/rest"
serviceClass="org.camelsample.rest.service.SampleRestService"
loggingFeatureEnabled="true" loggingSizeLimit="20">
<cxf:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
</cxf:providers>
</cxf:rsServer>
<camel:camelContext id="samplerestservice"
xmlns="http://camel.apache.org/schema/spring">
<contextScan />
<jmxAgent id="agent" createConnector="true" />
</camel:camelContext>
</beans>
The Service Class:
package org.camelsample.rest.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
public class SampleRestService {
#GET
#Path("/")
public String sampleService() {
return null;
}
}
The Route Class:
package org.camelsample.rest.route;
import org.apache.camel.spring.SpringRouteBuilder;
public class SampleRestRoute extends SpringRouteBuilder {
#Override
public void configure() throws Exception {
// TODO Auto-generated method stub
from("cxfrs:bean:rsServer").log("Into Sample Route").setBody(constant("Success"));
}
}
But when I try to hit and test using http://localhost:8080/rest, I always get the following error message:
2015-05-29 13:38:37.920 WARN 6744 --- [nio-8080-exec-2] o.a.c.t.servlet.ServletController : Can't find the the request for http://localhost:8080/favicon.ico's Observer
2015-05-29 13:38:40.295 WARN 6744 --- [nio-8080-exec-3] o.a.c.t.servlet.ServletController : Can't find the the request for http://localhost:8080/rest's Observer
Am using Spring boot to test the rest sample.
Does it work with this URL instead ?
http://localhost:8181/cxf/rest
If you just use address="/rest" as your address then you will probably get the default Jetty port 8181 and default CXF servlet path /cxf as the base URL.
If you specifically want to use the URL you have given then try this instead:
address="http://0.0.0.0:8080/rest"

Switchyard and Camel Transparent Proxy doesn't propagate SOAP header

I'm trying to create a transparent proxy with Switchyard.
The idea is having a "promoted" service exposed by Switchyard. Calls to this webservice will be redirected with Camel to a service reference. Service reference (proxified service) and promoted service have the same WSDL.
The problem is that the SOAP calls that the user does contains a custom token in the SOAP Envelope Header that it is NOT propagated. How can I solve that?
SOAP Call Example (User->Propagated Service):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.test/">
<soapenv:Header>
<token>foo</token>
</soapenv:Header>
<soapenv:Body>
<ser:readSomethings>
<something>
</something>
</ser:readSomethings>
</soapenv:Body>
</soapenv:Envelope>
SOAP Call that Switchyard does to the "proxified" service (It doesn't contain the token!!):
<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ser:readSomethings xmlns:ser=\"http://service.test/\">
<something></something>
</ser:readSomethings>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
switchyard.xml:
<?xml version="1.0" encoding="ASCII"?>
<switchyard xmlns="urn:switchyard-config:switchyard:1.0" xmlns:bean="urn:switchyard-component-bean:config:1.0" xmlns:camel="urn:switchyard-component-camel:config:1.0" xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912" xmlns:soap="urn:switchyard-component-soap:config:1.0" name="SomethingService" targetNamespace="urn:my.company.something:SomethingService:1.0">
<sca:composite name="SomethingService" targetNamespace="urn:my.company.something:SomethingService:1.0">
<sca:service name="PromotedProxyService" promote="ProxyService/ProxyService">
<soap:binding.soap>
<soap:contextMapper includes=".*"/>
<soap:wsdl>META-INF/SomethingWS.wsdl</soap:wsdl>
<soap:socketAddr>:${jettyPort}</soap:socketAddr>
<soap:contextPath>SomethingService</soap:contextPath>
</soap:binding.soap>
</sca:service>
<sca:reference name="ProxifiedService" multiplicity="0..1" promote="ProxyService/ProxifiedService">
<soap:binding.soap>
<soap:contextMapper includes=".*"/>
<soap:wsdl>META-INF/SomethingWS.wsdl</soap:wsdl>
<soap:endpointAddress>http://IP:8080/ws_admin/SomethingWS</soap:endpointAddress>
</soap:binding.soap>
</sca:reference>
<sca:component name="ProxyService">
<camel:implementation.camel>
<camel:java class="my.company.something.RouterCamel"/>
</camel:implementation.camel>
<sca:service name="ProxyService">
<sca:interface.wsdl interface="META-INF/SomethingWS.wsdl#wsdl.porttype(SomethingWS)"/>
</sca:service>
<sca:reference name="ProxifiedService">
<sca:interface.wsdl interface="META-INF/SomethingWS.wsdl#wsdl.porttype(SomethingWS)"/>
</sca:reference>
</sca:component>
</sca:composite>
<domain>
<properties>
<property name="org.switchyard.handlers.messageTrace.enabled" value="true"/>
</properties>
</domain>
</switchyard>
RouterCamel.java
public class RouterCamel extends org.apache.camel.builder.RouteBuilder{
public void configure() {
org.apache.camel.Processor myProc = new org.apache.camel.Processor(){
public void process(org.apache.camel.Exchange exchange) throws Exception {
System.out.println("------------------------------ ENTRO\n\n\n\n");
String body = exchange.getIn().getBody(String.class);
// change the message to say Hello
System.out.println("\n\n\n\n------------------------------ BODY: " + body);
exchange.getOut().setBody(body);
// copy headers from IN to OUT to propagate them
System.out.println("Header in values:");
for(Entry<String, Object> header: exchange.getIn().getHeaders().entrySet()){
System.out.println("Header: " + header.getKey() + " Value: " + header.getValue());
}
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
System.out.println("Header out values:");
for(Entry<String, Object> header: exchange.getOut().getHeaders().entrySet()){
System.out.println("Header: " + header.getKey() + " Value: " + header.getValue());
}
}
};
// Define routing rules here:
from("switchyard://ProxyService").process(myProc).to("switchyard://ProxifiedService");
}
}
The header was not propagated because it didn't have a namespace.
I found the answer here: https://developer.jboss.org/thread/243364
<sca:reference name="ProxyReference" multiplicity="0..1" promote="Proxy/ProxyService">
<sca:interface.wsdl interface="META-INF/wsdl/RegisterService.wsdl#wsdl.porttypePortType)"/>
<soap:binding.soap name="soap">
<soap:contextMapper class="WSHeaderContextMapper"/>
<soap:wsdl>META-INF/wsdl/RegisterService.wsdl</soap:wsdl>
<soap:endpointAddress>${service.address}</soap:endpointAddress>
<soap:timeout>12000</soap:timeout>
</soap:binding.soap>
</sca:reference>
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.message.WSSecUsernameToken;
import org.switchyard.Context;
import org.switchyard.component.soap.composer.SOAPBindingData;
import org.switchyard.component.soap.composer.SOAPContextMapper;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPFactory;
public class WSHeaderContextMapper extends SOAPContextMapper {
private String user = "user";
private String password = "pass";
#Override
public void mapTo(Context context, SOAPBindingData target) throws Exception {
SOAPFactory factory = SOAPFactory.newInstance();
SOAPElement element = factory.createElement("Security", "wsse", WSConstants.WSSE_NS);
WSSecUsernameToken utBuilder = createUsernameToken(this.user, password);
utBuilder.prepare(element.getOwnerDocument());
element.addChildElement(factory.createElement(utBuilder.getUsernameTokenElement()));
context.setProperty(element.getElementQName().toString(), element);
super.mapTo(context, target);
}
private WSSecUsernameToken createUsernameToken(String userName, String password) {
WSSecUsernameToken utBuilder = new WSSecUsernameToken();
utBuilder.setUserInfo(userName, password);
utBuilder.setPasswordType(WSConstants.PASSWORD_TEXT);
return utBuilder;
}
}

camel jpa waiting for namespace handlers

I'm trying to write a RouteTest class for my camel jpa example and it does not work as expected because of the following line :
Bundle RouteTest is waiting for namespace handlers [http://aries.apache.org/xmlns/jpa/v1.1.0]
Please find here blueprint.xml file
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
http://aries.apache.org/xmlns/jpa/v1.1.0 http://aries.apache.org/schemas/jpa/jpa_110.xsd">
<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
<jpa:unit unitname="persistence-pu" property="entityManagerFactory" />
</bean>
<camelContext trace="true" id="blueprintContext" xmlns="http://camel.apache.org/schema/blueprint">
<route id="persist">
<from uri="direct:persist"/>
<to uri="jpa:Person"/>
</route>
</camelContext>
</blueprint>
and here RouteTest class :
public class RouteTest extends CamelBlueprintTestSupport {
#Override
protected String getBlueprintDescriptor() {
return "/OSGI-INF/blueprint/blueprint.xml";
}
#Test
public void testRoute() throws Exception {
getMockEndpoint("mock:result").expectedMinimumMessageCount(1);
ProducerTemplate producerTemplate = new DefaultCamelContext().createProducerTemplate();
Person person = new Person();
person.setName("Bob");
producerTemplate.sendBody("direct:persist", person);
// assert expectations
assertMockEndpointsSatisfied();
}
}
you need to provide Aries Blueprint, and especially you need to provide the aries JPA dependencies. How do you Test your Routes? I'd suggest using Pax-Exam or probably better to use Pax-Exam-Karaf and intall the aries jpa features.

MULE ESB : Binding multiple Web Services to one Client

I have a client which connects to a Web service to get some information. I have a requirement where I have to send the same information to multiple services using different ports. To solve this without modifying the client code I found MULE ESB, which is supposed to do exactly what I need.
I've found a guide where I could connect one client to one service using MULE ESB and one port, but I cant find a way to chain the services so they all listen to one port but have different themselves.
This is how it's supposed to look like:
UPDATE :
here is my current Mule Applications config :
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="CE-3.2.1" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
<flow name="flows1Flow1" doc:name="flows1Flow1">
<http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:4433/miniwebservice" mimeType="text/xml" doc:name="HTTP"/>
<http:outbound-endpoint exchange-pattern="request-response" address="http://localhost:4434/miniwebservice?wsdl" mimeType="text/xml" doc:name="HTTP"/>
</flow>
</mule>
Here is the WebService :
Client :
package miniwebservice;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class TestWsClient
{
public static void main( final String[] args ) throws Throwable
{
String url = ( args.length > 0 ) ? args[0] : "http://localhost:4434/miniwebservice";
Service service = Service.create(
new URL( url + "?wsdl" ),
new QName( "http://miniwebservice/", "HalloWeltImplService" ) );
HalloWelt halloWelt = service.getPort( HalloWelt.class );
System.out.println( "\n" + halloWelt.hallo( args.length > 1 ? args[1] : "" ) );
}
}
Server :
package miniwebservice;
import javax.xml.ws.Endpoint;
public class TestWsServer
{
public static void main( final String[] args )
{
String url = ( args.length > 0 ) ? args[0] : "http://localhost:4434/miniwebservice";
Endpoint.publish( url, new HalloWeltImpl() );
}
}
InterfaceImpl :
package miniwebservice;
import javax.jws.WebService;
#WebService( endpointInterface="miniwebservice.HalloWelt" )
public class HalloWeltImpl implements HalloWelt
{
public String hallo( String wer )
{
return "Hallo " + wer;
}
}
interface :
package miniwebservice;
import javax.jws.*;
#WebService
public interface HalloWelt
{
public String hallo( #WebParam( name = "wer" ) String wer );
}
If I start the Server and the Mule aplication and try to reach http://localhost:4434/miniwebservice?wsdl ower http://localhost:4433/miniwebservice I get the folowing Exception in my Browser (FireFox 8.0) :
Couldn't create SOAP message due to exception: XML reader error: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
I just started to work with Mule so I thouth that this would be enouth to get redirect mule to the Service to get the wsdl but its seems like its a bit complicatet.
Disclaimers:
This is not the final solution to the whole problem, which includes dispatching to several services and aggregating results, but a step in the right direction.
This is not representative of how web service proxying is done in Mule (which is way simpler) but a barebone approach to HTTP request routing so aggregation can be added.
Since you want to forward HTTP GET requests to the ?wsdl processor and HTTP POST SOAP request to the web service, you need to handle the target HTTP method and request URI propagation yourself:
<flow name="flows1Flow1">
<http:inbound-endpoint exchange-pattern="request-response"
address="http://localhost:4433/miniwebservice" />
<message-properties-transformer scope="outbound">
<add-message-property key="http.method" value="#[header:INBOUND:http.method]" />
</message-properties-transformer>
<logger level="INFO" category="ddo" />
<http:outbound-endpoint exchange-pattern="request-response"
address="http://localhost:4434#[header:INBOUND:http.request]" />
</flow>
(tested and validated with TestWsClient and TestWsServer)