2017-07-05 18:42:04 +02:00
|
|
|
package org.gcube.spatial.data.sdi.rest;
|
|
|
|
|
2017-08-03 12:43:53 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
2017-07-05 18:42:04 +02:00
|
|
|
import java.io.InputStream;
|
2017-08-03 12:43:53 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashSet;
|
2017-07-05 18:42:04 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import javax.ws.rs.Consumes;
|
|
|
|
import javax.ws.rs.DefaultValue;
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
import javax.ws.rs.POST;
|
2017-08-03 12:43:53 +02:00
|
|
|
import javax.ws.rs.PUT;
|
2017-07-05 18:42:04 +02:00
|
|
|
import javax.ws.rs.Path;
|
2017-08-03 12:43:53 +02:00
|
|
|
import javax.ws.rs.PathParam;
|
2017-07-05 18:42:04 +02:00
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
import javax.ws.rs.QueryParam;
|
2017-08-03 12:43:53 +02:00
|
|
|
import javax.ws.rs.WebApplicationException;
|
2017-07-05 18:42:04 +02:00
|
|
|
import javax.ws.rs.core.MediaType;
|
2017-08-03 12:43:53 +02:00
|
|
|
import javax.ws.rs.core.Response.Status;
|
2017-07-05 18:42:04 +02:00
|
|
|
|
2017-08-03 12:43:53 +02:00
|
|
|
import org.gcube.smartgears.annotations.ManagedBy;
|
|
|
|
import org.gcube.spatial.data.sdi.SDIServiceManager;
|
2018-05-18 17:50:51 +02:00
|
|
|
import org.gcube.spatial.data.sdi.engine.GeoNetworkManager;
|
|
|
|
import org.gcube.spatial.data.sdi.engine.TemplateManager;
|
2017-08-03 12:43:53 +02:00
|
|
|
import org.gcube.spatial.data.sdi.engine.TemporaryPersistence;
|
2018-05-18 17:50:51 +02:00
|
|
|
import org.gcube.spatial.data.sdi.engine.impl.faults.gn.MetadataNotFoundException;
|
|
|
|
import org.gcube.spatial.data.sdi.engine.impl.gn.extension.GeoNetworkClient;
|
|
|
|
import org.gcube.spatial.data.sdi.engine.impl.gn.extension.GeoNetworkUtils;
|
2017-08-03 12:43:53 +02:00
|
|
|
import org.gcube.spatial.data.sdi.engine.impl.metadata.MetadataHandler;
|
|
|
|
import org.gcube.spatial.data.sdi.engine.impl.metadata.TemplateApplicationReport;
|
2017-07-05 18:42:04 +02:00
|
|
|
import org.gcube.spatial.data.sdi.model.ServiceConstants;
|
|
|
|
import org.gcube.spatial.data.sdi.model.metadata.MetadataReport;
|
2017-08-03 12:43:53 +02:00
|
|
|
import org.gcube.spatial.data.sdi.model.metadata.TemplateCollection;
|
2017-07-05 18:42:04 +02:00
|
|
|
import org.gcube.spatial.data.sdi.model.metadata.TemplateDescriptor;
|
2017-08-03 12:43:53 +02:00
|
|
|
import org.gcube.spatial.data.sdi.model.metadata.TemplateInvocation;
|
2018-05-18 17:50:51 +02:00
|
|
|
import org.gcube.spatial.data.sdi.model.service.GeoNetworkDescriptor;
|
2017-07-05 18:42:04 +02:00
|
|
|
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
|
|
|
|
import org.glassfish.jersey.media.multipart.FormDataParam;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@Path(ServiceConstants.Metadata.INTERFACE)
|
2017-08-03 12:43:53 +02:00
|
|
|
@ManagedBy(SDIServiceManager.class)
|
2017-07-05 18:42:04 +02:00
|
|
|
public class Metadata {
|
|
|
|
|
|
|
|
@Inject
|
2018-05-18 17:50:51 +02:00
|
|
|
TemplateManager templateManager;
|
2017-08-03 12:43:53 +02:00
|
|
|
@Inject
|
2018-05-18 17:50:51 +02:00
|
|
|
GeoNetworkManager geonetworkManager;
|
2017-08-03 12:43:53 +02:00
|
|
|
@Inject
|
|
|
|
TemporaryPersistence persistence;
|
|
|
|
|
2017-07-05 18:42:04 +02:00
|
|
|
@POST
|
2017-08-03 12:43:53 +02:00
|
|
|
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
2017-07-05 18:42:04 +02:00
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2017-08-03 12:43:53 +02:00
|
|
|
public String uploadMetadata(@FormDataParam(ServiceConstants.Metadata.UPLOADED_FILE_PARAMETER) InputStream uploadedMeta,
|
|
|
|
@FormDataParam(ServiceConstants.Metadata.UPLOADED_FILE_PARAMETER) FormDataContentDisposition uploadedMetaDetails){
|
|
|
|
try {
|
|
|
|
log.debug("Receiving metadata upload... size {} ",uploadedMetaDetails.getSize());
|
|
|
|
return persistence.store(uploadedMeta);
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("Unable to store file. ",e);
|
|
|
|
throw new WebApplicationException("Unable to store file locally. Cause : "+e.getMessage(),Status.INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@PUT
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Path("/{uploadedId}")
|
|
|
|
public MetadataReport applyTemplates(Collection<TemplateInvocation> templateInvocations, @PathParam("uploadedId") String uploadedId){
|
|
|
|
log.debug("Checking uploaded id {} ",uploadedId);
|
|
|
|
File uploaded=null;
|
|
|
|
try {
|
|
|
|
uploaded=persistence.getById(uploadedId);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
log.debug("Unable to ge uploaded with ID {}. Cause : ",uploadedId,e);
|
|
|
|
throw new WebApplicationException("Invalid upload id "+uploadedId);
|
|
|
|
}
|
|
|
|
MetadataReport toReturn=new MetadataReport();
|
|
|
|
Set<TemplateInvocation> metadataEnrichments=new HashSet<>(templateInvocations);
|
2017-07-05 18:42:04 +02:00
|
|
|
|
2017-08-03 12:43:53 +02:00
|
|
|
if(metadataEnrichments!=null && !metadataEnrichments.isEmpty()){
|
|
|
|
try{
|
|
|
|
log.debug("Applying invocations...");
|
2018-05-18 17:50:51 +02:00
|
|
|
TemplateApplicationReport report=templateManager.applyMetadataTemplates(uploaded, metadataEnrichments);
|
2017-08-03 12:43:53 +02:00
|
|
|
toReturn.setAppliedTemplates(report.getAppliedTemplates());
|
|
|
|
persistence.update(uploadedId, new FileInputStream(new File(report.getGeneratedFilePath())));
|
|
|
|
}catch(Throwable e){
|
|
|
|
log.debug("Unable to apply templates. ",e);
|
|
|
|
throw new WebApplicationException("Unable to apply templates. Cause : "+e.getMessage(),Status.INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn;
|
2017-07-05 18:42:04 +02:00
|
|
|
}
|
2017-08-03 12:43:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Path("/publish/{uploadedId}/{gnCategory}")
|
|
|
|
public MetadataReport publishMetadata(@QueryParam(ServiceConstants.Metadata.VALIDATE_PARAMETER) @DefaultValue("true") Boolean validate,
|
|
|
|
@QueryParam(ServiceConstants.Metadata.PUBLIC_PARAMETER) @DefaultValue("false") Boolean makePublic,
|
|
|
|
@QueryParam(ServiceConstants.Metadata.STYLESHEET_PARAMETER) @DefaultValue("_none_") String styleSheet,
|
|
|
|
@PathParam("gnCategory") String category, @PathParam("uploadedId") String uploadedId){
|
|
|
|
try{
|
|
|
|
log.debug("PUBLISHING METADATA. UPLOADED ID {} ",uploadedId);
|
|
|
|
MetadataReport toReturn=new MetadataReport();
|
|
|
|
File toPublish=persistence.getById(uploadedId);
|
|
|
|
log.debug("Publishing metadata.. ");
|
2018-05-18 17:50:51 +02:00
|
|
|
|
|
|
|
GeoNetworkDescriptor desc=geonetworkManager.getSuggestedInstances().get(0);
|
|
|
|
GeoNetworkClient client=geonetworkManager.getClient(desc);
|
|
|
|
|
2017-08-03 12:43:53 +02:00
|
|
|
String uuid=new MetadataHandler(toPublish).getUUID();
|
2018-05-18 17:50:51 +02:00
|
|
|
long id=0;
|
|
|
|
try {
|
|
|
|
id=client.insertMetadata(category,styleSheet,validate,Integer.parseInt(desc.getDefaultGroup()),makePublic,toPublish);
|
|
|
|
}catch(Exception e) {
|
|
|
|
log.info("INSERT OPERATION FAILED. TRYING TO UPDATE.");
|
|
|
|
try{
|
|
|
|
id=GeoNetworkUtils.getIDByUUID(client, uuid);
|
|
|
|
client.updateMeta(id, toPublish);
|
|
|
|
}catch(MetadataNotFoundException e1) {
|
|
|
|
throw new RuntimeException("Insert Operation failed with unexpected reason (Metadata with uuid "+uuid+" has not been found).",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-03 12:43:53 +02:00
|
|
|
toReturn.setPublishedID(id);
|
|
|
|
toReturn.setPublishedUUID(uuid);
|
|
|
|
return toReturn;
|
|
|
|
} catch (FileNotFoundException e) {
|
2019-11-20 18:00:05 +01:00
|
|
|
log.warn("Unable to get uploaded with ID {}. Cause : ",uploadedId,e);
|
2017-08-03 12:43:53 +02:00
|
|
|
throw new WebApplicationException("Invalid upload id "+uploadedId);
|
|
|
|
}catch(Throwable e ){
|
2019-11-20 18:00:05 +01:00
|
|
|
log.warn("Unexpected error while publishing {} . Cause : ",uploadedId,e);
|
2017-08-03 12:43:53 +02:00
|
|
|
throw new WebApplicationException("Unabel to publish metadata. Cause "+e.getMessage(),Status.INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-05 18:42:04 +02:00
|
|
|
@GET
|
|
|
|
@Path("/list")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2017-08-03 12:43:53 +02:00
|
|
|
public Collection<TemplateDescriptor> getList(){
|
|
|
|
log.debug("Received LIST method");
|
2018-05-18 17:50:51 +02:00
|
|
|
TemplateCollection coll= templateManager.getAvailableMetadataTemplates();
|
2017-08-03 12:43:53 +02:00
|
|
|
log.debug("Gonna respond with {} ",coll);
|
|
|
|
return coll.getAvailableTemplates();
|
2017-07-05 18:42:04 +02:00
|
|
|
}
|
2017-08-03 12:43:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-05 18:42:04 +02:00
|
|
|
}
|