|
|
|
@ -3,8 +3,6 @@ package org.gcube.datatransfer.resolver.services;
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
@ -19,6 +17,7 @@ import javax.ws.rs.POST;
|
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
|
import javax.ws.rs.PathParam;
|
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
|
|
|
@ -37,7 +36,6 @@ import org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkRequestFilterPar
|
|
|
|
|
import org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkRequestFilterParameters.VISIBILITY;
|
|
|
|
|
import org.gcube.datatransfer.resolver.gis.geonetwork.ReusableInputStream;
|
|
|
|
|
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
|
|
|
|
|
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
|
|
|
|
|
import org.gcube.datatransfer.resolver.util.GetResponseRecordFilter;
|
|
|
|
|
import org.gcube.datatransfer.resolver.util.HTTPCallsUtils;
|
|
|
|
|
import org.gcube.datatransfer.resolver.util.HTTPCallsUtils.HttpResponse;
|
|
|
|
@ -89,7 +87,7 @@ public class GeonetworkResolver {
|
|
|
|
|
|
|
|
|
|
protected Map<String, GeonetworkInstance> cacheGNInstances;
|
|
|
|
|
|
|
|
|
|
private String help = "https://wiki.gcube-system.org/gcube/GCube_Resource_Catalogue#Geonetwork_Resolver";
|
|
|
|
|
private String helpURI = "https://wiki.gcube-system.org/gcube/GCube_Resource_Catalogue#Geonetwork_Resolver";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the geonetwork request criteria.
|
|
|
|
@ -122,107 +120,110 @@ public class GeonetworkResolver {
|
|
|
|
|
@PathParam("filterValue") @Nullable String filterValue,
|
|
|
|
|
@PathParam(PATH_PARAM_REMAINPATH) @Nullable String remainPath,
|
|
|
|
|
@QueryParam(QUERY_PARAM_RESET_CACHE_PARAM) @Nullable String resetCache,
|
|
|
|
|
@QueryParam(QUERY_PARAM_RESET_CACHED_SCOPE_PARAM) @Nullable String resetScope) {
|
|
|
|
|
@QueryParam(QUERY_PARAM_RESET_CACHED_SCOPE_PARAM) @Nullable String resetScope) throws WebApplicationException{
|
|
|
|
|
|
|
|
|
|
logger.info(this.getClass().getSimpleName()+" GET starts...");
|
|
|
|
|
logger.info("Params are [mode: "+mode+", scope: "+scope+", visibility: "+visibility+", filterKey: "+filterKey+", filterValue: "+filterValue+", remainPath: "+remainPath+"]");
|
|
|
|
|
|
|
|
|
|
if(scope==null || scope.isEmpty()){
|
|
|
|
|
logger.error("Path Parameter 'scope' not found");
|
|
|
|
|
ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(mode==null || mode.isEmpty()){
|
|
|
|
|
logger.error("Path Parameter 'scope' not found");
|
|
|
|
|
ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope = ScopeUtil.normalizeScope(scope, "|");
|
|
|
|
|
mode = mode.toUpperCase();
|
|
|
|
|
try{
|
|
|
|
|
MODE.valueOf(mode);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
List<MODE> toPrint = Arrays.asList(MODE.values());
|
|
|
|
|
logger.error("The 'mode' parameter is wrong, Have you pass a valid parameter MODE like "+toPrint+"?");
|
|
|
|
|
ExceptionManager.wrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(visibility==null){
|
|
|
|
|
logger.error("Path Parameter 'visibility' not found");
|
|
|
|
|
ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
visibility = visibility.toUpperCase();
|
|
|
|
|
try{
|
|
|
|
|
VISIBILITY.valueOf(visibility);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
List<VISIBILITY> toPrint = Arrays.asList(VISIBILITY.values());
|
|
|
|
|
logger.error("The 'visibility' parameter is wrong, Have you pass a valid parameter VISIBILITY like "+toPrint+"?");
|
|
|
|
|
ExceptionManager.wrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(resetCache!=null && Boolean.parseBoolean(resetCache)){
|
|
|
|
|
purgeCacheGeonetworkInstances();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(resetScope!=null && Boolean.parseBoolean(resetScope)){
|
|
|
|
|
resetGeonetoworkInstanceCacheForScope(scope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("Remaining path is: "+remainPath);
|
|
|
|
|
String gnGetlURL = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
logger.info("Params are [mode: "+mode+", scope: "+scope+", visibility: "+visibility+", filterKey: "+filterKey+", filterValue: "+filterValue+", remainPath: "+remainPath+"]");
|
|
|
|
|
|
|
|
|
|
GeonetworkInstance gnInstance = getGeonetworkInstanceForScope(scope);
|
|
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
|
|
|
|
|
|
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
|
|
|
|
|
Configuration config = gnInstance.getGeonetworkPublisher().getConfiguration();
|
|
|
|
|
String geonetworkUrl = config.getGeoNetworkEndpoint();
|
|
|
|
|
String baseURL = remainPath==null ||remainPath.isEmpty()?geonetworkUrl+"/"+CSW_SERVER:geonetworkUrl+"/"+CSW_SERVER+remainPath;
|
|
|
|
|
logger.info("The base URL is: "+baseURL);
|
|
|
|
|
String queryString = req.getQueryString()==null || req.getQueryString().isEmpty()?"":"?"+req.getQueryString();
|
|
|
|
|
gnGetlURL = baseURL+queryString;
|
|
|
|
|
logger.info("Sending get request to URL: "+gnGetlURL);
|
|
|
|
|
HttpResponse proxedGNResponse = httpUtils.get(gnGetlURL);
|
|
|
|
|
|
|
|
|
|
switch(proxedGNResponse.getStatus()){
|
|
|
|
|
case HttpServletResponse.SC_OK:
|
|
|
|
|
|
|
|
|
|
//Building the response
|
|
|
|
|
InputStream streamToWrite = IOUtils.toInputStream(proxedGNResponse.getResponse());
|
|
|
|
|
StreamingOutput so = new SingleFileStreamingOutput(streamToWrite);
|
|
|
|
|
|
|
|
|
|
logger.info("Response return Content-Type: "+httpUtils.getLastContentType());
|
|
|
|
|
|
|
|
|
|
ResponseBuilder responseBuilder = Response
|
|
|
|
|
.ok(so)
|
|
|
|
|
.header(ConstantsResolver.CONTENT_TYPE, httpUtils.getLastContentType());
|
|
|
|
|
return responseBuilder.build();
|
|
|
|
|
|
|
|
|
|
case HttpServletResponse.SC_FORBIDDEN:
|
|
|
|
|
ExceptionManager.forbiddenException(req, "You are not authorized to perform the request "+gnGetlURL, this.getClass(), help);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
ExceptionManager.internalErrorException(req, "Sorry, an error occurred performing the geonetwork request "+gnGetlURL+" with scope "+scope, this.getClass(), help);
|
|
|
|
|
if(scope==null || scope.isEmpty()){
|
|
|
|
|
logger.error("Path Parameter 'scope' not found");
|
|
|
|
|
throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if(mode==null || mode.isEmpty()){
|
|
|
|
|
logger.error("Path Parameter 'scope' not found");
|
|
|
|
|
throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope = ScopeUtil.normalizeScope(scope, "|");
|
|
|
|
|
mode = mode.toUpperCase();
|
|
|
|
|
try{
|
|
|
|
|
MODE.valueOf(mode);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
List<MODE> toPrint = Arrays.asList(MODE.values());
|
|
|
|
|
logger.error("The 'mode' parameter is wrong, Have you pass a valid parameter MODE like "+toPrint+"?");
|
|
|
|
|
throw ExceptionManager.wrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(visibility==null){
|
|
|
|
|
logger.error("Path Parameter 'visibility' not found");
|
|
|
|
|
throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
visibility = visibility.toUpperCase();
|
|
|
|
|
try{
|
|
|
|
|
VISIBILITY.valueOf(visibility);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
List<VISIBILITY> toPrint = Arrays.asList(VISIBILITY.values());
|
|
|
|
|
logger.error("The 'visibility' parameter is wrong, Have you pass a valid parameter VISIBILITY like "+toPrint+"?");
|
|
|
|
|
throw ExceptionManager.wrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(resetCache!=null && Boolean.parseBoolean(resetCache)){
|
|
|
|
|
purgeCacheGeonetworkInstances();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(resetScope!=null && Boolean.parseBoolean(resetScope)){
|
|
|
|
|
resetGeonetoworkInstanceCacheForScope(scope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("Remaining path is: "+remainPath);
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
GeonetworkInstance gnInstance = getGeonetworkInstanceForScope(scope);
|
|
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
|
|
|
|
|
|
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
|
|
|
|
|
Configuration config = gnInstance.getGeonetworkPublisher().getConfiguration();
|
|
|
|
|
String geonetworkUrl = config.getGeoNetworkEndpoint();
|
|
|
|
|
String baseURL = remainPath==null ||remainPath.isEmpty()?geonetworkUrl+"/"+CSW_SERVER:geonetworkUrl+"/"+CSW_SERVER+remainPath;
|
|
|
|
|
logger.info("The base URL is: "+baseURL);
|
|
|
|
|
String queryString = req.getQueryString()==null || req.getQueryString().isEmpty()?"":"?"+req.getQueryString();
|
|
|
|
|
gnGetlURL = baseURL+queryString;
|
|
|
|
|
logger.info("Sending get request to URL: "+gnGetlURL);
|
|
|
|
|
HttpResponse proxedGNResponse = httpUtils.get(gnGetlURL);
|
|
|
|
|
|
|
|
|
|
switch(proxedGNResponse.getStatus()){
|
|
|
|
|
case HttpServletResponse.SC_OK:
|
|
|
|
|
|
|
|
|
|
//Building the response
|
|
|
|
|
InputStream streamToWrite = IOUtils.toInputStream(proxedGNResponse.getResponse());
|
|
|
|
|
StreamingOutput so = new SingleFileStreamingOutput(streamToWrite);
|
|
|
|
|
|
|
|
|
|
logger.info("Response return Content-Type: "+httpUtils.getLastContentType());
|
|
|
|
|
|
|
|
|
|
ResponseBuilder responseBuilder = Response
|
|
|
|
|
.ok(so)
|
|
|
|
|
.header(ConstantsResolver.CONTENT_TYPE, httpUtils.getLastContentType());
|
|
|
|
|
return responseBuilder.build();
|
|
|
|
|
|
|
|
|
|
case HttpServletResponse.SC_FORBIDDEN:
|
|
|
|
|
throw ExceptionManager.forbiddenException(req, "You are not authorized to perform the request "+gnGetlURL, this.getClass(), helpURI);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw ExceptionManager.internalErrorException(req, "Sorry, an error occurred performing the geonetwork request "+gnGetlURL+" with scope "+scope, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Exception:", e);
|
|
|
|
|
String error = "Sorry, an error occurred on resolving geonetwork request with scope "+scope+". Please, contact support!";
|
|
|
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
if(!(e instanceof WebApplicationException)){
|
|
|
|
|
//UNEXPECTED EXCEPTION managing it as WebApplicationException
|
|
|
|
|
String error = "Error during perform GET operation to: "+gnGetlURL+". Please, contact the support!";
|
|
|
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
//ALREADY MANAGED AS WebApplicationException
|
|
|
|
|
logger.error("Exception:", e);
|
|
|
|
|
String error = "Sorry, an error occurred on resolving geonetwork request with scope "+scope+". Please, contact support!";
|
|
|
|
|
ExceptionManager.internalErrorException(req, error, this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//An error occurred
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
throw new InternalServerException(req, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR, "Error during perform GET operation to: "+gnGetlURL, this.getClass(), new URI(help));
|
|
|
|
|
}
|
|
|
|
|
catch (URISyntaxException e) {
|
|
|
|
|
//silent
|
|
|
|
|
return null;
|
|
|
|
|
throw (WebApplicationException) e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -237,233 +238,238 @@ public class GeonetworkResolver {
|
|
|
|
|
@PathParam("filterValue") @Nullable String filterValue,
|
|
|
|
|
@PathParam(PATH_PARAM_REMAINPATH) @Nullable String remainPath,
|
|
|
|
|
@QueryParam(QUERY_PARAM_RESET_CACHE_PARAM) @Nullable String resetCache,
|
|
|
|
|
@QueryParam(QUERY_PARAM_RESET_CACHED_SCOPE_PARAM) @Nullable String resetScope) {
|
|
|
|
|
@QueryParam(QUERY_PARAM_RESET_CACHED_SCOPE_PARAM) @Nullable String resetScope) throws WebApplicationException{
|
|
|
|
|
|
|
|
|
|
logger.info(this.getClass().getSimpleName()+" POST starts...");
|
|
|
|
|
logger.info("Params are [mode: "+mode+", scope: "+scope+", visibility: "+visibility+", filterKey: "+filterKey+", filterValue: "+filterValue+", remainPath: "+remainPath+"]");
|
|
|
|
|
|
|
|
|
|
if(scope==null || scope.isEmpty()){
|
|
|
|
|
logger.error("Path Parameter 'scope' not found");
|
|
|
|
|
ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(mode==null || mode.isEmpty()){
|
|
|
|
|
logger.error("Path Parameter 'scope' not found");
|
|
|
|
|
ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope = ScopeUtil.normalizeScope(scope, "|");
|
|
|
|
|
mode = mode.toUpperCase();
|
|
|
|
|
try{
|
|
|
|
|
MODE.valueOf(mode);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
List<MODE> toPrint = Arrays.asList(MODE.values());
|
|
|
|
|
logger.error("The 'mode' parameter is wrong, Have you pass a valid parameter MODE like "+toPrint+"?");
|
|
|
|
|
ExceptionManager.wrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(visibility==null){
|
|
|
|
|
logger.error("Path Parameter 'visibility' not found");
|
|
|
|
|
ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
visibility = visibility.toUpperCase();
|
|
|
|
|
try{
|
|
|
|
|
VISIBILITY.valueOf(visibility);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
List<VISIBILITY> toPrint = Arrays.asList(VISIBILITY.values());
|
|
|
|
|
logger.error("The 'visibility' parameter is wrong, Have you pass a valid parameter VISIBILITY like "+toPrint+"?");
|
|
|
|
|
ExceptionManager.wrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//HOW TO PASS ANY FILTER?
|
|
|
|
|
//TODO INVESTIGATE ON HOW TO PASS MORE THAN ONE FILTER...
|
|
|
|
|
Map<String,String> filters = new HashMap<String, String>();
|
|
|
|
|
if(filterKey!=null && filterValue!=null){
|
|
|
|
|
//IGNORING THE EMPTY FILTER 'null|null'
|
|
|
|
|
if(filterKey.compareToIgnoreCase("null")!=0){
|
|
|
|
|
filters.put(filterKey, filterValue);
|
|
|
|
|
logger.debug("Added filter parmas to map filters: "+filters);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String gnCSWlURL = null;
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
GeonetworkServiceInterface gntwAccess = new GeonetworkAccessParameter(scope);
|
|
|
|
|
GeonetworkInstance gnInstance = gntwAccess.getGeonetworkInstance();
|
|
|
|
|
try{
|
|
|
|
|
logger.info("Params are [mode: "+mode+", scope: "+scope+", visibility: "+visibility+", filterKey: "+filterKey+", filterValue: "+filterValue+", remainPath: "+remainPath+"]");
|
|
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
|
logger.info("set scope provider "+scope);
|
|
|
|
|
Configuration config = gnInstance.getGeonetworkPublisher().getConfiguration();
|
|
|
|
|
Account account = config.getScopeConfiguration().getAccounts().get(Type.CKAN);
|
|
|
|
|
logger.info("CKAN user owner is: "+account.getUser());
|
|
|
|
|
if(scope==null || scope.isEmpty()){
|
|
|
|
|
logger.error("Path Parameter 'scope' not found");
|
|
|
|
|
throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// logger.info("Parameters..");
|
|
|
|
|
// for (Enumeration<String> e = req.getParameterNames(); e.hasMoreElements();){
|
|
|
|
|
// String p = e.nextElement();
|
|
|
|
|
// logger.debug("param "+p + " value "+Arrays.toString(req.getParameterValues(p)));
|
|
|
|
|
// }
|
|
|
|
|
if(mode==null || mode.isEmpty()){
|
|
|
|
|
logger.error("Path Parameter 'scope' not found");
|
|
|
|
|
ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//DEBUG BODY
|
|
|
|
|
// String readBody = IOUtils.toString(req.getReader());
|
|
|
|
|
// logger.debug("doPost read body request: "+readBody);
|
|
|
|
|
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
|
|
|
|
|
scope = ScopeUtil.normalizeScope(scope, "|");
|
|
|
|
|
mode = mode.toUpperCase();
|
|
|
|
|
try{
|
|
|
|
|
MODE.valueOf(mode);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
List<MODE> toPrint = Arrays.asList(MODE.values());
|
|
|
|
|
logger.error("The 'mode' parameter is wrong, Have you pass a valid parameter MODE like "+toPrint+"?");
|
|
|
|
|
throw ExceptionManager.wrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String geonetworkUrl = config.getGeoNetworkEndpoint();
|
|
|
|
|
if(visibility==null){
|
|
|
|
|
logger.error("Path Parameter 'visibility' not found");
|
|
|
|
|
throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SPECIFIC HANDLER FOR GEONETWORK REQUEST: /srv/en/mef.export
|
|
|
|
|
if(remainPath!=null && remainPath.compareTo(SRV_EN_MEF_EXPORT)==0){
|
|
|
|
|
logger.info("In case of mef.export, perfoming a custom handler");
|
|
|
|
|
gnCSWlURL = geonetworkUrl + SRV_EN_MEF_EXPORT;
|
|
|
|
|
String[] uuidValues = req.getParameterValues(UUID);
|
|
|
|
|
if(uuidValues!=null){
|
|
|
|
|
String data = null;
|
|
|
|
|
for (String uuid : uuidValues) {
|
|
|
|
|
data = UUID+"="+uuid;
|
|
|
|
|
}
|
|
|
|
|
if(data!=null){
|
|
|
|
|
logger.debug("Writing "+data +" into byte array");
|
|
|
|
|
byteArray.write(data.getBytes());
|
|
|
|
|
visibility = visibility.toUpperCase();
|
|
|
|
|
try{
|
|
|
|
|
VISIBILITY.valueOf(visibility);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
List<VISIBILITY> toPrint = Arrays.asList(VISIBILITY.values());
|
|
|
|
|
logger.error("The 'visibility' parameter is wrong, Have you pass a valid parameter VISIBILITY like "+toPrint+"?");
|
|
|
|
|
throw ExceptionManager.wrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//HOW TO PASS ANY FILTER?
|
|
|
|
|
//TODO INVESTIGATE ON HOW TO PASS MORE THAN ONE FILTER...
|
|
|
|
|
Map<String,String> filters = new HashMap<String, String>();
|
|
|
|
|
if(filterKey!=null && filterValue!=null){
|
|
|
|
|
//IGNORING THE EMPTY FILTER 'null|null'
|
|
|
|
|
if(filterKey.compareToIgnoreCase("null")!=0){
|
|
|
|
|
filters.put(filterKey, filterValue);
|
|
|
|
|
logger.debug("Added filter parmas to map filters: "+filters);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
GeonetworkServiceInterface gntwAccess = new GeonetworkAccessParameter(scope);
|
|
|
|
|
GeonetworkInstance gnInstance = gntwAccess.getGeonetworkInstance();
|
|
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
|
logger.info("set scope provider "+scope);
|
|
|
|
|
Configuration config = gnInstance.getGeonetworkPublisher().getConfiguration();
|
|
|
|
|
Account account = config.getScopeConfiguration().getAccounts().get(Type.CKAN);
|
|
|
|
|
logger.info("CKAN user owner is: "+account.getUser());
|
|
|
|
|
|
|
|
|
|
// logger.info("Parameters..");
|
|
|
|
|
// for (Enumeration<String> e = req.getParameterNames(); e.hasMoreElements();){
|
|
|
|
|
// String p = e.nextElement();
|
|
|
|
|
// logger.debug("param "+p + " value "+Arrays.toString(req.getParameterValues(p)));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
//DEBUG BODY
|
|
|
|
|
// String readBody = IOUtils.toString(req.getReader());
|
|
|
|
|
// logger.debug("doPost read body request: "+readBody);
|
|
|
|
|
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
|
|
String geonetworkUrl = config.getGeoNetworkEndpoint();
|
|
|
|
|
|
|
|
|
|
// SPECIFIC HANDLER FOR GEONETWORK REQUEST: /srv/en/mef.export
|
|
|
|
|
if(remainPath!=null && remainPath.compareTo(SRV_EN_MEF_EXPORT)==0){
|
|
|
|
|
logger.info("In case of mef.export, perfoming a custom handler");
|
|
|
|
|
gnCSWlURL = geonetworkUrl + SRV_EN_MEF_EXPORT;
|
|
|
|
|
String[] uuidValues = req.getParameterValues(UUID);
|
|
|
|
|
if(uuidValues!=null){
|
|
|
|
|
String data = null;
|
|
|
|
|
for (String uuid : uuidValues) {
|
|
|
|
|
data = UUID+"="+uuid;
|
|
|
|
|
}
|
|
|
|
|
if(data!=null){
|
|
|
|
|
logger.debug("Writing "+data +" into byte array");
|
|
|
|
|
byteArray.write(data.getBytes());
|
|
|
|
|
}else
|
|
|
|
|
IOUtils.copy(req.getInputStream(), byteArray);
|
|
|
|
|
}else
|
|
|
|
|
IOUtils.copy(req.getInputStream(), byteArray);
|
|
|
|
|
}else
|
|
|
|
|
}else{
|
|
|
|
|
logger.info("IS NOT A REQUEST TO "+SRV_EN_MEF_EXPORT);
|
|
|
|
|
gnCSWlURL = remainPath==null || remainPath.isEmpty()?geonetworkUrl+"/"+CSW_SERVER:geonetworkUrl+"/"+CSW_SERVER+remainPath;
|
|
|
|
|
IOUtils.copy(req.getInputStream(), byteArray);
|
|
|
|
|
}else{
|
|
|
|
|
logger.info("IS NOT A REQUEST TO "+SRV_EN_MEF_EXPORT);
|
|
|
|
|
gnCSWlURL = remainPath==null || remainPath.isEmpty()?geonetworkUrl+"/"+CSW_SERVER:geonetworkUrl+"/"+CSW_SERVER+remainPath;
|
|
|
|
|
IOUtils.copy(req.getInputStream(), byteArray);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
|
|
|
|
|
|
|
|
|
|
//PRIVATE LAYERS
|
|
|
|
|
if(visibility.equals(VISIBILITY.PRV.name())){
|
|
|
|
|
logger.info("Visibility: "+VISIBILITY.PRV+" getting private layers..");
|
|
|
|
|
//VRE LAYERS
|
|
|
|
|
if(mode.equals(MODE.VRE)){
|
|
|
|
|
logger.info("Getting "+MODE.VRE+" layers..");
|
|
|
|
|
//HARVESTED LAYERS
|
|
|
|
|
}else{
|
|
|
|
|
filters.put("isHarvested", "y");
|
|
|
|
|
logger.info("Getting "+MODE.HARVEST+" layers, I added 'isHarvested = y' to the filters ["+filters+"]");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(account.getUser()!=null){
|
|
|
|
|
boolean authorized = GNAuthentication.login(httpUtils, geonetworkUrl, account.getUser(), account. getPassword());
|
|
|
|
|
logger.info("Authorized on "+geonetworkUrl +" ? "+authorized);
|
|
|
|
|
}else
|
|
|
|
|
logger.info("Skipping authentication, ckan user (the owner) is null");
|
|
|
|
|
|
|
|
|
|
//PUBLIC LAYERS
|
|
|
|
|
}else{
|
|
|
|
|
logger.info("Visibility: "+VISIBILITY.PUB+" getting public layers..");
|
|
|
|
|
//VRE LAYERS
|
|
|
|
|
if(mode.equals(MODE.VRE.name())){
|
|
|
|
|
logger.info("Getting "+MODE.VRE+" layers, the VRE account: "+account.getUser() +" will be used as owner user for filtering... Is it right?");
|
|
|
|
|
filters.put("ownername", account.getUser());
|
|
|
|
|
//HARVESTED LAYERS
|
|
|
|
|
}else{
|
|
|
|
|
//filters.put("isHarvested", "y");
|
|
|
|
|
logger.info("Getting "+MODE.HARVEST+" layers, I'm applying the filters ["+filters+"]");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("Sending CSW POST request to URL: "+gnCSWlURL);
|
|
|
|
|
logger.info("Content-Type: "+req.getContentType());
|
|
|
|
|
|
|
|
|
|
//DEBUG
|
|
|
|
|
//logger.debug("POST - BODY : "+byteArray.toString());
|
|
|
|
|
InputStream in = httpUtils.post(gnCSWlURL, new ByteArrayInputStream(byteArray.toByteArray()), req.getContentType(), req.getParameterMap());
|
|
|
|
|
|
|
|
|
|
if(in==null){
|
|
|
|
|
logger.warn("Input stream returned is null, sending "+HttpServletResponse.SC_NOT_FOUND);
|
|
|
|
|
ExceptionManager.notFoundException(req, "Input stream is null to the request "+gnCSWlURL+ " with body: "+byteArray.toString(), this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
ReusableInputStream reus = new ReusableInputStream(in);
|
|
|
|
|
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
|
|
|
|
|
|
|
|
|
|
//PRIVATE LAYERS
|
|
|
|
|
if(visibility.equals(VISIBILITY.PRV.name())){
|
|
|
|
|
logger.info("Private VISIBILITY requested, retrieving public file identifiers to apply filtering..");
|
|
|
|
|
FilterGetRecords filterGetRecords = new FilterGetRecords(byteArray.toString());
|
|
|
|
|
if(filterGetRecords.getFoundPublicIds()!=null && filterGetRecords.getFoundPublicIds().size()>0){
|
|
|
|
|
logger.info("I'm removing list of public IDs with "+filterGetRecords.getFoundPublicIds().size() +" item/s. Is it right?");
|
|
|
|
|
in = GetResponseRecordFilter.overrideResponseIdsByListIds(reus, filterGetRecords.getFoundPublicIds(), REPLACED_A_PUBLIC_UUID_PLEASE_IGNORE);
|
|
|
|
|
logger.info("Visibility: "+VISIBILITY.PRV+" getting private layers..");
|
|
|
|
|
//VRE LAYERS
|
|
|
|
|
if(mode.equals(MODE.VRE)){
|
|
|
|
|
logger.info("Getting "+MODE.VRE+" layers..");
|
|
|
|
|
//HARVESTED LAYERS
|
|
|
|
|
}else{
|
|
|
|
|
filters.put("isHarvested", "y");
|
|
|
|
|
logger.info("Getting "+MODE.HARVEST+" layers, I added 'isHarvested = y' to the filters ["+filters+"]");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(account.getUser()!=null){
|
|
|
|
|
boolean authorized = GNAuthentication.login(httpUtils, geonetworkUrl, account.getUser(), account. getPassword());
|
|
|
|
|
logger.info("Authorized on "+geonetworkUrl +" ? "+authorized);
|
|
|
|
|
}else
|
|
|
|
|
logger.info("Skipping authentication, ckan user (the owner) is null");
|
|
|
|
|
|
|
|
|
|
//PUBLIC LAYERS
|
|
|
|
|
}else{
|
|
|
|
|
logger.info("Visibility: "+VISIBILITY.PUB+" getting public layers..");
|
|
|
|
|
//VRE LAYERS
|
|
|
|
|
if(mode.equals(MODE.VRE.name())){
|
|
|
|
|
logger.info("Getting "+MODE.VRE+" layers, the VRE account: "+account.getUser() +" will be used as owner user for filtering... Is it right?");
|
|
|
|
|
filters.put("ownername", account.getUser());
|
|
|
|
|
//HARVESTED LAYERS
|
|
|
|
|
}else{
|
|
|
|
|
//filters.put("isHarvested", "y");
|
|
|
|
|
logger.info("Getting "+MODE.HARVEST+" layers, I'm applying the filters ["+filters+"]");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(filters.size()>0){
|
|
|
|
|
logger.info("Applying filtering on geonet:info... filter/s used: "+filters);
|
|
|
|
|
Document doc = GetResponseRecordFilter.inputStreamToW3CDocument(reus);
|
|
|
|
|
List<String> fileIdentifiers = GetResponseRecordFilter.getTextContentStringsForTagName(doc, "gmd:fileIdentifier");
|
|
|
|
|
List<String> noMatchingFilter = new ArrayList<String>();
|
|
|
|
|
for (String fileId : fileIdentifiers) {
|
|
|
|
|
logger.info("Sending CSW POST request to URL: "+gnCSWlURL);
|
|
|
|
|
logger.info("Content-Type: "+req.getContentType());
|
|
|
|
|
|
|
|
|
|
//CKECKING THE FILTERS
|
|
|
|
|
for (String fkey : filters.keySet()) {
|
|
|
|
|
String value = GetResponseRecordFilter.getMetadataValueByFileIdentifier(fileId, config.getGeoNetworkEndpoint(),config.getAdminAccount().getUser(), config.getAdminAccount().getPassword(), fkey);
|
|
|
|
|
//String own = GetResponseRecordFilter.getMetaOwnerNameByFileIdentifier(fileId, config.getGeoNetworkEndpoint(),config.getAdminAccount().getUser(), config.getAdminAccount().getPassword());
|
|
|
|
|
String fValue = filters.get(fkey);
|
|
|
|
|
//Either the filter KEY doesn't matching any geonet:info (value==null)
|
|
|
|
|
//or the value of filterKey in geonet:info doesn't matching the passed filterValue (value.compareTo(fValue)!=0)
|
|
|
|
|
if(value==null || value.compareTo(fValue)!=0){
|
|
|
|
|
logger.trace(fkey +" of File Identifier "+fileId+" not matching the filter: "+fkey+" with value: "+fValue+", adding it to list to remove file identifier and exit from loop..");
|
|
|
|
|
noMatchingFilter.add(fileId);
|
|
|
|
|
//WHEN I ADD THE FILE IDENTIFIER TO FILTERED ID, I CAN EXIT FROM CKECKING FILTERS LOOP
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//DEBUG
|
|
|
|
|
//logger.debug("POST - BODY : "+byteArray.toString());
|
|
|
|
|
InputStream in = httpUtils.post(gnCSWlURL, new ByteArrayInputStream(byteArray.toByteArray()), req.getContentType(), req.getParameterMap());
|
|
|
|
|
|
|
|
|
|
if(in==null){
|
|
|
|
|
logger.warn("Input stream returned is null, sending "+HttpServletResponse.SC_NOT_FOUND);
|
|
|
|
|
throw ExceptionManager.notFoundException(req, "Input stream is null to the request "+gnCSWlURL+ " with body: "+byteArray.toString(), this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
ReusableInputStream reus = new ReusableInputStream(in);
|
|
|
|
|
|
|
|
|
|
if(visibility.equals(VISIBILITY.PRV.name())){
|
|
|
|
|
logger.info("Private VISIBILITY requested, retrieving public file identifiers to apply filtering..");
|
|
|
|
|
FilterGetRecords filterGetRecords = new FilterGetRecords(byteArray.toString());
|
|
|
|
|
if(filterGetRecords.getFoundPublicIds()!=null && filterGetRecords.getFoundPublicIds().size()>0){
|
|
|
|
|
logger.info("I'm removing list of public IDs with "+filterGetRecords.getFoundPublicIds().size() +" item/s. Is it right?");
|
|
|
|
|
in = GetResponseRecordFilter.overrideResponseIdsByListIds(reus, filterGetRecords.getFoundPublicIds(), REPLACED_A_PUBLIC_UUID_PLEASE_IGNORE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(noMatchingFilter.size()>0){
|
|
|
|
|
logger.info("Removing "+noMatchingFilter.size()+" layer/s that not macthing the filters: "+filters);
|
|
|
|
|
//Document doc2 = GetResponseRecordFilter.inputStreamToW3CDocument(reus);
|
|
|
|
|
in = GetResponseRecordFilter.overrideResponseIdsByListIds(reus, noMatchingFilter, REPLACED_UUID_BY_FILTER_PLEASE_IGNORE);
|
|
|
|
|
//in = GetResponseRecordFilter.w3CDocumentToInputStream(doc);
|
|
|
|
|
if(filters.size()>0){
|
|
|
|
|
logger.info("Applying filtering on geonet:info... filter/s used: "+filters);
|
|
|
|
|
Document doc = GetResponseRecordFilter.inputStreamToW3CDocument(reus);
|
|
|
|
|
List<String> fileIdentifiers = GetResponseRecordFilter.getTextContentStringsForTagName(doc, "gmd:fileIdentifier");
|
|
|
|
|
List<String> noMatchingFilter = new ArrayList<String>();
|
|
|
|
|
for (String fileId : fileIdentifiers) {
|
|
|
|
|
|
|
|
|
|
//CKECKING THE FILTERS
|
|
|
|
|
for (String fkey : filters.keySet()) {
|
|
|
|
|
String value = GetResponseRecordFilter.getMetadataValueByFileIdentifier(fileId, config.getGeoNetworkEndpoint(),config.getAdminAccount().getUser(), config.getAdminAccount().getPassword(), fkey);
|
|
|
|
|
//String own = GetResponseRecordFilter.getMetaOwnerNameByFileIdentifier(fileId, config.getGeoNetworkEndpoint(),config.getAdminAccount().getUser(), config.getAdminAccount().getPassword());
|
|
|
|
|
String fValue = filters.get(fkey);
|
|
|
|
|
//Either the filter KEY doesn't matching any geonet:info (value==null)
|
|
|
|
|
//or the value of filterKey in geonet:info doesn't matching the passed filterValue (value.compareTo(fValue)!=0)
|
|
|
|
|
if(value==null || value.compareTo(fValue)!=0){
|
|
|
|
|
logger.trace(fkey +" of File Identifier "+fileId+" not matching the filter: "+fkey+" with value: "+fValue+", adding it to list to remove file identifier and exit from loop..");
|
|
|
|
|
noMatchingFilter.add(fileId);
|
|
|
|
|
//WHEN I ADD THE FILE IDENTIFIER TO FILTERED ID, I CAN EXIT FROM CKECKING FILTERS LOOP
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(noMatchingFilter.size()>0){
|
|
|
|
|
logger.info("Removing "+noMatchingFilter.size()+" layer/s that not macthing the filters: "+filters);
|
|
|
|
|
//Document doc2 = GetResponseRecordFilter.inputStreamToW3CDocument(reus);
|
|
|
|
|
in = GetResponseRecordFilter.overrideResponseIdsByListIds(reus, noMatchingFilter, REPLACED_UUID_BY_FILTER_PLEASE_IGNORE);
|
|
|
|
|
//in = GetResponseRecordFilter.w3CDocumentToInputStream(doc);
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
logger.info("No replace on UUIDs was applied from filters: "+filters);
|
|
|
|
|
in = reus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
logger.info("No replace on UUIDs was applied from filters: "+filters);
|
|
|
|
|
in = reus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReusableInputStream reusIs = new ReusableInputStream(in);
|
|
|
|
|
|
|
|
|
|
//END DEBUG
|
|
|
|
|
logger.info("Response return Content-Type: "+httpUtils.getLastContentType());
|
|
|
|
|
return Response
|
|
|
|
|
.ok(reusIs)
|
|
|
|
|
//.header(ConstantsResolver.CONTENT_DISPOSITION,"attachment; filename = \""+fileName+"\"")
|
|
|
|
|
.header(ConstantsResolver.CONTENT_TYPE, httpUtils.getLastContentType()).build();
|
|
|
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
logger.error("Error on copy the response to send to client: ", e);
|
|
|
|
|
throw ExceptionManager.internalErrorException(req, "Error on copy the response!", this.getClass(), helpURI);
|
|
|
|
|
}finally{
|
|
|
|
|
IOUtils.closeQuietly(in);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReusableInputStream reusIs = new ReusableInputStream(in);
|
|
|
|
|
} catch (IllegalArgumentException e){
|
|
|
|
|
logger.error("IllegalArgumentException:", e);
|
|
|
|
|
throw ExceptionManager.badRequestException(req, "Illegal argument to carry out the request!", this.getClass(), helpURI);
|
|
|
|
|
|
|
|
|
|
//END DEBUG
|
|
|
|
|
logger.info("Response return Content-Type: "+httpUtils.getLastContentType());
|
|
|
|
|
return Response
|
|
|
|
|
.ok(reusIs)
|
|
|
|
|
//.header(ConstantsResolver.CONTENT_DISPOSITION,"attachment; filename = \""+fileName+"\"")
|
|
|
|
|
.header(ConstantsResolver.CONTENT_TYPE, httpUtils.getLastContentType()).build();
|
|
|
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
logger.error("Error on copy the response to send to client: ", e);
|
|
|
|
|
ExceptionManager.internalErrorException(req, "Error on copy the response!", this.getClass(), help);
|
|
|
|
|
}finally{
|
|
|
|
|
IOUtils.closeQuietly(in);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Exception:", e);
|
|
|
|
|
String error = "Sorry, an error occurred on resolving geonetwork request with scope "+scope+". Please, contact support!";
|
|
|
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (IllegalArgumentException e){
|
|
|
|
|
logger.error("IllegalArgumentException:", e);
|
|
|
|
|
ExceptionManager.badRequestException(req, "Illegal argument to carry out the request!", this.getClass(), help);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if(!(e instanceof WebApplicationException)){
|
|
|
|
|
//UNEXPECTED EXCEPTION managing it as WebApplicationException
|
|
|
|
|
String error = "Error during perform POST operation to: "+gnCSWlURL+". Please, contact the support!"+"\nCause: "+e.getCause().getMessage();
|
|
|
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
|
|
|
|
|
}
|
|
|
|
|
//ALREADY MANAGED AS WebApplicationException
|
|
|
|
|
logger.error("Exception:", e);
|
|
|
|
|
String error = "Sorry, an error occurred on resolving geonetwork request with scope "+scope+". Please, contact support!";
|
|
|
|
|
ExceptionManager.internalErrorException(req, error, this.getClass(), help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//An error occurred
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
throw new InternalServerException(req, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR, "Error during perform POST operation to: "+gnCSWlURL, this.getClass(), new URI(help));
|
|
|
|
|
}
|
|
|
|
|
catch (URISyntaxException e) {
|
|
|
|
|
//silent
|
|
|
|
|
return null;
|
|
|
|
|
throw (WebApplicationException) e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|