Search in sources :

Example 1 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class DigestPostTest method testServerWithHttpClientStreamContent.

@Test
public void testServerWithHttpClientStreamContent() throws Exception {
    String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
    HttpClient client = new HttpClient();
    try {
        AuthenticationStore authStore = client.getAuthenticationStore();
        authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
        client.start();
        String sent = IO.toString(new FileInputStream("src/test/resources/message.txt"));
        Request request = client.newRequest(srvUrl);
        request.method(HttpMethod.POST);
        request.content(new StringContentProvider(sent));
        _received = null;
        request = request.timeout(5, TimeUnit.SECONDS);
        ContentResponse response = request.send();
        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals(sent, _received);
    } finally {
        client.stop();
    }
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) FileInputStream(java.io.FileInputStream) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 2 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method test_BasicAuthentication_WithAuthenticationRemoved.

@Test
public void test_BasicAuthentication_WithAuthenticationRemoved() throws Exception {
    startBasic(new EmptyServerHandler());
    final AtomicReference<CountDownLatch> requests = new AtomicReference<>(new CountDownLatch(2));
    Request.Listener.Adapter requestListener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.get().countDown();
        }
    };
    client.getRequestListeners().add(requestListener);
    AuthenticationStore authenticationStore = client.getAuthenticationStore();
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
    BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "basic");
    authenticationStore.addAuthentication(authentication);
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    ContentResponse response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
    authenticationStore.removeAuthentication(authentication);
    requests.set(new CountDownLatch(1));
    request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
    Authentication.Result result = authenticationStore.findAuthenticationResult(request.getURI());
    Assert.assertNotNull(result);
    authenticationStore.removeAuthenticationResult(result);
    requests.set(new CountDownLatch(1));
    request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(401, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Authentication(org.eclipse.jetty.client.api.Authentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 3 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method test_RequestFailsAfterResponse.

@Test
public void test_RequestFailsAfterResponse() throws Exception {
    startBasic(new EmptyServerHandler());
    AuthenticationStore authenticationStore = client.getAuthenticationStore();
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
    BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "basic");
    authenticationStore.addAuthentication(authentication);
    CountDownLatch successLatch = new CountDownLatch(1);
    CountDownLatch resultLatch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure").content(content).onResponseSuccess(response -> successLatch.countDown());
    request.send(result -> {
        if (result.isFailed() && result.getResponseFailure() == null)
            resultLatch.countDown();
    });
    // Send some content to make sure the request is dispatched on the server.
    content.offer(ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)));
    // Wait for the response to arrive to
    // the authentication protocol handler.
    Thread.sleep(1000);
    // Trigger request failure.
    request.abort(new Exception());
    // Verify that the response was successful, it's the request that failed.
    Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
Also used : DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 4 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class DataSourceLoginServiceTest method startClient.

protected void startClient(String user, String pwd) throws Exception {
    _client = new HttpClient();
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName(executor.getName() + "-client");
    _client.setExecutor(executor);
    AuthenticationStore authStore = _client.getAuthenticationStore();
    authStore.addAuthentication(new BasicAuthentication(_baseUri, __realm, user, pwd));
    _client.start();
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore)

Example 5 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class JdbcLoginServiceTest method startClient.

protected void startClient(String user, String pwd) throws Exception {
    _client = new HttpClient();
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName(executor.getName() + "-client");
    _client.setExecutor(executor);
    AuthenticationStore authStore = _client.getAuthenticationStore();
    authStore.addAuthentication(new BasicAuthentication(_baseUri, __realm, user, pwd));
    _client.start();
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore)

Aggregations

AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)11 URI (java.net.URI)8 Request (org.eclipse.jetty.client.api.Request)8 BasicAuthentication (org.eclipse.jetty.client.util.BasicAuthentication)8 Test (org.junit.Test)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)6 DigestAuthentication (org.eclipse.jetty.client.util.DigestAuthentication)5 HttpClient (org.eclipse.jetty.client.HttpClient)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Authentication (org.eclipse.jetty.client.api.Authentication)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)2 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServletException (javax.servlet.ServletException)1 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)1 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)1