How do I add HTTP headers to JAXWS created proxy classes - java

I've created a set of proxy classes in NetBeans for a SOAP web service.
I'd like to add an HTTP header to the outgoing request.
Map<String, Object> requestHeaders = new HashMap<>();
requestHeaders.put("X-Header", header);
AccountManagementService service = new AccountManagementService();
AccountManagementServiceSoap soap = service.getAccountManagementServiceSoap();
GetMembershipSummaryResponse.GetMembershipSummaryResult membershipSummary = soap.getMembershipSummary("mikeb#foobar.com");
I saw a bunch of JAX-WS header examples for when you are getting the individual port from the service. Is there an easier way? Can I just add the header to some collection on service or soap object?

after some digging I found this post: jax-ws change Content-type to Content-Type because server is hyper sensitive
which isn't really a full answer, so I am going to post the full answer here:
AccountManagementService service = new AccountManagementService();
AccountManagementServiceSoap soap = service.getAccountManagementServiceSoap();
((BindingProvider)soap).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS,
Collections.singletonMap("X-Header",Collections.singletonList(header)));
GetMembershipSummaryResponse.GetMembershipSummaryResult membershipSummary = soap.getMembershipSummary("mikeb#foobar.com");

Related

What is the Java Apache CXF equivalent of C# WCF AddressHeader?

I'm trying to access an old SOAP based system written in C# from a Java backend service. This C# application is looking for an AddressHeader to be populated with a specific value on every request. I'm using Apache CXF to create the requests to this service. Unfortunately, for the life of me, I cannot find out how to add this address header to each of the requests. Does anyone know what the equivalent in Java is and how to add it using Apache CXF?
The address header is the same as the SOAP header, hence we only need to add a particular SOAP header to every request so that be capable of making a successful call.
Here is an example I found on the internet.
ClientProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(singz.ws.cxf.sample.SampleServiceInterface.class);
factory.setAddress("http://xxx.xxx.com/services/SampleService/v1");
SampleServiceInterface serviceClient = (SampleServiceInterface) factory.create();
Client proxy = ClientProxy.getClient(serviceClient);
List<Header> headersList = new ArrayList<Header>();
Header testSoapHeader1 = new Header(new QName("uri:singz.ws.sample", "soapheader1"), "SOAP Header Message 1", new JAXBDataBinding(String.class));
Header testSoapHeader2 = new Header(new QName("uri:singz.ws.sample", "soapheader2"), "SOAP Header Message 2", new JAXBDataBinding(String.class));
headersList.add(testSoapHeader1);
headersList.add(testSoapHeader2);
proxy.getRequestContext().put(Header.HEADER_LIST, headersList);
Please refer to the below links, wish it is useful to you.
https://dzone.com/articles/apache-cxf-how-add-custom-soap-0
How do you add a Soap Header defined in a wsdl to a web service client in CXF?
https://dzone.com/articles/apache-cxf-how-add-custom-soap
Feel free to let me know if there is anything I can help with.

how can i get 'jsessionid' using jersey client?

I'm using jersey-client 1.19.4 to test my web application.
If I test with postman, I can find the cookie "JSESSIONID" after 'send' action. And I can find 'jsessionid=...' in ClientResponse.toString(), But ClientResponse.getCookies() returns nothing.
WebResource webResource = client.resource(someUrl);
FormDataMultiPart formData = new FormDataMultiPart();
formData.bodyPart(new FormDataBodyPart("userId", userId));
formData.bodyPart(new FormDataBodyPart("uPasswd", uPasswd));
ClientResponse response = webResource.accept("*/*").type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, formData);
System.out.println("response: " + response.toString()); // 'jsessionid' found here
List<NewCookie> cookies = response.getCookies();
System.out.println("# of cookies: " + cookies.size()); // prints "# of cookies: 0"
How can I get "JSESSIONID" from ClientResponse?
JSESSIONID can be set in few different ways. As per
JSR-000315 Java Servlet 3.0 Final Release, chapter 7.1 Session Tracking Mechanisms, following can be used:
Cookies
SSL Sessions
URL Rewriting
In your case it appears that URL Rewriting is being used. The two most common reasons being:
The server is configured not to issue cookies
Your client doesn't support cookies
Since you get the cookie while using Postman it most likely means that your Jersey Client doesn't handle cookies. One way to integrate it with Apache HttpClient using jersey-apache-client as per this answer.
From the mailing list:
http://jersey.576304.n2.nabble.com/Session-Handling-not-working-with-Jersey-Client-td4519663.html
The Jersey client by default uses HttpURLConnection that does not
support cookie management (and thus sessions).
You need to switch to using the Apache HTTP client support.
Then set the following property to true: PROPERTY_HANDLE_COOKIES
DefaultApacheHttpClientConfig config = new
DefaultApacheHttpClientConfig(); config
.setProperty("com.sun.jersey.impl.client.httpclient.handleCookies",
true);
ApacheHttpClient c = ApacheHttpClient.create(config);
Plus you can also use authorization with the Apache HTTP client
integration.
DefaultApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
config.getState().setCredentials(null, null, -1, "foo", "bar");
ApacheHttpClient c = ApacheHttpClient.create(config);
WebResource r = c.resource("http://host/base");
String s = r.get(String.class);
s = r.post(String.class, s);

