예제 #1
0
  @Test
  public void testCookieResource() {
    ClientConfig cc = new ClientConfig();
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri());

    assertEquals("NO-COOKIE", r.request().get(String.class));
    assertEquals("value", r.request().get(String.class));
  }
예제 #2
0
  @Test
  public void testCookies() {
    ClientConfig cc = new ClientConfig();
    cc.connectorProvider(new ApacheConnectorProvider());
    JerseyClient client = JerseyClientBuilder.createClient(cc);
    WebTarget r = client.target(getBaseUri());

    assertEquals("NO-COOKIE", r.request().get(String.class));
    assertEquals("value", r.request().get(String.class));

    final ApacheConnector connector = (ApacheConnector) client.getConfiguration().getConnector();
    assertNotNull(connector.getCookieStore().getCookies());
    assertEquals(1, connector.getCookieStore().getCookies().size());
    assertEquals("value", connector.getCookieStore().getCookies().get(0).getValue());
  }
예제 #3
0
  @Bean
  public Client webClient() {
    final PoolingHttpClientConnectionManager poolingConnectionManager =
        new PoolingHttpClientConnectionManager();
    poolingConnectionManager.setMaxTotal(webClientConnectionPoolMaxTotal);
    poolingConnectionManager.setDefaultMaxPerRoute(webClientConnectionPoolDefaultMaxPerRoute);

    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(READ_TIMEOUT, 2000);
    clientConfig.property(CONNECT_TIMEOUT, 500);
    clientConfig.property(CONNECTION_MANAGER, poolingConnectionManager);
    clientConfig.connectorProvider(new ApacheConnectorProvider());

    return newClient(clientConfig).register(jacksonJsonProvider());
  }
예제 #4
0
 private static Client makeClient(long requestTimeout, int maxConnections, SSLContext sslContext) {
   ClientConfig clientConfig = new ClientConfig();
   int castRequestTimeout = Ints.checkedCast(requestTimeout);
   clientConfig.register(makeGZipFeature());
   clientConfig.property(ClientProperties.ASYNC_THREADPOOL_SIZE, maxConnections);
   clientConfig.property(ClientProperties.CONNECT_TIMEOUT, castRequestTimeout);
   clientConfig.property(ClientProperties.READ_TIMEOUT, castRequestTimeout);
   clientConfig.property(
       ApacheClientProperties.CONNECTION_MANAGER, makeConnectionManager(maxConnections));
   clientConfig.connectorProvider(new ApacheConnectorProvider());
   if (sslContext != null) {
     return ClientBuilder.newBuilder().sslContext(sslContext).withConfig(clientConfig).build();
   } else {
     return ClientBuilder.newClient(clientConfig);
   }
 }
예제 #5
0
  @Test
  public void testDisabledCookies() {
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.DISABLE_COOKIES, true);
    cc.connectorProvider(new ApacheConnectorProvider());
    JerseyClient client = JerseyClientBuilder.createClient(cc);
    WebTarget r = client.target(getBaseUri());

    assertEquals("NO-COOKIE", r.request().get(String.class));
    assertEquals("NO-COOKIE", r.request().get(String.class));

    final ApacheConnector connector = (ApacheConnector) client.getConfiguration().getConnector();
    if (connector.getCookieStore() != null) {
      assertTrue(connector.getCookieStore().getCookies().isEmpty());
    } else {
      assertNull(connector.getCookieStore());
    }
  }
