Wednesday, December 07, 2005

Getting the "HttpServletRequest" in a J2EE JAX-RPC service

Below is an example J2EE JAX-RPC web service that shows how to get the "HttpServletRequest" which can return the contextpath and access any servlet request data. NOTE: This was wrote in the editor of my blog, so it could contain syntax issues. It has not been tested. :)

/////////////////////////////////////////////////////////////////////////////////////////

import javax.xml.rpc.server.ServiceLifecycle;
import javax.xml.rpc.server.ServletEndpointContext;
import javax.xml.rpc.handler.MessageContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

Class TestService implements ServiceLifecycle {

private String ctxPath;
private ServletEndpointContext epc;
private MessageContext mc;
private ServletContext sc;
private HttpServletRequest request;

public void init(Object obj) throws ServiceException {
epc = (ServletEndpointContext)obj;
if (epc != null) {
mc = (MessageContext)epc.getMessageContext();
sc = (ServletContext)epc.getServletContext();
if (mc != null) {
request = (HttpServletRequest) mc.getProperty("transport.http.servletRequest");
if (request != null) {
ctxPath = request.getContextPath();
}
}
}
}

public String doAction () {
return (new String("The contextPath for this service is: " + this.ctxPath));
}

}

Wednesday, November 30, 2005

Sample Java SOAP client

Below is a sample Java SOAP client. Hope this comes in handy for someone looking for a sample. I will post some J2EE RPC style service clients as well as a J2EE application client. If anyone is interested in other clients wrote in different languages, just let me know.

/////////////////////////////////////////////////////////////////////////////////////////////////////

import java.util.*;
import java.net.URL;
import javax.xml.namespace.*;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;

class TestServiceClient {

public static void main (String [] args) throws Exception {

URL url = new URL("http://www.test.com/TestService")
QName svcName = new QName("http://com.test","TestService");
QName op = new QName("http://com.test/TestService","test")

ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(svcName);

Call call = (Call)service.createCall();
call.setTargetEndpointAddress(url.toString());
call.setOperationName(op);
call.setReturnType(XMLType.XSD_STRING);

String response = (String)call.invoke(new Object[] { });
System.out.println("response: " + response);

}

}

Monday, October 10, 2005

Accessing HTTPS web services in Java

If any of you are receiving the following errors while trying to consume an external web service, read below for a solution.

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

This was done for/on Linux, but the process should be very similar for windows based clients.
  • Fetch certificate from server.
  • Cut and paste the certificate (including BEGIN and END lines)
    into a local file (ie; sitewithservice.pem).
  • Add certificate (ie; sitewithservice.pem) to local java keystore.
    • keytool -import -alias sitewithservice.com -keystore /opt/java/keystore -file sitewithservice.pem
    • NOTE: Answer "Yes" to "Trust this certificate? [no]:".

  • Add the following lines to your java web service client.

    • System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

      java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

      System.setProperty("javax.net.ssl.trustStore",
      "[PATH TO YOUR KEYSTORE]");

      System.setProperty("javax.net.ssl.trustStorePassword",
      "[PASSWORD TO YOUR KEYSTORE]");
That is it!

Hope this helps someone.

-- ngeren