How can I make jaxws parse response without checking Content-Type header - soap

I am calling a soap webservice via jax ws client code (generated from wsdl).
But the service sends response with "Content-Type:text/html" where as the jax-ws implementation requires "text/xml" type.
The webservice folk won't change the response header.
Exception is :
com.sun.xml.internal.ws.client.ClientTransportException: The server
sent HTTP status code 200: OK at
com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.createResponsePacket(HttpTransportPipe.java:266)
~[?:1.8.0_171] at
com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:217)
~[?:1.8.0_171] at
com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:130)
~[?:1.8.0_171] at
com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:124)
~[?:1.8.0_171]
Also tried with Saaj Implementation. It also mandates response Content-Type header to be text/xml.
Here is the exception with saaj :
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:
Invalid Content-Type:text/html. Is this an error message instead of a
SOAP response? at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at
com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:655)
~[?:1.8.0_171] at
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
Is there a way to make jaxws ignore response header and continue parsing?

Didn't exactly find a solution with jax-ws but thanks to #JGlass found a pleasantly simple solution with apache cfx. Very flexible implementation of jax-ws. In love with it.
Here is the simple code that made it work :
Client client = ClientProxy.getClient(port);
client.getInInterceptors().add(new AbstractPhaseInterceptor<org.apache.cxf.message.Message>(Phase.RECEIVE) {
public void handleMessage(org.apache.cxf.message.Message message) {
message.put(org.apache.cxf.message.Message.CONTENT_TYPE, "text/xml");
}
});

Related

Testing REST client return handshake_failure when call https

I'm trying to call a REST API for this end point URL. But I'm getting this exception
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
I have already added the JCE and installed the certificate in the Java keystore.
I'm using Java 8 and part of my code looks like this:
Client client = ClientBuilder.newClient();
WebTarget target = client.target(END_POINT);
Invocation.Builder invocationBuilde = target.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();
String s = response.readEntity(String.class);
System.out.println(s)
In addition to this I activated the debug mode -Djavax.net.debug=all and this is all the negotiation:
Negotiation REST API

GWTTestCase - HTMLUnit - No permitted "Access-Control-Allow-Origin"

