Monday, February 16, 2009

Accessing WSO2 products back end directly

Recently wso2 release a set of products which address all aspects of any SOA solution. All these products are build upon a platform called WSO2 carbon which is base on OSGI. One of the common features of them is to ability to run as Front End (FE) and Back End (BE) services. FE server contains all the GUI components and BE server expose the back end functionality as web services. Therefore in a typical Adminconsole usage scenario client application runs on the browser makes servlet request to FE server and FE server makes a web service call to BE server.
As it can be seen it is possible to access these BE serer web services directly using web service clients generated using the respective WSDL files for the service. The following example code provides a code for such a client which try to directly access the WS02 registry back end services to put a resource and retrieve it back. Generally this code segment show how one can access a service using https transport and mange cookies between many stubs.

try {

// set the system properties to enable the https conection
System.setProperty("", "src/test/resources/wso2carbon.jks");
System.setProperty("", "wso2carbon");

// first authenticate the client.
AuthenticationAdminServiceStub authenticationStub =
new AuthenticationAdminServiceStub("");
boolean loggedIn = authenticationStub.login("admin", "admin", NetworkUtils.getLocalHostname());

// get the cooke to use in the next service invoations. This lets registry service to authenticate
// the second request
ServiceContext serviceContext = authenticationStub._getServiceClient().getLastOperationContext().getServiceContext();
String sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
// print the cookie
System.out.println("session Cookie " + sessionCookie);

// doing the registry update
ResourceAdminServiceStub stub = new ResourceAdminServiceStub("");
// create a path to add new resource
String path = "/testfolder/";
String mediaType = "application/xml";
String description = "test service";
// enable MTOM and set the previous cookie
stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
stub._getServiceClient().getOptions().setProperty(HTTPConstants.COOKIE_STRING, sessionCookie);
DataHandler dataHandler = new DataHandler(new FileDataSource("/home/amila/services.xml"));
// adding the resource
stub.addResource(path, mediaType, description, dataHandler);

// getting content
ContentDownloadBean contentDownloadBean = stub.getContentDownloadBean("/testfolder/testxml3");
DataHandler response = contentDownloadBean.getContent();
FileOutputStream fileOutputStream = new FileOutputStream("/home/amila/new_services2.xml");
} catch (AxisFault axisFault) {
} catch (java.rmi.RemoteException e) {
} catch ( exceptionException0) {
} catch ( authenticationExceptionException0) {
} catch (SocketException e) {
} catch (FileNotFoundException e) {
} catch (IOException e) {