コード例 #1
0
  @Test(groups = {"online", "default_provider"})
  public void testRequestProxy()
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    AsyncHttpClientConfig.Builder b = new AsyncHttpClientConfig.Builder();
    b.setFollowRedirects(true);

    ProxyServer ps = new ProxyServer(ProxyServer.Protocol.HTTPS, "127.0.0.1", port1);

    AsyncHttpClientConfig config = b.build();
    AsyncHttpClient asyncHttpClient = getAsyncHttpClient(config);

    RequestBuilder rb = new RequestBuilder("GET").setProxyServer(ps).setUrl(getTargetUrl2());
    Future<Response> responseFuture =
        asyncHttpClient.executeRequest(
            rb.build(),
            new AsyncCompletionHandlerBase() {

              public void onThrowable(Throwable t) {
                t.printStackTrace();
                log.debug(t.getMessage(), t);
              }

              @Override
              public Response onCompleted(Response response) throws Exception {
                return response;
              }
            });
    Response r = responseFuture.get();
    assertEquals(r.getStatusCode(), 200);
    assertEquals(r.getHeader("X-Proxy-Connection"), "keep-alive");

    asyncHttpClient.close();
  }
コード例 #2
0
 private AsyncHttpClient createHttpClient() {
   // Don't limit the number of connections per host
   // See https://github.com/ning/async-http-client/issues/issue/28
   final AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
   builder.setMaximumConnectionsPerHost(-1);
   return new AsyncHttpClient(builder.build());
 }
コード例 #3
0
 protected AsyncHttpClient getHttpClient(final ProxyRepository proxyRepository) {
   final AsyncHttpClientConfig.Builder clientConfigBuilder =
       ahcProvider.getAsyncHttpClientConfigBuilder(
           proxyRepository, proxyRepository.getRemoteStorageContext());
   clientConfigBuilder.setFollowRedirects(true);
   clientConfigBuilder.setMaximumNumberOfRedirects(3);
   clientConfigBuilder.setMaxRequestRetry(2);
   final AsyncHttpClient client = new AsyncHttpClient(clientConfigBuilder.build());
   return client;
 }
コード例 #4
0
ファイル: ClientUtil.java プロジェクト: ryeo/wasync
 public static final AsyncHttpClient createDefaultAsyncHttpClient(Options o) {
   AsyncHttpClientConfig.Builder b = new AsyncHttpClientConfig.Builder();
   int t = o.requestTimeoutInSeconds();
   b.setFollowRedirects(true)
       .setIdleConnectionTimeoutInMs(-1)
       .setRequestTimeoutInMs(t == -1 ? t : t * 1000)
       .setUserAgent(WASYNC_USER_AGENT);
   AsyncHttpClientConfig config = b.build();
   return new AsyncHttpClient(config);
 }
コード例 #5
0
  /**
   * Create an {@link AsyncHttpClientConfig} instance based on the values from {@link
   * RepositorySystemSession}
   *
   * @param session {link RepositorySystemSession}
   * @return a configured instance of
   */
  private AsyncHttpClientConfig createConfig(
      RepositorySystemSession session, RemoteRepository repository, boolean useCompression) {
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();

    String userAgent =
        ConfigUtils.getString(
            session,
            ConfigurationProperties.DEFAULT_USER_AGENT,
            ConfigurationProperties.USER_AGENT);
    if (!StringUtils.isEmpty(userAgent)) {
      configBuilder.setUserAgent(userAgent);
    }
    int connectTimeout =
        ConfigUtils.getInteger(
            session,
            ConfigurationProperties.DEFAULT_CONNECT_TIMEOUT,
            ConfigurationProperties.CONNECT_TIMEOUT);

    configBuilder.setConnectionTimeoutInMs(connectTimeout);
    configBuilder.setCompressionEnabled(useCompression);
    configBuilder.setFollowRedirects(true);
    configBuilder.setRequestTimeoutInMs(
        ConfigUtils.getInteger(
            session,
            ConfigurationProperties.DEFAULT_REQUEST_TIMEOUT,
            ConfigurationProperties.REQUEST_TIMEOUT));

    configBuilder.setProxyServer(getProxy(repository));
    configBuilder.setRealm(getRealm(repository));

    return configBuilder.build();
  }
