From 0bf84bbd4acdf81ce2c5de06efd97013b504c97d Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 22 Dec 2017 15:23:00 +0000 Subject: [PATCH] Refs #10810: Reorganize URLs to be REST compliant Task-Url: https://support.d4science.org/issues/10810 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-service@161875 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../service/AccountingResource.java | 4 +- .../service/resources/RecordManagement.java | 73 +++++++++++++++++++ .../service/resources/ResourceInsert.java | 49 ------------- 3 files changed, 75 insertions(+), 51 deletions(-) create mode 100644 src/main/java/org/gcube/accounting/service/resources/RecordManagement.java delete mode 100644 src/main/java/org/gcube/accounting/service/resources/ResourceInsert.java diff --git a/src/main/java/org/gcube/accounting/service/AccountingResource.java b/src/main/java/org/gcube/accounting/service/AccountingResource.java index e1c138b..165648e 100644 --- a/src/main/java/org/gcube/accounting/service/AccountingResource.java +++ b/src/main/java/org/gcube/accounting/service/AccountingResource.java @@ -3,7 +3,7 @@ package org.gcube.accounting.service; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.MediaType; -import org.gcube.accounting.service.resources.ResourceInsert; +import org.gcube.accounting.service.resources.RecordManagement; import org.glassfish.jersey.server.ResourceConfig; /** @@ -16,7 +16,7 @@ public class AccountingResource extends ResourceConfig { public static final String APPLICATION_JSON_CHARSET_UTF_8 = MediaType.APPLICATION_JSON + ";charset=UTF-8"; public AccountingResource() { - packages(ResourceInsert.class.getPackage().toString()); + packages(RecordManagement.class.getPackage().toString()); } } diff --git a/src/main/java/org/gcube/accounting/service/resources/RecordManagement.java b/src/main/java/org/gcube/accounting/service/resources/RecordManagement.java new file mode 100644 index 0000000..edaaac3 --- /dev/null +++ b/src/main/java/org/gcube/accounting/service/resources/RecordManagement.java @@ -0,0 +1,73 @@ +package org.gcube.accounting.service.resources; + +import java.util.List; + +import javax.validation.constraints.NotNull; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery; +import org.gcube.accounting.analytics.persistence.AccountingPersistenceQueryFactory; +import org.gcube.accounting.persistence.AccountingPersistence; +import org.gcube.accounting.persistence.AccountingPersistenceFactory; +import org.gcube.accounting.service.AccountingResource; +import org.gcube.accounting.service.AccountingServiceInitializer; +import org.gcube.documentstore.records.DSMapper; +import org.gcube.documentstore.records.Record; +import org.gcube.smartgears.annotations.ManagedBy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +@ManagedBy(AccountingServiceInitializer.class) +@Path(RecordManagement.RECORD_PATH_PART) +public class RecordManagement { + + private static final Logger log = LoggerFactory.getLogger(RecordManagement.class); + + public static final String RECORD_PATH_PART = "record"; + public static final String TYPE_PATH_PART = "type"; + public static final String RECORD_ID_PATH_PART = "recordID"; + + @POST + @Consumes({MediaType.TEXT_PLAIN, AccountingResource.APPLICATION_JSON_CHARSET_UTF_8}) + public Response add(String json) throws Exception { + + log.trace("Going to account : {}", json); + + AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence(); + + List records = DSMapper.unmarshalList(json); + for(Record record : records) { + accountingPersistence.account(record); + } + + log.trace("{} accounted successfully", json); + + return Response.status(Status.CREATED).build(); + } + + @GET + @Path("/{" + TYPE_PATH_PART + "}/{" + RECORD_ID_PATH_PART + "}/") + @Produces(AccountingResource.APPLICATION_JSON_CHARSET_UTF_8) + public Response get(@NotNull @PathParam(TYPE_PATH_PART) String type, + @NotNull @PathParam(RECORD_ID_PATH_PART) String recordId) throws Exception { + + log.debug("Requested {} having ID {}", type, recordId); + AccountingPersistenceQuery accountingPersistenceQuery = AccountingPersistenceQueryFactory.getInstance(); + + String record = accountingPersistenceQuery.getRecord(recordId, type); + return Response.status(200).entity(record).type(AccountingResource.APPLICATION_JSON_CHARSET_UTF_8).build(); + + } + +} diff --git a/src/main/java/org/gcube/accounting/service/resources/ResourceInsert.java b/src/main/java/org/gcube/accounting/service/resources/ResourceInsert.java deleted file mode 100644 index 37f8124..0000000 --- a/src/main/java/org/gcube/accounting/service/resources/ResourceInsert.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.gcube.accounting.service.resources; - -import java.util.List; - -import javax.ws.rs.Consumes; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; - -import org.gcube.accounting.persistence.AccountingPersistence; -import org.gcube.accounting.persistence.AccountingPersistenceFactory; -import org.gcube.accounting.service.AccountingResource; -import org.gcube.accounting.service.AccountingServiceInitializer; -import org.gcube.documentstore.records.DSMapper; -import org.gcube.documentstore.records.Record; -import org.gcube.smartgears.annotations.ManagedBy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @author Alessandro Pieve (alessandro.pieve@isti.cnr.it) - * @author Luca Frosini (ISTI - CNR) - */ -@ManagedBy(AccountingServiceInitializer.class) -@Path(ResourceInsert.INSERT_PATH_PART) -public class ResourceInsert { - - private static final Logger log = LoggerFactory.getLogger(ResourceInsert.class); - - public static final String INSERT_PATH_PART = "insert"; - - @POST - @Consumes({MediaType.TEXT_PLAIN, AccountingResource.APPLICATION_JSON_CHARSET_UTF_8}) - public Response add(String json) throws Exception { - - log.debug("Going to account : {}", json); - - AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence(); - - List records = DSMapper.unmarshalList(json); - for(Record record : records) { - accountingPersistence.account(record); - } - return Response.status(Status.CREATED).build(); - } - -}