Search in sources :

Example 1 with ProxyClient

use of org.apache.commons.httpclient.ProxyClient in project java-apns by notnoop.

the class TlsTunnelBuilder method makeTunnel.

@SuppressFBWarnings(value = "VA_FORMAT_STRING_USES_NEWLINE", justification = "use <CR><LF> as according to RFC, not platform-linefeed")
Socket makeTunnel(String host, int port, String proxyUsername, String proxyPassword, InetSocketAddress proxyAddress) throws IOException {
    if (host == null || port < 0 || host.isEmpty() || proxyAddress == null) {
        throw new ProtocolException("Incorrect parameters to build tunnel.");
    }
    logger.debug("Creating socket for Proxy : " + proxyAddress.getAddress() + ":" + proxyAddress.getPort());
    Socket socket;
    try {
        ProxyClient client = new ProxyClient();
        client.getParams().setParameter("http.useragent", "java-apns");
        client.getHostConfiguration().setHost(host, port);
        String proxyHost = proxyAddress.getAddress().toString().substring(0, proxyAddress.getAddress().toString().indexOf("/"));
        client.getHostConfiguration().setProxy(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());
                }
            }
        }
    } catch (Exception e) {
        throw new ProtocolException("Error occurred while creating proxy socket : " + e.toString());
    }
    if (socket != null) {
        logger.debug("Socket for proxy created successfully : " + socket.getRemoteSocketAddress().toString());
    }
    return socket;
}
Also used : ProtocolException(java.net.ProtocolException) ProxyClient(org.apache.commons.httpclient.ProxyClient) ConnectMethod(org.apache.commons.httpclient.ConnectMethod) Socket(java.net.Socket) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with ProxyClient

use of org.apache.commons.httpclient.ProxyClient in project java-apns by notnoop.

the class TlsTunnelBuilder method AuthenticateProxy.

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;
}
Also used : ProtocolException(java.net.ProtocolException) ProxyClient(org.apache.commons.httpclient.ProxyClient) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Socket(java.net.Socket) NTCredentials(org.apache.commons.httpclient.NTCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

ProtocolException (java.net.ProtocolException)2 Socket (java.net.Socket)2 ProxyClient (org.apache.commons.httpclient.ProxyClient)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 ConnectMethod (org.apache.commons.httpclient.ConnectMethod)1 NTCredentials (org.apache.commons.httpclient.NTCredentials)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 AuthScope (org.apache.commons.httpclient.auth.AuthScope)1