コード例 #6
0
  @Override
  protected void updateContext(ProxyRepository repository, RemoteStorageContext context)
      throws RemoteStorageException {
    if (context.hasContextObject(CTX_KEY_CLIENT)) {
      // proper shutdown of AHC, but cannot call getClient() here that would result in endless loop!
      AsyncHttpClient oldClient = (AsyncHttpClient) context.getContextObject(CTX_KEY_CLIENT);

      // TODO: AHC-26: current solution would kill ongoing downloads, be smarter
      oldClient.close();
    }

    final AsyncHttpClientConfig.Builder clientConfigBuilder =
        ahcProvider.getAsyncHttpClientConfigBuilder(repository, context);

    final AsyncHttpClient client = new AsyncHttpClient(clientConfigBuilder.build());

    context.putContextObject(CTX_KEY_CLIENT, client);

    context.putContextObject(CTX_KEY_S3_FLAG, new BooleanFlagHolder());
  }
コード例 #7
0
  public Client create(final OpenOptions options) {
    checkNotNull(options);

    AhcConfig config = new DefaultAhcConfig();
    AsyncHttpClientConfig.Builder builder = config.getAsyncHttpClientConfigBuilder();
    if (options.getUsername() != null && options.getPassword() != null) {
      Realm realm =
          new Realm.RealmBuilder()
              .setScheme(Realm.AuthScheme.BASIC)
              .setUsePreemptiveAuth(
                  true) // FIXME: ATM we must configure preemptive auth, to be replaced by session
                        // token
              .setPrincipal(options.getUsername())
              .setPassword(options.getPassword())
              .build();
      builder.setRealm(realm);
    }

    if (options.getProxyHost() != null) {
      ProxyServer proxyServer =
          new ProxyServer(
              ProxyServer.Protocol.HTTP,
              options.getProxyHost(),
              options.getProxyPort(),
              options.getProxyUsername(),
              options.getProxyPassword());
      builder.setProxyServer(proxyServer);
    }

    config.getClasses().add(JacksonProvider.class);

    AhcHttpClient client = AhcHttpClient.create(config, componentProviderFactory);
    client.setFollowRedirects(options.isFollowRedirects());

    // Last filter added is the first filter invoked
    client.addFilter(new FaultDecodingFilter());
    client.addFilter(new LoggingFilter());

    return client;
  }
コード例 #8
0
  /**
   * @param kbServerUrl Kill Bill url
   * @param username Kill Bill username
   * @param password Kill Bill password
   * @param apiKey Kill Bill api key
   * @param apiSecret Kill Bill api secret
   * @param proxyHost hostname of a proxy server that the client should use
   * @param proxyPort port number of a proxy server that the client should use
   * @param connectTimeOut connect timeout in milliseconds
   * @param readTimeOut read timeout in milliseconds
   * @param requestTimeout request timeout in milliseconds
   */
  public KillBillHttpClient(
      final String kbServerUrl,
      final String username,
      final String password,
      final String apiKey,
      final String apiSecret,
      final String proxyHost,
      final Integer proxyPort,
      final Integer connectTimeOut,
      final Integer readTimeOut,
      final Integer requestTimeout) {
    this.kbServerUrl = kbServerUrl;
    this.username = username;
    this.password = password;
    this.apiKey = apiKey;
    this.apiSecret = apiSecret;

    final AsyncHttpClientConfig.Builder cfg = new AsyncHttpClientConfig.Builder();

    cfg.setConnectTimeout(
        MoreObjects.firstNonNull(connectTimeOut, DEFAULT_HTTP_TIMEOUT_SEC * 1000));
    cfg.setReadTimeout(MoreObjects.firstNonNull(readTimeOut, DEFAULT_HTTP_TIMEOUT_SEC * 1000));
    cfg.setRequestTimeout(
        MoreObjects.firstNonNull(requestTimeout, DEFAULT_HTTP_TIMEOUT_SEC * 1000));
    cfg.setUserAgent(USER_AGENT);

    if (proxyHost != null && proxyPort != null) {
      final ProxyServer proxyServer = new ProxyServer(proxyHost, proxyPort);
      cfg.setProxyServer(proxyServer);
    }

    this.httpClient = new AsyncHttpClient(cfg.build());

    mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
  }
