Creating a new Rest API custom endpoint
Service creation
In a plugin, create the following class :
import java.util.HashSet;
import java.util.Set;
import com.noheto.api.basic.BasicException;
import com.noheto.extensions.interfaces.services.AbstractPluginBasicApiService;
import com.noheto.api.basic.BasicServiceAction;
public class HelloWorldRestApi extends AbstractPluginBasicApiService {
@Override
public Set<BasicServiceAction> getActions() {
Set<BasicServiceAction> lst = new HashSet<BasicServiceAction>();
lst.add(new GetHelloWorldAction());
lst.add(new EchoAction());
return lst;
}
@Override
public void prepare() throws BasicException {
}
@Override
public String getMapping() {
return "/edocs/example/rest/*";
}
}
Create the action GetHelloWorldAction:
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.noheto.api.basic.BasicParameterAction;
import com.noheto.api.basic.BasicServiceAction;
import com.noheto.plugins.IPlugin;
public class GetHelloWorldAction extends BasicServiceAction {
@Override
public void execute(IPlugin plugin, HttpServletRequest request, HttpServletResponse response) throws Throwable {
response.getWriter().println("Hello World!");
}
@Override
public String getAcceptPathInfo() {
return "/getHelloWorld";
}
@Override
public String getHelpMethod() {
return "Retourne HelloWorld";
}
@Override
public List<BasicParameterAction> getExposedParameters() {
return null;
}
@Override
public Iterable<String> getAcceptedRequestMethods() {
return Arrays.asList("GET","POST");
}
}
Create the action EchoAction:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.noheto.api.basic.BasicParameterAction;
import com.noheto.api.basic.BasicServiceAction;
import com.noheto.plugins.IPlugin;
public class EchoAction extends BasicServiceAction {
@Override
public void execute(IPlugin plugin, HttpServletRequest request, HttpServletResponse response) throws Throwable {
String message = checkMandatoryParameter(request, "message");
response.getWriter().println(message);
String complement = getParameter(request, "complement", null);
if (complement!=null) {
response.getWriter().println(complement);
}
}
@Override
public String getAcceptPathInfo() {
return "/echo";
}
@Override
public String getHelpMethod() {
return "Retourne le message indiqué et le complément si il y en a un.";
}
@Override
public List<BasicParameterAction> getExposedParameters() {
List<BasicParameterAction> params = new ArrayList<BasicParameterAction>();
params.add(BasicParameterAction.create().setName("message").setMandatory(true).setDescription("Quel message retourner ?"));
params.add(BasicParameterAction.create().setName("complement").setMandatory(false).setDescription("Message complémentaire ?"));
return params;
}
@Override
public Iterable<String> getAcceptedRequestMethods() {
return Arrays.asList("GET","POST");
}
}
Deploy the plugin
Show the documentation
To display all possible orders of this service and the documentation for each, you need to call the URL declared in the service's mapping.
Call the action GetHelloWorld
Cal the action Echo
Secure the service
Authorize calls only by a app user
To ensure that the service is only usable by an authenticated application user, it is necessary to modify the service to implement the method "isSecuredByApplicatifUser()".
The call URL will the be;
Authorize calls only by an admin user
To ensure that the service is only usable by an authenticated administrative user, it is necessary to modify the service to implement the method "getAcceptedAdminUserRoles()".
The URL will the be (with an admin login/pass ):
Next steps
You can add as many actions as you want. For each action, you can secure the acceptable HTTP methods. For example, the previous actions only accept POST or GET, if you try a call with -XPUT, it will be rejected.
You can just as easily accept the PUT method.