Friday, September 30, 2011

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("javax.net.ssl.trustStore", "src/test/resources/wso2carbon.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");

// first authenticate the client.
AuthenticationAdminServiceStub authenticationStub =
new AuthenticationAdminServiceStub("https://10.100.1.104:9443/services/AuthenticationAdminService");
authenticationStub._getServiceClient().getOptions().setManageSession(true);
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("https://10.100.1.104:9443/services/ResourceAdminService");
stub._getServiceClient().getOptions().setManageSession(true);
stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(60000000);
// create a path to add new resource
String path = "/testfolder/axis2.zip";
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");
response.writeTo(fileOutputStream);
fileOutputStream.flush();
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
} catch (java.rmi.RemoteException e) {
e.printStackTrace();
} catch (org.wso2.carbon.registry.mgt.ui.resource.services.ExceptionException0 exceptionException0) {
exceptionException0.printStackTrace();
} catch (org.wso2.carbon.core.services.authentication.AuthenticationExceptionException0 authenticationExceptionException0) {
authenticationExceptionException0.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

No comments:

Post a Comment