Wednesday, March 9, 2011

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;
  }

 }
}

0 comments: