minor fixes and added some of the fields requested by forth (short_title, stock_uri, traceability_flag etc)

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@133611 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-10-26 16:40:10 +00:00
parent 30885e0361
commit 33514c202a
10 changed files with 262 additions and 24 deletions

View File

@ -0,0 +1,19 @@
package org.gcube.data_catalogue.grsf_publish_ws;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/")
@Singleton
public class WelcomeService {
@GET
@Produces(MediaType.TEXT_HTML)
public Response sayHtmlHello() {
return Response.ok("<html><body><h2>The grsf publisher web service is up and running!</h2></body></html>").build();
}
}

View File

@ -3,6 +3,7 @@ package org.gcube.data_catalogue.grsf_publish_ws.filters;
import static org.gcube.common.authorization.client.Constants.authorizationService; import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter; import javax.ws.rs.container.ContainerRequestFilter;
@ -21,6 +22,10 @@ import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseBean; import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseBean;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.common.base.Charsets;
import eu.trentorise.opendata.traceprov.internal.org.apache.commons.io.IOUtils;
/** /**
* Requests filter: is invoked before any request reaches a service method * Requests filter: is invoked before any request reaches a service method
@ -31,7 +36,7 @@ public class RequestsAuthAccountingFilter implements ContainerRequestFilter{
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(RequestsAuthAccountingFilter.class); private static final org.slf4j.Logger logger = LoggerFactory.getLogger(RequestsAuthAccountingFilter.class);
private static final String AUTH_TOKEN = "gcube-token"; private static final String AUTH_TOKEN = "gcube-token";
@Context UriInfo info; @Context UriInfo info;
@Override @Override
@ -40,10 +45,25 @@ public class RequestsAuthAccountingFilter implements ContainerRequestFilter{
logger.info("Intercepted request, checking if it contains authorization token"); logger.info("Intercepted request, checking if it contains authorization token");
String pathRequest = info.getAbsolutePath().toString(); String pathRequest = info.getAbsolutePath().toString();
if(pathRequest.contains("hello") || pathRequest.endsWith("rest/")) if(pathRequest.contains("hello") || pathRequest.endsWith("rest/"))
return; return;
try {
if (isJson(requestContext)) {
String json = IOUtils.toString(requestContext.getEntityStream(), Charsets.UTF_8);
// do whatever you need with json
// replace input stream for Jersey as we've already read it
InputStream in = IOUtils.toInputStream(json);
logger.debug("JSON REQUEST IS " + json);
requestContext.setEntityStream(in);
}
} catch (Exception ex) {
logger.error("JSON IS MISSING", ex);
}
// check if the request contains gcube-token // check if the request contains gcube-token
String tokenInHeader = null, tokenAsQueryParameter = null; String tokenInHeader = null, tokenAsQueryParameter = null;
MultivaluedMap<String, String> headers = requestContext.getHeaders(); MultivaluedMap<String, String> headers = requestContext.getHeaders();
@ -109,4 +129,10 @@ public class RequestsAuthAccountingFilter implements ContainerRequestFilter{
return res; return res;
} }
boolean isJson(ContainerRequestContext request) {
// define rules when to read body
return request.getMediaType().toString().contains("application/json");
}
} }

View File

@ -71,7 +71,22 @@ public class Common {
@Group @Group
@CustomField(key="Type") @CustomField(key="Type")
private Type type; private Type type;
@JsonProperty("short_title")
@CustomField(key="Short Title")
@NotNull
private String shortTitle;
@JsonProperty("uuid_knowledge_base")
@CustomField(key="UUID Knowledge Base")
@NotNull
private String uuid;
@JsonProperty("traceability_flag")
@CustomField(key="Traceability Flag")
@NotNull
private boolean traceabilityFlag;
@JsonProperty("extras") @JsonProperty("extras")
private Map<String, String> extras = new HashMap<>(); private Map<String, String> extras = new HashMap<>();
@ -92,13 +107,18 @@ public class Common {
* @param sourceOfInformation * @param sourceOfInformation
* @param dataOwner * @param dataOwner
* @param type * @param type
* @param shortTitle
* @param uuid
* @param traceabilityFlag
* @param extras * @param extras
*/ */
public Common(String description, String license, String author, public Common(String description, String license, String author,
Long version, String authorContact, String maintainer, Long version, String authorContact, String maintainer,
String maintainerContact, String catchesOrLandings, String maintainerContact, String catchesOrLandings,
List<DatabaseSource> databaseSources, List<Resource> sourceOfInformation, List<DatabaseSource> databaseSources,
String dataOwner, Type type, Map<String, String> extras) { List<Resource> sourceOfInformation, String dataOwner, Type type,
String shortTitle, String uuid, boolean traceabilityFlag,
Map<String, String> extras) {
super(); super();
this.description = description; this.description = description;
this.license = license; this.license = license;
@ -112,6 +132,9 @@ public class Common {
this.sourceOfInformation = sourceOfInformation; this.sourceOfInformation = sourceOfInformation;
this.dataOwner = dataOwner; this.dataOwner = dataOwner;
this.type = type; this.type = type;
this.shortTitle = shortTitle;
this.uuid = uuid;
this.traceabilityFlag = traceabilityFlag;
this.extras = extras; this.extras = extras;
} }
@ -219,6 +242,30 @@ public class Common {
return extras; return extras;
} }
public String getShortTitle() {
return shortTitle;
}
public void setShortTitle(String shortTitle) {
this.shortTitle = shortTitle;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public boolean isTraceabilityFlag() {
return traceabilityFlag;
}
public void setTraceabilityFlag(boolean traceabilityFlag) {
this.traceabilityFlag = traceabilityFlag;
}
@Override @Override
public String toString() { public String toString() {
return "Common [description=" + description + ", license=" + license return "Common [description=" + description + ", license=" + license
@ -228,8 +275,9 @@ public class Common {
+ ", catchesOrLandings=" + catchesOrLandings + ", catchesOrLandings=" + catchesOrLandings
+ ", databaseSources=" + databaseSources + ", databaseSources=" + databaseSources
+ ", sourceOfInformation=" + sourceOfInformation + ", sourceOfInformation=" + sourceOfInformation
+ ", dataOwner=" + dataOwner + ", type=" + type + ", extras=" + ", dataOwner=" + dataOwner + ", type=" + type
+ ", shortTitle=" + shortTitle + ", uuid=" + uuid
+ ", traceabilityFlag=" + traceabilityFlag + ", extras="
+ extras + "]"; + extras + "]";
} }
} }

