- * To circumvent the limitations of older JREs that do not support connect timeout a - * controller thread is executed. The controller thread attempts to create a new socket - * within the given limit of time. If socket constructor does not return until the - * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException} - *
- * - * @param host the host name/IP - * @param port the port on the host - * @param params {@link HttpConnectionParams Http connection parameters} - * - * @return Socket a new socket - * - * @throws IOException if an I/O error occurs while creating the socket - * @throws UnknownHostException if the IP address of the host cannot be - * determined - */ - public Socket createSocket( - final String host, - final int port, - final InetAddress localAddress, - final int localPort, - final HttpConnectionParams params - ) throws IOException, UnknownHostException, ConnectTimeoutException { - if (params == null) { - throw new IllegalArgumentException("Parameters may not be null"); - } - int timeout = params.getConnectionTimeout(); - SocketFactory socketfactory = getSSLContext().getSocketFactory(); - if (timeout == 0) { - return socketfactory.createSocket(host, port, localAddress, localPort); - } else { - Socket socket = socketfactory.createSocket(); - SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); - SocketAddress remoteaddr = new InetSocketAddress(host, port); - socket.bind(localaddr); - socket.connect(remoteaddr, timeout); - return socket; - } - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int) - */ - public Socket createSocket(String host, int port) - throws IOException, UnknownHostException { - return getSSLContext().getSocketFactory().createSocket( - host, - port - ); - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean) - */ - public Socket createSocket( - Socket socket, - String host, - int port, - boolean autoClose) - throws IOException, UnknownHostException { - return getSSLContext().getSocketFactory().createSocket( - socket, - host, - port, - autoClose - ); - } - - public boolean equals(Object obj) { - return ((obj != null) && obj.getClass().equals(AllowAllSslProtocolSocketFactory.class)); - } - - public int hashCode() { - return AllowAllSslProtocolSocketFactory.class.hashCode(); - } -} diff --git a/src/main/java/eu/odp/harvest/geo/oai/http/AllowAllX509TrustManager.java b/src/main/java/eu/odp/harvest/geo/oai/http/AllowAllX509TrustManager.java deleted file mode 100644 index 8191325..0000000 --- a/src/main/java/eu/odp/harvest/geo/oai/http/AllowAllX509TrustManager.java +++ /dev/null @@ -1,61 +0,0 @@ -package eu.odp.harvest.geo.oai.http; - -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509TrustManager; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; - -/** - * Based on AllowAllX509TrustManager from Apache httpclient-ssl-contrib, but trusts all server certificates, not just - * self-signed. - * WARNING: Use of this class is insecure, as it basically nullifies the purpose of HTTPS. - * Do not use it if you need connections to trusted providers. - */ -public class AllowAllX509TrustManager implements X509TrustManager { - private X509TrustManager standardTrustManager = null; - - /** - * Constructor for AllowAllX509TrustManager. - */ - public AllowAllX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException { - super(); - TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); - factory.init(keystore); - TrustManager[] trustmanagers = factory.getTrustManagers(); - if (trustmanagers.length == 0) { - throw new NoSuchAlgorithmException("no trust manager found"); - } - this.standardTrustManager = (X509TrustManager)trustmanagers[0]; - } - - /** - * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType) - */ - public void checkClientTrusted(X509Certificate[] certificates,String authType) throws CertificateException { - standardTrustManager.checkClientTrusted(certificates,authType); - } - - /** - * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType) - */ - public void checkServerTrusted(X509Certificate[] certificates,String authType) throws CertificateException { -/* - if ((certificates != null) && (certificates.length == 1)) { - certificates[0].checkValidity(); - } else { - standardTrustManager.checkServerTrusted(certificates,authType); - } -*/ - } - - /** - * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() - */ - public X509Certificate[] getAcceptedIssuers() { - return this.standardTrustManager.getAcceptedIssuers(); - } -} diff --git a/src/main/java/eu/odp/harvest/geo/oai/xslt/HttpAwareUriResolver.java b/src/main/java/eu/odp/harvest/geo/oai/xslt/HttpAwareUriResolver.java index d4640af..9e70765 100644 --- a/src/main/java/eu/odp/harvest/geo/oai/xslt/HttpAwareUriResolver.java +++ b/src/main/java/eu/odp/harvest/geo/oai/xslt/HttpAwareUriResolver.java @@ -4,17 +4,18 @@ import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.xml.XsltUriResolver; -import org.apache.camel.impl.DefaultExchange; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; -import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.camel.component.xslt.XsltUriResolver; +import org.apache.camel.support.DefaultExchange; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.log4j.Logger; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamSource; -import java.io.ByteArrayInputStream; /** * Resolves a document reference for protocols "http" or "https" in an XSL document with HTTP client. @@ -25,7 +26,8 @@ public class HttpAwareUriResolver extends XsltUriResolver { private final static Logger LOG = Logger.getLogger(HttpAwareUriResolver.class); - private static HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); + private static HttpClient httpClient = HttpClients.custom() + .setConnectionManager(new PoolingHttpClientConnectionManager()).build(); private ProducerTemplate template; private CamelContext context; @@ -71,18 +73,11 @@ private Source resolveRoute(String href) { String[] parts = href.split("\\?"); exchange.getIn().setHeader("resourceIdentifiers", parts[1]); template.send(parts[0], exchange); - return new StreamSource(exchange.getOut().getBody(java.io.InputStream.class)); + return new StreamSource(exchange.getMessage().getBody(java.io.InputStream.class)); } private Source resolveHttp(String href) throws Exception { - GetMethod method = new GetMethod(href); - try { - httpClient.executeMethod(method); - byte[] bytes = method.getResponseBody(); - return new StreamSource(new ByteArrayInputStream(bytes)); - } - finally { - method.releaseConnection(); - } + HttpResponse response = httpClient.execute(new HttpGet(href)); + return new StreamSource(response.getEntity().getContent()); } } diff --git a/src/main/resources/camel-oai-pmh.xml b/src/main/resources/camel-oai-pmh.xml index 83a71a5..ba97401 100644 --- a/src/main/resources/camel-oai-pmh.xml +++ b/src/main/resources/camel-oai-pmh.xml @@ -1,11 +1,7 @@