bug fixing

added junit test


git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@173903 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-10-31 10:52:04 +00:00
parent 32717609c8
commit addfc794c2
5 changed files with 227 additions and 48 deletions

View File

@ -42,7 +42,7 @@ public class CatalogueResolver {
ApplicationProfileReaderForCatalogueResolver appPrCatResolver;
@GET
@Path("{entityContext:ctlg(-(o|g|p|d)?}/{vreName}/{entityName}")
@Path("{entityContext:ctlg(-(o|g|p|d))?}/{vreName}/{entityName}")
public Response resolveCatalogue(@PathParam("entityName") String entityName, @PathParam("vreName") String vreName, @PathParam("entityContext") String entityContext) {
InnerMethodName.instance.set("resolveCataloguePublicLink");
logger.info(CatalogueResolver.class.getSimpleName()+" GET starts...");

View File

@ -1,9 +1,13 @@
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.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -12,6 +16,7 @@ import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
@ -29,14 +34,22 @@ import org.gcube.datatransfer.resolver.SingleFileStreamingOutput;
import org.gcube.datatransfer.resolver.Util;
import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter;
import org.gcube.datatransfer.resolver.gis.GeonetworkInstance;
import org.gcube.datatransfer.resolver.gis.GeonetworkServiceInterface;
import org.gcube.datatransfer.resolver.gis.exception.GeonetworkInstanceException;
import org.gcube.datatransfer.resolver.gis.geonetwork.FilterGetRecords;
import org.gcube.datatransfer.resolver.gis.geonetwork.GNAuthentication;
import org.gcube.datatransfer.resolver.gis.geonetwork.HTTPCallsUtils;
import org.gcube.datatransfer.resolver.gis.geonetwork.HTTPCallsUtils.HttpResponse;
import org.gcube.datatransfer.resolver.gis.geonetwork.ReusableInputStream;
import org.gcube.datatransfer.resolver.gis.util.GetResponseRecordFilter;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.gcube.spatial.data.geonetwork.configuration.Configuration;
import org.gcube.spatial.data.geonetwork.model.Account;
import org.gcube.spatial.data.geonetwork.model.Account.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
/**
* The Class GeonetworkResolver.
@ -58,7 +71,7 @@ public class GeonetworkResolver {
public static final String CSW_SERVER = "srv/en/csw";
public static final String VALUE_OF_REQUEST_DELIMITIER = "$$";
public static final String PATH_PARAM_REMAINING = "remainPath";
public static final String PATH_PARAM_REMAINPATH = "remainPath";
public static final String QUERY_PARAM_RESET_CACHE_PARAM = "resetcache";
public static final String QUERY_PARAM_RESET_CACHED_SCOPE_PARAM = "resetcachedscope";
@ -85,7 +98,7 @@ public class GeonetworkResolver {
* @param mode the mode
* @param scope the scope
* @param visibility the visibility
* @param owner the owner
* @param owner the owner - pass 'null' as string if no filter applied
* @param requestDelimiter the request delimiter
* @param resetCache the reset cache
* @param resetScope the reset scope
@ -93,13 +106,13 @@ public class GeonetworkResolver {
*/
@GET
@Path("{"+PATH_PARAM_SCOPE+"}/{"+PATH_PARAM_MODE+"}/{"+PATH_PARAM_VISIBILITY+"}/{"+PATH_PARAM_OWNER+"}/$${remainPath:(/[^?$]+)?}")
@Path("{"+PATH_PARAM_SCOPE+"}/{"+PATH_PARAM_MODE+"}/{"+PATH_PARAM_VISIBILITY+"}/{"+PATH_PARAM_OWNER+"}/$${"+PATH_PARAM_REMAINPATH+":(/[^?$]+)?}")
public Response submitGet(@Context HttpServletRequest req,
@PathParam(PATH_PARAM_SCOPE) @Nullable String scope,
@PathParam(PATH_PARAM_MODE) @Nullable String mode,
@PathParam(PATH_PARAM_VISIBILITY) @Nullable String visibility,
@PathParam(PATH_PARAM_OWNER) @Nullable String owner,
@PathParam("remainPath") @Nullable String remainPath,
@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) {
@ -262,7 +275,7 @@ public class GeonetworkResolver {
/**
* Submit post.
*/
/* @SuppressWarnings("resource")
@SuppressWarnings("resource")
@POST
@Path("/{"+PATH_PARAM_SCOPE+":(/[^/?$]+)?}}/{"+PATH_PARAM_MODE+"}/{"+PATH_PARAM_VISIBILITY+"}/{"+PATH_PARAM_OWNER+"}/{"+PATH_PARAM_REQUEST_DELIMITER+"}")
public Response submitPost(@Context HttpServletRequest req,
@ -503,7 +516,7 @@ public class GeonetworkResolver {
return null;
}
}*/
}
/**
* Purge remain from query string.
@ -516,12 +529,12 @@ public class GeonetworkResolver {
// SCOPE is: /gcube/devsec/devVRE
// [INFO ] 2016-04-05 15:01:42,808 org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkResolver -
// Query String is: scope=/gcube/devsec/devVRE&version=2.0.2&request=GetCapabilities&service=CSW
int start = queryString.indexOf(PATH_PARAM_REMAINING+"=");
int start = queryString.indexOf(PATH_PARAM_REMAINPATH+"=");
if(start>=0){
int end = queryString.indexOf("&", start);
if(end==-1 && queryString.length()==(PATH_PARAM_REMAINING+"="+remain_value).length()){ //SCOPE IS THE UNIQUE PARAMETER INTO QUETY STRING
if(end==-1 && queryString.length()==(PATH_PARAM_REMAINPATH+"="+remain_value).length()){ //SCOPE IS THE UNIQUE PARAMETER INTO QUETY STRING
logger.debug("Scope is the unique parameter, returning empty query string");
return "";
}else if(end<queryString.length())

View File

@ -61,7 +61,7 @@ public class StorageIDResolver {
* @return the storage id
*/
@GET
@Path("{storage-id:(?!index)[^/?$]*}")
@Path("{storage-id}")
//@Path("{storage-id}")
public Response getStorageId(@Context HttpServletRequest httpRequest, @PathParam(STORAGE_ID) String storageId, @QueryParam(ConstantsResolver.QUERY_PARAM_FILE_NAME) String fileName, @QueryParam(ConstantsResolver.QUERY_PARAM_CONTENT_TYPE) String contentType, @QueryParam(ConstantsResolver.QUERY_PARAM_VALIDATION) Boolean validation) {
logger.info(this.getClass().getSimpleName()+" GET starts...");
@ -161,7 +161,7 @@ public class StorageIDResolver {
* @throws IOException Signals that an I/O exception has occurred.
*/
@HEAD
@Path("{storage-id:(?!index)[^/?$]*}")
@Path("{storage-id}")
public Response httpDoHead(@Context HttpServletRequest httpRequest, @PathParam(STORAGE_ID) String storageId, @QueryParam(ConstantsResolver.HPC) Boolean hproxycheck) throws ServletException, IOException {
logger.info(this.getClass().getSimpleName()+" HEAD starts...");

View File

@ -3,41 +3,29 @@
*/
package org.gcube.datatransfer.resolver.services;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
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.Status;
///**
// *
// * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
// * Oct 22, 2018
// */
//@Path("/")
//public class UriResolverIndex {
//
// private static Logger logger = LoggerFactory.getLogger(UriResolverIndex.class);
//
// @GET
// @Produces(MediaType.TEXT_HTML)
// @Path("index")
// public Viewable index(@Context HttpServletRequest req, @PathParam("index") String info){
//
// try{
// logger.info("UriResolverIndex called");
// return new Viewable("/index");
// }catch(Exception e){
// logger.error("Error on getting index.jsp", e);
// throw new InternalServerException(req, Status.NOT_FOUND, "Index.jsp not found", UriResolverIndex.class, null);
// }
//
// }
//}
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.glassfish.jersey.server.mvc.Viewable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Oct 22, 2018
*/
@Path("/")
public class UriResolverIndex {
private static Logger logger = LoggerFactory.getLogger(UriResolverIndex.class);
@GET
@Produces(MediaType.TEXT_HTML)
@Path("index")
public Viewable index(@Context HttpServletRequest req, @PathParam("index") String info){
try{
logger.info("UriResolverIndex called");
return new Viewable("/index");
}catch(Exception e){
logger.error("Error on getting index.jsp", e);
throw new InternalServerException(req, Status.NOT_FOUND, "Index.jsp not found", UriResolverIndex.class, null);
}
}
}

View File

@ -0,0 +1,178 @@
/**
*
*/
package rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpStatus;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class TestResolvers.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Oct 31, 2018
*/
public class TestResolvers {
public static final String URI_RESOLVER_SERVICE_ENDPOINT = "https://data1-d.d4science.net";
public static final Logger logger = LoggerFactory.getLogger(TestResolvers.class);
/**
* Storage hub test.
* @throws Exception
*/
//@Test
public void storageHubTest() throws Exception{
String storageHubId = "2019b881-083b-444a-81be-8abee4b91020";
String url = String.format("%s/shub/%s",URI_RESOLVER_SERVICE_ENDPOINT,storageHubId);
URL toURL;
int status;
try {
toURL = new URL(url);
HttpURLConnection con = (HttpURLConnection) toURL.openConnection();
con.setRequestMethod("GET");
con.connect();
status = con.getResponseCode();
String fileName = getFilename(con.getHeaderFields());
System.out.println("Response status is: "+status);
if(status==HttpStatus.SC_OK){
Path target = Files.createTempFile(FilenameUtils.getBaseName(fileName), "."+FilenameUtils.getExtension(fileName));
try (InputStream in = con.getInputStream()) {
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
}
System.out.println("File downloaded at: "+target.toAbsolutePath());
}else{
System.out.println("\nNo file downoladed");
System.out.println("Response: \n"+getContentReponse(con.getInputStream()));
}
//con.setRequestProperty("Content-Type", "application/json");
//con.setConnectTimeout(5000);
//con.setReadTimeout(5000)
}
catch (Exception e) {
// TODO Auto-generated catch block
logger.error("Error: ",e);
throw e;
}
}
/**
* Storage hub test.
* @throws Exception
*/
@Test
public void storageIDTest() throws Exception{
String storageID = "clZ2YmxTYytETzVLaHkwMjM3TmVETTFMb256YVRMS3lHbWJQNStIS0N6Yz0";
String url = String.format("%s/%s",URI_RESOLVER_SERVICE_ENDPOINT,storageID);
URL toURL;
int status;
try {
toURL = new URL(url);
HttpURLConnection con = (HttpURLConnection) toURL.openConnection();
con.setRequestMethod("GET");
con.connect();
status = con.getResponseCode();
//printHeaders(con.getHeaderFields());
String fileName = getFilename(con.getHeaderFields());
System.out.println("Response status is: "+status);
if(status==HttpStatus.SC_OK){
Path target = Files.createTempFile(FilenameUtils.getBaseName(fileName), "."+FilenameUtils.getExtension(fileName));
try (InputStream in = con.getInputStream()) {
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
}
System.out.println("File downloaded at: "+target.toAbsolutePath());
}else{
System.out.println("\nNo file downoladed");
System.out.println("Response: \n"+getContentReponse(con.getInputStream()));
}
//con.setRequestProperty("Content-Type", "application/json");
//con.setConnectTimeout(5000);
//con.setReadTimeout(5000)
}
catch (Exception e) {
// TODO Auto-generated catch block
logger.error("Error: ",e);
throw e;
}
}
/**
* Gets the content reponse.
*
* @param is the is
* @return the content reponse
*/
public static String getContentReponse(InputStream is){
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
StringBuffer content = new StringBuffer();
try {
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
}
catch (IOException e) {
e.printStackTrace();
}finally{
if(in!=null)
try {
in.close();
}
catch (IOException e) {
//silent
}
}
return content.toString();
}
public static void printHeaders(Map<String, List<String>> map){
System.out.println("Printing Response Header...");
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue());
}
}
public static String getFilename(Map<String, List<String>> map){
List<String> contentDispValue = map.get("content-disposition");
String fileName = "filename";
System.out.println("Printing content-disposition from Response Header...");
for (String value : contentDispValue) {
System.out.println(value);
int start = value.indexOf("\"");
if(start>=0){
int end = value.lastIndexOf("\"");
fileName = value.substring(start+1,end).trim();
}
}
System.out.println("Filename is: "+fileName);
return fileName;
}
}