Invoke Secured Webservice with WS Policy using Camel-CXF - web-services

We are trying to invoke Secured Webservice using Camel CXf component. WSDL has wsp:Policy defined in it and I am able to successfully invoke service from soapUI by passing username/password but facing lot of problems if i try to invoke service using Camel-Cxf component or invoke using generated stub. Any help is appreciated.
WSDL has following policy defined in it.
<wsp:Policy wsu:Id="WSHttpBinding_ReferenceLookupLists_policy">
<wsp:ExactlyOne>
<wsp:All>
<http:BasicAuthentication
xmlns:http="http://schemas.microsoft.com/ws/06/2004/policy/http"/>
<sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:TransportToken>
<wsp:Policy>
<sp:HttpsToken RequireClientCertificate="false"/>
</wsp:Policy>
</sp:TransportToken>
<sp:AlgorithmSuite>
<wsp:Policy>
<sp:Basic256/>
</wsp:Policy>
</sp:AlgorithmSuite>
<sp:Layout>
<wsp:Policy>
<sp:Strict/>
</wsp:Policy>
</sp:Layout>
</wsp:Policy>
</sp:TransportBinding>
<wsaw:UsingAddressing/>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
Cxf endpoint in blueprint file:
<camel-cxf:cxfEndpoint id="abclookupEndpoint"
wsdlURL="wsdl/abclookupLists.wsdl"
endpointName="tns:WSHttpBinding_abclookup"
serviceName="tns:abclookup"
address="http://abc.abc/2013/12/6/abclookup.svc"
xmlns:tns="http://abc.abc/2013/12/6/ServiceContracts/">
<camel-cxf:outInterceptors>
<ref component-id="abcOutInterceptor"/>
</camel-cxf:outInterceptors>
<camel-cxf:properties>
<entry key="dataFormat" value="PAYLOAD"/>
</camel-cxf:properties>
</camel-cxf:cxfEndpoint>
Route Builder class which uses above defined cxf endpoint
#Override
public void configure() throws Exception {
from("direct:retrieveData").routeId(getRouteId().concat("svc.refLookupList"))
.log(LoggingLevel.DEBUG, "Entered into direct:retrieveData route")
.setHeader("operationName", constant("GetCustomFieldDropdownList"))
.setHeader("SOAPAction",constant(getSoapActionHeader()))
.bean("soapRequestBean","constructRequest")
.to("cxf:bean:abclookupEndpoint?username=aaaaaaaa&password=bbbbbbbb")
.log(LoggingLevel.INFO, "Service Response : ${body}")
.end();
Tried adding AuthorizationPolicy like below but did not help
Map properties = new HashMap();
AuthorizationPolicy authPolicy = new AuthorizationPolicy();
authPolicy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_BASIC);
authPolicy.setUserName("aaaaaaaa");
authPolicy.setPassword("bbbbbbbb");
authPolicy.setAuthorization("true");
properties.put("org.apache.cxf.configuration.security.AuthorizationPolicy",authPolicy);
CxfEndpoint myCxfEp = (CxfEndpoint)getContext().getEndpoint("cxf:bean:abclookupEndpoint");
myCxfEp.setProperties(properties);*/
Exception message in logs when tried to invoke serivce:
"No assertion builder for type http://schemas.microsoft.com/ws/06/2004/policy/http}BasicAuthentication registered. Exception in thread "main" org.apache.cxf.ws.policy.PolicyException: None of the policy alternatives"

Related

Mule CXF SOAP service - Validate against XSD and send custom response instead of Soap fault

I have a Mule flow where I exposed a SOAP service using Mule's CXF inbound endpoint. I configured validationEnabled="true" and also wsdlLocation="path-to\my\wsdl". With this configuration of CXF inbound endpoint, it is able to validate the incoming SOAP request and throw a SOAP fault in case there are schema validation errors. So far so good.
Now I want to customise the SOAP Fault response in case of schema validation errors.
I don't want to send SOAP Fault at all, instead I would like to send something like below in the response body
<errorCode>123</errorCode>
<errorDescription>some error description</errorDescription>
Can any one please tell me how I can achieve this?
If you are exposing a SOAP web service and want to have a validation of incoming SOAP message against the schema and put custom message, then one of the best way is to use mulexml:schema-validation-filter
for example the following code :-
<mulexml:schema-validation-filter name="Schema_Validation" schemaLocations="yourSchema.xsd" returnResult="true" doc:name="Schema Validation" />
<flow name="ServiceFlow" >
<http:listener config-ref="HTTP_Listener_Configuration" path="mainData" doc:name="HTTP Connector"/>
<message-filter onUnaccepted="ValidationFailFlow" doc:name="filter to validate xml against xsd" throwOnUnaccepted="true" >
<filter ref="Schema_Validation"/>
</message-filter>
<cxf:jaxws-service serviceClass="com.test.services.schema.maindata.v1.MainData" validationEnabled="true" doc:name="SOAP"/>
<component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl" doc:name="JavaMain_ServiceImpl"/>
</flow>
and the create a sub flow to create your custom message
<errorCode>123</errorCode>
<errorDescription>some error description</errorDescription>
:-
<sub-flow name="ValidationFailFlow" >
<logger message="SOAP Request is not valid!!" level="INFO" doc:name="Logger"/>
<set-payload value="<errorCode>123</errorCode><errorDescription>Soap Validation fail!!!/errorDescription>" doc:name="Set Payload" mimeType="application/xml"/>
</sub-flow>
So now if validation is failing then it will route to your sub flow and show your custom message
note, you can create your custom message using set payload or Java class or XSLT or anything you wish :)
for more reference on mulexml:schema-validation-filter refer :- https://docs.mulesoft.com/mule-user-guide/v/3.7/schema-validation-filter