예제 #6
0
  private Configuration buildConfig(
      final String name,
      final ExecutorService threadPool,
      final ObjectMapper objectMapper,
      final Validator validator) {
    final ClientConfig config = new ClientConfig();

    for (Object singleton : this.singletons) {
      config.register(singleton);
    }

    for (Class<?> provider : this.providers) {
      config.register(provider);
    }

    config.register(new JacksonMessageBodyProvider(objectMapper, validator));

    for (Map.Entry<String, Object> property : this.properties.entrySet()) {
      config.property(property.getKey(), property.getValue());
    }

    config.register(new DropwizardExecutorProvider(threadPool));
    if (connectorProvider == null) {
      final ConfiguredCloseableHttpClient apacheHttpClient =
          apacheHttpClientBuilder.buildWithDefaultRequestConfiguration(name);
      connectorProvider =
          new ConnectorProvider() {
            @Override
            public Connector getConnector(Client client, Configuration runtimeConfig) {
              return new DropwizardApacheConnector(
                  apacheHttpClient.getClient(),
                  apacheHttpClient.getDefaultRequestConfig(),
                  configuration.isChunkedEncodingEnabled());
            }
          };
    }
    config.connectorProvider(connectorProvider);

    return config;
  }
  public EurekaJersey2ClientImpl(
      int connectionTimeout,
      int readTimeout,
      final int connectionIdleTimeout,
      ClientConfig clientConfig) {

    try {
      jerseyClientConfig = clientConfig;
      jerseyClientConfig.register(DiscoveryJerseyProvider.class);
      jerseyClientConfig.connectorProvider(new ApacheConnectorProvider());
      jerseyClientConfig.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout);
      jerseyClientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout);
      apacheHttpClient = ClientBuilder.newClient(jerseyClientConfig);
      connectionCleanerTask = new ConnectionCleanerTask(connectionIdleTimeout);
      eurekaConnCleaner.scheduleWithFixedDelay(
          connectionCleanerTask,
          HTTP_CONNECTION_CLEANER_INTERVAL_MS,
          HTTP_CONNECTION_CLEANER_INTERVAL_MS,
          TimeUnit.MILLISECONDS);
    } catch (Throwable e) {
      throw new RuntimeException("Cannot create Jersey2 client", e);
    }
  }
 protected void configureClient(ClientConfig clientConfig) {
   JacksonJsonProvider json = new JacksonJsonProvider();
   json.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
   clientConfig.register(json);
   clientConfig.connectorProvider(new GrizzlyConnectorProvider());
 }
예제 #9
0
 @Override
 protected void configureClient(ClientConfig config) {
   config.connectorProvider(new JettyConnectorProvider());
 }
  @Override
  public void init(DockerClientConfig dockerClientConfig) {
    checkNotNull(dockerClientConfig, "config was not specified");

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.connectorProvider(new ApacheConnectorProvider());
    clientConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);

    clientConfig.register(ResponseStatusExceptionFilter.class);
    clientConfig.register(JsonClientFilter.class);
    clientConfig.register(JacksonJsonProvider.class);

    // logging may disabled via log level
    clientConfig.register(new SelectiveLoggingFilter(LOGGER, true));

    if (readTimeout != null) {
      clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout);
    }

    if (connectTimeout != null) {
      clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout);
    }

    if (clientResponseFilters != null) {
      for (ClientResponseFilter clientResponseFilter : clientResponseFilters) {
        if (clientResponseFilter != null) clientConfig.register(clientResponseFilter);
      }
    }

    if (clientRequestFilters != null) {
      for (ClientRequestFilter clientRequestFilter : clientRequestFilters) {
        if (clientRequestFilter != null) clientConfig.register(clientRequestFilter);
      }
    }

    URI originalUri = dockerClientConfig.getUri();

    SSLContext sslContext = null;

    if (dockerClientConfig.getSslConfig() != null) {
      try {
        sslContext = dockerClientConfig.getSslConfig().getSSLContext();
      } catch (Exception ex) {
        throw new DockerClientException("Error in SSL Configuration", ex);
      }
    }

    PoolingHttpClientConnectionManager connManager =
        new PoolingHttpClientConnectionManager(getSchemeRegistry(originalUri, sslContext));

    if (maxTotalConnections != null) connManager.setMaxTotal(maxTotalConnections);
    if (maxPerRouteConnections != null) connManager.setDefaultMaxPerRoute(maxPerRouteConnections);

    clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connManager);

    // Configure connection pool timeout
    // clientConfig.property(ApacheClientProperties.REQUEST_CONFIG, RequestConfig.custom()
    // .setConnectionRequestTimeout(1000).build());

    ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);

    if (sslContext != null) {
      clientBuilder.sslContext(sslContext);
    }

    client = clientBuilder.build();

    if (originalUri.getScheme().equals("unix")) {
      dockerClientConfig.setUri(UnixConnectionSocketFactory.sanitizeUri(originalUri));
    }
    WebTarget webResource = client.target(dockerClientConfig.getUri());

    if (dockerClientConfig.getVersion() == null || dockerClientConfig.getVersion().isEmpty()) {
      baseResource = webResource;
    } else {
      baseResource = webResource.path("v" + dockerClientConfig.getVersion());
    }
  }