Empty SOAPAction Apache cxf

I'm creating a soap client using apache cxf.I used a network hook capture the data and find out in Http header SOAPAction is empty
SOAPAction : ""
Below is the code
URL wsdlurl=SOAPWebServiceTransport.class.getClassLoader().
getResource("my.wsdl");
OnlinePort service= new OnlinePortService(wsdlurl).getOnlinePortPort();
Client proxy = ClientProxy.getClient(service);
// Provides WS-Security
WSS4JOutInterceptor wss4jOut = new WSS4JOutInterceptor();
wss4jOut.setProperty("action", "UsernameToken");
wss4jOut.setProperty("user", userName);
wss4jOut.setProperty("passwordType", "PasswordText");
wss4jOut.setProperty("password", password);
wss4jOut.setProperty(WSHandlerConstants.ADD_UT_ELEMENTS,
WSConstants.NONCE_LN + " " + WSConstants.CREATED_LN);
wss4jOut.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, ServerPasswordCallback.class.getName());
proxy.getEndpoint().getOutInterceptors().add(wss4jOut);
setConduitProperties((HTTPConduit) proxy.getConduit(),url);
In the setConduitProperties I'm just setting some http properties.
While investigating this issue I found a work around
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("SOAPAction", Arrays.asList("myPrefix:mySoapMethod"));
proxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
But the problem is that if I have more than one method then in the Http header its putting all the method
SOAPAction : "myPrefix:mySoapMethod,myPrefix:mySoapMethod2"
Is there any better solution to handle this?
I would suggest to set the SOAPAction header per request. Otherwise, the server is not be able to tell which method do you want to invoke.

Apache Camel v2.12 | CXF Component | Basic Authentication

Respected Experts,
I am trying to access a web service that requires basic authentication. I am able to access using the CXF's JaxWsDynamicClientFactory. The code piece for auth looks like:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(ID_WSDL);
HTTPConduit conduit= (HTTPConduit) client.getConduit();
AuthorizationPolicy authorization = conduit.getAuthorization();
authorization.setUserName(USERNAME);
authorization.setPassword(PWD);
conduit.setAuthorization(authorization);
However, when I try to use Camel's CXF component to access the same Web Service I get 401 Unauthorized error, since Camel is not sending the authentication information to the Web Service.
My route looks like:
from("file://c:/test?fileName=request.txt&noop=true").routeId("myrouteId")
.process(processor)
.to(cxf)
.to("log:{body}");
In my processor, I am setting the credentials as follows:
Map<String, Object> properties = new HashMap<String, Object>();
AuthorizationPolicy authPolicy = new AuthorizationPolicy();
authPolicy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_BASIC);
authPolicy.setUserName(USERNAME);
authPolicy.setPassword(PWD);
properties.put("org.apache.cxf.configuration.security.AuthorizationPolicy", authPolicy);
myEndpoint.setProperties(properties);
myEndpoint is CXFEndpoint, retrieved from Exchange.
Am I missing something or something wrong here.
There is a similar question. I had raised my doubt there as a answer since I was not able to comment. However, my answer has been deleted. So, I am raising a fresh question in a hope that I will get some direction to move forward on this.
Thks & brgds
With Willem's help, was able to make this working. The authentication credentials need to passed to the CXF Endpoint in the Route Builder rather than in the Processor. This is as explained by Williem on Camel forum:
If you set the cxfEndpoint property in a processor, it’s a setting of runtime.
As the CxfProducer is created during the camel context start the route, the cxfEndpoint’s property is >not updated.
So, to fix this add the following code to the Route Builder:
Map<String, Object> properties = new HashMap<String, Object>();
AuthorizationPolicy authPolicy = new AuthorizationPolicy();
authPolicy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_BASIC);
authPolicy.setUserName(USERNAME);
authPolicy.setPassword(PWD);
authPolicy.setAuthorization("true");
//properties.put(AuthorizationPolicy.class.getName(), authPolicy);
properties.put("org.apache.cxf.configuration.security.AuthorizationPolicy", authPolicy);
CxfEndpoint myCxfEp = (CxfEndpoint)getContext().getEndpoint("cxf://");
myCxfEp.setProperties(properties);
Also, version 2.12.3 of Apache Camel is introducing username and password options for basic authentication.

