Calling Admin Services from Apps
WSO2 products are managed internally using SOAP Web services known as admin services. WSO2 products come with a management console UI, which communicates with these admin services to facilitate administration capabilities through the UI.There can be instances where you want to call back-end Web services directly. For example, in test automation, to minimize the overhead of having to change automation scripts whenever a UI change happens, developers prefer to call the underlying services in scripts. The topics below explain how you can discover and invoke these services from your applications.
Discovering the admin services
By default, the WSDLs of admin services are hidden from consumers. Follow the steps given below to discover them using the OSGi console.
Open theÂ
carbon.
xml file (from theÂ<ESB_HOME>/repository/conf
 directory), and set theÂ<HideAdminServiceWSDLs>
 element toÂfalse
.Start the ESB:
Open a terminal and navigate to theÂ
<ESB_HOME>
/bin directory.Execute the product startup script as explained below.
- On Windows:Â
wso2server.bat -DosgiConsole
- On Linux:Â
sh wso2server.sh -DosgiConsole
- On Windows:Â
- When the server starts, press the Enter/Return key on the keyboard several times to get the OSGI shell in the console.
- In the OSGi shell, type the following:
 osgi> listAdminServices
The admin services for your product will be listed on your console. For example, shown below is the list of admin services in the ESB.
Check the list of admin services shown above, and note that the URL of each admin service appears after the admin service's name. See the following example:
ModuleAdminService, ModuleAdminService, https://localhost:8243/services/ModuleAdminService
To see the service contract of an admin service, select the admin service's URL and then paste it into your browser with ?wsdl at the end. For example, if you want to access theÂ
ModuleAdminService
 of the ESB, use the following URL:Âhttps://localhost:8243/services/ModuleAdminService?wsdl
When you are accessing an admin service in the ESB, note that you are using the 8243 port (assuming 0 port offset). However, you should be accessing the admin services via the management console port, which is 9443
After discovering admin service, you can restart the server without
-DosgiConsole.
Invoking an admin service
Admin services are secured using common types of security protocols such as HTTP basic authentication, WS-Security username token, and session-based authentication, to prevent anonymous invocations. For example, the UserAdmin
 Web service is secured with HTTP basic authentication. To invoke an admin service, you can write your own client program using the Axis2 client API, or you can use an existing tool like SoapUI (4.5.1 or later), or wsdl2java.
Invoking an admin service involves the following steps:
- Authenticate yourself and get the session cookie.
- Access the back-end service.
Generating a client program to access admin services
To generate the stubs, you can write your own client program using the Axis2 client API, or use an existing tool like SoapUIÂ (4.5.1 or later), or wsdl2java.
The wsdl2java tool, which comes with WSO2 products by default, hides all the complexity and presents you with a proxy to the back-end service. The stub generation happens during the project build process within the Maven POM files. It uses the Maven ant run plug-in to execute the wsdl2java tool.
You can also use the Java client program given here to invoke admin services. All dependency JAR files that you need to run this client are found in the /lib
 directory.
Authenticate the user
The example code below authenticates the user and gets the session cookie:
import org.apache.axis2.AxisFault; import org.apache.axis2.transport.http.HTTPConstants; import org.wso2.carbon.authenticator.stub.AuthenticationAdminStub; import org.wso2.carbon.authenticator.stub.LoginAuthenticationExceptionException; import org.wso2.carbon.authenticator.stub.LogoutAuthenticationExceptionException; import org.apache.axis2.context.ServiceContext; import java.rmi.RemoteException; public class LoginAdminServiceClient { private final String serviceName = "AuthenticationAdmin"; private AuthenticationAdminStub authenticationAdminStub; private String endPoint; public LoginAdminServiceClient(String backEndUrl) throws AxisFault { this.endPoint = backEndUrl + "/services/" + serviceName; authenticationAdminStub = new AuthenticationAdminStub(endPoint); } public String authenticate(String userName, String password) throws RemoteException, LoginAuthenticationExceptionException { String sessionCookie = null; if (authenticationAdminStub.login(userName, password, "localhost")) { System.out.println("Login Successful"); ServiceContext serviceContext = authenticationAdminStub. _getServiceClient().getLastOperationContext().getServiceContext(); sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING); System.out.println(sessionCookie); } return sessionCookie; } public void logOut() throws RemoteException, LogoutAuthenticationExceptionException { authenticationAdminStub.logout(); } }
To resolve dependency issues, if any, add the following dependency JARs location to the class path: <PRODUCT_HOME>/repository/components/plugins
.
The the AuthenticationAdminStub
class requires org.apache.axis2.context.ConfigurationContext
as a parameter. You can give a null value there.
Generate the client stubs
https://localhost:<port>/services/ServiceAdmin
) in the service.xml
 file in the META-INF
 folder in the respective bundle that you find in the /plugins
 directory.import org.apache.axis2.AxisFault; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.wso2.carbon.service.mgt.stub.ServiceAdminStub; import org.wso2.carbon.service.mgt.stub.types.carbon.ServiceMetaDataWrapper; import java.rmi.RemoteException; public class ServiceAdminClient { private final String serviceName = "ServiceAdmin"; private ServiceAdminStub serviceAdminStub; private String endPoint; public ServiceAdminClient(String backEndUrl, String sessionCookie) throws AxisFault { this.endPoint = backEndUrl + "/services/" + serviceName; serviceAdminStub = new ServiceAdminStub(endPoint); //Authenticate Your stub from sessionCooke ServiceClient serviceClient; Options option; serviceClient = serviceAdminStub._getServiceClient(); option = serviceClient.getOptions(); option.setManageSession(true); option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, sessionCookie); } public void deleteService(String[] serviceGroup) throws RemoteException { serviceAdminStub.deleteServiceGroups(serviceGroup); } public ServiceMetaDataWrapper listServices() throws RemoteException { return serviceAdminStub.listServices("ALL", "*", 0); } }
The following sample code lists the back-end Web services:
import org.wso2.carbon.authenticator.stub.LoginAuthenticationExceptionException; import org.wso2.carbon.authenticator.stub.LogoutAuthenticationExceptionException; import org.wso2.carbon.service.mgt.stub.types.carbon.ServiceMetaData; import org.wso2.carbon.service.mgt.stub.types.carbon.ServiceMetaDataWrapper; import java.rmi.RemoteException; public class ListServices { public static void main(String[] args) throws RemoteException, LoginAuthenticationExceptionException, LogoutAuthenticationExceptionException { System.setProperty("javax.net.ssl.trustStore", "$ESB_HOME/repository/resources/security/wso2carbon.jks"); System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon"); System.setProperty("javax.net.ssl.trustStoreType", "JKS"); String backEndUrl = "https://localhost:9443"; LoginAdminServiceClient login = new LoginAdminServiceClient(backEndUrl); String session = login.authenticate("admin", "admin"); ServiceAdminClient serviceAdminClient = new ServiceAdminClient(backEndUrl, session); ServiceMetaDataWrapper serviceList = serviceAdminClient.listServices(); System.out.println("Service Names:"); for (ServiceMetaData serviceData : serviceList.getServices()) { System.out.println(serviceData.getName()); } login.logOut(); } }