httpClient instead of restTemplate
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-access/gcube-geonetwork-connector@160116 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
0469c391e9
commit
907ee88b47
|
@ -27,7 +27,7 @@
|
|||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_75">
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
|
|
26
pom.xml
26
pom.xml
|
@ -13,7 +13,12 @@
|
|||
<scm>
|
||||
<url>https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/gcube-geonetwork-connector/</url>
|
||||
</scm>
|
||||
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -64,14 +69,14 @@
|
|||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>3.2.13.RELEASE</version>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>3.2.13.RELEASE</version>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.json-lib</groupId>
|
||||
|
@ -84,8 +89,13 @@
|
|||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.6.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
|
@ -4,10 +4,15 @@ import java.io.IOException;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
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.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
@ -15,9 +20,7 @@ import org.gcube.data.access.connector.rest.entity.AccessibleCredentialsEntity;
|
|||
import org.gcube.data.access.connector.rest.entity.SDIEntity;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
|
@ -27,20 +30,28 @@ public class GCubeRestClient {
|
|||
private static String CONTEXT_USER = "CONTEXT_USER";
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
|
||||
public AccessibleCredentialsEntity getAccessibleCredentials(String url) {
|
||||
|
||||
System.out.println("REST call to URL: " + url);
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
|
||||
CloseableHttpClient httpclient = HttpClients.custom().build();
|
||||
|
||||
RequestConfig config = RequestConfig.custom().build();
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
httpget.setConfig(config);
|
||||
|
||||
logger.warn("Executing request " + httpget.getRequestLine() + " to " + url);
|
||||
|
||||
try {
|
||||
String response = restTemplate.getForObject(url, String.class);
|
||||
System.out.println("JSON response: \n" + response);
|
||||
|
||||
CloseableHttpResponse response = httpclient.execute(httpget);
|
||||
String json = EntityUtils.toString(response.getEntity());
|
||||
|
||||
logger.warn("JSON response: \n" + json);
|
||||
|
||||
JSONObject jsonObject = JSONObject.fromObject(response);
|
||||
JSONObject jsonObject = JSONObject.fromObject(json);
|
||||
return (AccessibleCredentialsEntity) JSONObject.toBean(jsonObject, AccessibleCredentialsEntity.class);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error in getAccessibleCredentials() method: " + e.getMessage());
|
||||
logger.error("Error in getAccessibleCredentials() method: " + e.getMessage());
|
||||
return new AccessibleCredentialsEntity();
|
||||
}
|
||||
}
|
||||
|
@ -48,17 +59,32 @@ public class GCubeRestClient {
|
|||
public AccessibleCredentialsEntity getGeneralAccessibleCredentials(String url, String host) {
|
||||
|
||||
logger.warn("REST call to URL: " + url);
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
CloseableHttpClient httpclient = HttpClients.custom().build();
|
||||
|
||||
HttpHost target = new HttpHost(host);
|
||||
|
||||
RequestConfig config = RequestConfig.custom().build();
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
httpget.setConfig(config);
|
||||
|
||||
logger.warn("Executing request " + httpget.getRequestLine() + " to " + url);
|
||||
|
||||
try {
|
||||
ResponseEntity<SDIEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, SDIEntity.class);
|
||||
|
||||
String baseEndpoint = response.getBody().getGeonetworkConfiguration().getBaseEndpoint();
|
||||
AccessibleCredentialsEntity result = new AccessibleCredentialsEntity();
|
||||
CloseableHttpResponse resp = httpclient.execute(target, httpget);
|
||||
String json = EntityUtils.toString(resp.getEntity());
|
||||
logger.warn("JSON response: \n" + json);
|
||||
|
||||
Gson gson = new Gson();
|
||||
SDIEntity response =(SDIEntity) gson.fromJson(json, SDIEntity.class);
|
||||
|
||||
String baseEndpoint = response.getGeonetworkConfiguration().get(0).getBaseEndpoint();
|
||||
AccessibleCredentialsEntity result = new AccessibleCredentialsEntity();
|
||||
|
||||
if (baseEndpoint.contains(host)){
|
||||
//get credentials from geonetworkConfiguration - to give priority on CONTEXT_MANAGER (if it exists), otherwise CONTEXT_USER
|
||||
List<AccessibleCredentialsEntity> credentials = response.getBody().getGeonetworkConfiguration().getAccessibleCredentials();
|
||||
|
||||
List<AccessibleCredentialsEntity> credentials = response.getGeonetworkConfiguration().get(0).getAccessibleCredentials();
|
||||
Iterator<AccessibleCredentialsEntity> iter = credentials.iterator();
|
||||
while (iter.hasNext()){
|
||||
AccessibleCredentialsEntity entity = iter.next();
|
||||
|
@ -89,20 +115,20 @@ public class GCubeRestClient {
|
|||
HttpGet httpget = new HttpGet(get);
|
||||
httpget.setConfig(config);
|
||||
|
||||
System.out.println("Executing request " + httpget.getRequestLine() + " to " + target);
|
||||
logger.warn("Executing request " + httpget.getRequestLine() + " to " + get);
|
||||
|
||||
try {
|
||||
CloseableHttpResponse response = httpclient.execute(target, httpget);
|
||||
String json = EntityUtils.toString(response.getEntity());
|
||||
System.out.println(json);
|
||||
logger.warn("JSON response: \n" + json);
|
||||
JSONObject jsonObject = JSONObject.fromObject(json);
|
||||
|
||||
AccessibleCredentialsEntity ac = (AccessibleCredentialsEntity) JSONObject.toBean(jsonObject, AccessibleCredentialsEntity.class);
|
||||
System.out.println(ac.getUsername());
|
||||
logger.info("Credentials Entity Username: "+ac.getUsername());
|
||||
return ac;
|
||||
} catch (IOException e) {
|
||||
|
||||
System.out.println(e.getMessage());
|
||||
logger.error("Error in getAccessibleCredentialsHttp() method: " + e.getMessage());
|
||||
return new AccessibleCredentialsEntity();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package org.gcube.data.access.connector.rest.entity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SDIEntity {
|
||||
|
||||
private String contextName;
|
||||
private InstancesEntity geonetworkConfiguration;
|
||||
private GeoserverClusterConfigurationEntity geoserverClusterConfiguration;
|
||||
private List<InstancesEntity> geonetworkConfiguration;
|
||||
private List<GeoserverClusterConfigurationEntity> geoserverClusterConfiguration;
|
||||
|
||||
public String getContextName() {
|
||||
return contextName;
|
||||
|
@ -14,19 +16,19 @@ public class SDIEntity {
|
|||
this.contextName = contextName;
|
||||
}
|
||||
|
||||
public InstancesEntity getGeonetworkConfiguration() {
|
||||
public List<InstancesEntity> getGeonetworkConfiguration() {
|
||||
return geonetworkConfiguration;
|
||||
}
|
||||
|
||||
public void setGeonetworkConfiguration(InstancesEntity geonetworkConfiguration) {
|
||||
public void setGeonetworkConfiguration(List<InstancesEntity> geonetworkConfiguration) {
|
||||
this.geonetworkConfiguration = geonetworkConfiguration;
|
||||
}
|
||||
|
||||
public GeoserverClusterConfigurationEntity getGeoserverClusterConfiguration() {
|
||||
public List<GeoserverClusterConfigurationEntity> getGeoserverClusterConfiguration() {
|
||||
return geoserverClusterConfiguration;
|
||||
}
|
||||
|
||||
public void setGeoserverClusterConfiguration(GeoserverClusterConfigurationEntity geoserverClusterConfiguration) {
|
||||
public void setGeoserverClusterConfiguration(List<GeoserverClusterConfigurationEntity> geoserverClusterConfiguration) {
|
||||
this.geoserverClusterConfiguration = geoserverClusterConfiguration;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,35 +2,58 @@ package org.gcube.data.access.connector;
|
|||
|
||||
import java.util.Arrays;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
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.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.gcube.data.access.connector.rest.entity.AccessibleCredentialsEntity;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Test {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger("Test");
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String url = "http://geonetwork-sdi.dev.d4science.org/geonetwork/srv/api/0.1/me";
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
logger.info("Test");
|
||||
|
||||
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
||||
String url = "http://geonetwork-sdi.dev.d4science.org/geonetwork/srv/api/0.1/me";
|
||||
|
||||
String token = "admin:admin";
|
||||
headers.add("Authorization", "Basic " + Base64.encodeBase64String(token.getBytes()));
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
|
||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
|
||||
System.out.println("Result - status ("+ response.getStatusCode() + ") has body: " + response.hasBody());
|
||||
System.out.println(response.getBody());
|
||||
// String response = restTemplate.getForObject(url, String.class);
|
||||
// System.out.println(response);
|
||||
|
||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(new AuthScope("proxy.eng.it", 3128), new UsernamePasswordCredentials("usr", "pass"));
|
||||
|
||||
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
||||
HttpHost proxy = new HttpHost("proxy.eng.it", 3128);
|
||||
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
httpget.setConfig(config);
|
||||
httpget.setHeader("Content-type", "application/json");
|
||||
httpget.setHeader("Authorization", "Basic " + Base64.encodeBase64String(token.getBytes()));
|
||||
logger.warn("Executing request " + httpget.getRequestLine() + " to " + url);
|
||||
|
||||
CloseableHttpResponse response = httpclient.execute(httpget);
|
||||
|
||||
System.out.println("response code: "+ response.getStatusLine().getStatusCode());
|
||||
String json = EntityUtils.toString(response.getEntity());
|
||||
logger.warn("JSON response: \n" + json);
|
||||
|
||||
logger.info("Result - status ("+ response.getStatusLine().getStatusCode() + ") has body: " + response.getEntity());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
logger.error("Error in Test: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,88 +7,108 @@ import java.net.Proxy;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
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.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.gcube.data.access.connector.rest.entity.AccessibleCredentialsEntity;
|
||||
import org.gcube.data.access.connector.rest.entity.SDIEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class TestGeonetworkConfiguration {
|
||||
|
||||
private static String CONTEXT_MANAGER = "CONTEXT_MANAGER";
|
||||
private static String CONTEXT_USER = "CONTEXT_USER";
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger("TestGeonetworkConfiguration");
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
|
||||
logger.info("TestGeonetworkConfiguration");
|
||||
|
||||
String url = "http://sdi-d-d4s.d4science.org/sdi-service/gcube/service/SDI?gcube-token=feda0617-cd9d-4841-b6f0-e047da5d32ed-98187548";
|
||||
RestTemplate restTemplate = new RestTemplate(); //getRestTemplate();
|
||||
|
||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(new AuthScope("proxy.eng.it", 3128), new UsernamePasswordCredentials("usr", "pass"));
|
||||
|
||||
//String response = restTemplate.getForObject(url, String.class);
|
||||
//System.out.println(response);
|
||||
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
||||
HttpHost proxy = new HttpHost("proxy.eng.it", 3128);
|
||||
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
httpget.setConfig(config);
|
||||
|
||||
ResponseEntity<SDIEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, SDIEntity.class);
|
||||
System.out.println("Result " + response.getBody().getGeonetworkConfiguration().getBaseEndpoint() );
|
||||
//System.out.println("Result " + response.getBody().getGeoserverClusterConfiguration().getAvailableInstances().get(0).getBaseEndpoint() );
|
||||
logger.warn("Executing request " + httpget.getRequestLine() + " to " + url);
|
||||
|
||||
System.out.println("Result " + getAccessibleCredentials().getAccessType());
|
||||
CloseableHttpResponse resp = httpclient.execute(httpget);
|
||||
String json = EntityUtils.toString(resp.getEntity());
|
||||
logger.warn("JSON response: \n" + json);
|
||||
|
||||
Gson gson = new Gson();
|
||||
SDIEntity response =(SDIEntity) gson.fromJson(json, SDIEntity.class);
|
||||
|
||||
logger.warn("SDIEntity: \n" + response.toString());
|
||||
|
||||
logger.info("Credentials AccessType " + getAccessibleCredentials().getAccessType());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
logger.error("Error in TestGeonetworkConfiguration: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static AccessibleCredentialsEntity getAccessibleCredentials() {
|
||||
String url = "http://sdi-d-d4s.d4science.org/sdi-service/gcube/service/SDI?gcube-token=feda0617-cd9d-4841-b6f0-e047da5d32ed-98187548";
|
||||
RestTemplate restTemplate = new RestTemplate(); //getRestTemplate();
|
||||
try {
|
||||
String url = "http://sdi-d-d4s.d4science.org/sdi-service/gcube/service/SDI?gcube-token=feda0617-cd9d-4841-b6f0-e047da5d32ed-98187548";
|
||||
|
||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(new AuthScope("proxy.eng.it", 3128), new UsernamePasswordCredentials("usr", "pass"));
|
||||
|
||||
String host = "geonetwork-sdi.dev.d4science.org";
|
||||
//String host = "geonetwork1-d-d4s.d4science.org";
|
||||
ResponseEntity<SDIEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, SDIEntity.class);
|
||||
//System.out.println("Result " + response.getBody().getGeonetworkConfiguration().getBaseEndpoint() );
|
||||
//System.out.println("Result " + response.getBody().getGeoserverClusterConfiguration().getAvailableInstances().get(0).getBaseEndpoint() );
|
||||
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
||||
HttpHost proxy = new HttpHost("proxy.eng.it", 3128);
|
||||
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
httpget.setConfig(config);
|
||||
|
||||
String baseEndpoint = response.getBody().getGeonetworkConfiguration().getBaseEndpoint();
|
||||
AccessibleCredentialsEntity result = new AccessibleCredentialsEntity();
|
||||
|
||||
if (baseEndpoint.contains(host)){
|
||||
List<AccessibleCredentialsEntity> credentials = response.getBody().getGeonetworkConfiguration().getAccessibleCredentials();
|
||||
Iterator<AccessibleCredentialsEntity> iter = credentials.iterator();
|
||||
while (iter.hasNext()){
|
||||
AccessibleCredentialsEntity entity = iter.next();
|
||||
if (CONTEXT_MANAGER.equals(entity.getAccessType())){
|
||||
return entity;
|
||||
}
|
||||
if ((result.getAccessType() == null) && CONTEXT_USER.equals(entity.getAccessType())){
|
||||
result = entity;
|
||||
String host = "geonetwork1-d-d4s.d4science.org";
|
||||
logger.warn("Executing request " + httpget.getRequestLine() + " to " + url);
|
||||
|
||||
CloseableHttpResponse resp = httpclient.execute(httpget);
|
||||
String json = EntityUtils.toString(resp.getEntity());
|
||||
logger.warn("JSON response: \n" + json);
|
||||
|
||||
Gson gson = new Gson();
|
||||
SDIEntity response =(SDIEntity) gson.fromJson(json, SDIEntity.class);
|
||||
|
||||
String baseEndpoint = response.getGeonetworkConfiguration().get(0).getBaseEndpoint();
|
||||
AccessibleCredentialsEntity result = new AccessibleCredentialsEntity();
|
||||
|
||||
if (baseEndpoint.contains(host)){
|
||||
List<AccessibleCredentialsEntity> credentials = response.getGeonetworkConfiguration().get(0).getAccessibleCredentials();
|
||||
Iterator<AccessibleCredentialsEntity> iter = credentials.iterator();
|
||||
while (iter.hasNext()){
|
||||
AccessibleCredentialsEntity entity = iter.next();
|
||||
if (CONTEXT_MANAGER.equals(entity.getAccessType())){
|
||||
return entity;
|
||||
}
|
||||
if ((result.getAccessType() == null) && CONTEXT_USER.equals(entity.getAccessType())){
|
||||
result = entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
logger.error("Error in getAccessibleCredentials: " + e.getMessage());
|
||||
}
|
||||
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private static RestTemplate getRestTemplate() {
|
||||
|
||||
final Authenticator authenticator = new Authenticator() {
|
||||
@Override
|
||||
public PasswordAuthentication getPasswordAuthentication() {
|
||||
return (new PasswordAuthentication("username", "password".toCharArray()));
|
||||
}
|
||||
};
|
||||
|
||||
Authenticator.setDefault(authenticator);
|
||||
|
||||
final SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||
final InetSocketAddress address = new InetSocketAddress("proxy.eng.it", 3128);
|
||||
final Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
|
||||
factory.setProxy(proxy);
|
||||
|
||||
final RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setRequestFactory(factory);// pass the factory instance to restTemplate
|
||||
|
||||
return restTemplate;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,19 +12,25 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.gcube.data.access.connector.rest.entity.AccessibleCredentialsEntity;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
public class TestRest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger("TestRest");
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
logger.info("TestRest");
|
||||
|
||||
String url = "sdi-d-d4s.d4science.org";
|
||||
|
||||
//http://sdi-d-d4s.d4science.org/sdi-service/gcube/service/GeoNetwork/credentials/geonetwork-sdi.dev.d4science.org?gcube-token=feda0617-cd9d-4841-b6f0-e047da5d32ed-98187548
|
||||
|
||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(new AuthScope("proxy.eng.it", 3128), new UsernamePasswordCredentials("user", "pwd"));
|
||||
credsProvider.setCredentials(new AuthScope("proxy.eng.it", 3128), new UsernamePasswordCredentials("usr", "pass"));
|
||||
|
||||
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
||||
try {
|
||||
|
@ -35,20 +41,18 @@ public class TestRest {
|
|||
HttpGet httpget = new HttpGet("/sdi-service/gcube/service/GeoNetwork/credentials/geonetwork-sdi.dev.d4science.org?gcube-token=feda0617-cd9d-4841-b6f0-e047da5d32ed-98187548");
|
||||
httpget.setConfig(config);
|
||||
|
||||
System.out.println("Executing request " + httpget.getRequestLine() + " to " + target + " via " + proxy);
|
||||
|
||||
logger.warn("Executing request " + httpget.getRequestLine() + " to " + target + " via " + proxy);
|
||||
CloseableHttpResponse response = httpclient.execute(target, httpget);
|
||||
try {
|
||||
System.out.println("----------------------------------------");
|
||||
System.out.println(response.getStatusLine());
|
||||
//System.out.println(EntityUtils.toString(response.getEntity()));
|
||||
logger.warn("Status Code: "+response.getStatusLine().getStatusCode());
|
||||
|
||||
String json = EntityUtils.toString(response.getEntity());
|
||||
System.out.println(json);
|
||||
logger.warn("JSON response: \n" + json);
|
||||
JSONObject jsonObject = JSONObject.fromObject(json);
|
||||
|
||||
AccessibleCredentialsEntity ac = (AccessibleCredentialsEntity) JSONObject.toBean(jsonObject, AccessibleCredentialsEntity.class);
|
||||
System.out.println(ac.getUsername());
|
||||
|
||||
logger.info("Credentials Entity Username"+ac.getUsername());
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue