Unified CVP API Client Sample Code
The following code snippet is based on the Jersey client framework.
/**
* Gets a resource or list of resources by calling CVP GET REST API.
* @param url. Resource RL - examples:
* to get a single resource - https://ip:port/cvp-config/snmp/community/Public_Community
* to get all resources - https://ip:port/cvp-config/snmp/community
* to get resources begin with - https://ip:port/cvp-config/snmp/community/Public*
* @return XML or JSON response.
*/
public String get(String url) {
Client client = null;
ClientFilter authFilter = null;
WebResource webResource = null;
ClientResponse res = null;
String jsonResponse = null;
try {
client = getSSLClient();
authFilter = new HTTPBasicAuthFilter(userName, password);
client.addFilter(authFilter);
webResource = client.resource(url);
res = webResource.accept(MediaType.APPLICATION_JSON).get(
ClientResponse.class);
log("get() - response: " + res.toString());
if (res != null && res.getStatus() == 200) {
jsonResponse = res.getEntity(new GenericType<String>() {
});
}
log("get() - response json: " + jsonResponse);
} catch (Exception e) {
e.printStackTrace();
}
return jsonResponse;
}
/**
* Creates a resource.
* @param url: E.g. - https://ip:port/cvp-config/snmp/community/Public_Community
* @return
*/
public String post(String url) {
Client client = null;
ClientFilter authFilter = null;
WebResource webResource = null;
ClientResponse res = null;
String jsonResponse = null;
// Reads json form data.
String jsonInput = readFile("payload.json");
try {
// Gets Jersey Client
client = getSSLClient();
authFilter = new HTTPBasicAuthFilter(userName, password);
client.addFilter(authFilter);
// Gets Web Resource fo the url
webResource = client.resource(url);
// Sends put request.
res = webResource.type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, jsonInput);
log(res.toString());
if (res != null && res.getStatus() == 201) {
jsonResponse = res.getEntity(new GenericType<String>() {
});
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonResponse;
}
/**
* Update a resource.
* @param url: E.g. - https://ip:port/cvp-config/snmp/community/Public_Community
* @return
*/
public String put(String url) {
Client client = null;
ClientFilter authFilter = null;
WebResource webResource = null;
ClientResponse res = null;
String jsonResponse = null;
// Reads json form data.
String jsonInput = readFile("payload.xml");
try {
// Gets Jersey Client
client = getSSLClient();
authFilter = new HTTPBasicAuthFilter(userName, password);
client.addFilter(authFilter);
// Gets Web Resource fo the url
webResource = client.resource(url);
// Sends put request.
res = webResource.type(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_XML)
.put(ClientResponse.class, jsonInput);
log(res.toString());
if (res != null && res.getStatus() == 200) {
jsonResponse = res.getEntity(new GenericType<String>() {
});
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonResponse;
}
/**
* Deletes a resource by calling CVP GET REST API.
* @param url. Resource RL - examples:
* https://ip:port/cvp-config/snmp/community/Public_Community
* @return XML or JSON response.
*/
public String delete(String url) {
Client client = null;
ClientFilter authFilter = null;
WebResource webResource = null;
ClientResponse res = null;
String jsonResponse = null;
try {
client = getSSLClient();
authFilter = new HTTPBasicAuthFilter(userName, password);
client.addFilter(authFilter);
webResource = client.resource(url);
res = webResource.accept(MediaType.APPLICATION_JSON).delete(
ClientResponse.class);
log("delete() - response: " + res.toString());
if (res != null && res.getStatus() == 200) {
jsonResponse = res.getEntity(new GenericType<String>() {
});
}
log("delete() - response json: " + jsonResponse);
} catch (Exception e) {
e.printStackTrace();
}
return jsonResponse;
}