Projects REST interface

pull/1/head
Fabio Sinibaldi 2 years ago
parent ce867018a7
commit 7bdf45340c

@ -34,4 +34,5 @@ public class ProfiledDocument {
private TemporalReference temporalReference;
private Document theDocument;
}

@ -13,6 +13,7 @@ public class InterfaceConstants {
public static final String PROFILES="profiles";
public static final String SECTIONS="sections";
public static final String PROJECTS="projects";
public static final String CONCESSIONI="concessioni";
public static final String MONGO_CONCESSIONI="mongo-concessioni";

@ -3,7 +3,7 @@ package org.gcube.application.geoportal.service;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.rest.ConcessioniOverMongo;
import org.gcube.application.geoportal.service.rest.Profiles;
import org.gcube.application.geoportal.service.rest.Projects;
import org.gcube.application.geoportal.service.rest.Sections;
import org.gcube.application.geoportal.service.utils.Serialization;
@ -23,7 +23,7 @@ public class GeoPortalService extends ResourceConfig{
registerClasses(ConcessioniOverMongo.class);
registerClasses(Projects.class);
registerClasses(Sections.class);
registerClasses(Profiles.class);

@ -12,7 +12,7 @@ public interface MongoManagerI<T> {
// create
public T registerNew(T toRegister) throws IOException;
public T registerNew(Document toRegister) throws IOException;
// update
public T update(String id,T toSet) throws IOException;
@ -27,7 +27,8 @@ public interface MongoManagerI<T> {
// query
public Iterable<T> query(QueryRequest request);
public Iterable<Document> query(QueryRequest request);
public Iterable<T> filter(QueryRequest request);
// materialize

@ -13,7 +13,10 @@ import org.gcube.application.geoportal.service.model.internal.faults.Configurati
import org.gcube.application.geoportal.service.model.internal.faults.DeletionException;
import org.gcube.application.geoportal.service.utils.Serialization;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;
@ -23,7 +26,16 @@ import static org.gcube.application.geoportal.service.engine.mongo.ConcessioniMo
public class ProfiledMongoManager extends MongoManager implements MongoManagerI<ProfiledDocument>{
public ProfiledMongoManager() throws ConfigurationException {
String profileId;
public ProfiledMongoManager(String profileId) throws ConfigurationException {
if(profileId==null) throw new InvalidParameterException("Profile ID cannot be null");
//check profile existance
//ProfileManager.get(profileId);
// TODO GET FROM PROFILES
if (!profileId.equals("profiled-concessioni")) throw new WebApplicationException("Profile "+profileId+" not registered", Response.Status.NOT_FOUND);
}
private ProfiledDocument onUpdate(ProfiledDocument updatedDocument){
@ -48,8 +60,13 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
}
@Override
public ProfiledDocument registerNew(ProfiledDocument toRegister) throws IOException {
super.insert(asDocument(toRegister),getCollectionName());
public ProfiledDocument registerNew(Document toRegisterDoc) throws IOException {
ProfiledDocument toRegister = new ProfiledDocument();
toRegister.setTheDocument(toRegisterDoc);
//TODO initialize ProfiledDocument values
insert(asDocument(toRegister),getCollectionName());
log.trace("Going to register {} ",toRegister);
toRegister=onUpdate(toRegister);
@ -107,7 +124,19 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
}
@Override
public Iterable<ProfiledDocument> query(QueryRequest queryRequest) {
public Iterable<Document> query(QueryRequest queryRequest) {
log.info("Querying {} ",queryRequest);
LinkedBlockingQueue queue=new LinkedBlockingQueue<Concessione>();
query(queryRequest,getCollectionName()).forEach(
(Consumer<? super Document>) (Document d)->{try{
queue.put(d);
}catch(Throwable t){log.warn("Unable to translate "+d);}});
log.info("Returned {} elements ",queue.size());
return queue;
}
@Override
public Iterable<ProfiledDocument> filter(QueryRequest queryRequest) {
log.info("Searching concessione for filter {} ",queryRequest);
LinkedBlockingQueue queue=new LinkedBlockingQueue<Concessione>();
query(queryRequest,getCollectionName()).forEach(

@ -65,6 +65,9 @@ public class DBConstants {
public static final String PAROLE_CHIAVE="parole_chiave";
//
public static final ArrayList<Field> COLUMNS=new ArrayList<PostgisTable.Field>();
public static final PostgisTable CENTROIDS=new PostgisTable("centroids_concessioni",

@ -1,5 +0,0 @@
package org.gcube.application.geoportal.service.rest;
public class Profiles {
}

@ -2,133 +2,117 @@ package org.gcube.application.geoportal.service.rest;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.gcube.application.geoportal.common.model.project.Project;
import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.rest.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.engine.mongo.ConcessioniMongoManager;
import org.gcube.application.geoportal.service.engine.mongo.ProfiledMongoManager;
import org.gcube.application.geoportal.service.engine.postgis.PostgisIndex;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.json.JSONArray;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Collections;
import java.util.List;
@Path(InterfaceConstants.Methods.PROJECTS)
@Path(InterfaceConstants.Methods.PROJECTS+"/{"+InterfaceConstants.Parameters.PROFILE_ID+"}")
@Slf4j
public class Projects {
//***************** GENERIC PROJECTS
// GET ALL
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Project> getAll() {
return new GuardedMethod<List<Project>>() {
protected List<Project> run() throws Exception ,WebApplicationException {
return Collections.singletonList(new Project());
};
}.execute().getResult();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/search")
public List<Project> getFilteredAll(Document filter){
return new GuardedMethod<List<Project>>() {
protected List<Project> run() throws Exception ,WebApplicationException {
return Collections.singletonList(new Project());
};
}.execute().getResult();
}
//***************** BY PROFILE ID
// Create new Project
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}")
public Project registerNew(@PathParam(InterfaceConstants.Parameters.PROFILE_ID)String profileId,
Document toRegister) {
return new GuardedMethod<Project>() {
@Override
protected Project run() throws Exception, WebApplicationException {
return new Project();
}
}.execute().getResult();
}
// GET ALL (Filters apply)
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/search/{"+InterfaceConstants.Parameters.PROFILE_ID+"}")
public List<Project> getFilteredAllInProfiles(@PathParam(InterfaceConstants.Parameters.PROFILE_ID)String profileId,
Document filters) {
return new GuardedMethod<List<Project>>() {
protected List<Project> run() throws Exception ,WebApplicationException {
return Collections.singletonList(new Project());
};
}.execute().getResult();
}
// GET ALL
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}")
public List<Project> getAllinProfile(@PathParam(InterfaceConstants.Parameters.PROFILE_ID)String profileId) {
return new GuardedMethod<List<Project>>() {
protected List<Project> run() throws Exception ,WebApplicationException {
return Collections.singletonList(new Project());
};
}.execute().getResult();
}
//***************** BY PROFILE ID + PROJECT ID
// GET BY ID
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public Project getByID(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profile,
@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
Project toReturn=new GuardedMethod<Project>() {
@Override
protected Project run() throws Exception ,WebApplicationException{
return new Project();
}
}.execute().getResult();
return toReturn;
}
// DELETE BY ID
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public void delete(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profile,
@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
new GuardedMethod<Project>() {
@Override
protected Project run() throws Exception ,WebApplicationException{
// TODO DELETE
return null;
}
}.execute().getResult();
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public Project updateDocument(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profile,
@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,Document toSetDocument) {
Project toReturn=new GuardedMethod<Project>() {
@Override
protected Project run() throws Exception ,WebApplicationException{
return new Project();
}
}.execute().getResult();
return toReturn;
}
private ProfiledMongoManager manager;
public Projects(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profileID) throws ConfigurationException {
log.debug("Accessing profiles "+profileID);
manager=new GuardedMethod<ProfiledMongoManager>(){
@Override
protected ProfiledMongoManager run() throws Exception {
return new ProfiledMongoManager(profileID);
}
}.execute().getResult();
}
@GET
@Path(InterfaceConstants.Methods.CONFIGURATION_PATH)
@Produces(MediaType.APPLICATION_JSON)
public Configuration getConfiguration(){
return new GuardedMethod<Configuration>(){
@Override
protected Configuration run() throws Exception, WebApplicationException {
//manager.getConfiguration();
throw new Exception("Implement This Method");
}
}.execute().getResult();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ProfiledDocument createNew(Document d) {
return new GuardedMethod<ProfiledDocument>() {
@Override
protected ProfiledDocument run() throws Exception, WebApplicationException {
return manager.registerNew(d);
}
}.execute().getResult();
}
//********************************** READ
@GET
@Produces(MediaType.APPLICATION_JSON)
public Iterable<?> list() {
return new GuardedMethod<Iterable<?>>() {
protected Iterable<?> run() throws Exception ,WebApplicationException {
return manager.query(new QueryRequest());
};
}.execute().getResult();
}
// BY ID
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public ProfiledDocument getById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<ProfiledDocument>() {
@Override
protected ProfiledDocument run() throws Exception, WebApplicationException {
return manager.getByID(id);
}
}.execute().getResult();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/"+InterfaceConstants.Methods.SEARCH_PATH)
public String search(String filter){
return new GuardedMethod<String>() {
@Override
protected String run() throws Exception, WebApplicationException {
QueryRequest req=new QueryRequest();
req.setFilter(Document.parse(filter));
return Serialization.write(manager.query(req));
}
}.execute().getResult();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/"+InterfaceConstants.Methods.QUERY_PATH)
public Iterable<?> query(String queryString){
return new GuardedMethod<Iterable<?>>() {
@Override
protected Iterable<?> run() throws Exception, WebApplicationException {
return manager.query(Serialization.parseQuery(queryString));
}
}.execute().getResult();
}
}

@ -33,40 +33,6 @@ public class BasicServiceTestUnit extends JerseyTest {
}
});
/*
ImplementationProvider.get().setStorageProvider(new StorageClientProvider() {
@Override
public IClient getObject() throws ConfigurationException {
TokenSetter.set(scope);
return super.getObject();
}
});
ImplementationProvider.get().setMongoConnectionProvider(new MongoConnectionProvider() {
@Override
public org.gcube.application.geoportal.service.model.internal.db.MongoConnection getObject() throws ConfigurationException {
TokenSetter.set(scope);
return super.getObject();
}
});
ImplementationProvider.get().setMongoClientProvider(new MongoClientProvider() {
@Override
public MongoClient getObject() throws ConfigurationException {
TokenSetter.set(scope);
return super.getObject();
}
});
ImplementationProvider.get().setDbProvider(new PostgisConnectionProvider() {
@Override
public PostgisDBManager getObject() throws ConfigurationException {
TokenSetter.set(scope);
return super.getObject();
}
});*/
}

@ -1,75 +1,91 @@
package org.gcube.application.geoportal.service;
import org.bson.Document;
import org.gcube.application.cms.tests.TokenSetter;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.junit.Before;
import org.junit.Test;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class ProjectTests extends BasicServiceTestUnit{
String testProfileId="profiled_concessione";
String projectId="asdlkjgdasfjklgadjhkl";
@Test
public void getAll() {
WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
System.out.println(target.request(MediaType.APPLICATION_JSON).get(List.class));
}
@Test
public void getFilteredAll() {
WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
Document document =new Document(Collections.singletonMap("key", "value"));
System.out.println(target.path("search").request(MediaType.APPLICATION_JSON).
post(Entity.entity(document, MediaType.APPLICATION_JSON)));
}
@Test
public void getAllByProfile() {
WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
System.out.println(target.path(testProfileId).request(MediaType.APPLICATION_JSON).get(List.class));
String testProfileId="profiled-concessioni";
@Before
public void setContext(){
TokenSetter.set(scope);
}
@Test
public void getFilteredByProfile() {
WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
Document document =new Document(Collections.singletonMap("key", "value"));
System.out.println(target.path("search").path(testProfileId).request(MediaType.APPLICATION_JSON).
post(Entity.entity(document, MediaType.APPLICATION_JSON)));
public void testMissingProfile(){
Response resp = target(InterfaceConstants.Methods.PROJECTS)
.path("non-existent-profile").request().get();
assertEquals(resp.getStatus(),404);
}
@Test
public void getById() {
WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
System.out.println(target.path(testProfileId).path(projectId).request(MediaType.APPLICATION_JSON).get().readEntity(String.class));
}
@Test
public void registerNew() {
WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
Document document =new Document(Collections.singletonMap("key", "value"));
System.out.println(target.path(testProfileId).request(MediaType.APPLICATION_JSON).
put(Entity.entity(document, MediaType.APPLICATION_JSON)));
public void getAll() {
System.out.println(target(InterfaceConstants.Methods.PROJECTS).path(testProfileId).request(MediaType.APPLICATION_JSON).get(List.class));
}
@Test
public void updateDocument() {
WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
Document document =new Document(Collections.singletonMap("key", "value"));
System.out.println(target.path(testProfileId).path(projectId).request(MediaType.APPLICATION_JSON).
put(Entity.entity(document, MediaType.APPLICATION_JSON)));
}
// @Test
// public void getFilteredAll() {
// WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
// Document document =new Document(Collections.singletonMap("key", "value"));
//
// System.out.println(target.path("search").request(MediaType.APPLICATION_JSON).
// post(Entity.entity(document, MediaType.APPLICATION_JSON)));
//
// }
//
// @Test
// public void getAllByProfile() {
// WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
// System.out.println(target.path(testProfileId).request(MediaType.APPLICATION_JSON).get(List.class));
// }
//
// @Test
// public void getFilteredByProfile() {
// WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
// Document document =new Document(Collections.singletonMap("key", "value"));
//
// System.out.println(target.path("search").path(testProfileId).request(MediaType.APPLICATION_JSON).
// post(Entity.entity(document, MediaType.APPLICATION_JSON)));
//
// }
//
//
// @Test
// public void getById() {
// WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
// System.out.println(target.path(testProfileId).path(projectId).request(MediaType.APPLICATION_JSON).get().readEntity(String.class));
// }
//
//
// @Test
// public void registerNew() {
// WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
// Document document =new Document(Collections.singletonMap("key", "value"));
//
// System.out.println(target.path(testProfileId).request(MediaType.APPLICATION_JSON).
// put(Entity.entity(document, MediaType.APPLICATION_JSON)));
// }
//
// @Test
// public void updateDocument() {
// WebTarget target=target(InterfaceConstants.Methods.PROJECTS);
// Document document =new Document(Collections.singletonMap("key", "value"));
//
// System.out.println(target.path(testProfileId).path(projectId).request(MediaType.APPLICATION_JSON).
// put(Entity.entity(document, MediaType.APPLICATION_JSON)));
// }
}

Loading…
Cancel
Save