Search in sources :

Example 46 with Proxy

use of java.net.Proxy in project tdi-studio-se by Talend.

the class DynamicsCRMClient method getAccessToken.

protected AuthenticationResult getAccessToken() throws ServiceUnavailableException {
    AuthenticationContext context = null;
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        context = new AuthenticationContext(clientConfiguration.getAuthoryEndpoint(), false, service);
        Proxy proxy = getProxy();
        if (proxy != null) {
            context.setProxy(proxy);
        }
        Future<AuthenticationResult> future = context.acquireToken(clientConfiguration.getResource(), clientConfiguration.getClientId(), clientConfiguration.getUserName(), clientConfiguration.getPassword(), null);
        result = future.get();
    } catch (Exception e) {
        throw new ServiceUnavailableException(e.getMessage());
    } finally {
        service.shutdown();
    }
    if (result == null) {
        throw new ServiceUnavailableException("Authenticated failed! Please check your configuration!");
    }
    return result;
}
Also used : Proxy(java.net.Proxy) AuthenticationContext(com.microsoft.aad.adal4j.AuthenticationContext) ExecutorService(java.util.concurrent.ExecutorService) ServiceUnavailableException(javax.naming.ServiceUnavailableException) URISyntaxException(java.net.URISyntaxException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) ODataClientErrorException(org.apache.olingo.client.api.communication.ODataClientErrorException) HttpClientException(org.apache.olingo.client.api.http.HttpClientException) AuthenticationResult(com.microsoft.aad.adal4j.AuthenticationResult)

Example 47 with Proxy

use of java.net.Proxy in project tdi-studio-se by Talend.

the class DynamicsCRMClient method getProxy.

/**
     * Get the proxy setting if there is proxy for system
     */
private Proxy getProxy() {
    String proxyHost = System.getProperty("https.proxyHost");
    String proxyPort = System.getProperty("https.proxyPort");
    if (proxyHost != null) {
        int port = -1;
        if (proxyPort != null && proxyPort.length() > 0) {
            port = Integer.parseInt(proxyPort);
        }
        SocketAddress addr = new InetSocketAddress(proxyHost, port);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
        return proxy;
    }
    return null;
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 48 with Proxy

use of java.net.Proxy in project tdi-studio-se by Talend.

the class DynamicsCRMClient method setHttpclientProxy.

/**
     * Setup proxy for httpClient
     */
private void setHttpclientProxy(DefaultHttpClient httpClient) {
    Proxy proxy = getProxy();
    String proxyUser = System.getProperty("https.proxyUser");
    String proxyPwd = System.getProperty("https.proxyPassword");
    // set by other components like tSetProxy
    if (proxy != null) {
        final HttpHost httpHost = new HttpHost(((InetSocketAddress) proxy.address()).getHostName(), ((InetSocketAddress) proxy.address()).getPort());
        // Sets usage of HTTP proxy
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
        // TODO Because the proxy of get accesToken can't support authentication. remove this ?
        if (proxyUser != null && proxyPwd != null) {
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(proxyUser, proxyPwd));
        }
    }
}
Also used : Proxy(java.net.Proxy) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 49 with Proxy

use of java.net.Proxy in project zm-mailbox by Zimbra.

the class HttpProxyConfig method getProxyConfig.

public static ProxyHostConfiguration getProxyConfig(HostConfiguration hc, String uriStr) {
    if (!LC.client_use_system_proxy.booleanValue())
        return null;
    URI uri = null;
    try {
        uri = new URI(uriStr);
    } catch (URISyntaxException x) {
        ZimbraLog.net.info(uriStr, x);
        return null;
    }
    //no need to filter out localhost as the DefaultProxySelector will do that.
    List<Proxy> proxies = ProxySelectors.defaultProxySelector().select(uri);
    for (Proxy proxy : proxies) {
        switch(proxy.type()) {
            case DIRECT:
                return null;
            case HTTP:
                InetSocketAddress addr = (InetSocketAddress) proxy.address();
                if (ZimbraLog.net.isDebugEnabled()) {
                    ZimbraLog.net.debug("URI %s to use HTTP proxy %s", safePrint(uri), addr.toString());
                }
                ProxyHostConfiguration nhc = new ProxyHostConfiguration(hc);
                nhc.setProxy(addr.getHostName(), addr.getPort());
                if (proxy instanceof AuthProxy) {
                    nhc.setUsername(((AuthProxy) proxy).getUsername());
                    nhc.setPassword(((AuthProxy) proxy).getPassword());
                }
                return nhc;
            //socks proxy can be handled at socket factory level
            case SOCKS:
            default:
                continue;
        }
    }
    return null;
}
Also used : AuthProxy(com.zimbra.common.net.AuthProxy) Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) AuthProxy(com.zimbra.common.net.AuthProxy) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ProxyHostConfiguration(com.zimbra.common.net.ProxyHostConfiguration)

Example 50 with Proxy

use of java.net.Proxy in project zm-mailbox by Zimbra.

the class ProxySelectorSocket method connect.

@Override
public void connect(SocketAddress endpoint, int timeout) throws IOException {
    Proxy proxy = proxy((InetSocketAddress) endpoint);
    switch(proxy.type()) {
        case DIRECT:
            LOG.debug("Connecting directly to %s", endpoint);
        case SOCKS:
            LOG.debug("Connecting to %s via SOCKS proxy %s", endpoint, proxy.address());
    }
    setDelegate(new Socket(proxy));
    super.connect(endpoint, timeout);
}
Also used : Proxy(java.net.Proxy) Socket(java.net.Socket)

Aggregations

Proxy (java.net.Proxy)132 InetSocketAddress (java.net.InetSocketAddress)71 URL (java.net.URL)44 IOException (java.io.IOException)35 Test (org.junit.Test)22 URI (java.net.URI)21 ProxySelector (java.net.ProxySelector)20 HttpURLConnection (java.net.HttpURLConnection)19 SocketAddress (java.net.SocketAddress)19 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)13 URISyntaxException (java.net.URISyntaxException)11 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 InterruptedIOException (java.io.InterruptedIOException)6 URLConnection (java.net.URLConnection)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 List (java.util.List)5 SSLSocket (javax.net.ssl.SSLSocket)5 OkHttpClient (okhttp3.OkHttpClient)5