CommandLine - ninazeina/SXP GitHub Wiki
What is this?
You may want to test your module at run-time, by throwing commands at it and see what they return.
But these commands may not be accessible via the current javascript GUI. And it may be useless, and lengthy, to develop a GUI for that.
There is a faster way: you can quickly set up things so that the method may be called directly, manually, at run-time, with whatever arguments. This is what we call command-line.
Actually, you will type your method calls in the browser, as a query. The rest module will map the query to your ModuleCommander, which in turn will make the appropriate instantiations and the call to the method to be invoked, to give you back a String.
In what follows, we show, as an example, how to set up a direct access to the hash function of the crypt module.
Creating a CryptCommander
If you are using the whole SXP application, put the following CryptCommander in the controller module and that's it.
package controller;
import javax.ws.rs.GET; //REST-related dependencies
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import crypt.api.hashs.Hasher; //module to test dependencies
import crypt.factories.HasherFactory;
import rest.api.ServletPath;
@ServletPath("/command/hash/*") //url path. PREFIX WITH COMMAND/ !!!
@Path("/")
public class CryptCommander {
@GET
@Path("/{input}") //a way to name the pieces of the query
public String hash(@PathParam("input") String input) { //this argument will be initialized with the piece of the query
Hasher hasher = HasherFactory.createDefaultHasher();
return new String(hasher.getHash(input.getBytes()));
}
}
Otherwise, if you are really working on a single module, independently of SXP, place it in src/main/java/crypt/CryptCommander.java. Then the first line must be package crypt.
In your main, possibly within CryptCommander.java itself, you must launch a REST server via
public static void main(String args[]) {
RestServerApi.createAndStartRestServer("jetty", 8080,"crypt");
}
In the build.gradle, make sure you have updated to you new main
mainClassName="crypt.CryptCommander.java"
Use your new command via a browser
Type as URL:
localhost:8080/command/hash/helloworld