Search in sources :

Example 6 with Authentication

use of org.eclipse.jetty.client.api.Authentication in project camel by apache.

the class CamelSalesforceMojo method createHttpClient.

protected SalesforceHttpClient createHttpClient() throws MojoExecutionException {
    final SalesforceHttpClient httpClient;
    // set ssl context parameters
    try {
        final SSLContextParameters contextParameters = sslContextParameters != null ? sslContextParameters : new SSLContextParameters();
        final SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setSslContext(contextParameters.createSSLContext());
        httpClient = new SalesforceHttpClient(sslContextFactory);
    } catch (GeneralSecurityException e) {
        throw new MojoExecutionException("Error creating default SSL context: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new MojoExecutionException("Error creating default SSL context: " + e.getMessage(), e);
    }
    // default settings
    httpClient.setConnectTimeout(DEFAULT_TIMEOUT);
    httpClient.setTimeout(DEFAULT_TIMEOUT);
    // enable redirects, no need for a RedirectListener class in Jetty 9
    httpClient.setFollowRedirects(true);
    // set HTTP client parameters
    if (httpClientProperties != null && !httpClientProperties.isEmpty()) {
        try {
            IntrospectionSupport.setProperties(httpClient, new HashMap<String, Object>(httpClientProperties));
        } catch (Exception e) {
            throw new MojoExecutionException("Error setting HTTP client properties: " + e.getMessage(), e);
        }
    }
    // wait for 1 second longer than the HTTP client response timeout
    responseTimeout = httpClient.getTimeout() + 1000L;
    // set HTTP proxy settings
    if (this.httpProxyHost != null && httpProxyPort != null) {
        Origin.Address proxyAddress = new Origin.Address(this.httpProxyHost, this.httpProxyPort);
        ProxyConfiguration.Proxy proxy;
        if (isHttpProxySocks4) {
            proxy = new Socks4Proxy(proxyAddress, isHttpProxySecure);
        } else {
            proxy = new HttpProxy(proxyAddress, isHttpProxySecure);
        }
        if (httpProxyIncludedAddresses != null && !httpProxyIncludedAddresses.isEmpty()) {
            proxy.getIncludedAddresses().addAll(httpProxyIncludedAddresses);
        }
        if (httpProxyExcludedAddresses != null && !httpProxyExcludedAddresses.isEmpty()) {
            proxy.getExcludedAddresses().addAll(httpProxyExcludedAddresses);
        }
        httpClient.getProxyConfiguration().getProxies().add(proxy);
    }
    if (this.httpProxyUsername != null && httpProxyPassword != null) {
        ObjectHelper.notEmpty(httpProxyAuthUri, "httpProxyAuthUri");
        ObjectHelper.notEmpty(httpProxyRealm, "httpProxyRealm");
        final Authentication authentication;
        if (httpProxyUseDigestAuth) {
            authentication = new DigestAuthentication(URI.create(httpProxyAuthUri), httpProxyRealm, httpProxyUsername, httpProxyPassword);
        } else {
            authentication = new BasicAuthentication(URI.create(httpProxyAuthUri), httpProxyRealm, httpProxyUsername, httpProxyPassword);
        }
        httpClient.getAuthenticationStore().addAuthentication(authentication);
    }
    // set session before calling start()
    final SalesforceSession session = new SalesforceSession(new DefaultCamelContext(), httpClient, httpClient.getTimeout(), new SalesforceLoginConfig(loginUrl, clientId, clientSecret, userName, password, false));
    httpClient.setSession(session);
    try {
        httpClient.start();
    } catch (Exception e) {
        throw new MojoExecutionException("Error creating HTTP client: " + e.getMessage(), e);
    }
    return httpClient;
}
Also used : Origin(org.eclipse.jetty.client.Origin) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) SalesforceHttpClient(org.apache.camel.component.salesforce.SalesforceHttpClient) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) SalesforceLoginConfig(org.apache.camel.component.salesforce.SalesforceLoginConfig) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters) HttpProxy(org.eclipse.jetty.client.HttpProxy) Socks4Proxy(org.eclipse.jetty.client.Socks4Proxy) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ProxyConfiguration(org.eclipse.jetty.client.ProxyConfiguration) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Authentication(org.eclipse.jetty.client.api.Authentication) SalesforceSession(org.apache.camel.component.salesforce.internal.SalesforceSession) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) SObject(org.apache.camel.component.salesforce.api.dto.SObject)

Aggregations

Authentication (org.eclipse.jetty.client.api.Authentication)6 URI (java.net.URI)5 BasicAuthentication (org.eclipse.jetty.client.util.BasicAuthentication)5 DigestAuthentication (org.eclipse.jetty.client.util.DigestAuthentication)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)3 Request (org.eclipse.jetty.client.api.Request)3 Test (org.junit.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 SalesforceSession (org.apache.camel.component.salesforce.internal.SalesforceSession)2 SSLContextParameters (org.apache.camel.util.jsse.SSLContextParameters)2 HttpProxy (org.eclipse.jetty.client.HttpProxy)2 Origin (org.eclipse.jetty.client.Origin)2 ProxyConfiguration (org.eclipse.jetty.client.ProxyConfiguration)2 Socks4Proxy (org.eclipse.jetty.client.Socks4Proxy)2 AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)2 Attributes (org.eclipse.jetty.util.Attributes)2 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)2 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1