Commit cefe3653 authored by Michael Ritter's avatar Michael Ritter
Browse files

use http-client when making connections to ACE partner sites to... remedy... stuff

parent c27c9ba5
...@@ -249,6 +249,11 @@ ...@@ -249,6 +249,11 @@
<version>1.9-SNAPSHOT</version> <version>1.9-SNAPSHOT</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
......
...@@ -97,6 +97,10 @@ public class PartnerSite implements Serializable { ...@@ -97,6 +97,10 @@ public class PartnerSite implements Serializable {
return pass; return pass;
} }
public String getCredentials() {
return user + ":" + pass;
}
@Override @Override
public boolean equals( Object object ) { public boolean equals( Object object ) {
// TODO: Warning - this method won't work in the case the id fields are not set // TODO: Warning - this method won't work in the case the id fields are not set
......
...@@ -35,6 +35,7 @@ import edu.umiacs.ace.util.EntityManagerServlet; ...@@ -35,6 +35,7 @@ import edu.umiacs.ace.util.EntityManagerServlet;
import edu.umiacs.util.Strings; import edu.umiacs.util.Strings;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
...@@ -44,6 +45,15 @@ import javax.servlet.RequestDispatcher; ...@@ -44,6 +45,15 @@ import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
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.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
...@@ -144,12 +154,14 @@ public final class PartnerSiteServlet extends EntityManagerServlet { ...@@ -144,12 +154,14 @@ public final class PartnerSiteServlet extends EntityManagerServlet {
try { try {
URL encodedUrl = new URL(url); URL encodedUrl = new URL(url);
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);
URLConnection uc = encodedUrl.openConnection(); provider.setCredentials(AuthScope.ANY, credentials);
uc.setRequestProperty("Authorization", "Basic " + encode(
user + ":" + pass)); HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
uc.getInputStream().close(); HttpGet get = new HttpGet(encodedUrl.toURI());
HttpResponse execute = client.execute(get);
execute.getEntity().getContent().close();
return FailType.SUCCESS; return FailType.SUCCESS;
} catch ( MalformedURLException e ) { } catch ( MalformedURLException e ) {
......
...@@ -32,18 +32,26 @@ ...@@ -32,18 +32,26 @@
package edu.umiacs.ace.remote; package edu.umiacs.ace.remote;
import edu.umiacs.ace.monitor.peers.PartnerSite; import edu.umiacs.ace.monitor.peers.PartnerSite;
import edu.umiacs.ace.monitor.peers.PartnerSiteServlet;
import edu.umiacs.util.Strings; import edu.umiacs.util.Strings;
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.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
/** /**
* Class to cache various ACE json elements according to some arbitrary policy. * Class to cache various ACE json elements according to some arbitrary policy.
...@@ -86,7 +94,7 @@ public class JsonGateway { ...@@ -86,7 +94,7 @@ public class JsonGateway {
URL u = new URL(site.getRemoteURL() + DIGEST_SUFFIX + collection); URL u = new URL(site.getRemoteURL() + DIGEST_SUFFIX + collection);
LOG.trace("Attempting to pull: " + u); LOG.trace("Attempting to pull: " + u);
return getURLInputStream(u, site.getUser() + ":" + site.getPass()); return getURLInputStream(u, site.getCredentials());
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
LOG.error("Error reading site " + site.getRemoteURL(), ioe); LOG.error("Error reading site " + site.getRemoteURL(), ioe);
return null; return null;
...@@ -188,7 +196,7 @@ public class JsonGateway { ...@@ -188,7 +196,7 @@ public class JsonGateway {
URL u = new URL(site.getRemoteURL() + ITEMROOT_SUFFIX + collectionid); URL u = new URL(site.getRemoteURL() + ITEMROOT_SUFFIX + collectionid);
LOG.trace("Attempting to pull: " + u); LOG.trace("Attempting to pull: " + u);
return readJSONValue(u, ParentChildBean.class); return readJSONValue(u, site.getCredentials(), ParentChildBean.class);
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
LOG.error("Error reading site " + site.getRemoteURL(), ioe); LOG.error("Error reading site " + site.getRemoteURL(), ioe);
return null; return null;
...@@ -202,7 +210,7 @@ public class JsonGateway { ...@@ -202,7 +210,7 @@ public class JsonGateway {
+ parentpath); + parentpath);
LOG.trace("Attempting to pull: " + u); LOG.trace("Attempting to pull: " + u);
return readJSONValue(u, ParentChildBean.class); return readJSONValue(u, site.getCredentials(), ParentChildBean.class);
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
LOG.error("Error reading site " + site.getRemoteURL(), ioe); LOG.error("Error reading site " + site.getRemoteURL(), ioe);
return null; return null;
...@@ -215,7 +223,7 @@ public class JsonGateway { ...@@ -215,7 +223,7 @@ public class JsonGateway {
URL u = new URL(site.getRemoteURL() + REPORT_SUFFIX + collectionid); URL u = new URL(site.getRemoteURL() + REPORT_SUFFIX + collectionid);
LOG.trace("Attempting to pull: " + u); LOG.trace("Attempting to pull: " + u);
return readJSONValue(u, ReportBean.class); return readJSONValue(u, site.getCredentials(), ReportBean.class);
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
LOG.error("Error reading site " + site.getRemoteURL(), ioe); LOG.error("Error reading site " + site.getRemoteURL(), ioe);
return null; return null;
...@@ -228,7 +236,7 @@ public class JsonGateway { ...@@ -228,7 +236,7 @@ public class JsonGateway {
URL u = new URL(site.getRemoteURL() + STATUS_SUFFIX); URL u = new URL(site.getRemoteURL() + STATUS_SUFFIX);
LOG.trace("Attempting to pull: " + u); LOG.trace("Attempting to pull: " + u);
return readJSONValue(u, StatusBean.class); return readJSONValue(u, site.getCredentials(), StatusBean.class);
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
LOG.error("Error reading site " + site.getRemoteURL(), ioe); LOG.error("Error reading site " + site.getRemoteURL(), ioe);
return null; return null;
...@@ -241,7 +249,7 @@ public class JsonGateway { ...@@ -241,7 +249,7 @@ public class JsonGateway {
URL u = new URL(site.getRemoteURL() + SUMMARY_SUFFIX); URL u = new URL(site.getRemoteURL() + SUMMARY_SUFFIX);
LOG.trace("Attempting to pull: " + u); LOG.trace("Attempting to pull: " + u);
//mapper.readValue(u, ); //mapper.readValue(u, );
return readJSONValue(u, SummaryBean.class); return readJSONValue(u, site.getCredentials(), SummaryBean.class);
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
LOG.error("Error reading site " + site.getRemoteURL(), ioe); LOG.error("Error reading site " + site.getRemoteURL(), ioe);
return null; return null;
...@@ -264,14 +272,8 @@ public class JsonGateway { ...@@ -264,14 +272,8 @@ public class JsonGateway {
private Map<Long, Long> itemRootMapUpdate = new HashMap<Long, Long>(); private Map<Long, Long> itemRootMapUpdate = new HashMap<Long, Long>();
} }
private <T> T readJSONValue( URL u, Class<T> clazz ) throws IOException { private <T> T readJSONValue( URL u, String auth, Class<T> clazz ) throws IOException {
return mapper.readValue(getURLInputStream(u), clazz); return mapper.readValue(getURLInputStream(u, auth), clazz);
}
private InputStream getURLInputStream( URL u ) throws IOException {
URLConnection uc = u.openConnection();
uc.setReadTimeout(5000);
return uc.getInputStream();
} }
/** /**
...@@ -285,10 +287,18 @@ public class JsonGateway { ...@@ -285,10 +287,18 @@ public class JsonGateway {
* @throws IOException * @throws IOException
*/ */
private InputStream getURLInputStream(URL u, String auth) throws IOException { private InputStream getURLInputStream(URL u, String auth) throws IOException {
URLConnection uc = u.openConnection(); CredentialsProvider provider = new BasicCredentialsProvider();
uc.setRequestProperty("Authorization", "Basic " + PartnerSiteServlet.encode(auth)); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(auth);
uc.setReadTimeout(5000); provider.setCredentials(AuthScope.ANY, credentials);
return uc.getInputStream();
try {
HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
HttpGet get = new HttpGet(u.toURI());
HttpResponse execute = client.execute(get);
return execute.getEntity().getContent();
} catch (URISyntaxException e) {
// should never happen
return null;
}
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment