Let’s Create Java Client to Invoke Admin Service of WSO2 EI

Isuru Liyanage
CodeBlog
Published in
4 min readDec 9, 2022

In this blog i’m going to walk-through you on how to create a simple Java client which calls the admin service of the EI server and will get the status of the Inbound-Endpoints in the EI server.

How it works

Our Java Client is going to read a Text file which has the name of the Inbound-Endpoints. After getting the names to the Java client, it will invoke an admin service request to the EI serve to get the status of the particular Inbound-Endpoint. After getting the status of the Inbound-Endpoint, the Java client will write down the name of the Inbound-Endpoint and its status to a new file name “ServiceList.txt”.

System Diagram

Step 1 : Java Project

First let’s create a Java project. I’m using Eclipse IDE to create the Java project.

Step 2 : LoginAdminServiceClient Class

We need to write a code which will authenticate with WSO2 EI admin services because the 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.
So After creating a Java project lets create a new class name “LoginAdminServiceClient”. This class will be used to authenticate with the admin services of EI server.

{if you are getting dependency issues, you can add following dependency JARs location to the class path: <PRODUCT_HOME>/repository/components/plugins.}

Step 3 : InboundAdminServiceClient Class

We have written the class “LoginAdminServiceClient” which will authenticate the user and retrieve the session cookie.
As the step 3, Now we have to use the retrieved admin cookie with the service endpoint URL.So that we can get the status of the Inbound-Endpoint which are in the EI service. The management service name is InboundAdmin.
You can find its URL (e.g., https://localhost:9443/services/InboundAdmin) in the service.xml file in the META-INF folder in the respective bundle that you find in <PRODUCT_HOME>/repository/components/plugins.

Lets create a class named “InboundAdminServiceClient”. This class will use the retrieved admin cookie to get the status of the Inbound-Endpoints in the WSO2 EI server.

Step 4: Main Class

In the Main class we are going read the names of the inbound-endpoint from a text file. Invoke the above two class and get the status of the Inbound-Endpoints and write it down to a text file.

From the below code snippet we are going to read the text file and get the names of the Inbound-Endpoints that we have written in the “Inbound.txt” file.

 BufferedReader bufReader = new BufferedReader(new FileReader("inbound.txt")); 
ArrayList<String> listOfLines = new ArrayList<>();
String line = bufReader.readLine();
while (line != null)
{
listOfLines.add(line); line = bufReader.readLine();
}
bufReader.close();

We are going to input 4 parameters to the JAR when we are running the Java Client.

  1. The path of the wso2carbon.jks file in the EI server
  2. The Password of the wso2carbon.jks keystore
  3. The Username of a user who have access to invoke the admin services
  4. The Password of that user

So from the below code snippet we are going to take that 4 input parameters. We are setting the trustStoreType to JKS and setting the backEndUrl to https://localhost:9443 to invoke the admin services(if there is no offset).

    String TrustStroepath = args[0]; 
String TrustStroepass = args[1];
String Username = args[2];
String Password = args[3];
System.setProperty("javax.net.ssl.trustStore", TrustStroepath);
System.setProperty("javax.net.ssl.trustStorePassword", TrustStroepass);
System.setProperty("javax.net.ssl.trustStoreType", "JKS");
String backEndUrl = "https://localhost:9443";

From the below code snippet we are authenticating the user with provided username/password and creating a session.

LoginAdminServiceClient login = new LoginAdminServiceClient(backEndUrl);
String session = login.authenticate(Username, Password);

Now we are going to loop through the inbound-endpoint names from the array list and get the status of the each inbound-endpoint. At the end we are logging out from the created session.

 for (String name : listOfLines) {         
InboundAdminServiceClient endpointServiceClient = new InboundAdminServiceClient(backEndUrl, session);
boolean status = endpointServiceClient.inboundEndpointStatus(name);

myWriter.write("ServiceName:"+"ID_"+ name+" ");
myWriter.write("ServiceStatus:"+ status);
myWriter.append("\n");

}
myWriter.close();
login.logOut();

below is the full Main.Java code

Step 5 : Export the JAR

Now lets save our work and export the Java project as a runnable JAR. You can do that by right click on Java project and click on export. After that search for runnable JAR format and export it.

Step 6 : Run the JAVA client

First you have to start the EI server and you need to have some inbound-endpoint created in the EI server. You can check if there are inbound endpoint by going in to the Management console and select inbound-endpoint from the right side Menu.

Now you need to add the names of the inbound-endpoints to the file “inbound.txt”. In my example ill be adding the inbound endpoint names “rrr” and “testme” to the file.

After that you can run the Java client using below command

java -jar CheckStatus.jar "<Path to keystore>" "<keystore password>" "<Username>" "<Password>"

Example :

java -jar CheckStatus.jar "/home/isurul/Documents/wso2ei-6.6.0/repository/resources/security/wso2carbon.jks" "wso2carbon" "admin" "admin"

After running the above command a text file named “servicelist.txt” will be generated. This file will contain the inbound-endpoint name with ID_ prefix and the status of the inbound-endpoint.

You can find the full project from here GitHub repository [1]

Hope you have learned new things from this blog. Happy Coding!!!

[1].https://github.com/isurul28/WSO2_AdminService_JavaClient

[2].https://docs.wso2.com/display/EI660/Working+with+Admin+Services

--

--