REST sample code using Java class
Posted by Chris Kuhlman, Last modified by Aly Essa on 04 August 2016 12:02 PM

The sample provided below is a Java class, which performs REST calls to Central to accomplish its work.  

You don't need Java to make the calls; this could have been a javascript or C# .NET application.

To make this work, the customer will need to include JAR files found within Central.

JERSEY classes (to make REST calls):

  • jersey-core-1.13.jar
  • jersey-client-1.13.jar
  • jackson-core-asl-1.9.2.jar
  • jersey-json-1.13.jar

FileCatalyst classes (which contain some application logic):

  • FileCatalystCentral.jar

All required files are found in the /lib folder of Central, wherever it is installed.

 


 

package filcatalystsample;

import java.util.ArrayList;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;

import unlimited.fc.monitor.agent.AgentBase.ProductType;
import unlimited.fc.rest.central.model.ModelHelper;
import unlimited.fc.rest.central.model.NodeModel;
import unlimited.fc.rest.central.model.NodesModel;
import unlimited.fc.rest.central.model.TasksModel;
import unlimited.fc.rest.central.type.ConnectionStatusType;
import unlimited.fc.rest.client.WebResourceConnector;
import unlimited.fc.rest.hotfolder.model.TaskModel;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;
import com.sun.jersey.core.util.Base64;


/**
* Class to demonstrate the REST Tasks services
*/
public class TaskModificationSampleCode {
private String authorization;
private WebResourceConnector webResourceConnector;

public TaskModificationSampleCode(String username, String password, String centralHost, int centralPort) {
authorization = new String(Base64.encode(username + ":" + password));
webResourceConnector = new WebResourceConnector(centralHost, centralPort);
}

/**
* Returns a TasksModel containing all of the tasks from a particular HotFolder NodeModel.
*
* @param hfNode
* @return TasksModel object
* @throws Exception if the ModelHelper is unable to process the returned json or if the hfNode is not a HotFolder.
*/
public TasksModel getTasks(NodeModel hfNode) throws Exception {
if (isHotFolder(hfNode)) {
WebResource resource = webResourceConnector.getWebResource(hfNode.getHref().getPath() + "/hf/tasks");
Builder builder = resource.accept(MediaType.APPLICATION_JSON).header("RESTAuthorization", authorization);
ClientResponse response = builder.get(ClientResponse.class);
String responseDoc = response.getEntity(String.class);
TasksModel nodes = ModelHelper.toModelFromJSONString(TasksModel.class, responseDoc);
return nodes;
}
else {
throw new Exception(hfNode.getNodeName() + " is not a HotFolder");
}
}

/**
* Posts a task to a HotFolder. This will set the task values of the task with the matching Task ID to the values of
* the task.
*
* @param hotFolderNode: host Hotfolder
* @param task: new task values. Must have a Task ID that matches a Task in the hot folder.
* @throws Exception if the status returned is not OK.
*/
public void updateTask(NodeModel hotFolderNode, TaskModel task) throws Exception {
WebResource resource = webResourceConnector.getWebResource(hotFolderNode.getHref().getPath() + "/hf/tasks/" + task.getTaskId());
Builder builder = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).header("RESTAuthorization", authorization);
ClientResponse response = builder.post(ClientResponse.class, task);
if (!(Status.OK.getStatusCode() == response.getStatus())) {
throw new Exception("Trying to update task id = " + task.getTaskId() + "returned status: " + response.getStatus());

}
}

/**
* Gets an ArrayList containing all of the HotFolder node models returned by the Central Server.
* @return
* @throws Exception
*/
public ArrayList<NodeModel> getAllHotFolders() throws Exception {
ArrayList<NodeModel> hotFolderNodes = new ArrayList<NodeModel>();
WebResource resource = webResourceConnector.getWebResource("/rs/nodes");
WebResource.Builder builder = resource.accept(MediaType.APPLICATION_JSON).header("RESTAuthorization", authorization);

ClientResponse response = builder.get(ClientResponse.class);
String responseDoc = response.getEntity(String.class);

NodesModel nodes = ModelHelper.toModelFromJSONString(NodesModel.class, responseDoc);
for (NodeModel node : nodes.getAaData()) {
if (isActive(node) && isHotFolder(node) ) {
hotFolderNodes.add(node);
}
}

return hotFolderNodes;
}

private boolean isHotFolder(NodeModel node) {
return node.getNodeType().equals(ProductType.HOTFOLDER.toString());
}
private boolean isActive(NodeModel node) {
return node.getConnectionStatus().equals(ConnectionStatusType.ACTIVE);
}

public void updateAllAccessableTasksToNewPostURL(String newPostURL) throws Exception {

ArrayList<NodeModel> hotFolders = getAllHotFolders();
System.out.println("hotfolder Count: " + hotFolders.size() );
for (NodeModel hotFolder : hotFolders) {
TasksModel allTasks = getTasks(hotFolder);
System.out.println("hotfolder "+ hotFolder.getNodeName()+" task Count: " + allTasks.getAaData().size());
for (TaskModel task : allTasks.getAaData()) {
task.setPostURL(newPostURL);
updateTask(hotFolder, task);
System.out.println("setPostURL for task "+ task.getTaskId()+ ":" + task.getTaskName());
}
}

}

/**
* Sample main method. Log into the localhost Central server, and modify all HotFolder tasks to have a new
* PostURL value of "http://transferAgent.com" for all connected HotFolders.
*
*/
public static void main(String[] args) {
TaskModificationSampleCode tmsc = new TaskModificationSampleCode("admin", "admin", "localhost", 8080);
try {
tmsc.updateAllAccessableTasksToNewPostURL("http://transferAgent.com");
} catch (Exception e) {
e.printStackTrace();
}
}
}