Archive for February 8, 2008

SSL and Web Service

SOAP doesn’t deal with whether the connection is SSL or not. This is a HTTP issue,so there is nothing to do in web service implementation to secure the connection.
If a secure connection is needed to establish then there are thing to do. Assume that the web container is Tomcat server then in TOMCAT_HOME/conf/server.xml the connection attibute has to be uptaded to ensure that Tomcat server accepts ssl connection requests.
After that, the certificate has to be loaded to the client, to achieve this, keytool command is used
keytool -import -trustcacerts -alias FOO -file “FOO_PATH\foo.cer” -storetype JKS -keystore “FOO_PATH\foo.keystore”
Secondly the code below has to be added to just before getting connetion to the server.


System.setProperty("javax.net.ssl.keyStore", "FOO_PATH/foo.keystore");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");
System.setProperty("javax.net.ssl.keyStoreType","JKS");
System.setProperty("javax.net.ssl.trustStore", "FOO_PATH/foo.keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

Leave a Comment

How to Implement Session in JAXWS

Server

package com.foo.ws.session;
import javax.annotation.Resource;
import  javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.handler.MessageContext;
@WebServicepublic class Hello {

//webServiceContext is injected by the JAXWS API
@Resource private WebServiceContext wsContext;

@WebMethod public int getCounter(){

       MessageContext mc = wsContext.getMessageContext();
      HttpSession session = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
      // Get a session property "counter" from context
      if (session == null)
          throw new WebServiceException("No session in WebServiceContext");
      Integer counter = (Integer)session.getAttribute("counter");
      if (counter == null) {
           counter = new Integer(0);
           System.out.println("Starting the Session");
      }
    counter = new Integer(counter.intValue() + 1);
    session.setAttribute("counter", counter);
    return counter;
  }
}

Client


package com.foo.ws.session.client;
import java.util.Map;
import javax.xml.ws.BindingProvider;
public class ClientMain {
public static void main(String[] args) {
      Hello proxy = new HelloService().getHelloPort();
      //setting requestContext so that session could be maintained through client and server
      Map requestContext = ((BindingProvider)proxy).getRequestContext();
      requestContext.put(BindingProvider.SESSION_MAINTAIN_PROPERTY,true);
      int result = proxy.getCounter();
      System.out.println(result);
      result = proxy.getCounter();
      System.out.println(result);
}
}

the output for the client is 1 and 2 as expected

Comments (1)

Older Posts »