When running GWTTestCase that call an external Restful service with GWT (Using Restlet-GWT) the app throws the error below.
Error:
Jan 05, 2015 1:24:41 PM com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl runSingleJob
SEVERE: Job run failed with unexpected RuntimeException: Wrapped java.lang.RuntimeException: No permitted "Access-Control-Allow-Origin" header.
net.sourceforge.htmlunit.corejs.javascript.WrappedException: Wrapped java.lang.RuntimeException: No permitted "Access-Control-Allow-Origin" header.
at net.sourceforge.htmlunit.corejs.javascript.Context.throwAsScriptRuntimeEx(Context.java:1889)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.doSend(XMLHttpRequest.java:681)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.access$000(XMLHttpRequest.java:94)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest$1.run(XMLHttpRequest.java:603)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:602)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
at com.gargoylesoftware.htmlunit.javascript.background.JavascriptXMLHttpRequestJob.run(JavascriptXMLHttpRequestJob.java:36)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:328)
at com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:162)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.RuntimeException: No permitted "Access-Control-Allow-Origin" header.
... 9 more
From my research this seems to be an issue that HTMLUnit would not allow to access Cross-domain, is there a work around with this?
You can add the support of CORS to your application.
A support has been provided in 2.3 release of the Restlet framework (cf http://restlet.com/technical-resources/restlet-framework/guide/2.3/introduction/whats-new/2.3).
If you are using the 2.2 release, and because we won't add this feature to 2.2, you can add the required classes in your own code manually (cf https://github.com/restlet/restlet-framework-java/blob/2.3/modules/org.restlet/src/org/restlet/service/CorsService.java, https://github.com/restlet/restlet-framework-java/blob/2.3/modules/org.restlet/src/org/restlet/engine/application/CorsFilter.java, and https://github.com/restlet/restlet-framework-java/blob/2.3/modules/org.restlet/src/org/restlet/engine/application/CorsResponseHelper.java).
I was stumped by this very same error response but I am not able to refactor the Restlet framework into my client & server codebases.
A couple of tips for handling diagnosis of the problem.
use a proxy or inspect dev tool to record the exact headers within the request and response for both the OPTIONS and GET/POST pairs.
OPTIONS request header "Origin" must match response header "Access-Control-Allow-Origin" exactly. No wildcards. Perhaps write your response to just echo the request like:
response.addHeader("Access-control-allow-origin", request.getHeader("Origin"));
Your request header names must be fully enumerated within the OPTIONS "Access-control-request-headers" header value AND match the "Access-control-Allow-Headers" response header.
Note: that a mismatch in #3 will trigger the same error message as a mismatch in #2.

Apache CXF (Mule) Invalid QName in mapping: SOAP-ENV:Client

I'm using Apache CXF 2.5.9 within Mule ESB 3.5.0 CE for a Web Service client. If the soap message validation fails on server I get a soap fault (naturally) that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring>https://10.1.104.105:8444/DohvatiIzOIBaF2FAServiceTest: cvc-simple-type 1: element {http://www.apis-it.hr/fin/2008/elements/DohvatIzOIBa}OIB value '716500116650' is not a valid instance of type {http://www.apis-it.hr/fin/2008/types/f63}String11BrojType</faultstring>
<faultactor>OIB CIS pristupni čvor</faultactor>
</soapenv:Fault>
..but when client side tries to unmarshall the fault I get this exception:
2014-12-13 13:52:34,199 [WARN] org.apache.cxf.phase.PhaseInterceptorChain - Interceptor for {http://www.apis-it.hr/fin/2010/services/DohvatiIzOIBaF2FAService}DohvatiIzOIBaF2ServicePortTypeService#{http://www.apis-it.hr/fin/2010/services/DohvatiIzOIBaF2FAService}DohvatiFOPoOIBu has thrown exception, unwinding now
java.lang.RuntimeException: Invalid QName in mapping: SOAP-ENV:Client
at org.apache.cxf.staxutils.StaxUtils.readQName(StaxUtils.java:1399)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.unmarshalFault(Soap11FaultInInterceptor.java:59)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:46)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:35)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:114)
at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:69)
at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:34)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:801)
I want the CXF client to unmarshall the soap fault to exception. How to get around this error?
Try making SOAP-ENV:Client something like this 12345, making the fault code a number solved it for me.
Mule won't catch the exception itself.
You should probably use a cxf fault interceptor. From there you can compose the message you are expecting.
Alternativly you could use the CXF transform feature
You need:
<entry key="faultcode" value="faultcode=soapenv:Client"/>
This will map the original invalid fault from the server into the following valid fault so that it can be accepted by the cxf client
<soapenv:Fault>
<faultcode>soapenv:Client</faultcode>
<faultstring>An error occurred</faultstring>
</soapenv:Fault>

Forward gwt rpc request to another HTTP Servlet

How can I forward GWTRPC request to another HTTP Sevrvlet
I'm using :
RequestDispatcher rd = super.getServletContext().getRequestDispatcher("/abc?" + sURL);
rd.forward(super.getRequest(), super.getResponse());
but its giving me error :
java.lang.IllegalStateException: getWriter() has already been called for this response

Unable to process multipart/form-data from ajax in JAX-RS jersey web service

I am sending a form data request from the following Javascript:
xmlHttpReq.open("POST", "https://server.com/resource", true);
formData = new FormData(form);
xmlHttpReq.send(formData);
The server code running in a tomcat container is as following:
#POST
#Path("{computation}")
#Consumes({"application/x-www-form-urlencoded,multipart/form-data"})
String doPost(MultivaluedMap <String, String> formParams) {
....
return strVar;
}
The server receives the content type
content-type="multipart/form-data;
boundary=----WebKitFormBoundaryM1A3izVTDMdn1xsJ"
and throws the following exception:
SEVERE: A message body reader for Java class javax.ws.rs.core.MultivaluedMap,
and Java type javax.ws.rs.core.MultivaluedMap<java.lang.String, java.lang.String>,
and MIME media type multipart/form-data;
boundary=----WebKitFormBoundaryM1A3izVTDMdn1xsJ was not found.
How can I get the server to accept the multipart/form-data request? It works fine for application/x-www-form-urlencoded content type.
Thank you.
Your #Consumes should be formatted like this:
#Consumes("application/x-www-form-urlencoded","multipart/form-data")