JaxWS : Externalize Http Conduit name Attribute

I have the WSDL file for the SOAP webservice that i need to invoke over http. Using cxf wsdl2java plugin i have created the stub methods.
I have created the webservice client using jaxws. The webservice has basic authentication enabled. I am trying to configure http conduit
my application.properties
--------------------------
webservices.http.auth.username=username
webservices.http.auth.password=password
fold.webservices.http.auth.authtype=Basic
webservices.http.conduit.property.name=https://fixed_deposits-test.co.in/fold-webservices/services.*
fold.updateservice.soap.address=https://fixed_deposits-test.co.in/fold-webservices/services/UpdateService
----------------------------
My Spring Context...
<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:sec="http://cxf.apache.org/configuration/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="properties" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="locations">
<util:list>
<value>file:${config.dir}/application.properties</value>
</util:list>
</property>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
<jaxws:client id="updateServiceClient" serviceClass="com.fold.facade.v1.UpdateService" address="${fold.updateservice.soap.address}" >
<jaxws:inInterceptors>
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" >
<property name="prettyLogging" value="true" />
</bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" >
<property name="prettyLogging" value="true" />
</bean>
</jaxws:outInterceptors>
</jaxws:client>
<http-conf:conduit name="***?????????***">
<http-conf:authorization>
<sec:UserName>${fold.webservices.http.auth.username}</sec:UserName>
<sec:Password>${fold.webservices.http.auth.password}</sec:Password>
<sec:AuthorizationType>${fold.webservices.http.auth.authtype}</sec:AuthorizationType>
</http-conf:authorization>
</http-conf:conduit>
I have done a lot of searching online so as to what should be the valid value for name attribute..
accouring to CXF documentation it should be...
{WSDL_endpoint_target_namespace}PortName.http-conduit
my WSDL File has..
...
targetNamespace="http://facade.fold.com/" and
...
<wsdl:port binding="tns:UpdateServiceImplServiceSoapBinding"
name="UpdateServiceImplPort">
<soap:address
location="https://fixed_deposits-test.co.in/fold-webservices/services/UpdateService" />
</wsdl:port>
so i tried with these..
<http-conf:conduit name="{http://facade.fold.com/}UpdateServiceImplPort.http_conduit">
or
<http-conf:conduit name="*UpdateServiceImplPort.http_conduit">
or
<http-conf:conduit name="{http://facade.fold.com/}*.http_conduit">
But none of them work as i get 401 unauthorized exception..
org.apache.cxf.transport.http.HTTPException: HTTP response '401: Unauthorized' when communicating with https://fixed_deposits-test.co.in/fold-webservices/services/UpdateService
THERE ARE COUPLE OF WAYS I GOT IT TO WORK
a) <http-conf:conduit name="*.http_conduit">
but i really don't want to do it this way...
b) <http-conf:conduit name="https://fixed_deposits-test.co.in/fold-webservices/services/UpdateService">
this is hardcoding the SOAP service URL... which i don't want as i am looking for externalizing URL as my SOAP URL's are different for different environment..(dev /test /prod etc)
Below is my best shot at externalization, but it failed with 401 Unauthorized Exception...
properties were replaced in all other instances in my spring context, but not for http-conf:conduit name attribute :(
<http-conf:conduit name="${webservices.http.conduit.property.name}">
As a workaround i am currently using the regex approach which works..
<http-conf:conduit name="*.*/fold-webservices/services/UpdateService">
But i really want to figure out if it possible to externalize it and read from properties file. And i want to do it the Spring
configuration way. Please help me !!!
We had the same issue with JBoss Fuse 6.1 (Camel CXF 2.12.0).
You can see what the http-conf:conduit name is set to by enabling DEBUG log level and looking at your log, there should be a log line such as:
DEBUG 10:40:41,437 [Camel (cnpms-as-mnp-ctx) thread #0 - JmsConsumer[cnpms-queue]] org.apache.cxf.transport.http.HTTPConduit.setTlsClientParameters(HTTPConduit.java:901) - Conduit '{http://acme.com/manageporting/v1}ManageportingPortPort.http-conduit' has been (re)configured for plain http.
So in this case you would set the name as:
<http-conf:conduit name="{http://acme.com/manageporting/v1}ManageportingPortPort.http-conduit"
But the Web Service (WS) Interface Class is defined as:
#WebService(targetNamespace = "http://acme.com/manageporting/v1", name = "ManageportingPort")
#SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface ManageportingPort {
Generated from WSDL:
<wsdl:portType name="ManageportingPort">
Note that by following the CXF documentation you would expect the port name component to be "ManageportingPort" NOT "ManageportingPortPort" i.e. with "Port" appended to it.
However looking at how the portQName is resolved in org.apache.cxf.jaxws.support.JaxWsImplementorInfo.getEndpointName(), if the port name is not set in the WS Interface Class, and the name is not null or blank, it sets the port name to portName = name + "Port" otherwise it sets it to portName = implementorClass.getSimpleName() + "Port".
I had the same problem...
In my case the following 2 changes helped:
1) add "Port" postfix to the port name, despite it is not defined in the wsdl this was
e.g wsdl:
<wsdl:port name="XXXSoap">
will be "XXXSoapPort" in the conduit definition
2) remove the "\" at the end of the target namespace name
==> therefore try
<http-conf:conduit name="{http://facade.fold.com}UpdateServiceImplPort.http_conduit">
or
<http-conf:conduit name="{http://facade.fold.com}UpdateServiceImplPortPort.http_conduit">
I came across the same challenge and found no existing solution. Spring doesn't seem to resolve placeholders in bean names (make lots of sense). However, this is a valid case unless cxf allows conduit matching using another attribute.
There are a few ways to solve this problem:
Define conduit beans programmatically (lose the neat of xml declaration)
Find a way to resolve the bean names which contains placeholders
I prefer option 1 and this is the implementation which I'm happy with. Please note that PropertyPlaceholderResolver is our own utility which uses the same defined resolver bean.
public class NameWithPlaceholderBeanFactoryPostProcessor implements BeanFactoryPostProcessor
{
#Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException
{
if (!(beanFactory instanceof DefaultListableBeanFactory))
return;
DefaultListableBeanFactory bf = (DefaultListableBeanFactory) beanFactory;
String[] names = bf.getBeanDefinitionNames();
for (String name : names)
{
if (name.indexOf('$') < 0)
continue;
BeanDefinition bd = bf.getBeanDefinition(name);
bf.removeBeanDefinition(name);
bf.registerBeanDefinition(PropertyPlaceholderResolver.resolvePlaceHolders(name), bd);
}
}
}
The final step is to define this as a spring bean.

