There are several libraries that allow Java access to REST resources. The code shown below is based on the Apache HTTPClient.
package com.cisco.ncs.nbi;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class SampleClient {
/**
* @param args
*/
public static void main(String[] args) {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("ncs-xmp-build-2", 443),
new UsernamePasswordCredentials("root", "Public123"));
SchemeSocketFactory sf;
try {
sf = new SSLSocketFactory(new TrustSelfSignedStrategy());
Scheme https = new Scheme("https", 443, sf);
httpclient.getConnectionManager().getSchemeRegistry()
.register(https);
HttpGet httpget = new HttpGet(
"https://ncs-xmp-build-2/webacs/api/v1/data/InventoryDetails");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
httpclient.getConnectionManager().shutdown();
}
}
The code above allows the use of a self signed certificate in Prime Infrastructure. Production code should not have this enabled.