diff --git a/pom.xml b/pom.xml index 84646588..f6b01f84 100644 --- a/pom.xml +++ b/pom.xml @@ -259,11 +259,11 @@ --> - - commons-httpclient - commons-httpclient - 3.1 - + + org.apache.httpcomponents + httpclient + 4.3.4 + uk.org.lidalia diff --git a/src/main/java/com/notnoop/apns/internal/TlsTunnelBuilder.java b/src/main/java/com/notnoop/apns/internal/TlsTunnelBuilder.java index 5e0d68cf..79ea6d5d 100644 --- a/src/main/java/com/notnoop/apns/internal/TlsTunnelBuilder.java +++ b/src/main/java/com/notnoop/apns/internal/TlsTunnelBuilder.java @@ -35,16 +35,17 @@ import java.net.ProtocolException; import java.net.Proxy; import java.net.Socket; + import javax.net.ssl.SSLSocketFactory; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import org.apache.commons.httpclient.ConnectMethod; -import org.apache.commons.httpclient.NTCredentials; -import org.apache.commons.httpclient.ProxyClient; -import org.apache.commons.httpclient.UsernamePasswordCredentials; -import org.apache.commons.httpclient.auth.AuthScope; + +import org.apache.http.HttpHost; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.impl.client.ProxyClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + /** * Establishes a TLS connection using an HTTP proxy. See RFC 2817 5.2. This class does @@ -88,28 +89,18 @@ Socket makeTunnel(String host, int port, String proxyUsername, Socket socket; try { ProxyClient client = new ProxyClient(); - client.getParams().setParameter("http.useragent", "java-apns"); - client.getHostConfiguration().setHost(host, port); + HttpHost target = new HttpHost(host, port); String proxyHost = proxyAddress.getAddress().toString().substring(0, proxyAddress.getAddress().toString().indexOf("/")); - client.getHostConfiguration().setProxy(proxyHost, proxyAddress.getPort()); + HttpHost proxy = new HttpHost(proxyHost, proxyAddress.getPort()); - - ProxyClient.ConnectResponse response = client.connect(); - socket = response.getSocket(); - if (socket == null) { - ConnectMethod method = response.getConnectMethod(); - // Read the proxy's HTTP response. - if(method.getStatusLine().getStatusCode() == 407) { - // Proxy server returned 407. We will now try to connect with auth Header - if(proxyUsername != null && proxyPassword != null) { - socket = AuthenticateProxy(method, client,proxyHost, proxyAddress.getPort(), - proxyUsername, proxyPassword); - } else { - throw new ProtocolException("Socket not created: " + method.getStatusLine()); - } - } + UsernamePasswordCredentials credentials = null; + if(proxyUsername != null && proxyPassword != null) { + credentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword); + }else{ + credentials = new UsernamePasswordCredentials("anonymous", "anonymous"); } + socket = client.tunnel(proxy, target, credentials); } catch (Exception e) { throw new ProtocolException("Error occurred while creating proxy socket : " + e.toString()); } @@ -117,31 +108,6 @@ Socket makeTunnel(String host, int port, String proxyUsername, logger.debug("Socket for proxy created successfully : " + socket.getRemoteSocketAddress().toString()); } return socket; - } - - private Socket AuthenticateProxy(ConnectMethod method, ProxyClient client, - String proxyHost, int proxyPort, - String proxyUsername, String proxyPassword) throws IOException { - if("ntlm".equalsIgnoreCase(method.getProxyAuthState().getAuthScheme().getSchemeName())) { - // If Auth scheme is NTLM, set NT credentials with blank host and domain name - client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), - new NTCredentials(proxyUsername, proxyPassword,"","")); - } else { - // If Auth scheme is Basic/Digest, set regular Credentials - client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), - new UsernamePasswordCredentials(proxyUsername, proxyPassword)); - } - - ProxyClient.ConnectResponse response = client.connect(); - Socket socket = response.getSocket(); - - if (socket == null) { - method = response.getConnectMethod(); - throw new ProtocolException("Proxy Authentication failed. Socket not created: " - + method.getStatusLine()); - } - return socket; - } - + } }