View File

@ -9,12 +9,14 @@ import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Production_System_Type; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Production_System_Type;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* A fishery record bean * A fishery record bean
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/ */
@JsonIgnoreProperties(ignoreUnknown = true)
public class FisheryRecord extends Common{ public class FisheryRecord extends Common{
@JsonProperty("fishery_name") @JsonProperty("fishery_name")

View File

@ -1,5 +1,7 @@
package org.gcube.data_catalogue.grsf_publish_ws.json.input; package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import java.util.List;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
@ -10,12 +12,14 @@ import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Abundance_Level;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Exploitation_Rate; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Exploitation_Rate;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* A stock record bean * A stock record bean
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/ */
@JsonIgnoreProperties(ignoreUnknown = true)
public class StockRecord extends Common{ public class StockRecord extends Common{
@JsonProperty("stock_name") @JsonProperty("stock_name")
@ -37,8 +41,8 @@ public class StockRecord extends Common{
@JsonProperty("assessment_distribution_area") @JsonProperty("assessment_distribution_area")
@CustomField(key="Assessment distribution area") @CustomField(key="Assessment distribution area")
@NotNull(message="area cannot be null") @NotNull(message="assessment distribution cannot be null")
@Size(min=1, message="area cannot be empty") @Size(min=1, message="assessment distribution area cannot be empty")
private String area; private String area;
@JsonProperty("exploiting_fishery") @JsonProperty("exploiting_fishery")
@ -84,7 +88,7 @@ public class StockRecord extends Common{
@JsonProperty("reporting_year") @JsonProperty("reporting_year")
@CustomField(key="Reporting year") @CustomField(key="Reporting year")
private Long reportingYear; private Long reportingYear;
@JsonProperty("status") @JsonProperty("status")
@Tag @Tag
@Group @Group
@ -92,6 +96,16 @@ public class StockRecord extends Common{
@CustomField(key="Status") @CustomField(key="Status")
private Status status; private Status status;
@JsonProperty("stock_uri")
@NotNull(message="stock_uri cannot be null")
@CustomField(key="Stock Uri")
private String stockUri;
@JsonProperty("water_area")
@NotNull(message="water_area cannot be null")
@CustomField(key="Water Area")
private List<String> waterArea;// TODO check that multiple values are mapped to ckan
/** /**
* *
*/ */
@ -114,6 +128,9 @@ public class StockRecord extends Common{
* @param scientificAdvice * @param scientificAdvice
* @param reportingEntity * @param reportingEntity
* @param reportingYear * @param reportingYear
* @param status
* @param stockUri
* @param waterArea
*/ */
public StockRecord(String stockName, String stockID, public StockRecord(String stockName, String stockID,
String speciesScientificName, String area, String speciesScientificName, String area,
@ -121,7 +138,8 @@ public class StockRecord extends Common{
String assessmentMethods, String stateOfMarineResource, String assessmentMethods, String stateOfMarineResource,
Exploitation_Rate exploitationRate, Abundance_Level abundanceLevel, Exploitation_Rate exploitationRate, Abundance_Level abundanceLevel,
String narrativeStateAndTrend, String scientificAdvice, String narrativeStateAndTrend, String scientificAdvice,
String reportingEntity, Long reportingYear) { String reportingEntity, Long reportingYear, Status status,
String stockUri, List<String> waterArea) {
super(); super();
this.stockName = stockName; this.stockName = stockName;
this.stockID = stockID; this.stockID = stockID;
@ -137,6 +155,25 @@ public class StockRecord extends Common{
this.scientificAdvice = scientificAdvice; this.scientificAdvice = scientificAdvice;
this.reportingEntity = reportingEntity; this.reportingEntity = reportingEntity;
this.reportingYear = reportingYear; this.reportingYear = reportingYear;
this.status = status;
this.stockUri = stockUri;
this.waterArea = waterArea;
}
public String getStockUri() {
return stockUri;
}
public void setStockUri(String stockUri) {
this.stockUri = stockUri;
}
public List<String> getWaterArea() {
return waterArea;
}
public void setWaterArea(List<String> waterArea) {
this.waterArea = waterArea;
} }
public String getStockName() { public String getStockName() {
@ -250,7 +287,7 @@ public class StockRecord extends Common{
public void setReportingYear(Long reportingYear) { public void setReportingYear(Long reportingYear) {
this.reportingYear = reportingYear; this.reportingYear = reportingYear;
} }
public Status getStatus() { public Status getStatus() {
return status; return status;
} }
@ -261,7 +298,7 @@ public class StockRecord extends Common{
@Override @Override
public String toString() { public String toString() {
return "StockRecord [" + super.toString() + ", stockName=" + stockName + ", stockID=" + stockID return "StockRecord [" + super.toString() + ",stockName=" + stockName + ", stockID=" + stockID
+ ", speciesScientificName=" + speciesScientificName + ", speciesScientificName=" + speciesScientificName
+ ", area=" + area + ", exploitingFishery=" + exploitingFishery + ", area=" + area + ", exploitingFishery=" + exploitingFishery
+ ", managementEntity=" + managementEntity + ", managementEntity=" + managementEntity
@ -272,7 +309,7 @@ public class StockRecord extends Common{
+ ", narrativeStateAndTrend=" + narrativeStateAndTrend + ", narrativeStateAndTrend=" + narrativeStateAndTrend
+ ", scientificAdvice=" + scientificAdvice + ", scientificAdvice=" + scientificAdvice
+ ", reportingEntity=" + reportingEntity + ", reportingYear=" + ", reportingEntity=" + reportingEntity + ", reportingYear="
+ reportingYear + ", status=" + status + "]"; + reportingYear + ", status=" + status + ", stockUri="
+ stockUri + ", waterArea=" + waterArea + "]";
} }
} }

View File

@ -124,7 +124,7 @@ public class GrsfPublisherFisheryService {
// check the user has editor/admin role into the org // check the user has editor/admin role into the org
String organization = HelperMethods.retrieveOrgNameFromScope(contextInWhichPublish); String organization = HelperMethods.retrieveOrgNameFromScope(contextInWhichPublish);
if(!catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username)).equals(RolesCkanGroupOrOrg.ADMIN)){ if(!catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username)).equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){
status = Status.FORBIDDEN; status = Status.FORBIDDEN;
responseBean.setId(null); responseBean.setId(null);

View File

@ -124,7 +124,7 @@ public class GrsfPublisherStockService {
// check the user has editor/admin role into the org // check the user has editor/admin role into the org
String organization = HelperMethods.retrieveOrgNameFromScope(contextInWhichPublish); String organization = HelperMethods.retrieveOrgNameFromScope(contextInWhichPublish);
if(!catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username)).equals(RolesCkanGroupOrOrg.ADMIN)){ if(!catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username)).equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){
status = Status.FORBIDDEN; status = Status.FORBIDDEN;
responseBean.setId(null); responseBean.setId(null);

View File

@ -249,10 +249,9 @@ public abstract class HelperMethods {
try(CloseableHttpClient client = HttpClientBuilder.create().build();){ try(CloseableHttpClient client = HttpClientBuilder.create().build();){
String baseUrl = GcoreEndpointReaderSocialWS.getUrlSocialWS(context); String baseUrl = new ServiceEndPointReaderSocial(context).getBasePath();
logger.debug("Base url is " + baseUrl); String url = baseUrl.replace("http", "https") + "/users/getUserEmail?gcube-token=" + token;
String url = baseUrl.replace("80", "") + "/users/getUserEmail?gcube-token=" + token; // TODO move to version 2 logger.debug("Request url is " + baseUrl);
logger.debug("Request url is " + url);
HttpGet getRequest = new HttpGet(url); HttpGet getRequest = new HttpGet(url);
HttpResponse response = client.execute(getRequest); HttpResponse response = client.execute(getRequest);
@ -291,9 +290,8 @@ public abstract class HelperMethods {
try(CloseableHttpClient client = HttpClientBuilder.create().build();){ try(CloseableHttpClient client = HttpClientBuilder.create().build();){
String baseUrl = GcoreEndpointReaderSocialWS.getUrlSocialWS(context); String baseUrl = new ServiceEndPointReaderSocial(context).getBasePath();
logger.debug("Base url is " + baseUrl); String url = baseUrl.replace("http", "https") + "/users/getUserEmail?gcube-token=" + token;
String url = baseUrl.replace("80", "") + "/users/getUserFullname?gcube-token=" + token; // TODO move to version 2
logger.debug("Request url is " + url); logger.debug("Request url is " + url);
HttpGet getRequest = new HttpGet(url); HttpGet getRequest = new HttpGet(url);
HttpResponse response = client.execute(getRequest); HttpResponse response = client.execute(getRequest);

View File

@ -0,0 +1,104 @@
package org.gcube.data_catalogue.grsf_publish_ws.utils;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.Iterator;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Retrieves the base url of the social-networking service in the scope provided
* @author Costantino Perciante at ISTI-CNR
* (costantino.perciante@isti.cnr.it)
*/
public class ServiceEndPointReaderSocial {
private String basePath = null;
private static Logger logger = LoggerFactory.getLogger(ServiceEndPointReaderSocial.class);
private final static String RUNTIME_RESOURCE_NAME = "SocialNetworking";
private final static String CATEGORY = "Portal";
public ServiceEndPointReaderSocial(String context){
if(context == null || context.isEmpty())
throw new IllegalArgumentException("A valid context is needed to discover the service");
String oldContext = ScopeProvider.instance.get();
ScopeProvider.instance.set(context);
try{
List<ServiceEndpoint> resources = getConfigurationFromIS();
if (resources.size() == 0){
logger.error("There is no Runtime Resource having name " + RUNTIME_RESOURCE_NAME +" and Category " + CATEGORY + " in this scope.");
throw new Exception("There is no Runtime Resource having name " + RUNTIME_RESOURCE_NAME +" and Category " + CATEGORY + " in this scope.");
}
else {
for (ServiceEndpoint res : resources) {
Iterator<AccessPoint> accessPointIterator = res.profile().accessPoints().iterator();
while (accessPointIterator.hasNext()) {
ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator
.next();
// get base path
basePath = accessPoint.address();
// break
break;
}
}
}
}catch(Exception e){
logger.error("Unable to retrieve such service endpoint information!", e);
}finally{
if(oldContext != null && !oldContext.equals(context))
ScopeProvider.instance.set(oldContext);
}
logger.info("Found base path " + basePath + " for the service");
}
/**
* Retrieve endpoints information from IS for DB
* @return list of endpoints for ckan database
* @throws Exception
*/
private List<ServiceEndpoint> getConfigurationFromIS() throws Exception{
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_RESOURCE_NAME +"'");
query.addCondition("$resource/Profile/Category/text() eq '"+ CATEGORY +"'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> toReturn = client.submit(query);
return toReturn;
}
/**
* Get the base path of the social networking service
* @return
*/
public String getBasePath() {
return basePath;
}
}

View File

@ -1,5 +1,8 @@
package org.gcube.data_catalogue.grsf_publish_ws.utils.groups; package org.gcube.data_catalogue.grsf_publish_ws.utils.groups;
import org.gcube.data_catalogue.grsf_publish_ws.filters.RequestsAuthAccountingFilter;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.annotation.JsonValue;
@ -42,6 +45,7 @@ public enum Status {
*/ */
@JsonCreator @JsonCreator
public static Status onDeserialize(String statusString) { public static Status onDeserialize(String statusString) {
System.out.println("STATUS IS " + statusString);
if(statusString != null) { if(statusString != null) {
for(Status status : Status.values()) { for(Status status : Status.values()) {
if (status.toString().equalsIgnoreCase(statusString.trim())) if (status.toString().equalsIgnoreCase(statusString.trim()))