6.3 Viewpoint Server Methods

6.3.1 ViewpointUtilities

The class ViewpointUtilities was made specifically for groovy scripts. It is recommended that most calls to Viewpoint call this class directly instead of calling the underlying class actually doing the work. This is so you don't have to worry about unknown exceptions or other caveats. Viewpoint developers will try to make this class as static as possible between revisions.

6.3.1.1 Execute Request

In the Java-API, there are various classes that implement the Request interface (such as SubmitJobRequest). To execute these requests, you can call "executeRequest". This method has the following signature:

public static List<MoabCommandResults> executeRequest(Request request, ViewpointUser user) throws MoabCommunicationException;

The caller should create and populate a request object, then pass in this object to the method along with the user attempting to execute the request. You should get back a list of command results. This will throw a MoabCommunicationException if there was a problem communicating with Moab. Please note that just because there was no MoabCommunicationException, it does not mean the command was successful. Parse the command results to ensure that the command executed properly.

6.3.1.2 Simple Execute Request

The other option to execute requests without having to worry about exceptions is the simple execute request. This is its method signature:

public static boolean simpleExecuteRequest(Request request, ViewpointUser user);

This is very similar to the ExecuteRequest method call, except this version will not throw an exception and does not return any results. Instead, it returns false if there was an error, or true if everything executed correctly. Callers do not need to parse any results to determine if the request executed properly.

6.3.1.3 Get User

In the core.xml, administrators configure the security Viewpoint should use, along with user permissions and roles. This method allows callers the ability to get the User object associated with any incoming request, with all user permissions and settings. This method has the following signature:

public static ViewpointUser getUserOrFail(HttpServletRequest request, HttpServletResponse response) throws IOException;

As noted in the method name, if there was a problem authenticating the request, the response is populated with a 401 Unauthorized response. The caller does not need to worry about populating the response, but may change the response if they desire. This will.

Once the script contains a ViewpointUser object, the script may check permissions on that object. Here is an example:

def user = ViewpointUtilities.getUserOrFail(request, response); if (!user) return;
if (!user.hasPermission("vc.create")) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "You do not have permission to create environments"); }

6.3.1.4 Get JSON From Request

Many HTTP requests will contain a message that contains JSON. Viewpoint contains a utility method to get the message body from the request and attempt to parse this message to a JSONObject. This is the method signature for this method:

public static JSONObject getRequestJsonOrFail(HttpServletRequest request, HttpServletResponse response) throws IOException;

As noted in the method name, if there was a problem parsing the message, the response is populated with a 400 Bad request response. The caller does not need to worry about populating the response, but may change the response if they desire.