コード例 #9
0
ファイル: AhcComponent.java プロジェクト: RAvontuur/camel
  @Override
  protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters)
      throws Exception {
    String addressUri = createAddressUri(uri, remaining);

    // Do not set the HTTP URI because we still have all of the Camel internal
    // parameters in the URI at this point.
    AhcEndpoint endpoint = createAhcEndpoint(uri, this, null);
    setEndpointHeaderFilterStrategy(endpoint);
    endpoint.setClient(getClient());
    endpoint.setClientConfig(getClientConfig());
    endpoint.setBinding(getBinding());
    endpoint.setSslContextParameters(getSslContextParameters());

    setProperties(endpoint, parameters);

    if (IntrospectionSupport.hasProperties(parameters, CLIENT_CONFIG_PREFIX)) {
      AsyncHttpClientConfig.Builder builder =
          endpoint.getClientConfig() == null
              ? new AsyncHttpClientConfig.Builder()
              : AhcComponent.cloneConfig(endpoint.getClientConfig());

      if (endpoint.getClient() != null) {
        LOG.warn(
            "The user explicitly set an AsyncHttpClient instance on the component or "
                + "endpoint, but this endpoint URI contains client configuration parameters.  "
                + "Are you sure that this is what was intended?  The AsyncHttpClient will be used"
                + " and the URI parameters will be ignored.");
      } else if (endpoint.getClientConfig() != null) {
        LOG.warn(
            "The user explicitly set an AsyncHttpClientConfig instance on the component or "
                + "endpoint, but this endpoint URI contains client configuration parameters.  "
                + "Are you sure that this is what was intended?  The URI parameters will be applied"
                + " to a clone of the supplied AsyncHttpClientConfig in order to prevent unintended modification"
                + " of the explicitly configured AsyncHttpClientConfig.  That is, the URI parameters override the"
                + " settings on the explicitly configured AsyncHttpClientConfig for this endpoint.");
      }

      // special for realm builder
      Realm.RealmBuilder realmBuilder = null;
      if (IntrospectionSupport.hasProperties(parameters, CLIENT_REALM_CONFIG_PREFIX)) {
        realmBuilder = new Realm.RealmBuilder();

        // set and validate additional parameters on client config
        Map<String, Object> realmParams =
            IntrospectionSupport.extractProperties(parameters, CLIENT_REALM_CONFIG_PREFIX);
        setProperties(realmBuilder, realmParams);
        validateParameters(uri, realmParams, null);
      }

      // set and validate additional parameters on client config
      Map<String, Object> clientParams =
          IntrospectionSupport.extractProperties(parameters, CLIENT_CONFIG_PREFIX);
      setProperties(builder, clientParams);
      validateParameters(uri, clientParams, null);

      if (realmBuilder != null) {
        builder.setRealm(realmBuilder.build());
      }
      endpoint.setClientConfig(builder.build());
    }

    // restructure uri to be based on the parameters left as we dont want to include the Camel
    // internal options
    addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
    URI httpUri = URISupport.createRemainingURI(new URI(addressUri), parameters);
    endpoint.setHttpUri(httpUri);

    return endpoint;
  }