uri-resolver/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java

192 lines
7.5 KiB
Java

/**
*
*/
package org.gcube.datatransfer.resolver.services;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
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.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.gcube.datatransfer.resolver.catalogue.ItemCatalogueURLs;
import org.gcube.datatransfer.resolver.catalogue.ResourceCatalogueCodes;
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit;
import org.gcube.datatransfer.resolver.parthenos.ParthenosRequest;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class PartheosRegistryResolver.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Nov 16, 2018
*/
@Path("parthenosregistry")
public class PartheosRegistryResolver {
/**
*
*/
//private static final String PARTHENOS_REGISTRY_VRE_NAME = "PARTHENOS_Registry";
private static Logger logger = LoggerFactory.getLogger(PartheosRegistryResolver.class);
private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver";
/**
* Resolve parthenos url.
*
* @param req the req
* @param provider the provider
* @param path the path
* @param remainPath the remain path
* @return the response
*/
@GET
@Path("/{provider}/{path}{remainPath:(/[^?$]+)?}")
public Response resolveParthenosURL(@Context HttpServletRequest req,@PathParam("provider") String provider, @PathParam("path") String path, @PathParam("remainPath") String remainPath) {
logger.info(this.getClass().getSimpleName()+" GET starts...");
String remainPathParthenosURL = null;
try {
logger.debug("provider is: "+provider);
logger.debug("path is: "+path);
logger.debug("remainPath is: "+remainPath);
remainPathParthenosURL = String.format("%s/%s",provider,path);
if(remainPath!=null && !remainPath.isEmpty()){
remainPathParthenosURL+=remainPath.startsWith("/")?remainPath:"/"+remainPath;
}
logger.info("Resolving parthenos URL: "+remainPathParthenosURL);
//APPLYING URL DECODING
remainPathParthenosURL = URLDecoder.decode(remainPathParthenosURL, "UTF-8");
//APPLYING NAME TRANSFORMATION
String normalizedEntityName = toNameForCatalogue(remainPathParthenosURL);
logger.info("Trying to resolve with Catalogue EntityName: "+normalizedEntityName);
ItemCatalogueURLs itemCatalogueURLs = CatalogueResolver.getItemCatalogueURLs(UriResolverSmartGearManagerInit.getParthenosVREName(), ResourceCatalogueCodes.CTLGD.getId(), normalizedEntityName);
return Response.seeOther(new URL(itemCatalogueURLs.getPrivateCataloguePortletURL()).toURI()).build();
}catch (Exception e) {
logger.error("Exception:", e);
String error = "Error occurred on resolving the path "+remainPathParthenosURL+". Please, contact the support!";
ExceptionManager.throwInternalErrorException(req, error, this.getClass(), helpURI);
return null;
}
}
/**
* Post catalogue.
*
* @param req the req
* @param jsonRequest the json request
* @return the response
*/
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response postCatalogue(@Context HttpServletRequest req, ParthenosRequest jsonRequest) {
logger.info(this.getClass().getSimpleName()+" POST starts...");
try{
String entityName = jsonRequest.getEntity_name();
if(entityName==null || entityName.isEmpty()){
logger.error("Entity Name Parameter like 'entity_name' not found or empty");
ExceptionManager.throwBadRequestException(req, "Mandatory body parameter 'entity_name' not found or empty", this.getClass(), helpURI);
}
//REMOVING FIRST '/' IF EXISTS
entityName = entityName.startsWith("/")?entityName.substring(1,entityName.length()):entityName;
//APPLYING NAME TRANSFORMATION
String normalizedEntityName = toNameForCatalogue(entityName);
ItemCatalogueURLs itemCatalogueURLs = CatalogueResolver.getItemCatalogueURLs(UriResolverSmartGearManagerInit.getParthenosVREName(), ResourceCatalogueCodes.CTLGD.getId(), normalizedEntityName);
logger.info("Returining Catalogue URL: "+itemCatalogueURLs.getPrivateCataloguePortletURL());
return Response.ok(normalizedEntityName).header("Location", itemCatalogueURLs.getPrivateCataloguePortletURL()).build();
//TODO
/*InnerMethodName.instance.set("postCataloguePublicLink");
logger.info(CatalogueResolver.class.getSimpleName()+" POST starts...");
logger.info("The body contains the request: "+jsonRequest.toString());
//final CatalogueEntityRequest cer = new CatalogueEntityRequest();
//CHECK IF INPUT SCOPE IS VALID
String scope = jsonRequest.getGcube_scope();
if(!scope.startsWith("/")){
logger.info("Scope not start with char '/' adding it");
scope+="/"+scope;
}
String serverUrl = getServerURL(req);
final String vreName = scope.substring(scope.lastIndexOf("/")+1, scope.length());
String fullScope = null;
//CHECK IF THE vreName has a valid scope, so it is a valid VRE
try {
fullScope = LoadingVREsScopeCache.getCache().get(vreName);
}
catch (ExecutionException e1) {
logger.error("Error on getting full scope for vre name: "+vreName, e1);
}
if(fullScope==null)
ExceptionManager.throwNotFoundException(req, "The scope '"+scope+"' does not matching any scope in the infrastructure. Is it valid?", this.getClass(), helpURI);
ResourceCatalogueCodes rc = ResourceCatalogueCodes.valueOfCodeValue(jsonRequest.getEntity_context());
if(rc==null){
logger.error("Entity context is null/malformed");
ExceptionManager.throwBadRequestException(req, "Entity context is null/malformed", this.getClass(), helpURI);
//throw new WebApplicationException("Entity context is null/malformed", Status.BAD_REQUEST);
}
String linkURL = String.format("%s/%s/%s/%s", serverUrl, rc.getId(), vreName, jsonRequest.getEntity_name());
logger.info("Returining Catalogue URL: "+linkURL);
return Response.ok(linkURL).header("Location", linkURL).build();*/
}catch (Exception e) {
logger.error("Exception:", e);
String error = "Error occurred on transforming the "+jsonRequest+". Please, contact the support!";
ExceptionManager.throwInternalErrorException(req, error, this.getClass(), helpURI);
return null;
}
}
/**
* To name for catalogue.
* this method applyes a fuction to transform a parthenos URL to acceptable catalogue name (that is URL)
* @param remainPathParthenosURL the remain path parthenos url
* @return the string
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
protected String toNameForCatalogue(final String remainPathParthenosURL) throws UnsupportedEncodingException {
//need to double decode for URLs like: http://parthenos.d4science.org/handle/Parthenos/REG/Dataset/Appellation/Isidore%2520Dataset
String name = StringUtils.replaceChars(URLDecoder.decode(remainPathParthenosURL,"UTF-8"),"/ .:", "_").toLowerCase().replaceAll("[^A-Za-z0-9]", "_");
//TO LOWERCASE FOR CKAN SUPPORTING
return name.toLowerCase();
}
// public static void main(String[] args) throws UnsupportedEncodingException {
//
// String remainPathParthenosURL = "Culturalitalia/unknown/Dataset/oai%3Aculturaitalia.it%3Aoai%3Aculturaitalia.it%3Amuseiditalia-mus_11953";
// System.out.println(URLDecoder.decode(remainPathParthenosURL, "UTF-8"));
//
// }
}