Mule cxf client and wcf service interaction

I have the workflow like
<flow name="testmulewcfFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/GetData" doc:name="HTTP"/>
<set-payload value="#[message.inboundProperties.'http.query.params'.value]" doc:name="Set Payload"/>
<cxf:jaxws-client port="CXFWebservicePort" operation="GetData" serviceClass="com.mulesoft.wcfconsumer.IService1" doc:name="CXF"/>
<object-to-string-transformer doc:name="Object to String"/>
</flow>
And very simple wcf service which I want to use from mule anypoint studio
public class Service1 : IService1
{
public string GetData(string value)
{
return string.Format("You entered: {0}", value);
}
}
But I when I make a call: http://localhost:8081/GetData?value=hello
I get the error:
Unexpected wrapper element {http://tempuri.org/}GetData found. Expected {http://tempuri.org/}GetDataResponse.. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: PushbackInputStream
What am I doing wrong?
That issue you are facing due to not implementing an outbound endpoint at the end after cxf:jaxws-client
Put an out bound endpoint at the end that will call your external web service with a http:request component :- http://www.mulesoft.org/documentation/display/current/HTTP+Connector

How to access a huge JSON coming (from a spring RESTful Service) in a spring MVC app using RestTemplate

My Spring RESTful web service is returning a JSON form of-
[{"key1":"value1","key2":"value2","key3":"value3"},{"key4":"value4","key5":"value5","key6":"value6"}]
Now when my spring MVC app, try to access it, to show in a JSP then Exception occurs saying-no suitable HttpMessageConverter found Please help me where I going wrong.Here is my code-
Inside #Controller class of my spring MVC app calling the RESTful service
//**com.songs.controllers.FrontSongController.java**
</*
author Rohit Tiwari
*/>
#RequestMapping(value="/alls",method=RequestMethod.POST)
public String getAllSongs(ModelMap md)
{
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=new RestTemplate();
//SongResource.class is for representation on incoming JSON see below for its code
//This is the line no 48 that you will see in below browser logs
ResponseEntity<SongResource> listofallsongs=rt.exchange(url,HttpMethod.GET,entity, SongResource.class);
md.addAttribute("listname", "Songs available in the repository:");
System.out.println("Response Entity object= "+listofallsongs);
System.out.println("Response Entity body= "+listofallsongs.getBody().toString());
return "Sucess";
}
Inside config-servlet.xml of my spring MVC app calling the RESTful service
<context:component-scan base-package="com.songs.controllers" />
<mvc:annotation-driven />
<context:annotation-config/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Inside SongResource.java of my spring MVC app, which I am trying to use for converting the coming JSON to my SongResource.class object, that my spring MVC app can use in a jsp
//**com.songs.service.resource.SongResource.java**
public class SongResource
{
private String name;
private String film;
private String singer;
public SongResource(String name,String film,String singer)
{
this.name=name;
this.film=film;
this.singer=singer;
}
//setter & getters of all above
}
On calling the spring REST service from my spring MVC app the browser is saying as below-
Error 500--Internal Server Error
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.songs.service.resource.SongResource] and content type [application/json]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java :77)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:619)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:377)
at com.songs.controllers.FrontSongController.getAllSongs(FrontSongController.java:48)
//and so on
Try this, hope it will help you
#RequestMapping(value="/alls",method=RequestMethod.POST)
public String getAllSongs(ModelMap md)
{
String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=new RestTemplate();
SongResource[] songRs = template.getForObject(url, SongResource[].class);
List<SongResource> songs = Arrays.asList(songRs);
md.addAttribute("listname", "Songs available in the repository:");
md.addAttribute("listValues", songs);
return "Sucess";
}

