context-manager/src/main/java/org/gcube/vremanagement/contextmanager/services/ContextService.java

161 lines
4.6 KiB
Java
Raw Normal View History

2020-12-03 14:24:34 +01:00
package org.gcube.vremanagement.contextmanager.services;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
2020-12-04 19:57:56 +01:00
import javax.ws.rs.Consumes;
2020-12-03 14:24:34 +01:00
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
2020-12-23 10:42:07 +01:00
import javax.ws.rs.POST;
2020-12-03 14:24:34 +01:00
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
2020-12-03 18:13:34 +01:00
import javax.ws.rs.WebApplicationException;
2020-12-03 14:24:34 +01:00
import javax.ws.rs.core.MediaType;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.smartgears.annotations.ManagedBy;
2020-12-23 10:42:07 +01:00
import org.gcube.vremanagement.contextmanager.ContextManager;
import org.gcube.vremanagement.contextmanager.ContextServiceAppManager;
2020-12-03 18:13:34 +01:00
import org.gcube.vremanagement.contextmanager.ResourceManager;
2020-12-14 20:32:46 +01:00
import org.gcube.vremanagement.contextmanager.Utils;
import org.gcube.vremanagement.contextmanager.handlers.ContextContainer;
2020-12-23 10:42:07 +01:00
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
2020-12-14 20:32:46 +01:00
import org.gcube.vremanagement.contextmanager.model.types.Context;
2020-12-04 19:57:56 +01:00
import org.gcube.vremanagement.contextmanager.model.types.StringList;
import org.glassfish.jersey.media.multipart.FormDataParam;
2020-12-03 14:24:34 +01:00
import org.slf4j.Logger;
@Path("contexts")
2020-12-23 10:42:07 +01:00
@ManagedBy(ContextServiceAppManager.class)
2020-12-03 14:24:34 +01:00
public class ContextService {
2020-12-23 10:42:07 +01:00
@Inject Logger log;
2020-12-03 14:24:34 +01:00
2020-12-23 10:42:07 +01:00
@RequestScoped
2020-12-03 14:24:34 +01:00
@Inject
2020-12-23 10:42:07 +01:00
ContextServiceAppManager appManager ;
@Inject
ContextManager contextHandler;
2020-12-03 14:24:34 +01:00
@Inject
ResourceManager resourceHandler;
2020-12-14 20:32:46 +01:00
@Inject
ContextContainer contextContainer;
2020-12-03 14:24:34 +01:00
@RequestScoped
2020-12-04 19:57:56 +01:00
@PathParam("contextId")
String contextId;
2020-12-03 14:24:34 +01:00
2020-12-03 18:13:34 +01:00
2020-12-03 14:24:34 +01:00
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("")
2020-12-04 19:57:56 +01:00
public StringList getContexts() {
2020-12-03 18:13:34 +01:00
log.info("get Context called");
try {
2020-12-04 19:57:56 +01:00
List<String> contexts = contextHandler.getAvailableContexts();
2020-12-03 18:13:34 +01:00
log.debug("found {}",contexts);
2020-12-04 19:57:56 +01:00
return new StringList(contexts);
2020-12-03 18:13:34 +01:00
}catch (Exception e) {
2020-12-04 19:57:56 +01:00
log.error("error retrieving contexts",e);
2020-12-03 18:13:34 +01:00
throw new WebApplicationException(e);
}
2020-12-03 14:24:34 +01:00
}
2020-12-23 10:42:07 +01:00
@POST
2020-12-04 19:57:56 +01:00
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/{contextId}")
public void createVREContext(@FormDataParam("resources") StringList resourceIds, @FormDataParam("vreName") String vreName ){
log.info("create VRE called");
2020-12-03 14:24:34 +01:00
try {
2020-12-04 19:57:56 +01:00
//validate Call
2020-12-23 10:42:07 +01:00
contextHandler.createContext(contextId, vreName, resourceIds.getValues());
2020-12-03 14:24:34 +01:00
}catch (Exception e) {
2020-12-04 19:57:56 +01:00
log.error("error creating context {}",contextId,e);
throw new WebApplicationException(e);
2020-12-03 14:24:34 +01:00
}
}
@DELETE
2020-12-04 19:57:56 +01:00
@Path("{contextId}")
public void disposeVREContext() {
log.info("dispose VRE called");
2020-12-03 14:24:34 +01:00
try {
2020-12-04 19:57:56 +01:00
//Context context = contextContainer.getContextById(contextId);
//validateCall(fullContext);
2020-12-23 10:42:07 +01:00
contextHandler.disposeContext(contextId);
2020-12-03 14:24:34 +01:00
}catch (Exception e) {
2020-12-04 19:57:56 +01:00
log.error("error disposing context {}",contextId,e);
throw new WebApplicationException(e);
2020-12-03 14:24:34 +01:00
}
}
@DELETE
2020-12-23 10:42:07 +01:00
@Path("/{contextId:(.*(?=/resources))}/resources/{resourceId}")
2020-12-14 20:32:46 +01:00
public void removeResourceFromContext(@PathParam("resourceId") String resourceId) {
log.info("remove resource {} from context {}", resourceId, contextId);
2020-12-03 14:24:34 +01:00
try {
2020-12-14 20:32:46 +01:00
Context context = contextContainer.getContextById(contextId);
validateCall(Utils.getScopeFromContext(context));
2020-12-03 14:24:34 +01:00
resourceHandler.removeResourceFromContext(context, resourceId);
}catch (Exception e) {
// TODO: handle exception
2020-12-14 20:32:46 +01:00
}
2020-12-03 14:24:34 +01:00
}
2020-12-04 19:57:56 +01:00
2020-12-23 10:42:07 +01:00
2020-12-03 14:24:34 +01:00
@PUT
2020-12-23 10:42:07 +01:00
@Path("/{contextId:(.*(?=/resources))}/resources")
public String addResourceToContext(String resourceId) {
log.info("adding resource {} to context {}", resourceId, contextId);
2020-12-03 14:24:34 +01:00
try {
2020-12-23 10:42:07 +01:00
Context context = contextContainer.getContextById(contextId);
validateCall(Utils.getScopeFromContext(context));
if (appManager.isManaged(context))
resourceHandler.addResourceToContext(context, resourceId);
else {
log.debug("the context {} is not managed", contextId);
//TODO: else manage queue to add resource in another ContextManager
}
2020-12-03 14:24:34 +01:00
}catch (Exception e) {
// TODO: handle exception
2020-12-23 10:42:07 +01:00
}
2020-12-03 14:24:34 +01:00
return null;
}
2020-12-04 19:57:56 +01:00
@GET
@Produces(MediaType.APPLICATION_JSON)
2020-12-23 10:42:07 +01:00
@Path("/{contextId:(.*(?=/resources))}/resources")
2020-12-04 19:57:56 +01:00
public StringList getResources() {
log.info("getResource in context {} called",contextId);
try {
//Context context = contextContainer.getContextById(contextId);
//validateCall(fullContext);
return new StringList(contextHandler.getAssociatedResources(contextId));
}catch (Exception e) {
log.error("error getting resource for context {}",contextId,e);
throw new WebApplicationException(e);
}
}
2020-12-03 14:24:34 +01:00
2020-12-23 10:42:07 +01:00
private void validateCall(String fullContext) throws InvalidContextException {
2020-12-03 14:24:34 +01:00
String currentContext = ScopeProvider.instance.get();
if (!fullContext.startsWith(currentContext))
2020-12-23 10:42:07 +01:00
throw new InvalidContextException("invalid context");
2020-12-03 14:24:34 +01:00
}
}