Problem with passing SOAP headers using Apache CXF client

I am trying to implement a simple client for a webservice, the only problem I am facing with the webservice is, it has a generic endpoint: http://myserver3333.com:8080/ws/services and the way you lookup for web services deployed is via SOAP header.
So, for example, if you try to hit the service vis SOAP UI,
the endpoint I specify is: http://myserver3333.com:8080/ws/services
In the SOAP headers I specific the following:
SERVICE-NAME = MyAwesomeService
OPERATION-NAME = makeMeMoreAwesome
So, how can I do the same thing using apache cxf client?
My current code:
URL wsdlLocation = new URL("http://myserver3333.com:8080/ws/service");
MyAwesomeService service = new MyAwesomeService(wsdlLocation);
MyAwesomeServicePort port = service.getMyAwesomeServiceSOAPPort();
List<Header> headers = new ArrayList<Header>();
Header operationNameHeader = new Header(new QName("OPERATION-NAME"), "makeMeMoreAwesome",
new JAXBDataBinding(String.class));
Header serviceNameHeader = new Header(new QName("SERVICE-NAME"), "MyAwesomeService",
new JAXBDataBinding(String.class));
headers.add(operationNameHeader);
headers.add(serviceNameHeader);
BindingProvider bindingProvider = (BindingProvider)port;
bindingProvider.getRequestContext().put(Header.HEADER_LIST, headers);
MakeMeMoreAwesomeRequest request = new MakeMeMoreAwesomeRequest();
MakeMeMoreAwesomeResponse response = port.makeMeMoreAwesome(request);
System.out.println(response.getAck());
But when I run this, I get this error:
Exception in thread "main" com.sun.xml.ws.wsdl.parser.InaccessibleWSDLException: 2 counts of InaccessibleWSDLException.
java.io.IOException: Server returned HTTP response code: 500 for URL: http://myserver3333.com:8080/ws/services
java.io.IOException: Server returned HTTP response code: 500 for URL: http://myserver3333.com:8080/ws/services?wsdl
Which is correct because there is no WSDL at that location, it need to follow the soap header to get the service.
Update:
After two points from #Daniel Kulp I am here:
I added a new line: bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://myserver3333.com:8080/ws/services");
And now I get this error:
org.apache.cxf.binding.soap.SoapFault: "http://www.myserver.com/ws/services", the namespace on the "errorMessage" element, is not a valid SOAP version.
at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.readVersion(ReadHeadersInterceptor.java:115)
at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:141)
at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:60)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:771)
My assumption is, this error is same as this one. But I am not using ?wsdl. So, any suggestions?
2 comments:
1) You aren't picking up CXF. Check your classpath to make sure CXF is there and not the Sun/Oracle implementation. com.sun.xml.ws.wsdl.parser.InaccessibleWSDLException shows you are picking up the Sun implementation.
2) The URL passed into MyAwesomeService(wsdlLocation) MUST be a URL to the WSDL, not the endpoint itself.

Categories

Resources