List of usage examples for io.vertx.core.net ProxyOptions setPassword
public ProxyOptions setPassword(String password)
From source file:io.gravitee.fetcher.http.HttpFetcher.java
License:Apache License
private CompletableFuture<Buffer> fetchContent() { CompletableFuture<Buffer> future = new VertxCompletableFuture<>(vertx); URI requestUri = URI.create(httpFetcherConfiguration.getUrl()); boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme()); final HttpClientOptions options = new HttpClientOptions().setSsl(ssl).setTrustAll(true).setMaxPoolSize(1) .setKeepAlive(false).setTcpKeepAlive(false).setConnectTimeout(httpClientTimeout); if (httpFetcherConfiguration.isUseSystemProxy()) { ProxyOptions proxyOptions = new ProxyOptions(); proxyOptions.setType(ProxyType.valueOf(httpClientProxyType)); if (HTTPS_SCHEME.equals(requestUri.getScheme())) { proxyOptions.setHost(httpClientProxyHttpsHost); proxyOptions.setPort(httpClientProxyHttpsPort); proxyOptions.setUsername(httpClientProxyHttpsUsername); proxyOptions.setPassword(httpClientProxyHttpsPassword); } else {/*w w w . java 2s.co m*/ proxyOptions.setHost(httpClientProxyHttpHost); proxyOptions.setPort(httpClientProxyHttpPort); proxyOptions.setUsername(httpClientProxyHttpUsername); proxyOptions.setPassword(httpClientProxyHttpPassword); } options.setProxyOptions(proxyOptions); } final HttpClient httpClient = vertx.createHttpClient(options); final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80); try { HttpClientRequest request = httpClient.request(HttpMethod.GET, port, requestUri.getHost(), requestUri.toString()); request.setTimeout(httpClientTimeout); request.handler(response -> { if (response.statusCode() == HttpStatusCode.OK_200) { response.bodyHandler(buffer -> { future.complete(buffer); // Close client httpClient.close(); }); } else { future.complete(null); } }); request.exceptionHandler(event -> { try { future.completeExceptionally(event); // Close client httpClient.close(); } catch (IllegalStateException ise) { // Do not take care about exception when closing client } }); request.end(); } catch (Exception ex) { logger.error("Unable to fetch content using HTTP", ex); future.completeExceptionally(ex); } return future; }
From source file:io.gravitee.gateway.http.connector.VertxHttpClient.java
License:Apache License
@Override protected void doStart() throws Exception { httpClientOptions = new HttpClientOptions(); httpClientOptions.setPipelining(endpoint.getHttpClientOptions().isPipelining()); httpClientOptions.setKeepAlive(endpoint.getHttpClientOptions().isKeepAlive()); httpClientOptions.setIdleTimeout((int) (endpoint.getHttpClientOptions().getIdleTimeout() / 1000)); httpClientOptions.setConnectTimeout((int) endpoint.getHttpClientOptions().getConnectTimeout()); httpClientOptions.setUsePooledBuffers(true); httpClientOptions.setMaxPoolSize(endpoint.getHttpClientOptions().getMaxConcurrentConnections()); httpClientOptions.setTryUseCompression(endpoint.getHttpClientOptions().isUseCompression()); httpClientOptions.setLogActivity(true); // Configure proxy HttpProxy proxy = endpoint.getHttpProxy(); if (proxy != null && proxy.isEnabled()) { ProxyOptions proxyOptions = new ProxyOptions(); proxyOptions.setHost(proxy.getHost()); proxyOptions.setPort(proxy.getPort()); proxyOptions.setUsername(proxy.getUsername()); proxyOptions.setPassword(proxy.getPassword()); proxyOptions.setType(ProxyType.valueOf(proxy.getType().name())); httpClientOptions.setProxyOptions(proxyOptions); }/*w w w.ja v a 2s . com*/ URI target = URI.create(endpoint.getTarget()); HttpClientSslOptions sslOptions = endpoint.getHttpClientSslOptions(); if (HTTPS_SCHEME.equalsIgnoreCase(target.getScheme())) { // Configure SSL httpClientOptions.setSsl(true); if (sslOptions != null) { httpClientOptions.setVerifyHost(sslOptions.isHostnameVerifier()) .setTrustAll(sslOptions.isTrustAll()); // Client trust configuration if (!sslOptions.isTrustAll() && sslOptions.getTrustStore() != null) { switch (sslOptions.getTrustStore().getType()) { case PEM: PEMTrustStore pemTrustStore = (PEMTrustStore) sslOptions.getTrustStore(); PemTrustOptions pemTrustOptions = new PemTrustOptions(); if (pemTrustStore.getPath() != null && !pemTrustStore.getPath().isEmpty()) { pemTrustOptions.addCertPath(pemTrustStore.getPath()); } else if (pemTrustStore.getContent() != null && !pemTrustStore.getContent().isEmpty()) { pemTrustOptions .addCertValue(io.vertx.core.buffer.Buffer.buffer(pemTrustStore.getContent())); } else { throw new EndpointException( "Missing PEM certificate value for endpoint " + endpoint.getName()); } this.httpClientOptions.setPemTrustOptions(pemTrustOptions); break; case PKCS12: PKCS12TrustStore pkcs12TrustStore = (PKCS12TrustStore) sslOptions.getTrustStore(); PfxOptions pfxOptions = new PfxOptions(); pfxOptions.setPassword(pkcs12TrustStore.getPassword()); if (pkcs12TrustStore.getPath() != null && !pkcs12TrustStore.getPath().isEmpty()) { pfxOptions.setPath(pkcs12TrustStore.getPath()); } else if (pkcs12TrustStore.getContent() != null && !pkcs12TrustStore.getContent().isEmpty()) { pfxOptions.setValue(io.vertx.core.buffer.Buffer.buffer(pkcs12TrustStore.getContent())); } else { throw new EndpointException("Missing PKCS12 value for endpoint " + endpoint.getName()); } this.httpClientOptions.setPfxTrustOptions(pfxOptions); break; case JKS: JKSTrustStore jksTrustStore = (JKSTrustStore) sslOptions.getTrustStore(); JksOptions jksOptions = new JksOptions(); jksOptions.setPassword(jksTrustStore.getPassword()); if (jksTrustStore.getPath() != null && !jksTrustStore.getPath().isEmpty()) { jksOptions.setPath(jksTrustStore.getPath()); } else if (jksTrustStore.getContent() != null && !jksTrustStore.getContent().isEmpty()) { jksOptions.setValue(io.vertx.core.buffer.Buffer.buffer(jksTrustStore.getContent())); } else { throw new EndpointException("Missing JKS value for endpoint " + endpoint.getName()); } this.httpClientOptions.setTrustStoreOptions(jksOptions); break; } } // Client authentication configuration if (sslOptions.getKeyStore() != null) { switch (sslOptions.getKeyStore().getType()) { case PEM: PEMKeyStore pemKeyStore = (PEMKeyStore) sslOptions.getKeyStore(); PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions(); if (pemKeyStore.getCertPath() != null && !pemKeyStore.getCertPath().isEmpty()) { pemKeyCertOptions.setCertPath(pemKeyStore.getCertPath()); } else if (pemKeyStore.getCertContent() != null && !pemKeyStore.getCertContent().isEmpty()) { pemKeyCertOptions .setCertValue(io.vertx.core.buffer.Buffer.buffer(pemKeyStore.getCertContent())); } if (pemKeyStore.getKeyPath() != null && !pemKeyStore.getKeyPath().isEmpty()) { pemKeyCertOptions.setKeyPath(pemKeyStore.getKeyPath()); } else if (pemKeyStore.getKeyContent() != null && !pemKeyStore.getKeyContent().isEmpty()) { pemKeyCertOptions .setKeyValue(io.vertx.core.buffer.Buffer.buffer(pemKeyStore.getKeyContent())); } this.httpClientOptions.setPemKeyCertOptions(pemKeyCertOptions); break; case PKCS12: PKCS12KeyStore pkcs12KeyStore = (PKCS12KeyStore) sslOptions.getKeyStore(); PfxOptions pfxOptions = new PfxOptions(); pfxOptions.setPassword(pkcs12KeyStore.getPassword()); if (pkcs12KeyStore.getPath() != null && !pkcs12KeyStore.getPath().isEmpty()) { pfxOptions.setPath(pkcs12KeyStore.getPath()); } else if (pkcs12KeyStore.getContent() != null && !pkcs12KeyStore.getContent().isEmpty()) { pfxOptions.setValue(io.vertx.core.buffer.Buffer.buffer(pkcs12KeyStore.getContent())); } this.httpClientOptions.setPfxKeyCertOptions(pfxOptions); break; case JKS: JKSKeyStore jksKeyStore = (JKSKeyStore) sslOptions.getKeyStore(); JksOptions jksOptions = new JksOptions(); jksOptions.setPassword(jksKeyStore.getPassword()); if (jksKeyStore.getPath() != null && !jksKeyStore.getPath().isEmpty()) { jksOptions.setPath(jksKeyStore.getPath()); } else if (jksKeyStore.getContent() != null && !jksKeyStore.getContent().isEmpty()) { jksOptions.setValue(io.vertx.core.buffer.Buffer.buffer(jksKeyStore.getContent())); } this.httpClientOptions.setKeyStoreOptions(jksOptions); break; } } } } printHttpClientConfiguration(httpClientOptions); }
From source file:io.gravitee.gateway.http.vertx.VertxHttpClient.java
License:Apache License
@Override protected void doStart() throws Exception { // TODO: Prepare HttpClientOptions according to the endpoint to improve performance when creating a new // instance of the Vertx client httpClientOptions = new HttpClientOptions(); httpClientOptions.setPipelining(endpoint.getHttpClientOptions().isPipelining()); httpClientOptions.setKeepAlive(endpoint.getHttpClientOptions().isKeepAlive()); httpClientOptions.setIdleTimeout((int) (endpoint.getHttpClientOptions().getIdleTimeout() / 1000)); httpClientOptions.setConnectTimeout((int) endpoint.getHttpClientOptions().getConnectTimeout()); httpClientOptions.setUsePooledBuffers(true); httpClientOptions.setMaxPoolSize(endpoint.getHttpClientOptions().getMaxConcurrentConnections()); httpClientOptions.setTryUseCompression(endpoint.getHttpClientOptions().isUseCompression()); // Configure proxy HttpProxy proxy = endpoint.getHttpProxy(); if (proxy != null && proxy.isEnabled()) { ProxyOptions proxyOptions = new ProxyOptions(); proxyOptions.setHost(proxy.getHost()); proxyOptions.setPort(proxy.getPort()); proxyOptions.setUsername(proxy.getUsername()); proxyOptions.setPassword(proxy.getPassword()); proxyOptions.setType(ProxyType.valueOf(proxy.getType().name())); httpClientOptions.setProxyOptions(proxyOptions); }/*from w w w. j a v a 2s .co m*/ URI target = URI.create(endpoint.getTarget()); // Configure SSL HttpClientSslOptions sslOptions = endpoint.getHttpClientSslOptions(); if (sslOptions != null && sslOptions.isEnabled()) { httpClientOptions.setSsl(sslOptions.isEnabled()).setVerifyHost(sslOptions.isHostnameVerifier()) .setTrustAll(sslOptions.isTrustAll()); if (sslOptions.getPem() != null && !sslOptions.getPem().isEmpty()) { httpClientOptions.setPemTrustOptions(new PemTrustOptions() .addCertValue(io.vertx.core.buffer.Buffer.buffer(sslOptions.getPem()))); } } else if (HTTPS_SCHEME.equalsIgnoreCase(target.getScheme())) { // SSL is not configured but the endpoint scheme is HTTPS so let's enable the SSL on Vert.x HTTP client // automatically httpClientOptions.setSsl(true).setTrustAll(true); } printHttpClientConfiguration(httpClientOptions); }
From source file:org.apache.servicecomb.serviceregistry.client.http.HttpClientPool.java
License:Apache License
@Override public HttpClientOptions createHttpClientOptions() { HttpVersion ver = ServiceRegistryConfig.INSTANCE.getHttpVersion(); HttpClientOptions httpClientOptions = new HttpClientOptions(); httpClientOptions.setProtocolVersion(ver); httpClientOptions.setConnectTimeout(ServiceRegistryConfig.INSTANCE.getConnectionTimeout()); httpClientOptions.setIdleTimeout(ServiceRegistryConfig.INSTANCE.getIdleConnectionTimeout()); if (ServiceRegistryConfig.INSTANCE.isProxyEnable()) { ProxyOptions proxy = new ProxyOptions(); proxy.setHost(ServiceRegistryConfig.INSTANCE.getProxyHost()); proxy.setPort(ServiceRegistryConfig.INSTANCE.getProxyPort()); proxy.setUsername(ServiceRegistryConfig.INSTANCE.getProxyUsername()); proxy.setPassword(ServiceRegistryConfig.INSTANCE.getProxyPasswd()); httpClientOptions.setProxyOptions(proxy); }/*from w ww . j av a 2 s . c o m*/ if (ver == HttpVersion.HTTP_2) { LOGGER.debug("service center client protocol version is HTTP/2"); httpClientOptions.setHttp2ClearTextUpgrade(false); } if (ServiceRegistryConfig.INSTANCE.isSsl()) { LOGGER.debug("service center client performs requests over TLS"); VertxTLSBuilder.buildHttpClientOptions(SSL_KEY, httpClientOptions); } return httpClientOptions; }