Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Service creation

In a plugin, create the following class :

Code Block
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:

Code Block
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:

Code Block
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.

Code Block
curl http://localhost/contextpath/api/edocs/example/rest/

Call the action GetHelloWorld

Code Block
curl http://localhost/contextpath/api/edocs/example/rest/getHelloWorld

Cal the action Echo

Code Block
curl "http://localhost/contextpath/api/edocs/example/rest/echo"
curl "http://localhost/contextpath/api/edocs/example/rest/echo?message=kkk"
curl "http://localhost/contextpath/api/edocs/example/rest/echo?message=kkk&complement=ccc"

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()".

Code Block
@Override
public boolean isSecuredByApplicatifUser() {
    return true;
}

The call URL will the be;

Code Block
curl "http://localhost/contextpath/api/edocs/example/rest/getHelloWorld" -u "login:pass"
curl "http://localhost/contextpath/api/edocs/example/rest/echo" -u "login:pass"

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()".

Code Block
	@Override
	public Set<String> getAcceptedAdminUserRoles() {
		Set<String> lst = new HashSet<String>();
		lst.addAll(Arrays.asList("Administrators", "TestOperators", "..."));
		return lst;
	}

The URL will the be (with an admin login/pass ):

Code Block
curl "http://localhost/contextpath/api/edocs/example/rest/getHelloWorld" -u "login:pass"
curl "http://localhost/contextpath/api/edocs/example/rest/echo" -u "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.