Search in sources :

Example 46 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project jersey by jersey.

the class AuthTest method testAuthGet.

@Test
public void testAuthGet() {
    CredentialsProvider credentialsProvider = new org.apache.http.impl.client.BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("name", "password"));
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri()).path("test");
    assertEquals("GET", r.request().get(String.class));
}
Also used : CredentialsProvider(org.apache.http.client.CredentialsProvider) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 47 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project jersey by jersey.

the class AuthTest method testAuthPost.

@Test
@Ignore("JERSEY-1750: Cannot retry request with a non-repeatable request entity. How to buffer the entity?" + " Allow repeatable write in jersey?")
public void testAuthPost() {
    CredentialsProvider credentialsProvider = new org.apache.http.impl.client.BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("name", "password"));
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri()).path("test");
    assertEquals("POST", r.request().post(Entity.text("POST"), String.class));
}
Also used : CredentialsProvider(org.apache.http.client.CredentialsProvider) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Ignore(org.junit.Ignore) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 48 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project SmartAndroidSource by jaychou2012.

the class PreemtiveAuthorizationHttpRequestInterceptor method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 49 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesCredentialsProvider.

@Test
public void usesCredentialsProvider() throws Exception {
    final CredentialsProvider credentialsProvider = new CredentialsProvider() {

        @Override
        public void setCredentials(AuthScope authscope, Credentials credentials) {
        }

        @Override
        public Credentials getCredentials(AuthScope authscope) {
            return null;
        }

        @Override
        public void clear() {
        }
    };
    assertThat(builder.using(configuration).using(credentialsProvider).createClient(apacheBuilder, connectionManager, "test")).isNotNull();
    assertThat(spyHttpClientBuilderField("credentialsProvider", apacheBuilder)).isSameAs(credentialsProvider);
}
Also used : AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 50 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project elasticsearch by elastic.

the class TransportReindexAction method buildRestClient.

/**
     * Build the {@link RestClient} used for reindexing from remote clusters.
     * @param remoteInfo connection information for the remote cluster
     * @param taskId the id of the current task. This is added to the thread name for easier tracking
     * @param threadCollector a list in which we collect all the threads created by the client
     */