Consume a simple web service using mule

I am trying to consume a public service using Mule + apache cxf. The service is available at http://www.html2xml.nl/Services/Calculator/Version1/Calculator.asmx?WSDL
This is a very simple service which does basic arithmetic operations. I am trying to call the operation "Add" here. My mule configuration is as below
<flow name="calculator" doc:name="calculator">
<stdio:inbound-endpoint system="IN" doc:name="STDIO"/>
<custom-transformer class="com.calculator.transformer.CalculatorClient" doc:name="Java"/>
<outbound-endpoint address="http://localhost:28081/service/Calculator?WSDL" exchange-pattern="request-response" doc:name="HTTP">
<cxf:jaxws-client clientClass="com.calculator.wsdl.Calculator" enableMuleSoapHeaders="true" port="CalculatorHttpPost" wsdlLocation="classpath:/wsdl/Calculator.wsdl" operation="Add">
<cxf:inInterceptors>
<spring:bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</cxf:inInterceptors>
<cxf:outInterceptors>
<spring:bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</cxf:outInterceptors>
</cxf:jaxws-client>
</outbound-endpoint>
<transformer ref="CalculatorResponse" doc:name="Transformer Reference"/>
<mulexml:jaxb-object-to-xml-transformer name="CalculatortoXML" jaxbContext-ref="myJaxbCal" />
<stdio:outbound-endpoint system="OUT" doc:name="STDIO"/>
</flow>
Before calling the client class i added a transformer as below. This just sets the 2 numbers to add.
Code
package com.calculator.transformer;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
import com.calculator.wsdl.Add;
public class CalculatorClient extends AbstractMessageTransformer {
#Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
Add add= new Add();
add.setA(3);
add.setB(3);
return add;
}
}
Once i start mule i receive the error.Not sure what i am doing wrong.
ERROR 2014-01-16 01:09:46,237 [[weatherproject].calculator.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy:
Message : wrong number of arguments. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: Add
Code : MULE_ERROR--2
Exception stack is:
1. wrong number of arguments (java.lang.IllegalArgumentException)
sun.reflect.NativeMethodAccessorImpl:-2 (null)
2. wrong number of arguments. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: Add (org.mule.api.transport.DispatchException)
org.mule.module.cxf.CxfOutboundMessageProcessor:148 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transport/DispatchException.html)
Root Exception stack trace:
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
you have mentioned http://localhost:28081/service/Calculator?WSDL as your address and I suppose it should be http://localhost:28081/service/Calculator .
This post helped me to solve the problem
Mule SOAP client wrapper as parameter instead of object array
By using the JAXB bindings as suggested CXF will generate the wrapper objects.