follow redirects

This commit is contained in:
Fabio Sinibaldi 2020-10-20 15:43:18 +02:00
parent 0f5b212359
commit 0130e8e8c7
4 changed files with 30 additions and 10 deletions

View File

@ -7,3 +7,4 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixes ### Fixes
- Integration with gcube distribution (boms 2.0.0) - Integration with gcube distribution (boms 2.0.0)
- TransfererBuilder.getTransfererByHost now follows redirects

View File

@ -3,21 +3,19 @@ package org.gcube.data.transfer.library.transferers;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.net.MalformedURLException; import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.gcube.common.resources.gcore.HostingNode; import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.data.transfer.library.caches.CapabilitiesCache;
import org.gcube.data.transfer.library.client.Client; import org.gcube.data.transfer.library.client.Client;
import org.gcube.data.transfer.library.faults.HostingNodeNotFoundException; import org.gcube.data.transfer.library.faults.HostingNodeNotFoundException;
import org.gcube.data.transfer.library.faults.ServiceNotFoundException; import org.gcube.data.transfer.library.faults.ServiceNotFoundException;
import org.gcube.data.transfer.library.faults.UnreachableNodeException; import org.gcube.data.transfer.library.faults.UnreachableNodeException;
import org.gcube.data.transfer.model.TransferCapabilities;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class TransfererBuilder { public class TransfererBuilder {
@ -29,9 +27,17 @@ public class TransfererBuilder {
public static Transferer getTransfererByHost(String endpoint) throws UnreachableNodeException, ServiceNotFoundException{ public static Transferer getTransfererByHost(String endpoint) throws UnreachableNodeException, ServiceNotFoundException{
log.debug("Get transferer by Host "+endpoint); log.debug("Get transferer by Host "+endpoint);
try{ try{
URL url=new URL(endpoint); log.debug("Resolving redirects.. ");
String toUseEndpoint=resolveRedirects(endpoint);
log.debug("Using "+toUseEndpoint);
URL url=new URL(toUseEndpoint);
String baseUrl=url.getProtocol()+"://"+url.getHost()+":"+url.getPort(); String baseUrl=url.getProtocol()+"://"+url.getHost()+":"+url.getPort();
//TODO Implement checks //TODO Implement checks
// if(!Utils.pingURL(host, timeout)) throw new UnreachableNodeException("No response from host in "+timeout); // if(!Utils.pingURL(host, timeout)) throw new UnreachableNodeException("No response from host in "+timeout);
@ -71,4 +77,16 @@ public class TransfererBuilder {
return "http://"+found.get(0).profile().description().name(); return "http://"+found.get(0).profile().description().name();
} }
private static String resolveRedirects(String url) throws IOException{
log.debug("Resolving redirect for url {} ",url);
URL urlObj=new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
int status=connection.getResponseCode();
if(status>=300&&status<400){
String newUrl=connection.getHeaderField("Location");
log.debug("Following redirect from {} to {} ",url,newUrl);
return resolveRedirects(newUrl);
}else return url;
}
} }

View File

@ -28,9 +28,10 @@ import org.junit.Test;
public class TestClientCalls { public class TestClientCalls {
static String hostname="http://thredds-d-d4s.d4science.org:80"; // static String hostname="http://thredds-d-d4s.d4science.org:80";
static String hostname="https://geoserver1.dev.d4science.org";
// static String hostname="https://geoserver1-spatial-dev.d4science.org"; // static String hostname="https://geoserver1-spatial-dev.d4science.org";
static String scope="/gcube/devsec/devVRE"; static String scope="/gcube/devNext/NextNext";
// static String scope="/pred4s/preprod/preVRE"; // static String scope="/pred4s/preprod/preVRE";
static Client client; static Client client;

View File

@ -32,7 +32,7 @@ import org.junit.Test;
public class TransfererTest { public class TransfererTest {
static String hostname="http://thredds-d-d4s.d4science.org:80"; static String hostname="http://geoserver1.dev.d4science.org:80";
// static String nodeId="462b68c5-463f-4295-86da-37d6c0abc7ea"; // static String nodeId="462b68c5-463f-4295-86da-37d6c0abc7ea";
static String scope="/gcube/devsec/devVRE"; static String scope="/gcube/devsec/devVRE";