static RestClient buildRestClient(RemoteInfo remoteInfo, long taskId, List<Thread> threadCollector) {
    Header[] clientHeaders = new Header[remoteInfo.getHeaders().size()];
    int i = 0;
    for (Map.Entry<String, String> header : remoteInfo.getHeaders().entrySet()) {
        clientHeaders[i] = new BasicHeader(header.getKey(), header.getValue());
    }
    return RestClient.builder(new HttpHost(remoteInfo.getHost(), remoteInfo.getPort(), remoteInfo.getScheme())).setDefaultHeaders(clientHeaders).setRequestConfigCallback(c -> {
        c.setConnectTimeout(Math.toIntExact(remoteInfo.getConnectTimeout().millis()));
        c.setSocketTimeout(Math.toIntExact(remoteInfo.getSocketTimeout().millis()));
        return c;
    }).setHttpClientConfigCallback(c -> {
        if (remoteInfo.getUsername() != null) {
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials(remoteInfo.getUsername(), remoteInfo.getPassword());
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, creds);
            c.setDefaultCredentialsProvider(credentialsProvider);
        }
        AtomicInteger threads = new AtomicInteger();
        c.setThreadFactory(r -> {
            String name = "es-client-" + taskId + "-" + threads.getAndIncrement();
            Thread t = new Thread(r, name);
            threadCollector.add(t);
            return t;
        });
        c.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build());
        return c;
    }).build();
}
Also used : Versions(org.elasticsearch.common.lucene.uid.Versions) RemoteScrollableHitSource(org.elasticsearch.index.reindex.remote.RemoteScrollableHitSource) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) Property(org.elasticsearch.common.settings.Setting.Property) BiFunction(java.util.function.BiFunction) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Header(org.apache.http.Header) VersionType(org.elasticsearch.index.VersionType) AutoCreateIndex(org.elasticsearch.action.support.AutoCreateIndex) RemoteInfo(org.elasticsearch.index.reindex.remote.RemoteInfo) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) IndexRequest(org.elasticsearch.action.index.IndexRequest) ClusterState(org.elasticsearch.cluster.ClusterState) Operations(org.apache.lucene.util.automaton.Operations) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ParentBulkByScrollTask(org.elasticsearch.action.bulk.byscroll.ParentBulkByScrollTask) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Failure(org.elasticsearch.action.bulk.BulkItemResponse.Failure) Automata(org.apache.lucene.util.automaton.Automata) AbstractAsyncBulkByScrollAction(org.elasticsearch.action.bulk.byscroll.AbstractAsyncBulkByScrollAction) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) WorkingBulkByScrollTask(org.elasticsearch.action.bulk.byscroll.WorkingBulkByScrollTask) BulkByScrollResponse(org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse) ActionFilters(org.elasticsearch.action.support.ActionFilters) Setting(org.elasticsearch.common.settings.Setting) Collections.synchronizedList(java.util.Collections.synchronizedList) Automaton(org.apache.lucene.util.automaton.Automaton) Collections.emptyList(java.util.Collections.emptyList) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) CredentialsProvider(org.apache.http.client.CredentialsProvider) Task(org.elasticsearch.tasks.Task) RestClient(org.elasticsearch.client.RestClient) XContentType(org.elasticsearch.common.xcontent.XContentType) ClusterService(org.elasticsearch.cluster.service.ClusterService) SearchRequest(org.elasticsearch.action.search.SearchRequest) INTERNAL(org.elasticsearch.index.VersionType.INTERNAL) Function(java.util.function.Function) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) ScrollableHitSource(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource) HandledTransportAction(org.elasticsearch.action.support.HandledTransportAction) ParentTaskAssigningClient(org.elasticsearch.client.ParentTaskAssigningClient) Objects.requireNonNull(java.util.Objects.requireNonNull) Regex(org.elasticsearch.common.regex.Regex) TransportService(org.elasticsearch.transport.TransportService) CharacterRunAutomaton(org.apache.lucene.util.automaton.CharacterRunAutomaton) Script(org.elasticsearch.script.Script) BulkByScrollParallelizationHelper(org.elasticsearch.action.bulk.byscroll.BulkByScrollParallelizationHelper) Client(org.elasticsearch.client.Client) IOException(java.io.IOException) MinimizationOperations(org.apache.lucene.util.automaton.MinimizationOperations) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) XContentParser(org.elasticsearch.common.xcontent.XContentParser) VersionFieldMapper(org.elasticsearch.index.mapper.VersionFieldMapper) AuthScope(org.apache.http.auth.AuthScope) SearchFailure(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource.SearchFailure) BasicHeader(org.apache.http.message.BasicHeader) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) HttpHost(org.apache.http.HttpHost) ScriptService(org.elasticsearch.script.ScriptService) ActionListener(org.elasticsearch.action.ActionListener) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpHost(org.apache.http.HttpHost) Map(java.util.Map) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)92 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)65 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)57 AuthScope (org.apache.http.auth.AuthScope)51 HttpHost (org.apache.http.HttpHost)28 HttpGet (org.apache.http.client.methods.HttpGet)19 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)17 Test (org.junit.Test)17 HttpResponse (org.apache.http.HttpResponse)16 Credentials (org.apache.http.auth.Credentials)16 IOException (java.io.IOException)13 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)12 BasicScheme (org.apache.http.impl.auth.BasicScheme)12 Header (org.apache.http.Header)11 HttpRequest (org.apache.http.HttpRequest)10 AuthCache (org.apache.http.client.AuthCache)10 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)10 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 HttpEntity (org.apache.http.HttpEntity)8