Exemplo n.º 1
0
  /**
   * Create the new Jetty client connector.
   *
   * @param jaxrsClient JAX-RS client instance, for which the connector is created.
   * @param config client configuration.
   */
  JettyConnector(final Client jaxrsClient, final Configuration config) {

    final SSLContext sslContext = getSslContext(jaxrsClient, config);
    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setSslContext(sslContext);
    this.client = new HttpClient(sslContextFactory);

    final Object connectTimeout = config.getProperties().get(ClientProperties.CONNECT_TIMEOUT);
    if (connectTimeout != null
        && connectTimeout instanceof Integer
        && (Integer) connectTimeout > 0) {
      client.setConnectTimeout((Integer) connectTimeout);
    }
    final Object threadPoolSize =
        config.getProperties().get(ClientProperties.ASYNC_THREADPOOL_SIZE);
    if (threadPoolSize != null
        && threadPoolSize instanceof Integer
        && (Integer) threadPoolSize > 0) {
      final String name = HttpClient.class.getSimpleName() + "@" + hashCode();
      final QueuedThreadPool threadPool = new QueuedThreadPool((Integer) threadPoolSize);
      threadPool.setName(name);
      client.setExecutor(threadPool);
    }
    Boolean disableCookies =
        (Boolean) config.getProperties().get(JettyClientProperties.DISABLE_COOKIES);
    disableCookies = (disableCookies != null) ? disableCookies : false;

    final AuthenticationStore auth = client.getAuthenticationStore();
    final Object basicAuthProvider =
        config.getProperty(JettyClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION);
    if (basicAuthProvider != null && (basicAuthProvider instanceof BasicAuthentication)) {
      auth.addAuthentication((BasicAuthentication) basicAuthProvider);
    }

    final Object proxyUri = config.getProperties().get(ClientProperties.PROXY_URI);
    if (proxyUri != null) {
      final URI u = getProxyUri(proxyUri);
      final ProxyConfiguration proxyConfig = client.getProxyConfiguration();
      proxyConfig.getProxies().add(new HttpProxy(u.getHost(), u.getPort()));
    }

    if (disableCookies) {
      client.setCookieStore(new HttpCookieStore.Empty());
    }

    try {
      client.start();
    } catch (final Exception e) {
      throw new ProcessingException("Failed to start the client.", e);
    }
    this.cookieStore = client.getCookieStore();
  }
  @Test
  public void test_BasicAuthentication_WithAuthenticationRemoved() throws Exception {
    startBasic(new EmptyServerHandler());

    final AtomicReference<CountDownLatch> requests = new AtomicReference<>(new CountDownLatch(2));
    Request.Listener.Empty requestListener =
        new Request.Listener.Empty() {
          @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));
  }
  private void test_Authentication(Authentication authentication) throws Exception {
    AuthenticationStore authenticationStore = client.getAuthenticationStore();

    final AtomicReference<CountDownLatch> requests = new AtomicReference<>(new CountDownLatch(1));
    Request.Listener.Empty requestListener =
        new Request.Listener.Empty() {
          @Override
          public void onSuccess(Request request) {
            requests.get().countDown();
          }
        };
    client.getRequestListeners().add(requestListener);

    // Request without Authentication causes a 401
    Request request =
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    ContentResponse response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(401, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
    client.getRequestListeners().remove(requestListener);

    authenticationStore.addAuthentication(authentication);

    requests.set(new CountDownLatch(2));
    requestListener =
        new Request.Listener.Empty() {
          @Override
          public void onSuccess(Request request) {
            requests.get().countDown();
          }
        };
    client.getRequestListeners().add(requestListener);

    // Request with authentication causes a 401 (no previous successful authentication) + 200
    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));
    client.getRequestListeners().remove(requestListener);

    requests.set(new CountDownLatch(1));
    requestListener =
        new Request.Listener.Empty() {
          @Override
          public void onSuccess(Request request) {
            requests.get().countDown();
          }
        };
    client.getRequestListeners().add(requestListener);

    // Further requests do not trigger 401 because there is a previous successful authentication
    // Remove existing header to be sure it's added by the implementation
    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));
    client.getRequestListeners().remove(requestListener);
  }