Showing posts with label Web Services. Show all posts
Showing posts with label Web Services. Show all posts

Monday, April 22, 2013

How to Programatically Parse a Custom Web Services SoapFault and Map it to an Object Defined in the WSDL's Schema xsd

Sometimes the server side Web Services provider offers a custom soap fault detail implementation. The client side needs to parse it and map it back into a java object generated from the WSDL/xsd.
For example, an soapFault look likes this with the custom :

<s:Body>
 <s:Fault>
  <faultcode>s:Client</faultcode>
  <faultstring xml:lang="en-US">Operation parameter is invalid.</faultstring>
  <detail>
   <ServerCustomizedError>
    <ErrorMessage>Operation parameter is invalid.</ErrorMessage>
    <Id>56a145c6-a7f3-4069-94a4-6ec945d95613</Id>
    <ParameterName>input</ParameterName>
    <ValidationDetails>
     <Message>Why is this wrong?</Message>
     <Target>wrong value</Target>
     <Key>wrong data</Key>
    </ValidationDetails>
   </ServerCustomizedError>
  </detail>
 </s:Fault>
</s:Body>


The corresponding client side java code to parse the above Soap Fault will look like:
Iterator it = soapFaultClientException.getSoapFault().getFaultDetail().getDetailEntries();
while (it.hasNext())
{
 Source errSource = it.next().getSource();
 @SuppressWarnings("unchecked")
 JAXBElement errJaxb = (JAXBElement) springWebServiceTemplate.getUnmarshaller().unmarshal(errSource);
 ServerCustomizedError err = errJaxb.getValue();
 ....
}

Tuesday, September 18, 2012

Access Web Services over a SSL Layer

I have a Java application to access web services hosted behind a HTTPS transport protocol. The client side java application gave me the error message like:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

I did the following to resolve the issue:
1. Try to access the web services endpoint from a browser and export its certificate using the 'X.509 certificate format' into a sample.cert file.
2. Locate the JVM's keystore. For example, \dev\Java\jdk1.6.0_34\jre\lib\security
3. Back up its original keystore and run the following command under the above directory:
\dev\Java\jdk1.6.0_34\jre\bin\keytool -import -keystore cacerts -file sample.cer -alias sampleXYZ
The default password is 'changeit'. It's fine if the certificate is a self-signed one. If so, be sure to answer 'Yes' for the question to trust the certificate.
4. After restart of the Java application, another error may show up as:
CertificateException: No subject alternative names present
I tried to edit C:\Windows\System32\drivers\etc\hosts as Administrator and put the ip address matching to the CN host name defined in the cert
To review the CN name, run the following command:
keytool -list -v -keystore .\cacerts -alias sampleXYZ
The dns needs to be refreshed from a command line console to pick up the new definition:
ipconfig /flushdns

Tuesday, May 6, 2008

How to Test a Web Service

When you develop a Web Service application, either on the client side or on the server side, you need a tester tool. If you are on the client side, before spending too much time on your code, it's good to test whether the Web Services server is stable and have some general idea of the sample requests and responses. If you are on the server side, you definitely need a tester to prove your deliveries. Sometimes developers come up with an in-house GUI or web application tester and end up in a situation where there are more bugs in the tester than the application itself. Fortunately there are free, open-source and stable tools available nowadays. We don't need to reinvent the wheel.

Most of the time, Web Services are nothing more than SOAP/XML over HTTP. In the simplest cases, any tool that can send text over HTTP to an URL can be used to test Web Services. But you will find it a must-have to generate sample SOAP requests from any given WSDL. The famous XMLSpy provides this kind of feature, unfortunately with a hefty fee. The free version of soapUI by Eviware is my favorite.

Friday, April 25, 2008

Log Web Services Incoming Requests in JAX-WS

For any server side Web Services development, logging the incoming requests is crucial. The following code snippet gives an example. Even though this particular example uses SOAPMessageContext from JAX-WS's SOAPHandler, the approach can be easily modified to fit into other Web Services solutions.

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
soapMessageContext.getMessage().writeTo(buffer);
log.info("SOAP Request:\n" + new String(buffer.toByteArray()));


Note that a lot of times logging comes hand in hand with incoming requests validation. Read this blog for more details.