Revisited REST interface

This commit is contained in:
Luca Frosini 2019-08-02 17:50:39 +02:00
parent f38f846ebe
commit 8fdda4e3e8
2 changed files with 75 additions and 30 deletions

View File

@ -0,0 +1,14 @@
package org.gcube.vremanagement.executor.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.HttpMethod;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PURGE")
public @interface PURGE {
}

View File

@ -11,10 +11,14 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.vremanagement.executor.ResourceInitializer;
import org.gcube.vremanagement.executor.annotation.PURGE;
import org.gcube.vremanagement.executor.api.rest.RestConstants;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.exception.ExecutorException;
@ -39,25 +43,50 @@ public class RestSmartExecutor {
private static Logger logger = LoggerFactory.getLogger(RestSmartExecutor.class);
private static final String UUID_PATH_PARAM = "uuid";
private static final String PLUGIN_NAME_PATH_PARAM = "pluginName";
private static final String UUID_PATH_PARAM = "UUID";
private static final String PLUGIN_NAME_PATH_PARAM = "NAME";
protected static final String LOCATION_HEADER = "Location";
@Context
protected UriInfo uriInfo;
protected void setCalledMethod(String method) {
logger.trace("Going to set calledMethod as {}", method);
CalledMethodProvider.instance.set(method);
logger.info("{}", uriInfo.getAbsolutePath());
}
protected ResponseBuilder addLocation(ResponseBuilder responseBuilder, String id) {
return responseBuilder.header(LOCATION_HEADER,
String.format("%s/%s", uriInfo.getAbsolutePath().toString(), id));
}
@GET
@Path("")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String list() throws ExecutorException {
CalledMethodProvider.instance.set(HttpMethod.GET + " /" + RestConstants.PLUGINS_PATH_PART);
public String listPlugins() throws ExecutorException {
setCalledMethod(HttpMethod.GET.getClass().getSimpleName() + " /" + RestConstants.PLUGINS_PATH_PART);
return "[]";
}
@GET
@Path("/{" + PLUGIN_NAME_PATH_PARAM + "}/" + RestConstants.EXECUTIONS_PATH_PART)
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String listExecutions(@PathParam(PLUGIN_NAME_PATH_PARAM) String pluginName) throws ExecutorException {
setCalledMethod(HttpMethod.GET.getClass().getSimpleName() + " /" + RestConstants.PLUGINS_PATH_PART + "/"
+ pluginName + "/" + RestConstants.EXECUTIONS_PATH_PART);
return "[]";
}
@POST
@Path("/{" + PLUGIN_NAME_PATH_PARAM + "}")
@Path("/{" + PLUGIN_NAME_PATH_PARAM + "}/" + RestConstants.EXECUTIONS_PATH_PART)
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
@Produces(MediaType.TEXT_PLAIN)
public String launch(@PathParam(PLUGIN_NAME_PATH_PARAM) String pluginName, String launchParameterString)
throws ExecutorException {
CalledMethodProvider.instance
.set(HttpMethod.POST + " /" + RestConstants.PLUGINS_PATH_PART + "/" + PLUGIN_NAME_PATH_PARAM);
setCalledMethod(HttpMethod.POST.getClass().getSimpleName() + " /" + RestConstants.PLUGINS_PATH_PART + "/"
+ pluginName + "/" + RestConstants.EXECUTIONS_PATH_PART);
try {
logger.info("Requested to launch {} ({})", pluginName, launchParameterString);
@ -89,23 +118,23 @@ public class RestSmartExecutor {
}
@GET
@Path("/{" + PLUGIN_NAME_PATH_PARAM + "}" + "/" + "{" + UUID_PATH_PARAM + "}")
@Path("/{" + PLUGIN_NAME_PATH_PARAM + "}/" + RestConstants.EXECUTIONS_PATH_PART + "/" + "{" + UUID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String getPluginStateEvolution(@PathParam(PLUGIN_NAME_PATH_PARAM) String pluginName,
@PathParam(UUID_PATH_PARAM) String executionIdentifier,
@QueryParam(RestConstants.ITERATION_NUMBER_PARAM) Integer iterationNumber) throws ExecutorException {
@QueryParam(RestConstants.ITERATION_PARAM) Integer iteration) throws ExecutorException {
CalledMethodProvider.instance.set(HttpMethod.GET + " /" + RestConstants.PLUGINS_PATH_PART + "/"
+ PLUGIN_NAME_PATH_PARAM + "/" + UUID_PATH_PARAM);
setCalledMethod(HttpMethod.GET.getClass().getSimpleName() + " /" + RestConstants.PLUGINS_PATH_PART + "/"
+ pluginName + "/" + RestConstants.EXECUTIONS_PATH_PART + "/{" + UUID_PATH_PARAM + "}");
PluginStateEvolution pluginStateEvolution = null;
try {
SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory
.getPersistenceConnector();
pluginStateEvolution = persistenceConnector.getPluginInstanceState(UUID.fromString(executionIdentifier),
iterationNumber);
iteration);
logger.info("{} for {} (iteration n. {}) is {}", PluginStateEvolution.class.getSimpleName(),
executionIdentifier, iterationNumber, pluginStateEvolution);
executionIdentifier, iteration, pluginStateEvolution);
} catch(ExecutorException e) {
throw e;
} catch(Exception e) {
@ -127,28 +156,32 @@ public class RestSmartExecutor {
}
@DELETE
@Path("/{" + PLUGIN_NAME_PATH_PARAM + "}" + "/" + "{" + UUID_PATH_PARAM + "}")
@Path("/{" + PLUGIN_NAME_PATH_PARAM + "}/" + RestConstants.EXECUTIONS_PATH_PART + "/" + "{" + UUID_PATH_PARAM + "}")
public boolean delete(@PathParam(PLUGIN_NAME_PATH_PARAM) String pluginName,
@PathParam(UUID_PATH_PARAM) String executionIdentifier,
@QueryParam(RestConstants.GLOBALLY_PARAM) Boolean globally) throws ExecutorException {
CalledMethodProvider.instance.set(HttpMethod.DELETE + " /" + RestConstants.PLUGINS_PATH_PART + "/"
+ PLUGIN_NAME_PATH_PARAM + "/" + UUID_PATH_PARAM);
@QueryParam(RestConstants.UNSCHEDULE_PARAM) Boolean unschedule) throws ExecutorException {
if(unschedule) {
setCalledMethod(PURGE.class.getSimpleName() + " /" + RestConstants.PLUGINS_PATH_PART + "/" + pluginName
+ "/" + RestConstants.EXECUTIONS_PATH_PART + "/{" + UUID_PATH_PARAM + "}");
} else {
setCalledMethod(HttpMethod.DELETE.getClass().getSimpleName() + " /" + RestConstants.PLUGINS_PATH_PART + "/"
+ pluginName + "/" + RestConstants.EXECUTIONS_PATH_PART + "/{" + UUID_PATH_PARAM + "}");
}
try {
if(globally == null) {
globally = false;
if(unschedule == null) {
unschedule = false;
}
logger.info("Requested to delete for {} with UUID {} {}", pluginName, executionIdentifier,
globally ? RestConstants.GLOBALLY_PARAM : "");
logger.info("Requested to delete for {} with UUID {}{}", pluginName, executionIdentifier,
unschedule ? "globally" : "");
boolean currentStopped = true;
try {
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory
.getSmartExecutorScheduler();
UUID uuid = UUID.fromString(executionIdentifier);
smartExecutorScheduler.stop(uuid, globally);
smartExecutorScheduler.stop(uuid, unschedule);
} catch(SchedulerNotFoundException e) {
// currentStopped = true;
logger.error("Error unscheduling task {}", executionIdentifier, e);
@ -175,15 +208,13 @@ public class RestSmartExecutor {
} catch(Exception e) {
throw new ExecutorException(e);
}
}
@GET
@Path(RestConstants.SCHEDULED_PATH_PART)
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String all(@QueryParam(RestConstants.GLOBALLY_PARAM) Boolean globally) throws ExecutorException {
CalledMethodProvider.instance.set(HttpMethod.GET + " /" + RestConstants.SCHEDULED_PATH_PART);
return "[]";
@PURGE
@Path("/{" + PLUGIN_NAME_PATH_PARAM + "}/" + RestConstants.EXECUTIONS_PATH_PART + "/" + "{" + UUID_PATH_PARAM + "}")
public boolean purge(@PathParam(PLUGIN_NAME_PATH_PARAM) String pluginName,
@PathParam(UUID_PATH_PARAM) String executionIdentifier) throws ExecutorException {
return delete(pluginName, executionIdentifier, true);
}
}