Environment
Novell Operations Center 5.0 Dashboard
NetIQ Operations Center
NetIQ Operations Center
Situation
This sample code demonstrates how to use the Jersey client APIs for connecting to dashboard/NOC server through the RESTful Web services. It provides java class "jerseySample" with the code, and two batch files (DOS/Windows syntax) for compilation and running. Sample code is step-by-step commented, for better understanding.
Resolution
Please do the following:
a) install Web2Connect package on your myMO server. Please see documentation for details.
b) make sure your Web2Connect service is up and running
c) download jersey-bundle-1.17.jar from
https://maven.java.net/service/local/repositories/releases/content/com/sun/jersey/jersey-bundle/1.17/jersey-bundle-1.17.jar
d) read the documentation and study provided library source code
e) modify the sample code below according to your environment ( hard-coded ip, port, user, password )
f) compile the code
g) run compiled class in console/DosBox window
Suggested batch file for compiling the code:
Suggested batch file for running the code:
Here is our Jersey client API sample code:
a) install Web2Connect package on your myMO server. Please see documentation for details.
b) make sure your Web2Connect service is up and running
c) download jersey-bundle-1.17.jar from
https://maven.java.net/service/local/repositories/releases/content/com/sun/jersey/jersey-bundle/1.17/jersey-bundle-1.17.jar
d) read the documentation and study provided library source code
e) modify the sample code below according to your environment ( hard-coded ip, port, user, password )
f) compile the code
g) run compiled class in console/DosBox window
Suggested batch file for compiling the code:
@echo off
set JAVA_HOME=c:\Program Files (x86)\Java\jdk1.6.0_29
set MO_LIB=lib\moweb2cn.jar;lib\mocommon.jar
set JERSEY_LIB=lib\jersey-bundle-1.17.jar
set COMMON=%MO_LIB%;%JERSEY_LIB%
set CMD="%JAVA_HOME%\bin\javac.exe" -Xlint:unchecked -verbose -cp %COMMON% jerseySample.java
echo %CMD%
%CMD%
Suggested batch file for running the code:
@echo off
set JAVA_HOME=c:\Program Files (x86)\Java\jdk1.6.0_29
set MO_LIB=lib\moweb2cn.jar;lib\mocommon.jar
set JERSEY_LIB=lib\jersey-bundle-1.17.jar
set COMMON=.;%MO_LIB%;%JERSEY_LIB%
set CMD="%JAVA_HOME%\bin\java.exe" -cp %COMMON% jerseySample
echo %CMD%
%CMD%
Here is our Jersey client API sample code:
import java.io.IOException;
import java.util.Map;
import java.util.List;
import javax.ws.rs.core.NewCookie;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.WebResource;
public class jerseySample
{
public static String baseUrl = "http://192.168.163.118:8087/moweb2cn/rest";
public static Client client;
public static WebResource resource, resource2;
public static WebResource.Builder builder;
public static ClientResponse response;
public static List<NewCookie> cookies;
public static void main( String args[] )
{
// First we have to create new Jersey client object
System.out.print("\n\nNow creating new Jersey client object..");
try
{
ClientConfig config = new DefaultClientConfig();
// Here we can modify our configuration, if needed
// config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
//config.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, 1000);
//config.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 1000);
client = Client.create(config);
}
catch( ClientHandlerException e )
{
System.out.print("\n\tGot exception: " + e.getMessage());
}
resource = client.resource(baseUrl);
// Next we try to get configuration, we don't need to be authenticated yet
System.out.print("\n\nNow trying to get configuration..");
try
{
response = resource.path("reports").path("config").get(ClientResponse.class);
System.out.print("\n\tResult= " + response.getEntity(String.class));
}
catch( ClientHandlerException e )
{
System.out.print("\n\tGot exception: " + e.getMessage());
}
// Next we try to authenticate ourselves
System.out.print("\n\nNow trying to athenticate..");
try
{
response = resource.path("session").queryParam("username", "admin").queryParam("password", "formula").get(ClientResponse.class);
String s = response.getEntity(String.class);
System.out.println("\n\tResult= " + s);
// We have to retrieve the cookie we received from the server after successful authentication
// This cookie is being sent once only, so we should not miss it !
cookies = response.getCookies();
for (NewCookie c : cookies) System.out.print("\n\tCookie: " + c.toString());
}
catch( ClientHandlerException e )
{
System.out.print("\n\tGot exception: " + e.getMessage());
}
// Next we try to ping the server
// We have to modify our request with cookies we received from the server.
// This is possible (only?) through the RequestBuilder class, not much fun
System.out.print("\n\nNow trying to ping the server..");
try
{
resource2 = resource.path("ping");
builder = resource2.getRequestBuilder();
builder = builder.cookie(cookies.get(0));
response = builder.get(ClientResponse.class);
System.out.print("\n\tResult= " + response.getEntity(String.class));
}
catch( ClientHandlerException e )
{
System.out.print("\n\tGot exception: " + e.getMessage());
}
// Next we try to get user information
System.out.print("\n\nNow trying to get user information..");
try
{
resource2 = resource.path("user");
builder = resource2.getRequestBuilder();
builder = builder.cookie(cookies.get(0));
response = builder.get(ClientResponse.class);
System.out.print("\n\tResult= " + response.getEntity(String.class));
}
catch( ClientHandlerException e )
{
System.out.print("\n\tGot exception: " + e.getMessage());
}
// Next we try to delete our existing session
System.out.print("\n\nNow trying to DELETE our existing session..");
try
{
resource2 = resource.path("session");
builder = resource2.getRequestBuilder();
builder = builder.cookie(cookies.get(0));
response = builder.delete(ClientResponse.class);
System.out.print("\n\tResult= " + response.getEntity(String.class));
}
catch( ClientHandlerException e )
{
System.out.print("\n\tGot exception: " + e.getMessage());
}
// Finally, some cleaning job
System.out.print("\n\nNow deleting Jersey client object..");
try
{
client.destroy();
}
catch( ClientHandlerException e )
{
System.out.print("\n\tGot exception: " + e.getMessage());
}
System.out.print("\n\nFinishing and closing..\n");
}
}