Showing posts with label JAX-WS. Show all posts
Showing posts with label JAX-WS. Show all posts

Invoking WSO2 Carbon Admin Services with a JAX-WS Client

Any functionality available to you via WSO2 Carbon management console can also be invoked via a web service call.

In other word all these management related functionalities are exposed via web services.

To access these web services - also known as admin services, you need to be an authenticated user and also need to have the required set of permissions.

Following client demonstrates how you can create an authenticated session with WSO2 Carbon and then invoke an admin service with a JAX-WS client.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.soap.SOAPBinding;

public class AdminServiceClient {

 private final static String AUTH_ADMIN_EPR = "https://localhost:9443/services/AuthenticationAdmin";
 private final static String USER_ADMIN_EPR = "https://localhost:9443/services/UserAdmin";

 /**
  * @param args
  */
 public static void main(String[] args) {

  try {
   QName serviceName = null;
   QName portName = null;
   Service service = null;
   Dispatch<SOAPMessage> dispatch = null;
   BindingProvider provider = null;
   SOAPElement operation = null;
   Map<String, Object> headers = null;
   Map<String, List<String>> reqHeaders = null;
   List<String> cookieList = null;
   SOAPMessage response = null;
   SOAPMessage request = null;
   SOAPPart part = null;
   SOAPEnvelope env = null;
   SOAPBody body = null;
   SOAPElement user = null;

   serviceName = new QName("http://mgt.user.carbon.wso2.org", "UserAdmin");
   portName = new QName("http://mgt.user.carbon.wso2.org", "UserAdminHttpsSoap11Endpoint");

   service = Service.create(serviceName);
   service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, USER_ADMIN_EPR);

   dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
   provider = (BindingProvider) dispatch;

   MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);

   request = mf.createMessage();
   part = request.getSOAPPart();
   env = part.getEnvelope();
   body = env.getBody();

   // We are going to get all the roles belong to the user "admin"
   operation = body.addChildElement("getRolesOfUser", "ns",
     "http://mgt.user.carbon.wso2.org");

   user = operation.addChildElement("userName");
   user.addTextNode("admin");

   request.saveChanges();

   // We can't just invoke the above method - since it belongs to an admin service. First
   // we need to authenticate with AuthenticationAdmin and get the authenticated cookie.
   // Then we need to add that cookie to the HTTP header of the request.
   headers = provider.getRequestContext();
   reqHeaders = (Map) headers.get(MessageContext.HTTP_REQUEST_HEADERS);

   if (reqHeaders == null) {
    reqHeaders = new HashMap<String, List<String>>();
   }

   cookieList = new ArrayList<String>();

   // Let's authenticate as admin/admin and get the cookie.
   cookieList.add(getCookie("admin", "admin"));

   // Need to set this http header as Cookie
   reqHeaders.put("Cookie", cookieList);

   headers.put(MessageContext.HTTP_REQUEST_HEADERS, reqHeaders);
   response = dispatch.invoke(request);

   System.out.println(response.getSOAPBody().getFirstChild().getTextContent());

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private static String getCookie(String userName, String password) throws Exception {

  QName serviceName = null;
  QName portName = null;
  Service service = null;
  Dispatch<SOAPMessage> dispatch = null;
  BindingProvider provider = null;
  SOAPElement operation = null;
  SOAPMessage response = null;
  SOAPMessage request = null;
  SOAPPart part = null;
  SOAPEnvelope env = null;
  SOAPBody body = null;
  SOAPElement user = null;
  SOAPElement pwd = null;

  SOAPElement remoteAddr = null;

  serviceName = new QName("http://authentication.services.core.carbon.wso2.org",
    "AuthenticationAdmin");
  portName = new QName("http://authentication.services.core.carbon.wso2.org",
    "AuthenticationAdminHttpsSoap11Endpoint");

  service = Service.create(serviceName);
  service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, AUTH_ADMIN_EPR);

  dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);

  provider = (BindingProvider) dispatch;

  MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);

  request = mf.createMessage();
  part = request.getSOAPPart();

  env = part.getEnvelope();
  body = env.getBody();

  operation = body.addChildElement("login", "ns",
    "http://authentication.services.core.carbon.wso2.org");

  user = operation.addChildElement("username");
  user.addTextNode(userName);

  pwd = operation.addChildElement("password");
  pwd.addTextNode(password);

  remoteAddr = operation.addChildElement("remoteAddress");
  remoteAddr.addTextNode("wso2.org");

  request.saveChanges();

  response = dispatch.invoke(request);

  if ("true".equals(response.getSOAPBody().getFirstChild().getTextContent())) {
   List cookieList = null;
   Map respHeaders = null;
   Map<String, Object> headers = null;

   headers = provider.getResponseContext();
   respHeaders = (Map) headers.get(MessageContext.HTTP_RESPONSE_HEADERS);
   // This is the JSESSIONID cookie.
   cookieList = (List) respHeaders.get("Set-cookie");
   return (String) cookieList.get(0);
  } else {
   return null;
  }

 }
}

Writing a JAX-WS client to an Axis2 web service

1. Deploying the Axis2 Service

Download and launch Apache Axis2 - it comes with a sample web service which is by default available at http://localhost:8080/axis2/services/Version - we will be using this as the service to write our JAX-WS client.

2. JAX-WS

The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services. It is part of the Java EE platform from Sun Microsystems. Like the other Java EE APIs, JAX-WS uses annotations, introduced in Java SE 5, to simplify the development and deployment of web service clients and endpoints.The Reference Implementation of JAX-WS is developed as an open source project and is part of project GlassFish.

3. Axis2 and JAX-WS

http://axis.apache.org/axis2/java/core/docs/jaxws-guide.html

4. JAX-WS client

First we need to generate the Stub classes for the service we are going to invoke. This can be done using the wsimport tool. The tool is available at [JAVA_HOME]\bin distribution.
$ pwd
/Users/prabath/blog/jax-ws
$ wsimport -p org.wso2 http://localhost:8080/axis2/services/Version?wsdl
The above resulted with the following error.
[ERROR] A class/interface with the same name "wso2.Exception" is already in use. Use a class customization to resolve this conflict.
line 11 of http://localhost:8080/axis2/services/Version?wsdl
So I got to hand edit the wsdl and save it as wsdl.xml - and run wsimport against wsdl.xml
$ pwd
/Users/prabath/blog/jax-ws
$ wsimport -p org.wso2 wsdl.xml
$ jar -cvf version-stub.jar *
Following is the client code - and you need to have version-stub.jar, which was generated in the previous step.
package jax.ws.client;

import javax.xml.ws.WebServiceRef;

import org.wso2.Version;
import org.wso2.VersionPortType;

public class VersionClient {
 @WebServiceRef(wsdlLocation = "http://localhost:8080/axis2/services/Version?wsdl")

 public static void main(String[] args) {
  try {
   Version service = new Version();
   VersionPortType port = service.getVersionHttpSoap11Endpoint();
   String response = port.getVersion().getReturn().getValue();
   System.out.println(response);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}