Search in sources :

Example 21 with BasicAuthentication

use of org.eclipse.jetty.client.util.BasicAuthentication in project jersey by jersey.

the class AuthTest method testAuthGet.

@Test
public void testAuthGet() {
    ClientConfig config = new ClientConfig();
    config.property(JettyClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, new BasicAuthentication(getBaseUri(), "WallyWorld", "name", "password"));
    config.connectorProvider(new JettyConnectorProvider());
    Client client = ClientBuilder.newClient(config);
    Response response = client.target(getBaseUri()).path(PATH).request().get();
    assertEquals("GET", response.readEntity(String.class));
    client.close();
}
Also used : Response(javax.ws.rs.core.Response) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 22 with BasicAuthentication

use of org.eclipse.jetty.client.util.BasicAuthentication in project camel by apache.

the class SalesforceComponent method doStart.

@Override
protected void doStart() throws Exception {
    if (loginConfig == null) {
        if (ObjectHelper.isNotEmpty(password)) {
            loginConfig = new SalesforceLoginConfig(loginUrl, clientId, clientSecret, userName, password, lazyLogin);
        } else if (ObjectHelper.isNotEmpty(refreshToken)) {
            loginConfig = new SalesforceLoginConfig(loginUrl, clientId, clientSecret, refreshToken, lazyLogin);
        } else if (ObjectHelper.isNotEmpty(keystore)) {
            loginConfig = new SalesforceLoginConfig(loginUrl, clientId, userName, keystore, lazyLogin);
        } else {
            throw new IllegalArgumentException("Cannot define a login configuration, the component configuration" + " does not contain `password`, `refreshToken` or `keystore` parameters. Specifying one of those" + " determines the type of authentication performed.");
        }
        LOG.debug("Created login configuration: {}", loginConfig);
    } else {
        LOG.debug("Using shared login configuration: {}", loginConfig);
    }
    // create a Jetty HttpClient if not already set
    if (null == httpClient) {
        if (config != null && config.getHttpClient() != null) {
            httpClient = config.getHttpClient();
        } else {
            // set ssl context parameters if set
            final SSLContextParameters contextParameters = sslContextParameters != null ? sslContextParameters : new SSLContextParameters();
            final SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setSslContext(contextParameters.createSSLContext(getCamelContext()));
            httpClient = new SalesforceHttpClient(sslContextFactory);
            // default settings, use httpClientProperties to set other properties
            httpClient.setConnectTimeout(CONNECTION_TIMEOUT);
        }
    }
    // set HTTP client parameters
    if (httpClientProperties != null && !httpClientProperties.isEmpty()) {
        IntrospectionSupport.setProperties(getCamelContext().getTypeConverter(), httpClient, new HashMap<String, Object>(httpClientProperties));
    }
    // 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(new URI(httpProxyAuthUri), httpProxyRealm, httpProxyUsername, httpProxyPassword);
        } else {
            authentication = new BasicAuthentication(new URI(httpProxyAuthUri), httpProxyRealm, httpProxyUsername, httpProxyPassword);
        }
        httpClient.getAuthenticationStore().addAuthentication(authentication);
    }
    // support restarts
    if (this.session == null) {
        this.session = new SalesforceSession(getCamelContext(), httpClient, httpClient.getTimeout(), loginConfig);
    }
    // set session before calling start()
    httpClient.setSession(this.session);
    // start the Jetty client to initialize thread pool, etc.
    httpClient.start();
    // login at startup if lazyLogin is disabled
    if (!loginConfig.isLazyLogin()) {
        ServiceHelper.startService(session);
    }
    if (packages != null && packages.length > 0) {
        // parse the packages to create SObject name to class map
        classMap = parsePackages();
        LOG.info("Found {} generated classes in packages: {}", classMap.size(), Arrays.asList(packages));
    } else {
        // use an empty map to avoid NPEs later
        LOG.warn("Missing property packages, getSObject* operations will NOT work");
        classMap = new HashMap<String, Class<?>>(0);
    }
    if (subscriptionHelper != null) {
        ServiceHelper.startService(subscriptionHelper);
    }
}
Also used : Origin(org.eclipse.jetty.client.Origin) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) 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) Authentication(org.eclipse.jetty.client.api.Authentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) SalesforceSession(org.apache.camel.component.salesforce.internal.SalesforceSession) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication)

Example 23 with BasicAuthentication

use of org.eclipse.jetty.client.util.BasicAuthentication 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

BasicAuthentication (org.eclipse.jetty.client.util.BasicAuthentication)23 URI (java.net.URI)17 Test (org.junit.Test)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)9 Request (org.eclipse.jetty.client.api.Request)9 IOException (java.io.IOException)8 ServletException (javax.servlet.ServletException)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)6 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 DigestAuthentication (org.eclipse.jetty.client.util.DigestAuthentication)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 HttpClient (org.eclipse.jetty.client.HttpClient)4 HttpProxy (org.eclipse.jetty.client.HttpProxy)4 Authentication (org.eclipse.jetty.client.api.Authentication)4 Client (javax.ws.rs.client.Client)3 Response (javax.ws.rs.core.Response)3 Origin (org.eclipse.jetty.client.Origin)3