completed WekeoResolverTest

This commit is contained in:
Francesco Mangiacrapa 2021-04-01 17:15:03 +02:00
parent c3f0a7663f
commit c6adaba745
18 changed files with 329 additions and 289 deletions

20
pom.xml
View File

@ -137,7 +137,6 @@
<scope>compile</scope>
</dependency>
<!-- weld -->
<dependency>
<groupId>javax.enterprise</groupId>
@ -170,6 +169,11 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
</dependency>
<!-- GIS RESOLVER DEPENDENCIES -->
@ -180,13 +184,25 @@
<scope>compile</scope>
</dependency>
<!-- 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

@ -94,7 +94,7 @@ public class WekeoResolver {
//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);
String msg = String.format("Found the username ' %s' and the address '%s' to perform the request", wekeoUsername, wekeoAddress);
logger.debug(msg);
}
@ -214,6 +214,4 @@ public class WekeoResolver {
}
}
}

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

@ -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

@ -1,18 +1,31 @@
package org.gcube.datatransfer.test;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
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.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
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.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -31,13 +44,16 @@ public class WekeoResolverTest {
public static final String scope = "/gcube/devsec/devVRE";
public static void main(String[] args) {
@Test
public void testWekeo() throws Exception{
StringBuilder wekeoResponse = new StringBuilder();
try {
logger.info("starts...");
String wekeoToken = null;
logger.info(" test starts...");
ScopeProvider.instance.set(scope);
AccessPoint wekeoAccessPoint = WekeoResolver.readWekeoServiceEndpoint(null, scope);
if (wekeoAccessPoint != null) {
@ -48,46 +64,61 @@ public class WekeoResolverTest {
// printing the access point found
if (logger.isDebugEnabled()) {
String msg = String.format("Found the username %s and the address %s to perform the request",
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(wekeoAccessPoint.password());
logger.info("Returning decrypted pwd registered into " + RUNTIME_WKEO_RESOURCE_NAME + " SE: "
+ wekeoPwd.substring(0, wekeoPwd.length() / 2) + "....");
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 {
// performing the HTTP request with Basic Authentication
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(wekeoUsername,
wekeoPwd);
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
logger.info("calling the URL and performing basic authentication to: " + wekeoAddress);
HttpResponse response = client.execute(new HttpGet(wekeoAddress));
int statusCode = response.getStatusLine().getStatusCode();
logger.info("the response stus code is: " + statusCode);
if (statusCode == 200) {
InputStream is = response.getEntity().getContent();
BufferedInputStream bif = new BufferedInputStream(is);
wekeoToken = bif.toString();
logger.info("got the wekeo token: " + wekeoToken.substring(0, wekeoToken.length() / 2)
+ "....");
} else {
String error = String.format("The request to %s returned status code %d", wekeoAddress,
statusCode);
throw new Exception(error);
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 {
@ -99,16 +130,17 @@ public class WekeoResolverTest {
}
// to be sure
if (wekeoToken == null) {
if (wekeoResponse.length() == 0) {
String error = String
.format("Sorry an rrror occured on getting the wekeo token. Please, retry the request");
.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);
}
}
}