Merge pull request 'task_20993' (#1) from task_20993 into master

Reviewed-on: #1
This commit is contained in:
Francesco Mangiacrapa 2021-04-09 11:11:49 +02:00
commit 71d0c64fe3
30 changed files with 847 additions and 298 deletions

View File

@ -29,6 +29,5 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="var" path="GCUBE_DEV_KEYS"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -4,11 +4,22 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v2-5-0-SNAPSHOT] - 2021-04-08
**New features**
[#20993] Supported new resource "Wekeo Interface" - gettoken.
**Bug fixes**
[#21093] StorageHubResolver HEAD request does not support Content-Length
## [v2-4-1] - 2021-01-13
**Bug Fixes**
[Task #19942] Fixing master build fails
[#19942] Fixing master build fails
## [v2-4-0] [r4-24-0]- 2020-06-18

39
pom.xml
View File

@ -9,7 +9,7 @@
</parent>
<groupId>org.gcube.data.transfer</groupId>
<artifactId>uri-resolver</artifactId>
<version>2.4.1</version>
<version>2.5.0-SNAPSHOT</version>
<packaging>war</packaging>
<description>The URI Resolver is an HTTP URI resolver implemented as an REST service which gives access trough HTTP to different gcube Resolvers and gCube Applications.</description>
@ -92,14 +92,17 @@
</dependency>
<!-- TODO REMOVE THIS IMPORT -->
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
<scope>compile</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.gcube.common</groupId> -->
<!-- <artifactId>gxHTTP</artifactId> -->
<!-- <scope>compile</scope> -->
<!-- </dependency> -->
<!-- //TO BE ADDED THE VERSION 18.0 <dependency> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> </dependency> -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
@ -137,7 +140,6 @@
<scope>compile</scope>
</dependency>
<!-- weld -->
<dependency>
<groupId>javax.enterprise</groupId>
@ -170,6 +172,11 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
</dependency>
<!-- GIS RESOLVER DEPENDENCIES -->
@ -180,13 +187,25 @@
<scope>compile</scope>
</dependency>
<!-- REMOVE THIS ON RELESE, IT IS NEEDED TO compile 'geonetwork' -->
<!-- <dependency> -->
<!-- <groupId>org.opengis</groupId> -->
<!-- <artifactId>geoapi</artifactId> -->
<!-- <version>3.0.0</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.geotoolkit</groupId> -->
<!-- <artifactId>geotk-xml-base</artifactId> -->
<!-- <version>3.20-geoapi-3.0</version> -->
<!-- </dependency> -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- DOM -->
<dependency>
<groupId>org.w3c</groupId>

View File

@ -189,23 +189,4 @@ public class MetadataConverter {
return gisLI;
}
/*
public static void main(String[] args) throws Exception {
// String geoserver = "http://www.fao.org/figis/a/wms/?service=WMS&version=1.1.0&request=GetMap&layers=area:FAO_AREAS&styles=Species_prob, puppa&bbox=-180.0,-88.0,180.0,90.0000000694&width=667&height=330&srs=EPSG:4326&format=image%2Fpng";
// System.out.println(MetadataConverter.getGeoserverBaseUri(geoserver));
String user ="admin";
String pwd = "admin";
boolean authenticate = true;
// String uuid ="177e1c3c-4a22-4ad9-b015-bfc443d16cb8";
String uuid ="fao-species-map-bep";
// String uuid ="fao-species-map-bon"; //FAO
String geoNetworkUrl ="http://geoserver-dev2.d4science-ii.research-infrastructures.eu/geonetwork";
GeonetworkInstance geonetowrkInstance = new GeonetworkInstance(geoNetworkUrl, user, pwd, authenticate);
String onLineResource = getWMSOnLineResource(geonetowrkInstance, uuid);
System.out.println(onLineResource);
}*/
}

View File

@ -71,7 +71,7 @@ public class StorageHubResolver {
ItemManagerClient client = AbstractPlugin.item().build();
StreamDescriptor descriptor = client.resolvePublicLink(id);
ResponseBuilder response = Response.noContent();
ResponseBuilder response = Response.ok();
response = new StorageHubMetadataResponseBuilder(req, response).fillMetadata(descriptor, id, CONTENT_DISPOSITION_VALUE.attachment);
return response.build();

View File

@ -0,0 +1,277 @@
package org.gcube.datatransfer.resolver.services;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collection;
import java.util.List;
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.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.codec.binary.Base64;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.encryption.StringEncrypter;
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.datatransfer.resolver.requesthandler.RequestHandler;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.NotFoundException;
import org.gcube.datatransfer.resolver.util.Util;
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;
/**
* The Class WekeoResolver.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 30, 2021
*/
@Path("wekeo")
public class WekeoResolver {
private static Logger logger = LoggerFactory.getLogger(WekeoResolver.class);
private final static String RUNTIME_WKEO_RESOURCE_NAME = "WekeoDataBroker";
private final static String CATEGORY_WEKEO_TYPE = "OnlineService";
private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver#Wekeo_Resolver";
/**
* Gets the token.
*
* @param req the req
* @return the token
* @throws WebApplicationException the web application exception
*/
@GET
@Path("/gettoken")
@Produces(MediaType.APPLICATION_JSON)
public Response getToken(@Context HttpServletRequest req) throws WebApplicationException{
logger.info(this.getClass().getSimpleName()+" getToken starts...");
try {
String contextToken = SecurityTokenProvider.instance.get();
String scope = ScopeProvider.instance.get();
logger.info("ScopeProvider has scope: "+scope);
String appToken = req.getServletContext().getInitParameter(RequestHandler.ROOT_APP_TOKEN);
if(contextToken.compareTo(appToken)==0){
logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"...");
throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI);
}
StringBuilder wekeoResponse = new StringBuilder();
AccessPoint wekeoAccessPoint = WekeoResolver.readWekeoServiceEndpoint(req, scope);
if (wekeoAccessPoint != null) {
String wekeoUsername = wekeoAccessPoint.username();
String wekeoAddress = wekeoAccessPoint.address();
String wekeoPwd = wekeoAccessPoint.password();
// printing the access point found
if (logger.isDebugEnabled()) {
String msg = String.format("Found the username '%s' and the address '%s' to perform the request",
wekeoUsername, wekeoAddress);
logger.debug(msg);
}
logger.info("The pwd is: "+wekeoPwd);
// decrypting the pwd
if (wekeoPwd != null) {
wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoPwd);
logger.info("Decrypted pwd registered into Access Point '" + wekeoAccessPoint.name() + "' is: "
+ wekeoPwd.substring(0,wekeoPwd.length()/2)+"...");
}
if (wekeoUsername != null && wekeoPwd != null & wekeoAddress != null) {
HttpURLConnection connection = null;
InputStream content = null;
InputStreamReader in = null;
try {
String authString = wekeoUsername + ":" + wekeoPwd;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
logger.debug("Base64 encoded auth string: " + authStringEnc);
logger.info("Performing the request to: "+wekeoAddress);
URL url = new URL(wekeoAddress);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
content = (InputStream) connection.getInputStream();
in = new InputStreamReader(content);
logger.info("the response code is: "+connection.getResponseCode());
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
logger.debug("reading the response...");
while ((numCharsRead = in.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
wekeoResponse.append(sb.toString());
//System.out.println(wekeoResponse);
} catch (Exception e) {
logger.error(e.getMessage(), e);
String error = String.format("Error on performing request to %s", wekeoAddress);
throw new Exception(error);
} finally {
try {
if (content!= null && in != null) {
in.close();
content.close();
}
}catch (Exception e) {
//silent
}
}
} else {
String error = String.format(
"I cannot read the configurations (adress, username,password) from %s in the scope %s",
RUNTIME_WKEO_RESOURCE_NAME, scope);
throw new Exception(error);
}
}
// to be sure
if (wekeoResponse.length() == 0) {
String error = String
.format("Sorry an error occured on getting the access token from Wekeo. Please, retry the request");
throw new Exception(error);
}
String theResponse = wekeoResponse.toString();
logger.info("returning: \n"+theResponse);
return Response.ok(theResponse).build();
}catch (Exception e) {
//ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}
}
@GET
@Produces({MediaType.TEXT_HTML})
@Path("")
public InputStream index(@Context HttpServletRequest req) throws WebApplicationException{
String indexFile = "/WEB-INF/jsp/wekeo.jsp";
try{
logger.info(this.getClass().getSimpleName() +" index called");
String realPath = req.getServletContext().getRealPath(indexFile);
return new FileInputStream(new File(realPath));
}catch (Exception e) {
if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = "wekeo.jsp not found. Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), null);
}
//ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}
}
/**
* Reads the wekeo endpoint information from IS. {The SE name is: @link WekeoResolver#RUNTIME_WKEO_RESOURCE_NAME}
*
* @param req the req
* @param scope the scope
* @return the string
*/
public static AccessPoint readWekeoServiceEndpoint(HttpServletRequest req, String scope){
String callerScope = null;
try{
callerScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
logger.info("Searching SE "+RUNTIME_WKEO_RESOURCE_NAME+" configurations in the scope: "+ScopeProvider.instance.get());
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_WKEO_RESOURCE_NAME +"'");
query.addCondition("$resource/Profile/Category/text() eq '"+ CATEGORY_WEKEO_TYPE +"'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> toReturn = client.submit(query);
logger.info("The query returned "+toReturn.size()+ " ServiceEndpoint/s");
if(toReturn.size()==0){
String errorMessage = String.format("Missing the RR with Name '%s' and Category '%s' in the scope '%s'. Please contact the support.",RUNTIME_WKEO_RESOURCE_NAME,CATEGORY_WEKEO_TYPE,ScopeProvider.instance.get());
logger.error(errorMessage);
throw ExceptionManager.notFoundException(req, errorMessage, WekeoResolver.class, helpURI);
}
String wekeoUsername = null;
String wekeoPwd = null;
ServiceEndpoint se = toReturn.get(0);
Collection<AccessPoint> theAccessPoints = se.profile().accessPoints().asCollection();
for (AccessPoint accessPoint : theAccessPoints) {
wekeoUsername = accessPoint.username();
wekeoPwd = accessPoint.password();
if(wekeoUsername!=null && wekeoPwd!=null) {
logger.info("returning the access point with name: "+accessPoint.name());
return accessPoint;
}
}
return null;
}catch(Exception e){
if(e instanceof NotFoundException)
throw e;
String errorMessage = "Error occurred on reading the "+RUNTIME_WKEO_RESOURCE_NAME+" SE registered in the scope: "+ScopeProvider.instance.get();
logger.error(errorMessage, e);
throw ExceptionManager.internalErrorException(req, errorMessage, WekeoResolver.class, helpURI);
}finally{
if(callerScope!=null){
logger.info("Setting to the callerScope scope: "+callerScope);
ScopeProvider.instance.set(callerScope);
}else{
logger.info("Reset scope");
ScopeProvider.instance.reset();
}
}
}
}

View File

@ -58,8 +58,10 @@ public class StorageHubMetadataResponseBuilder {
responseBuilder.header("Content-Location", contentLocation);
//Managing "Content-Type"
if (streamDescriptor.getContentType()!= null && !streamDescriptor.getContentType().isEmpty())
if (streamDescriptor.getContentType()!= null && !streamDescriptor.getContentType().isEmpty()) {
responseBuilder.header("Content-Type", streamDescriptor.getContentType());
}
//Managing "Content-Lenght"
if(streamDescriptor.getContentLenght()>0) {

View File

@ -120,14 +120,4 @@ public class HttpRequestUtil {
return false;
}
/**
* The main method.
*
* @param args the arguments
* @throws Exception the exception
*/
public static void main(String[] args) throws Exception {
System.out.println(HttpRequestUtil.urlExists("http://geoserver2.d4science.research-infrastructures.eu/geoserver/wms", true));
}
}

View File

@ -5,4 +5,5 @@
<description>URIResolver RESTful</description>
<include>/analytics/create/*</include>
<include>/knime/create/*</include>
<include>/wekeo/gettoken/*</include>
</application>

View File

@ -1,22 +0,0 @@
<!-- EASTER EGG - TO BE REMOVED -->
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<style type="text/css">
body {
background-color: #f8f6f2;
color: #225f97;
font-family: 'Droid Sans', arial, sans-serif;
font-size: 16px;
}
</style>
</head>
<body>
<div>
<img style="-webkit-user-select: none; cursor: zoom-out;"
src="https://www.sportmediaset.mediaset.it/binary/26.$plit/C_29_fotogallery_1024745__ImageGallery__imageGalleryItem_4_image.jpg"
width="2598" height="2074">
</div>
</body>
</html>

View File

@ -1,6 +1,8 @@
<!DOCTYPE HTML>
<html lang="en-US">
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<style type="text/css">
body {
background-color: #f8f6f2;

View File

@ -0,0 +1,74 @@
<!DOCTYPE HTML>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script type="text/javascript">
function goToURL(relativePath) {
var x = "/wekeo/" + relativePath;
window.open(x, "_blank");
}
</script>
<style type="text/css">
body {
background-color: #f8f6f2;
color: #225f97;
font-family: 'Droid Sans', arial, sans-serif;
font-size: 16px;
}
.mydiv {
top: 50%;
left: 50%;
width: 600px;
height: 300px;
margin-top: -150px; /*set to a negative number 1/2 of your height*/
margin-left: -300px; /*set to a negative number 1/2 of your width*/
/* border: 1px solid #ccc; */
/* background-color: #9b9b9b; */
position: fixed;
text-align: center;
/* vertical-align: middle; */
}
.myTitle {
font-size: 22px;
font-weight: bold;
}
.myListOperations {
margin-top: 20px;
margin-bottom: 40px;
color: #333;
}
.uri-footer {
padding: 20px 20px;
font-size: 14px;
}
</style>
</head>
<body>
<div class="mydiv">
<img alt=""
src="https://www.d4science.org/image/layout_set_logo?img_id=12630" />
<div class="myTitle">The Wekeo Resolver</div>
<div class="myListOperations">
Available Operations:
<p>
# authentication operator
<button onclick="goToURL('gettoken')">Get Token</button>
(gCube AuthN required)
</p>
</div>
<div class="uri-footer">
See wiki page at <a
href="https://gcube.wiki.gcube-system.org/gcube/URI_Resolver#Wekeo_Resolver"
target="_blank">gCube Wiki Wekeo Resolver</a>
</div>
</div>
</body>
</html>

View File

@ -1,231 +0,0 @@
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.spatial.data.geonetwork.GeoNetwork;
import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher;
import org.gcube.spatial.data.geonetwork.GeoNetworkReader;
import org.gcube.spatial.data.geonetwork.LoginLevel;
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.opengis.metadata.Metadata;
import it.geosolutions.geonetwork.util.GNSearchRequest;
import it.geosolutions.geonetwork.util.GNSearchResponse;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Aug 31, 2016
*/
public class GeonetworkQueryTest {
private static final int MAX = 10;
private static final String UUID = "8a878105-ef06-4b1f-843f-120fc525b22b";
//private String[] scopesProd = {"/gcube/devsec/devVRE"};
//private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"};
//private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/fisheriesandecosystematmii"};
//private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research"};
//private String[] scopesProd = {"/d4science.research-infrastructures.eu"};
//private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/FAO_TunaAtlas"};
//private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research/Blue-Datathon"};
private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab"};
private LoginLevel loginLevel = LoginLevel.CKAN;
private Type accountType = Type.SCOPE;
private String textToSearch = "geo_fea";
//@Test
public void getCount() throws Exception{
try{
for(String scope:scopesProd){
ScopeProvider.instance.set(scope);
GeoNetworkPublisher reader=GeoNetwork.get();
Configuration config = reader.getConfiguration();
Account account=config.getScopeConfiguration().getAccounts().get(accountType);
//System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword());
System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
try{
String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword());
System.out.println("Decrypted Password: "+decryptedPassword);
}catch(Exception e){
System.out.println("ignoring exception during pwd decrypting");
}
// req.addParam("keyword", "Thredds");
final GNSearchRequest req=new GNSearchRequest();
// req.addParam(GNSearchRequest.Param.any,"Thredds");
GNSearchResponse resp = reader.query(req);
int publicCount=resp.getCount();
reader.login(loginLevel);
int totalCount=reader.query(req).getCount();
System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
if(totalCount==0)
return;
/*try{
int last = totalCount>MAX?totalCount:MAX;
for(int i=0; i<last; i++){
String xml = reader.getByIdAsRawString(resp.getMetadata(i).getUUID());
System.out.println(i+") is Thredds? "+containsString(xml, "Thredds"));
}
}catch(Exception e ){
e.printStackTrace();
}*/
}
}catch(Exception e){
e.printStackTrace();
}
}
//@Test
public void getLayerByUUID() throws Exception{
try{
for(String scope:scopesProd){
ScopeProvider.instance.set(scope);
GeoNetworkPublisher reader=GeoNetwork.get();
Configuration config = reader.getConfiguration();
Account account=config.getScopeConfiguration().getAccounts().get(accountType);
//System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword());
System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
Metadata meta = reader.getById(UUID);
try{
String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword());
System.out.println("Decrypted Password: "+decryptedPassword);
}catch(Exception e){
System.out.println("ignoring exception during pwd decrypting");
}
System.out.println("SCOPE "+scope+" found meta "+meta);
}
}catch(Exception e){
System.err.println("Error on getting layer by UUID: "+UUID);
e.printStackTrace();
}
}
//@Test
public void getLayersBySearch() throws Exception{
try{
for(String scope:scopesProd){
ScopeProvider.instance.set(scope);
GeoNetworkPublisher reader=GeoNetwork.get();
Configuration config = reader.getConfiguration();
Account account=config.getScopeConfiguration().getAccounts().get(accountType);
//System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword());
System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
try{
String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword());
System.out.println("Decrypted Password: "+decryptedPassword);
}catch(Exception e){
System.out.println("ignoring exception during pwd decrypting");
}
// req.addParam("keyword", "Thredds");
//final GNSearchRequest req=new GNSearchRequest();
final GNSearchRequest req = getRequest(true, textToSearch);
// req.addParam(GNSearchRequest.Param.any,"Thredds");
GNSearchResponse resp = reader.query(req);
int publicCount=resp.getCount();
reader.login(loginLevel);
int totalCount=reader.query(req).getCount();
System.out.println("QUERY "+textToSearch);
System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
// if(totalCount==0)
// return;
// try{
// int last = totalCount>MAX?totalCount:MAX;
//
// for(int i=0; i<last; i++){
// //String xml = reader.getByIdAsRawString(resp.getMetadata(i).getUUID());
// GNMetadata xml = resp.getMetadata(i);
// //System.out.println(i+") is Thredds? "+containsString(xml, "Thredds"));
// System.out.println(xml.toString());
// }
// }catch(Exception e ){
// e.printStackTrace();
// }
}
}catch(Exception e){
e.printStackTrace();
}
}
private boolean containsString(String txt, String value){
return txt.contains(value);
}
/**
* Gets the request.
*
* @param sortByTitle the sort by title
* @param textToSearch the text to search
* @return the request
*/
public GNSearchRequest getRequest(boolean sortByTitle, String textToSearch) {
GNSearchRequest req = new GNSearchRequest();
if(sortByTitle)
req.addConfig(GNSearchRequest.Config.sortBy, "title");
if(textToSearch==null || textToSearch.isEmpty()){
req.addParam(GNSearchRequest.Param.any, textToSearch);
System.out.println("search by any text");
}else{
req.addParam(GNSearchRequest.Param.title, textToSearch);
req.addConfig(GNSearchRequest.Config.similarity, "1");
System.out.println("search by title");
}
System.out.println("text to search "+textToSearch);
return req;
}
// @Test
public void getCountProd() throws Exception{
try{
for(String scope:scopesProd){
ScopeProvider.instance.set(scope);
GeoNetworkReader reader=GeoNetwork.get();
final GNSearchRequest req=new GNSearchRequest();
// req.addParam("keyword", "Thredds");
// req.addParam(GNSearchRequest.Param.any,"Oscar");
int publicCount=reader.query(req).getCount();
reader.login(loginLevel);
int totalCount=reader.query(req).getCount();
System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import org.gcube.datatransfer.resolver.catalogue.resource.CatalogueStaticConfigurations;
public class CatalogueNameExtractor {

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader;
import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference;

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import java.util.concurrent.ExecutionException;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;

View File

@ -0,0 +1,232 @@
package org.gcube.datatransfer.test;
//import org.gcube.common.encryption.StringEncrypter;
//import org.gcube.common.scope.api.ScopeProvider;
//import org.gcube.spatial.data.geonetwork.GeoNetwork;
//import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher;
//import org.gcube.spatial.data.geonetwork.GeoNetworkReader;
//import org.gcube.spatial.data.geonetwork.LoginLevel;
//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.opengis.metadata.Metadata;
//
//import it.geosolutions.geonetwork.util.GNSearchRequest;
//import it.geosolutions.geonetwork.util.GNSearchResponse;
//
///**
// *
// * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
// * Aug 31, 2016
// */
//public class GeonetworkQueryTest {
//
// private static final int MAX = 10;
//
// private static final String UUID = "8a878105-ef06-4b1f-843f-120fc525b22b";
//
// //private String[] scopesProd = {"/gcube/devsec/devVRE"};
//
// //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"};
//
// //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/fisheriesandecosystematmii"};
//
// //private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research"};
//
// //private String[] scopesProd = {"/d4science.research-infrastructures.eu"};
//
// //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/FAO_TunaAtlas"};
//
// //private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research/Blue-Datathon"};
// private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab"};
//
// private LoginLevel loginLevel = LoginLevel.CKAN;
//
// private Type accountType = Type.SCOPE;
//
// private String textToSearch = "geo_fea";
//
// //@Test
// public void getCount() throws Exception{
// try{
// for(String scope:scopesProd){
// ScopeProvider.instance.set(scope);
// GeoNetworkPublisher reader=GeoNetwork.get();
//
// Configuration config = reader.getConfiguration();
// Account account=config.getScopeConfiguration().getAccounts().get(accountType);
//
// //System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword());
// System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
//
// try{
// String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword());
// System.out.println("Decrypted Password: "+decryptedPassword);
// }catch(Exception e){
// System.out.println("ignoring exception during pwd decrypting");
// }
//
//
// // req.addParam("keyword", "Thredds");
// final GNSearchRequest req=new GNSearchRequest();
//// req.addParam(GNSearchRequest.Param.any,"Thredds");
// GNSearchResponse resp = reader.query(req);
// int publicCount=resp.getCount();
// reader.login(loginLevel);
// int totalCount=reader.query(req).getCount();
// System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
// if(totalCount==0)
// return;
// /*try{
// int last = totalCount>MAX?totalCount:MAX;
// for(int i=0; i<last; i++){
// String xml = reader.getByIdAsRawString(resp.getMetadata(i).getUUID());
// System.out.println(i+") is Thredds? "+containsString(xml, "Thredds"));
// }
// }catch(Exception e ){
// e.printStackTrace();
// }*/
//
// }
// }catch(Exception e){
// e.printStackTrace();
// }
// }
//
// //@Test
// public void getLayerByUUID() throws Exception{
// try{
// for(String scope:scopesProd){
// ScopeProvider.instance.set(scope);
// GeoNetworkPublisher reader=GeoNetwork.get();
//
// Configuration config = reader.getConfiguration();
// Account account=config.getScopeConfiguration().getAccounts().get(accountType);
//
// //System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword());
// System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
//
// Metadata meta = reader.getById(UUID);
//
//
// try{
// String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword());
// System.out.println("Decrypted Password: "+decryptedPassword);
// }catch(Exception e){
// System.out.println("ignoring exception during pwd decrypting");
// }
//
// System.out.println("SCOPE "+scope+" found meta "+meta);
//
// }
// }catch(Exception e){
// System.err.println("Error on getting layer by UUID: "+UUID);
// e.printStackTrace();
// }
// }
//
//
// //@Test
// public void getLayersBySearch() throws Exception{
// try{
// for(String scope:scopesProd){
// ScopeProvider.instance.set(scope);
// GeoNetworkPublisher reader=GeoNetwork.get();
//
// Configuration config = reader.getConfiguration();
// Account account=config.getScopeConfiguration().getAccounts().get(accountType);
//
// //System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword());
// System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
//
// try{
// String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword());
// System.out.println("Decrypted Password: "+decryptedPassword);
// }catch(Exception e){
// System.out.println("ignoring exception during pwd decrypting");
// }
//
//
// // req.addParam("keyword", "Thredds");
// //final GNSearchRequest req=new GNSearchRequest();
// final GNSearchRequest req = getRequest(true, textToSearch);
//// req.addParam(GNSearchRequest.Param.any,"Thredds");
// GNSearchResponse resp = reader.query(req);
// int publicCount=resp.getCount();
// reader.login(loginLevel);
// int totalCount=reader.query(req).getCount();
// System.out.println("QUERY "+textToSearch);
// System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
//
//// if(totalCount==0)
//// return;
//// try{
//// int last = totalCount>MAX?totalCount:MAX;
////
//// for(int i=0; i<last; i++){
//// //String xml = reader.getByIdAsRawString(resp.getMetadata(i).getUUID());
//// GNMetadata xml = resp.getMetadata(i);
//// //System.out.println(i+") is Thredds? "+containsString(xml, "Thredds"));
//// System.out.println(xml.toString());
//// }
//// }catch(Exception e ){
//// e.printStackTrace();
//// }
//
// }
// }catch(Exception e){
// e.printStackTrace();
// }
// }
//
//
// private boolean containsString(String txt, String value){
// return txt.contains(value);
// }
//
// /**
// * Gets the request.
// *
// * @param sortByTitle the sort by title
// * @param textToSearch the text to search
// * @return the request
// */
// public GNSearchRequest getRequest(boolean sortByTitle, String textToSearch) {
//
// GNSearchRequest req = new GNSearchRequest();
//
// if(sortByTitle)
// req.addConfig(GNSearchRequest.Config.sortBy, "title");
//
// if(textToSearch==null || textToSearch.isEmpty()){
// req.addParam(GNSearchRequest.Param.any, textToSearch);
// System.out.println("search by any text");
// }else{
// req.addParam(GNSearchRequest.Param.title, textToSearch);
// req.addConfig(GNSearchRequest.Config.similarity, "1");
// System.out.println("search by title");
// }
// System.out.println("text to search "+textToSearch);
//
// return req;
// }
//
//
//// @Test
// public void getCountProd() throws Exception{
// try{
// for(String scope:scopesProd){
// ScopeProvider.instance.set(scope);
// GeoNetworkReader reader=GeoNetwork.get();
// final GNSearchRequest req=new GNSearchRequest();
// // req.addParam("keyword", "Thredds");
//// req.addParam(GNSearchRequest.Param.any,"Oscar");
// int publicCount=reader.query(req).getCount();
// reader.login(loginLevel);
// int totalCount=reader.query(req).getCount();
// System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
// }
// }catch (Exception e) {
// e.printStackTrace();
// }
// }
//}

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
//import javax.servlet.ServletException;
//
//import org.gcube.datatransfer.resolver.GeonetworkRequestDecoder;

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import java.io.StringReader;

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.gis.entity.GisLayerItem;
import org.gcube.datatransfer.resolver.services.GisResolver;

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import java.io.IOException;

View File

@ -0,0 +1,64 @@
package org.gcube.datatransfer.test;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
import org.gcube.common.storagehub.client.proxies.ItemManagerClient;
import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader;
import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference;
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit;
import org.gcube.datatransfer.resolver.services.CatalogueResolver;
import org.gcube.datatransfer.resolver.services.StorageHubResolver;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.shub.StorageHubMetadataResponseBuilder;
import org.junit.Before;
import org.junit.Test;
import com.itextpdf.text.log.SysoCounter;
/**
* The Class CatalogueResolverTest.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* May 13, 2019
*/
public class StorageHubTest {
private static String entityName = "using_e-infrastructures_for_biodiversity_conservation";
private static String entityContext = "ctlg";
private static String vreName = "BlueBridgeProject";
private String rootContextScope = "/d4science.research-infrastructures.eu";
private String authorizationToken = "ea16e0fa-722a-4589-83b0-0a731d2d4039-843339462";
//@Before
public void init() {
UriResolverSmartGearManagerInit.setRootContextScope(rootContextScope);
}
//@Test
public void testStreamDescriptorInfo() {
System.out.println("testStreamDescriptorInfo starts...");
ScopeProvider.instance.set(rootContextScope);
SecurityTokenProvider.instance.set(authorizationToken);
String storageHubId = "E_NGJIUEYvU09sNG1YY0R2VGIyaStWdGhDSW9sSjRNdDRkdVI2RHRGb1BZMVBaVFlzMG1mOU5QUEtFM1hQeE9kbw==";
try{
ItemManagerClient client = AbstractPlugin.item().build();
StreamDescriptor descriptor = client.resolvePublicLink(storageHubId);
System.out.println("Descriptor: "+descriptor);
}catch(Exception e){
e.printStackTrace();
}
}
}

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

View File

@ -1,3 +1,4 @@
package org.gcube.datatransfer.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

View File

@ -0,0 +1,129 @@
package org.gcube.datatransfer.test;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.services.WekeoResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class WekeoResolverTest.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 31, 2021
*/
public class WekeoResolverTest {
private static Logger logger = LoggerFactory.getLogger(WekeoResolverTest.class);
private final static String RUNTIME_WKEO_RESOURCE_NAME = "WekeoDataBroker";
public static final String scope = "/gcube/devsec/devVRE";
//@Test
public void testWekeo() throws Exception{
StringBuilder wekeoResponse = new StringBuilder();
try {
logger.info(" test starts...");
ScopeProvider.instance.set(scope);
AccessPoint wekeoAccessPoint = WekeoResolver.readWekeoServiceEndpoint(null, scope);
if (wekeoAccessPoint != null) {
String wekeoUsername = wekeoAccessPoint.username();
String wekeoAddress = wekeoAccessPoint.address();
String wekeoPwd = wekeoAccessPoint.password();
// printing the access point found
if (logger.isDebugEnabled()) {
String msg = String.format("Found the username '%s' and the address '%s' to perform the request",
wekeoUsername, wekeoAddress);
logger.debug(msg);
}
logger.info("The pwd is: "+wekeoPwd);
// decrypting the pwd
if (wekeoPwd != null) {
wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoPwd);
logger.info("Decrypted pwd registered into Access Point '" + wekeoAccessPoint.name() + "' is: "
+ wekeoPwd.substring(0,wekeoPwd.length()/2)+"...");
}
if (wekeoUsername != null && wekeoPwd != null & wekeoAddress != null) {
HttpURLConnection connection = null;
try {
String authString = wekeoUsername + ":" + wekeoPwd;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
logger.debug("Base64 encoded auth string: " + authStringEnc);
logger.info("Performing the request to: "+wekeoAddress);
URL url = new URL(wekeoAddress);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream content = (InputStream) connection.getInputStream();
InputStreamReader in = new InputStreamReader(content);
logger.info("the response code is: "+connection.getResponseCode());
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
logger.debug("reading the response...");
while ((numCharsRead = in.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
wekeoResponse.append(sb.toString());
//System.out.println(wekeoResponse);
} catch (Exception e) {
logger.error(e.getMessage(), e);
String error = String.format("Error on performing request to %s", wekeoAddress);
throw new Exception(error);
} finally {
try {
if (connection != null) {
connection.disconnect();
}
}catch (Exception e) {
}
}
} else {
String error = String.format(
"I cannot read the configurations (adress, username,password) from %s in the scope %s",
RUNTIME_WKEO_RESOURCE_NAME, scope);
throw new Exception(error);
}
}
// to be sure
if (wekeoResponse.length() == 0) {
String error = String
.format("Sorry an error occured on getting the access token from Wekeo. Please, retry the request");
throw new Exception(error);
}
logger.info("returning: \n"+wekeoResponse.toString());
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
}

View File

@ -15,10 +15,19 @@ import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
import org.gcube.common.storagehub.client.proxies.ItemManagerClient;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
import org.gcube.datatransfer.resolver.services.StorageHubResolver;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.shub.StorageHubMetadataResponseBuilder;
import org.gcube.datatransfer.resolver.util.HTTPCallsUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -81,7 +90,6 @@ public class TestResolvers {
}
}
/**
* Storage hub test.
*

View File

@ -11,3 +11,4 @@
/gcube.gcubekey
/preprod.gcubekey
/pred4s.gcubekey
/log4j.properties