This commit is contained in:
Fabio Sinibaldi 2020-11-19 18:50:20 +01:00
parent 6dc433b834
commit a65830d21a
13 changed files with 132 additions and 19 deletions

View File

@ -3,6 +3,7 @@ package org.gcube.application.geoportal.service.engine;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAmount;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
import org.gcube.application.geoportal.service.utils.ContextUtils;
@ -25,13 +26,12 @@ public abstract class AbstractScopedMap<T> implements Engine<T>{
@NonNull
private String name;
@Synchronized
public T getObject() throws ConfigurationException {
String currentScope=ContextUtils.getCurrentScope();
log.debug(name+" : obtaining object for context "+currentScope);
TTLObject<T> found=scopeMap.putIfAbsent(currentScope, new TTLObject<T>(LocalDateTime.now(),retrieveObject()));
scopeMap.putIfAbsent(currentScope, new TTLObject<T>(LocalDateTime.now(),retrieveObject()));
TTLObject<T> found=scopeMap.get(currentScope);
if(TTL!=null) {
if(!found.getCreationTime().plus(TTL).isBefore(LocalDateTime.now())) {
@ -48,7 +48,11 @@ public abstract class AbstractScopedMap<T> implements Engine<T>{
public void shustdown() {
log.warn(name + ": shutting down");
scopeMap.forEach((String s,TTLObject<T> o)->{
dispose(o.getTheObject());
try{if(o!=null&&o.getTheObject()!=null)
dispose(o.getTheObject());
}catch(Throwable t) {
log.warn(name +": unable to dispose ",t);
}
});
}

View File

@ -1,10 +1,14 @@
package org.gcube.application.geoportal.service.engine;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.eclipse.persistence.internal.sessions.remote.SequencingFunctionCall.GetNextValue;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
import org.gcube.application.geoportal.service.utils.ContextUtils;
@ -13,23 +17,35 @@ import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendEx
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
import org.gcube.data.transfer.library.utils.Utils;
public class StorageClientProvider implements Engine<IClient>{
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class StorageClientProvider extends AbstractScopedMap<IClient>{
public StorageClientProvider() {
super("Storage client cache");
setTTL(Duration.of(10, ChronoUnit.MINUTES));
}
@Override
public IClient getObject() throws ConfigurationException {
protected IClient retrieveObject() throws ConfigurationException {
return new StorageClient(InterfaceConstants.SERVICE_CLASS, InterfaceConstants.SERVICE_NAME, ContextUtils.getCurrentCaller(), AccessType.SHARED, MemoryType.VOLATILE).getClient();
}
@Override
public void shustdown() {
// TODO Auto-generated method stub
protected void dispose(IClient toDispose) {
try {
toDispose.close();
}catch(Throwable t) {
log.warn(" unable to dispose "+toDispose,t);
}
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@ -40,4 +56,9 @@ public class StorageClientProvider implements Engine<IClient>{
return new URL(getObject().getHttpsUrl().RFileById(id)).openConnection().getInputStream();
}
public String store(InputStream is) throws RemoteBackendException, ConfigurationException {
return getObject().put(true).LFile(is).RFile(Utils.getUniqueString());
}
}

View File

@ -25,7 +25,6 @@ public class AddSectionToConcessioneRequest {
private Section section;
private String concessioneID;
private AssociatedContent toRegister;
private List<SHUBFileDescriptor> streams;

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@ -34,6 +35,30 @@ import lombok.extern.slf4j.Slf4j;
public class Concessioni {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("publish/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String publish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
try {
log.info("Loading Concessione by id {} ",id);
Concessione conc=(Concessione) ConcessioneManager.getByID(Long.parseLong(id));
ConcessioneManager manager=ManagerFactory.getByRecord(conc);
log.debug("Loaded object {} ",conc);
PublicationReport rep=manager.commitSafely(true);
return rep.prettyPrint();
}catch(WebApplicationException e){
log.warn("Unable to serve request",e);
throw e;
}catch(Throwable e){
log.warn("Unable to serve request",e);
throw new WebApplicationException("Unable to serve request", e);
}
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")

View File

@ -1,5 +1,7 @@
package org.gcube.application.geoportal.service.legacy;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
@ -23,16 +25,25 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.utils.Files;
import org.gcube.application.geoportal.managers.AbstractRecordManager;
import org.gcube.application.geoportal.managers.DefatulEMFProvider;
import org.gcube.application.geoportal.model.concessioni.Concessione;
import org.gcube.application.geoportal.model.concessioni.LayerConcessione;
import org.gcube.application.geoportal.model.content.AssociatedContent;
import org.gcube.application.geoportal.model.content.UploadedImage;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
import org.gcube.application.geoportal.model.report.PublicationReport;
import org.gcube.application.geoportal.service.GeoportalService;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.engine.StorageClientProvider;
import org.gcube.application.geoportal.service.model.internal.rest.AddSectionToConcessioneRequest;
import org.gcube.application.geoportal.service.model.internal.rest.AddSectionToConcessioneRequest.SHUBFileDescriptor;
import org.gcube.application.geoportal.service.model.internal.rest.AddSectionToConcessioneRequest.Section;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.BeforeClass;
import org.junit.Test;
@ -48,6 +59,8 @@ public class Concessioni extends JerseyTest {
return new GeoportalService();
}
@BeforeClass
public static void init() {
String scope="/gcube/devNext/NextNext";
@ -71,7 +84,13 @@ public class Concessioni extends JerseyTest {
}
@Test
public void failPublish() throws com.fasterxml.jackson.core.JsonProcessingException, IOException {
Concessione toCreate=TestModel.prepareEmptyConcessione();
Concessione conc=pushConcessione(toCreate);
System.out.println(publish(conc.getId()+"").prettyPrint());
}
@Test
public void list() throws JsonProcessingException, IOException{
@ -93,17 +112,12 @@ public class Concessioni extends JerseyTest {
@Test
public void createNew() throws IOException {
Concessione toCreate=TestModel.prepareEmptyConcessione();
WebTarget target=target(InterfaceConstants.Methods.CONCESSIONI);
Response resp=target.request(MediaType.APPLICATION_JSON).put(Entity.entity(toCreate.asJson(), MediaType.APPLICATION_JSON));
String resString=resp.readEntity(String.class);
System.out.println("Resp String is "+resString);
Concessione registered=Serialization.read(resString, Concessione.class);
System.out.println("Registered concessione at : "+registered);
pushConcessione(toCreate);
}
@Test
public void publishNew() throws IOException {
public void publishNew() throws IOException, RemoteBackendException, ConfigurationException {
Concessione toCreate=TestModel.prepareEmptyConcessione();
WebTarget target=target(InterfaceConstants.Methods.CONCESSIONI);
Response resp=target.request(MediaType.APPLICATION_JSON).put(Entity.entity(toCreate.asJson(), MediaType.APPLICATION_JSON));
@ -111,8 +125,56 @@ public class Concessioni extends JerseyTest {
System.out.println("Resp String is "+resString);
Concessione registered=Serialization.read(resString, Concessione.class);
System.out.println("Registered concessione at : "+registered);
Concessione fullTemplate=TestModel.prepareConcessione();
publishSection(registered.getId()+"",formRequest(Section.RELAZIONE,fullTemplate.getRelazioneScavo(),"concessioni/relazione.pdf"));
for(UploadedImage img:fullTemplate.getImmaginiRappresentative())
publishSection(registered.getId()+"",formRequest(Section.UPLOADED_IMG,img,"concessioni/immagine.png"));
publishSection(registered.getId()+"",formRequest(Section.POSIZIONAMENTO,fullTemplate.getPosizionamentoScavo(),"concessioni/pos.dbf","concessioni/pos.shp"));
for(LayerConcessione l:fullTemplate.getPianteFineScavo())
publishSection(registered.getId()+"",formRequest(Section.PIANTA,l,"concessioni/pos.dbf","concessioni/pos.shp"));
System.out.println("REPORT IS "+publish(registered.getId()+""));
}
private PublicationReport publish(String id) throws com.fasterxml.jackson.core.JsonProcessingException, IOException {
WebTarget target=target(InterfaceConstants.Methods.CONCESSIONI);
Response resp=target.path(id).request(MediaType.APPLICATION_JSON).post(null);
String resString=resp.readEntity(String.class);
System.out.println("Resp String is "+resString);
PublicationReport registered=Serialization.read(resString, PublicationReport.class);
System.out.println("Registered concessione at : "+registered);
return registered;
}
private AddSectionToConcessioneRequest formRequest(Section section,AssociatedContent content,String... files) throws RemoteBackendException, FileNotFoundException, ConfigurationException {
AddSectionToConcessioneRequest toReturn=new AddSectionToConcessioneRequest();
toReturn.setSection(section);
toReturn.setToRegister(content);
for(String f:files) {
SHUBFileDescriptor desc=new SHUBFileDescriptor();
desc.setFilename(f.substring(f.lastIndexOf("/")));
String sId=ImplementationProvider.get().getStorageProvider().store(
new FileInputStream(Files.getFileFromResources(f)));
desc.setShubID(sId);
}
return toReturn;
}
private Response publishSection(String id, AddSectionToConcessioneRequest request) {
WebTarget target=target(InterfaceConstants.Methods.CONCESSIONI);
return target.path(id).request(MediaType.APPLICATION_JSON).put(Entity.entity(request, MediaType.APPLICATION_JSON));
}
private Concessione pushConcessione(Concessione c) throws com.fasterxml.jackson.core.JsonProcessingException, IOException {
WebTarget target=target(InterfaceConstants.Methods.CONCESSIONI);
Response resp=target.request(MediaType.APPLICATION_JSON).put(Entity.entity(c.asJson(), MediaType.APPLICATION_JSON));
String resString=resp.readEntity(String.class);
System.out.println("Resp String is "+resString);
Concessione registered=Serialization.read(resString, Concessione.class);
System.out.println("Registered concessione at : "+registered);
return registered;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

Binary file not shown.

Binary file not shown.

Binary file not shown.