implemented create resource through multipart (to be tested)

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/catalogue-ws@146513 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-04-02 19:53:33 +00:00
parent 24ad4c8600
commit bfc940ce68
3 changed files with 96 additions and 20 deletions

View File

@ -4,6 +4,9 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="ckan-util-library-2.2.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ckan-util-library/ckan-util-library">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="catalogue-ws"/>
<property name="java-output-path" value="/catalogue-ws/target/classes"/>
</wb-module>

View File

@ -9,6 +9,11 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
@ -42,6 +47,7 @@ import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataGrouping;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataTagging;
import org.geojson.GeoJsonObject;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@ -453,9 +459,9 @@ public class CatalogueUtils {
int lowerBound = entry.getValue();
int upperBound = fieldsMandatoryUpperBoundMap.get(entry.getKey());
int inserted = numberFieldsSameKeyMap.get(entry.getKey());
logger.info("Field with key '" + entry.getKey() + "' has been found " + inserted + " times and its lower bound is " + lowerBound + " and upper bound " + upperBound);
if(inserted < lowerBound || inserted > upperBound)
throw new Exception("Field with key '" + entry.getKey() + "' is mandatory, but it's not present among the provided fields or its cardinality is not respected (" + lowerBound + ").");
}
@ -549,14 +555,14 @@ public class CatalogueUtils {
lowerBound = fieldsMandatoryLowerBoundMap.get(metadataFieldName) + 1;
fieldsMandatoryLowerBoundMap.put(metadataFieldName, lowerBound);
// upper bound
boolean hasVocabulary = metadataField.getVocabulary() != null;
int upperBound = hasVocabulary ? (metadataField.getVocabulary().isMultiSelection() ? Integer.MAX_VALUE : 1) : 1;
if(fieldsMandatoryUpperBoundMap.containsKey(metadataFieldName))
upperBound += fieldsMandatoryUpperBoundMap.get(metadataFieldName);
fieldsMandatoryUpperBoundMap.put(metadataFieldName, lowerBound);
int countPerFields = fieldsFoundWithThisKey;
@ -956,6 +962,82 @@ public class CatalogueUtils {
}
/**
* Execute post with multipart (e.g. for resource upload)
* @param caller
* @param context
* @param resourceCreate
* @param multiPart
* @param uriInfo
* @return
*/
public static String delegatePost(Caller caller, String context,
String method, FormDataMultiPart multiPart, UriInfo uriInfo) {
String username = caller.getClient().getId();
DataCatalogue catalogue = CatalogueUtils.getCatalogue();
if(catalogue == null){
String msg = "There is no catalogue instance in context " + context + " or a temporary problem arised.";
logger.warn(msg);
return CatalogueUtils.createJSONOnFailure(msg);
}else{
try{
String authorization = catalogue.getApiKeyFromUsername(username);
String requestPath = catalogue.getCatalogueUrl().endsWith("/") ? catalogue.getCatalogueUrl() : catalogue.getCatalogueUrl() + "/";
requestPath += method + "?";
MultivaluedMap<String, String> undecodedParams = uriInfo.getQueryParameters(false);
Iterator<Entry<String, List<String>>> iterator = undecodedParams.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<java.lang.String, java.util.List<java.lang.String>> entry = (Map.Entry<java.lang.String, java.util.List<java.lang.String>>) iterator
.next();
if(entry.getKey().equals("gcube-token"))
continue;
else{
List<String> values = entry.getValue();
for (String value : values) {
requestPath += entry.getKey() + "=" + value + "&";
}
}
}
if(requestPath.endsWith("&"))
requestPath = requestPath.substring(0, requestPath.length() - 1);
logger.trace("POST request url is going to be " + requestPath);
// use jersey client
logger.trace("Sending multipart to CKAN " + multiPart);
Client client = ClientBuilder.newClient();
WebTarget webResource = client.target(requestPath);
String jsonRes =
webResource
.request(MediaType.APPLICATION_JSON)
.header(Constants.AUTH_CKAN_HEADER, authorization)
.post(Entity.entity(multiPart, multiPart.getMediaType()), String.class);
logger.trace("Result from CKAN is " + jsonRes);
// substitute "help" field
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(jsonRes);
obj.put(HELP_KEY, HELP_URL_GCUBE_CATALOGUE);
return obj.toJSONString();
}catch(Exception e){
logger.error("Failed to serve the request", e);
return CatalogueUtils.createJSONOnFailure("Failed to serve the request: " + e.getMessage());
}
}
}
// =======================================================================
// SOCIAL FACILITIES
// =======================================================================

View File

@ -1,9 +1,6 @@
package org.gcube.datacatalogue.catalogue.ws;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
@ -12,8 +9,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
@ -21,8 +16,6 @@ import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils;
import org.gcube.datacatalogue.catalogue.utils.Constants;
import org.glassfish.jersey.media.multipart.BodyPart;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
@Path(Constants.RESOURCES)
@ -57,12 +50,12 @@ public class Resource {
@Path(Constants.CREATE_METHOD)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response create(
public String create(
FormDataMultiPart multiPart, @Context UriInfo uriInfo
){
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.resource_create
// see also multipart https://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
/*List<BodyPart> bodyParts = multiPart.getBodyParts();
for (BodyPart bodyPart : bodyParts) {
System.out.println("Body name is " + bodyPart.getContentDisposition().getFileName());
@ -71,12 +64,10 @@ public class Resource {
Map<String, List<FormDataBodyPart>> fields = multiPart.getFields();
System.out.println(fields);*/
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.resource_create
// see also multipart https://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
/*Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return CatalogueUtils.delegatePost(caller, context, Constants.RESOURCE_CREATE, multiPart, uriInfo);*/
return Response.status(Status.NOT_IMPLEMENTED).build();
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
return CatalogueUtils.delegatePost(caller, context, Constants.RESOURCE_CREATE, multiPart, uriInfo);
//return Response.status(Status.NOT_IMPLEMENTED).build();
}