Added activate operation on contexts

This commit is contained in:
Luca Frosini 2024-11-04 11:29:12 +01:00
parent 64a2ced9ec
commit 648fd0a8ae
3 changed files with 88 additions and 1 deletions

View File

@ -263,7 +263,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
if(context.getState().compareTo(ContextState.ACTIVE.getState())!=0) { if(context.getState().compareTo(ContextState.ACTIVE.getState())!=0) {
Set<String> allowedRoles = workingEnvironment.getAllowedRoles(); Set<String> allowedRoles = workingEnvironment.getAllowedRoles();
if(!workingEnvironment.isUserAllowed(allowedRoles)) { if(!workingEnvironment.isUserAllowed(allowedRoles)) {
throw new ForbiddenException("You are not allowed to operate in non " + ContextState.ACTIVE.getState() + " Contexts. Allowed roles are " + Environment.getAllOperationsAllowedRoles()); throw new ForbiddenException("You are not allowed to operate in non " + ContextState.ACTIVE.getState() + " Contexts. Allowed roles are " + allowedRoles);
} }
} }
} }

View File

@ -8,7 +8,9 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotAuthorizedException; import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.WebApplicationException;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException; import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
@ -16,6 +18,9 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.NullNode; import org.gcube.com.fasterxml.jackson.databind.node.NullNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.user.User;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.contexts.reference.ContextState; import org.gcube.informationsystem.contexts.reference.ContextState;
@ -35,8 +40,10 @@ import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.ServerContextCache; import org.gcube.informationsystem.resourceregistry.contexts.ServerContextCache;
import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement; import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement;
import org.gcube.informationsystem.resourceregistry.environments.Environment; import org.gcube.informationsystem.resourceregistry.environments.Environment;
import org.gcube.informationsystem.resourceregistry.environments.Environment.PermissionMode;
import org.gcube.informationsystem.resourceregistry.environments.contexts.ContextEnvironment; import org.gcube.informationsystem.resourceregistry.environments.contexts.ContextEnvironment;
import org.gcube.informationsystem.resourceregistry.environments.instances.InstanceEnvironment; import org.gcube.informationsystem.resourceregistry.environments.instances.InstanceEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.queries.operators.ComparisonOperator; import org.gcube.informationsystem.resourceregistry.queries.operators.ComparisonOperator;
import org.gcube.informationsystem.resourceregistry.queries.operators.LogicalOperator; import org.gcube.informationsystem.resourceregistry.queries.operators.LogicalOperator;
import org.gcube.informationsystem.resourceregistry.rest.requests.RequestUtility; import org.gcube.informationsystem.resourceregistry.rest.requests.RequestUtility;
@ -642,4 +649,59 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// Nothing to do // Nothing to do
} }
public void activate() throws ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
Environment environment = getWorkingEnvironment();
Set<String> allowedRoles = workingEnvironment.getAllowedRoles();
if(!workingEnvironment.isUserAllowed(allowedRoles)) {
throw new ForbiddenException("You are not allowed to operate in non " + ContextState.ACTIVE.getState() + " " + Context.NAME +". Allowed roles are " + allowedRoles);
}
oDatabaseDocument = environment.getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin();
setAsEntryPoint();
setOperation(Operation.UPDATE);
try {
getElement().setProperty(Context.STATE, ContextState.ACTIVE.getState());
} catch(NotFoundException e) {
throw e;
}
oDatabaseDocument.commit();
// TODO Notify to subscriptionNotification
} catch(ResourceRegistryException e) {
logger.error("Unable to activate {} with UUID {}", Context.NAME, uuid);
if(oDatabaseDocument != null) {
oDatabaseDocument.rollback();
}
throw e;
} catch(WebApplicationException e) {
SecretManager secretManager = SecretManagerProvider.instance.get();
User user = secretManager.getUser();
logger.warn("The requesting user {} has not enought privileges to activate {} with UUID {}. {}", user.getUsername(), Context.NAME, uuid, e.getMessage());
if(oDatabaseDocument != null) {
oDatabaseDocument.rollback();
}
throw e;
} catch(Exception e) {
logger.error("Unable to activate {} with UUID {}", Context.NAME, uuid, e);
if(oDatabaseDocument != null) {
oDatabaseDocument.rollback();
}
throw new ResourceRegistryException(e);
} finally {
if(oDatabaseDocument != null) {
oDatabaseDocument.close();
}
if(current!=null) {
current.activateOnCurrentThread();
}
}
}
} }

View File

@ -5,6 +5,7 @@ import java.util.UUID;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
@ -108,6 +109,30 @@ public class ContextManager extends BaseRest {
return contextManagement.createOrUpdate(); return contextManagement.createOrUpdate();
} }
/*
* POST /contexts/{UUID}
* e.g. POST /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
*
* BODY: {...}
*
*/
@POST
@Path("{" + ContextManager.CONTEXT_UUID_PATH_PARAMETER + "}")
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
public void activate(@PathParam(ContextManager.CONTEXT_UUID_PATH_PARAMETER) String uuid)
throws ResourceRegistryException {
logger.info("Requested to activate {} with UUID {}", Context.NAME, uuid);
setAccountingMethod(Method.UPDATE, Context.NAME);
ServerRequestInfo serverRequestInfo = initRequestInfo();
serverRequestInfo.setIncludeMeta(true);
serverRequestInfo.setAllMeta(true);
ContextManagement contextManagement = new ContextManagement();
contextManagement.setUUID(UUID.fromString(uuid));
contextManagement.activate();
}
/* /*
* DELETE /contexts/{UUID} * DELETE /contexts/{UUID}
* e.g. DELETE /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0 * e.g. DELETE /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0