263 lines
10 KiB
Java
263 lines
10 KiB
Java
package org.gcube.data.access.storagehub.services;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URL;
|
|
|
|
import javax.inject.Inject;
|
|
import javax.jcr.RepositoryException;
|
|
import javax.jcr.Session;
|
|
import javax.servlet.ServletContext;
|
|
import javax.ws.rs.Consumes;
|
|
import javax.ws.rs.FormParam;
|
|
import javax.ws.rs.POST;
|
|
import javax.ws.rs.Path;
|
|
import javax.ws.rs.PathParam;
|
|
import javax.ws.rs.core.Context;
|
|
import javax.ws.rs.core.MediaType;
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import org.apache.commons.compress.archivers.ArchiveException;
|
|
import org.gcube.common.gxrest.response.outbound.GXOutboundErrorResponse;
|
|
import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
|
|
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
|
|
import org.gcube.common.storagehub.model.items.GCubeItem;
|
|
import org.gcube.data.access.storagehub.StorageHubAppllicationManager;
|
|
import org.gcube.data.access.storagehub.handlers.CredentialHandler;
|
|
import org.gcube.data.access.storagehub.handlers.items.ItemHandler;
|
|
import org.gcube.data.access.storagehub.handlers.items.builders.ArchiveStructureCreationParameter;
|
|
import org.gcube.data.access.storagehub.handlers.items.builders.FileCreationParameters;
|
|
import org.gcube.data.access.storagehub.handlers.items.builders.FolderCreationParameters;
|
|
import org.gcube.data.access.storagehub.handlers.items.builders.GCubeItemCreationParameters;
|
|
import org.gcube.data.access.storagehub.handlers.items.builders.ItemsParameterBuilder;
|
|
import org.gcube.data.access.storagehub.handlers.items.builders.URLCreationParameters;
|
|
import org.gcube.smartgears.annotations.ManagedBy;
|
|
import org.gcube.smartgears.utils.InnerMethodName;
|
|
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
|
|
import org.glassfish.jersey.media.multipart.FormDataParam;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
|
|
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
|
|
|
|
@Path("items")
|
|
@ManagedBy(StorageHubAppllicationManager.class)
|
|
@RequestHeaders({
|
|
@RequestHeader(name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"), })
|
|
public class ItemsCreator extends Impersonable {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ItemsCreator.class);
|
|
|
|
@Context
|
|
ServletContext context;
|
|
|
|
RepositoryInitializer repository = StorageHubAppllicationManager.repository;
|
|
|
|
@Inject
|
|
ItemHandler itemHandler;
|
|
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
|
@Path("/{id}/create/FOLDER")
|
|
public Response createFolder(@PathParam("id") String id, @FormParam("name") String name,
|
|
@FormParam("description") String description, @FormParam("hidden") boolean hidden) {
|
|
InnerMethodName.instance.set("createItem(FOLDER)");
|
|
log.info("create folder item called");
|
|
Session ses = null;
|
|
String toReturn = null;
|
|
try {
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
ItemsParameterBuilder<FolderCreationParameters> builder = FolderCreationParameters.builder().name(name)
|
|
.description(description).hidden(hidden).on(id).with(ses).author(currentUser);
|
|
toReturn = itemHandler.create(builder.build());
|
|
} catch (StorageHubException she) {
|
|
log.error(she.getErrorMessage(), she);
|
|
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
|
|
} catch (RepositoryException re) {
|
|
log.error("jcr error creating item", re);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
|
|
} catch (Throwable e) {
|
|
log.error("unexpected error", e);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
|
|
} finally {
|
|
if (ses != null)
|
|
ses.logout();
|
|
|
|
}
|
|
return Response.ok(toReturn).build();
|
|
}
|
|
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
|
@Path("/{id}/create/EXTERNALFOLDER")
|
|
public Response createExternalFolder(@PathParam("id") String id, @FormParam("name") String name,
|
|
@FormParam("description") String description, @FormParam("hidden") boolean hidden,
|
|
@FormParam("pluginName") String pluginName, @FormParam("parameters") String pluginParameters) {
|
|
InnerMethodName.instance.set("createItem(EXTERNALFOLDER)");
|
|
log.info("create folder item called");
|
|
Session ses = null;
|
|
String toReturn = null;
|
|
try {
|
|
// TODO
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
ItemsParameterBuilder<FolderCreationParameters> builder = FolderCreationParameters.builder().name(name)
|
|
.description(description).onRepository(pluginName).withParameters(null).hidden(hidden).on(id)
|
|
.with(ses).author(currentUser);
|
|
toReturn = itemHandler.create(builder.build());
|
|
} catch (StorageHubException she) {
|
|
log.error(she.getErrorMessage(), she);
|
|
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
|
|
} catch (RepositoryException re) {
|
|
log.error("jcr error creating item", re);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
|
|
} catch (Throwable e) {
|
|
log.error("unexpected error", e);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
|
|
} finally {
|
|
if (ses != null)
|
|
ses.logout();
|
|
|
|
}
|
|
return Response.ok(toReturn).build();
|
|
}
|
|
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
|
@Path("/{id}/create/URL")
|
|
public Response createURL(@PathParam("id") String id, @FormParam("name") String name,
|
|
@FormParam("description") String description, @FormParam("value") URL value) {
|
|
InnerMethodName.instance.set("createItem(URL)");
|
|
log.info("create url called");
|
|
Session ses = null;
|
|
String toReturn = null;
|
|
try {
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
ItemsParameterBuilder<URLCreationParameters> builder = URLCreationParameters.builder().name(name)
|
|
.description(description).url(value).on(id).with(ses).author(currentUser);
|
|
|
|
toReturn = itemHandler.create(builder.build());
|
|
} catch (StorageHubException she) {
|
|
log.error(she.getErrorMessage(), she);
|
|
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
|
|
} catch (RepositoryException re) {
|
|
log.error("jcr error creating item", re);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
|
|
} catch (Throwable e) {
|
|
log.error("unexpected error", e);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
|
|
} finally {
|
|
if (ses != null)
|
|
ses.logout();
|
|
|
|
}
|
|
return Response.ok(toReturn).build();
|
|
}
|
|
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
@Path("/{id}/create/GCUBEITEM")
|
|
public String createGcubeItem(@PathParam("id") String id, GCubeItem item) {
|
|
InnerMethodName.instance.set("createItem(GCUBEITEM)");
|
|
log.info("create Gcube item called");
|
|
Session ses = null;
|
|
String toReturn = null;
|
|
|
|
try {
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
ItemsParameterBuilder<GCubeItemCreationParameters> builder = GCubeItemCreationParameters.builder()
|
|
.item(item).on(id).with(ses).author(currentUser);
|
|
|
|
toReturn = itemHandler.create(builder.build());
|
|
} catch (StorageHubException she) {
|
|
log.error(she.getErrorMessage(), she);
|
|
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
|
|
} catch (RepositoryException re) {
|
|
log.error("jcr error creating item", re);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
|
|
} catch (Throwable e) {
|
|
log.error("unexpected error", e);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
|
|
} finally {
|
|
if (ses != null)
|
|
ses.logout();
|
|
}
|
|
return toReturn;
|
|
}
|
|
|
|
@POST
|
|
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
|
@Path("/{id}/create/FILE")
|
|
public String createFileItem(@PathParam("id") String id, @FormDataParam("name") String name,
|
|
@FormDataParam("description") String description, @FormDataParam("file") InputStream stream,
|
|
@FormDataParam("file") FormDataContentDisposition fileDetail) {
|
|
InnerMethodName.instance.set("createItem(FILE)");
|
|
|
|
Session ses = null;
|
|
String toReturn = null;
|
|
try {
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
ItemsParameterBuilder<FileCreationParameters> builder = FileCreationParameters.builder().name(name)
|
|
.description(description).stream(stream).fileDetails(fileDetail).on(id).with(ses)
|
|
.author(currentUser);
|
|
|
|
toReturn = itemHandler.create(builder.build());
|
|
|
|
} catch (RepositoryException re) {
|
|
log.error("jcr error creating file item", re);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating file item", re));
|
|
} catch (StorageHubException she) {
|
|
log.error(she.getErrorMessage(), she);
|
|
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
|
|
} catch (Throwable e) {
|
|
log.error("unexpected error", e);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
|
|
} finally {
|
|
if (ses != null && ses.isLive()) {
|
|
log.info("session closed");
|
|
ses.logout();
|
|
}
|
|
}
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
@POST
|
|
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
|
@Path("/{id}/create/ARCHIVE")
|
|
public String uploadArchive(@PathParam("id") String id, @FormDataParam("parentFolderName") String parentFolderName,
|
|
@FormDataParam("file") InputStream stream, @FormDataParam("file") FormDataContentDisposition fileDetail) {
|
|
InnerMethodName.instance.set("createItem(ARCHIVE)");
|
|
|
|
Session ses = null;
|
|
String toReturn = null;
|
|
try {
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
ItemsParameterBuilder<ArchiveStructureCreationParameter> builder = ArchiveStructureCreationParameter
|
|
.builder().parentName(parentFolderName).stream(stream).fileDetails(fileDetail).on(id).with(ses)
|
|
.author(currentUser);
|
|
|
|
toReturn = itemHandler.create(builder.build());
|
|
|
|
} catch (RepositoryException | ArchiveException | IOException re) {
|
|
log.error("jcr error extracting archive", re);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error extracting archive", re));
|
|
} catch (StorageHubException she) {
|
|
log.error(she.getErrorMessage(), she);
|
|
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
|
|
} catch (Throwable e) {
|
|
log.error("unexpected error", e);
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
|
|
} finally {
|
|
if (ses != null)
|
|
ses.logout();
|
|
|
|
}
|
|
return toReturn;
|
|
}
|
|
|
|
}
|