WSO2 – How to call admin services

Posted: October 3, 2013 in java, linux, ubuntu, wso2
Tags: ,
  • This post will describe basic steps in calling an admin service using an axis2 client. Note that if you want to invoke admin services using SOAP UI, please fallow the post by Charitha on Invoking WSO2 Carbon admin services with soapUI
    Let’s say we want to call the admin service name “ProvisioningAdminService” which will give us the set of installed features of the carbon server.

A call to an admin service should be authenticated first. So we call the AuthenticationAdmin admin service first to get a authentication session cookie. We add this session cookie to the subsequent requests so they will be authenticated.

private static String SEVER_URL = "https://backend.appserver.ec2-test.wso2.com/services/";
private static String USER_NAME = "admin";private static String PASSWORD = "admin";
String AUTHENTICATION_ADMIN_SERVICE_URL = "AuthenticationAdmin";

//Create an instance of a Authentication stub
AuthenticationAdminStub authenticationStub = new AuthenticationAdminStub(SEVER_URL +  AUTHENTICATION_ADMIN_SERVICE_URL);

// log in with the username=admin and password=admin.
// They are the default username, passowrd of carbon super admin user.
boolean loginStatus = authenticationStub.login(USER_NAME, PASSWORD,(new URL(SEVER_URL)).getHost());
System.out.println("login " + loginStatus);

ServiceContext serviceContext = authenticationStub._getServiceClient()
.getLastOperationContext()
.getServiceContext();

//Let's get the session cookie string of the last call to authentication admin service. Subsequent requests will be authenticated only if the request contains this session cookie header.
String sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);

// Let's try to call ProvisioningAdminService admin service.
serviceEndPoint = SEVER_URL + "ProvisioningAdminService";
ProvisioningAdminServiceStub provisioningAdminService = new ProvisioningAdminServiceStub(serviceEndPoint);

ServiceClient client2 = provisioningAdminService._getServiceClient();
Options option2 = client2.getOptions();
option2.setManageSession(true);

// set the session cookie of the previous call to AuthenticationAdmin service
option2.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, sessionCookie);

//print the installed services
ProvisioningAdminServiceStub.Feature [] features;
features = provisioningAdminService.getAllInstalledFeatures();
for (ProvisioningAdminServiceStub.Feature f : features) {
     System.out.println(f.getFeatureName());
}

}

If you didn’t set the sessionManage to true for the request to the ProvisioningAdminService you will get the following error since your request does not carries the authentication session cookie.

org.apache.axis2.AxisFault: Transport error: 401 Error: Unauthorized
at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:308)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:194)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:450)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:277)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:398)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:224)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
at org.wso2.carbon.feature.mgt.services.prov.ProvisioningAdminServiceStub.getAllInstalledFeatures(ProvisioningAdminServiceStub.java:2132)
at org.wso2.carbon.user.mgt.sample.SampleUserRoleMgtClient.main(SampleUserRoleMgtClient.java:148)

Following are recommended for a comprehensive knowledge on WSO2 authentication.
Authentication and Authorization Choices in WSO2 Platform 
Securing Web Service Integration
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s