List of usage examples for org.apache.http.impl.client HttpClientBuilder setConnectionManager
public final HttpClientBuilder setConnectionManager(final HttpClientConnectionManager connManager)
From source file:com.arangodb.http.HttpManager.java
public void init() { // socket factory for HTTP ConnectionSocketFactory plainsf = new PlainConnectionSocketFactory(); // socket factory for HTTPS SSLConnectionSocketFactory sslsf = null; if (configure.getSslContext() != null) { sslsf = new SSLConnectionSocketFactory(configure.getSslContext()); } else {//from www.ja v a 2 s.c om sslsf = new SSLConnectionSocketFactory(SSLContexts.createSystemDefault()); } // register socket factories Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", plainsf).register("https", sslsf).build(); // ConnectionManager cm = new PoolingHttpClientConnectionManager(r); cm.setDefaultMaxPerRoute(configure.getMaxPerConnection()); cm.setMaxTotal(configure.getMaxTotalConnection()); Builder custom = RequestConfig.custom(); // RequestConfig if (configure.getConnectionTimeout() >= 0) { custom.setConnectTimeout(configure.getConnectionTimeout()); } if (configure.getTimeout() >= 0) { custom.setConnectionRequestTimeout(configure.getTimeout()); custom.setSocketTimeout(configure.getTimeout()); } custom.setStaleConnectionCheckEnabled(configure.isStaleConnectionCheck()); RequestConfig requestConfig = custom.build(); HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig); builder.setConnectionManager(cm); // KeepAlive Strategy ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { // Honor 'keep-alive' header HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } // otherwise keep alive for 30 seconds return 30 * 1000; } }; builder.setKeepAliveStrategy(keepAliveStrategy); // Retry Handler builder.setRetryHandler(new DefaultHttpRequestRetryHandler(configure.getRetryCount(), false)); // Proxy if (configure.getProxyHost() != null && configure.getProxyPort() != 0) { HttpHost proxy = new HttpHost(configure.getProxyHost(), configure.getProxyPort(), "http"); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); builder.setRoutePlanner(routePlanner); } // Client client = builder.build(); // Basic Auth // if (configure.getUser() != null && configure.getPassword() != null) { // AuthScope scope = AuthScope.ANY; // TODO // this.credentials = new // UsernamePasswordCredentials(configure.getUser(), // configure.getPassword()); // client.getCredentialsProvider().setCredentials(scope, credentials); // } }
From source file:de.undercouch.gradle.tasks.download.internal.DefaultHttpClientFactory.java
@Override public CloseableHttpClient createHttpClient(HttpHost httpHost, boolean acceptAnyCertificate) { HttpClientBuilder builder = HttpClientBuilder.create(); //configure proxy from system environment builder.setRoutePlanner(new SystemDefaultRoutePlanner(null)); //accept any certificate if necessary if ("https".equals(httpHost.getSchemeName()) && acceptAnyCertificate) { SSLConnectionSocketFactory icsf = getInsecureSSLSocketFactory(); builder.setSSLSocketFactory(icsf); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", icsf).build(); HttpClientConnectionManager cm = new BasicHttpClientConnectionManager(registry); builder.setConnectionManager(cm); }//from w w w . j a v a 2s . co m //add an interceptor that replaces the invalid Content-Type //'none' by 'identity' builder.addInterceptorFirst(new ContentEncodingNoneInterceptor()); CloseableHttpClient client = builder.build(); return client; }
From source file:org.apache.gobblin.http.ApacheHttpClient.java
public ApacheHttpClient(HttpClientBuilder builder, Config config, SharedResourcesBroker<GobblinScopeTypes> broker) { super(broker, HttpUtils.createApacheHttpClientLimiterKey(config)); config = config.withFallback(FALLBACK); RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT) .setSocketTimeout(config.getInt(REQUEST_TIME_OUT_MS_KEY)) .setConnectTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)) .setConnectionRequestTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)).build(); builder.disableCookieManagement().useSystemProperties().setDefaultRequestConfig(requestConfig); builder.setConnectionManager(getHttpConnManager(config)); client = builder.build();/*from w w w . j a va 2s. co m*/ }
From source file:org.glassfish.jersey.apache.connector.ApacheConnector.java
/** * Create the new Apache HTTP Client connector. * * @param client JAX-RS client instance for which the connector is being created. * @param config client configuration./*from w w w .ja v a 2 s .co m*/ */ ApacheConnector(final Client client, final Configuration config) { final Object connectionManager = config.getProperties().get(ApacheClientProperties.CONNECTION_MANAGER); if (connectionManager != null) { if (!(connectionManager instanceof HttpClientConnectionManager)) { LOGGER.log(Level.WARNING, LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(ApacheClientProperties.CONNECTION_MANAGER, connectionManager.getClass().getName(), HttpClientConnectionManager.class.getName())); } } Object reqConfig = config.getProperties().get(ApacheClientProperties.REQUEST_CONFIG); if (reqConfig != null) { if (!(reqConfig instanceof RequestConfig)) { LOGGER.log(Level.WARNING, LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(ApacheClientProperties.REQUEST_CONFIG, reqConfig.getClass().getName(), RequestConfig.class.getName())); reqConfig = null; } } final SSLContext sslContext = client.getSslContext(); final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(getConnectionManager(client, config, sslContext)); clientBuilder.setConnectionManagerShared(PropertiesHelper.getValue(config.getProperties(), ApacheClientProperties.CONNECTION_MANAGER_SHARED, false, null)); clientBuilder.setSslcontext(sslContext); final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); final Object credentialsProvider = config.getProperty(ApacheClientProperties.CREDENTIALS_PROVIDER); if (credentialsProvider != null && (credentialsProvider instanceof CredentialsProvider)) { clientBuilder.setDefaultCredentialsProvider((CredentialsProvider) credentialsProvider); } final Object retryHandler = config.getProperties().get(ApacheClientProperties.RETRY_HANDLER); if (retryHandler != null && (retryHandler instanceof HttpRequestRetryHandler)) { clientBuilder.setRetryHandler((HttpRequestRetryHandler) retryHandler); } final Object proxyUri; proxyUri = config.getProperty(ClientProperties.PROXY_URI); if (proxyUri != null) { final URI u = getProxyUri(proxyUri); final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); final String userName; userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); if (userName != null) { final String password; password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); if (password != null) { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(u.getHost(), u.getPort()), new UsernamePasswordCredentials(userName, password)); clientBuilder.setDefaultCredentialsProvider(credsProvider); } } clientBuilder.setProxy(proxy); } final Boolean preemptiveBasicAuthProperty = (Boolean) config.getProperties() .get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION); this.preemptiveBasicAuth = (preemptiveBasicAuthProperty != null) ? preemptiveBasicAuthProperty : false; final boolean ignoreCookies = PropertiesHelper.isProperty(config.getProperties(), ApacheClientProperties.DISABLE_COOKIES); if (reqConfig != null) { final RequestConfig.Builder reqConfigBuilder = RequestConfig.copy((RequestConfig) reqConfig); if (ignoreCookies) { reqConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); } requestConfig = reqConfigBuilder.build(); } else { if (ignoreCookies) { requestConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); } requestConfig = requestConfigBuilder.build(); } if (requestConfig.getCookieSpec() == null || !requestConfig.getCookieSpec().equals(CookieSpecs.IGNORE_COOKIES)) { this.cookieStore = new BasicCookieStore(); clientBuilder.setDefaultCookieStore(cookieStore); } else { this.cookieStore = null; } clientBuilder.setDefaultRequestConfig(requestConfig); this.client = clientBuilder.build(); }
From source file:com.github.dockerjava.jaxrs.connector.ApacheConnector.java
/** * Create the new Apache HTTP Client connector. * * @param config client configuration./*from w w w . j av a 2s . c om*/ */ ApacheConnector(Configuration config) { Object reqConfig = null; if (config != null) { final Object connectionManager = config.getProperties().get(ApacheClientProperties.CONNECTION_MANAGER); if (connectionManager != null) { if (!(connectionManager instanceof HttpClientConnectionManager)) { LOGGER.log(Level.WARNING, LocalizationMessages.IGNORING_VALUE_OF_PROPERTY( ApacheClientProperties.CONNECTION_MANAGER, connectionManager.getClass().getName(), HttpClientConnectionManager.class.getName())); } } reqConfig = config.getProperties().get(ApacheClientProperties.REQUEST_CONFIG); if (reqConfig != null) { if (!(reqConfig instanceof RequestConfig)) { LOGGER.log(Level.WARNING, LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(ApacheClientProperties.REQUEST_CONFIG, reqConfig.getClass().getName(), RequestConfig.class.getName())); reqConfig = null; } } } final SSLContext sslContext = getSslContext(config); final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(getConnectionManager(config, sslContext)); clientBuilder.setSslcontext(sslContext); final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); int connectTimeout = 0; int socketTimeout = 0; boolean ignoreCookies = false; if (config != null) { connectTimeout = ClientProperties.getValue(config.getProperties(), ClientProperties.CONNECT_TIMEOUT, 0); socketTimeout = ClientProperties.getValue(config.getProperties(), ClientProperties.READ_TIMEOUT, 0); ignoreCookies = PropertiesHelper.isProperty(config.getProperties(), ApacheClientProperties.DISABLE_COOKIES); final Object credentialsProvider = config.getProperty(ApacheClientProperties.CREDENTIALS_PROVIDER); if (credentialsProvider != null && (credentialsProvider instanceof CredentialsProvider)) { clientBuilder.setDefaultCredentialsProvider((CredentialsProvider) credentialsProvider); } Object proxyUri; proxyUri = config.getProperty(ClientProperties.PROXY_URI); if (proxyUri != null) { final URI u = getProxyUri(proxyUri); final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); String userName; userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); if (userName != null) { String password; password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); if (password != null) { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(u.getHost(), u.getPort()), new UsernamePasswordCredentials(userName, password)); clientBuilder.setDefaultCredentialsProvider(credsProvider); } } clientBuilder.setProxy(proxy); } final Boolean preemptiveBasicAuthProperty = (Boolean) config.getProperties() .get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION); this.preemptiveBasicAuth = (preemptiveBasicAuthProperty != null) ? preemptiveBasicAuthProperty : false; } else { this.preemptiveBasicAuth = false; } if (reqConfig != null) { RequestConfig.Builder reqConfigBuilder = RequestConfig.copy((RequestConfig) reqConfig); if (connectTimeout > 0) { reqConfigBuilder.setConnectTimeout(connectTimeout); } if (socketTimeout > 0) { reqConfigBuilder.setSocketTimeout(socketTimeout); } if (ignoreCookies) { reqConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); } requestConfig = reqConfigBuilder.build(); } else { requestConfigBuilder.setConnectTimeout(connectTimeout); requestConfigBuilder.setSocketTimeout(socketTimeout); if (ignoreCookies) { requestConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); } requestConfig = requestConfigBuilder.build(); } if (requestConfig.getCookieSpec() == null || !requestConfig.getCookieSpec().equals(CookieSpecs.IGNORE_COOKIES)) { this.cookieStore = new BasicCookieStore(); clientBuilder.setDefaultCookieStore(cookieStore); } else { this.cookieStore = null; } clientBuilder.setDefaultRequestConfig(requestConfig); this.client = clientBuilder.build(); }
From source file:org.codelibs.solr.lib.server.SolrLibHttpSolrServer.java
public void init() { if (clientConnectionManager == null) { final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setDefaultMaxPerRoute(defaultMaxConnectionsPerHost); connectionManager.setMaxTotal(maxTotalConnections); idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, connectionMonitorInterval, connectionIdelTimeout); idleConnectionMonitorThread.start(); clientConnectionManager = connectionManager; }/*from w w w.j a v a2 s . c om*/ requestConfigBuilder.setRedirectsEnabled(followRedirects); final HttpClientBuilder builder = HttpClients.custom(); if (allowCompression) { builder.addInterceptorLast(new UseCompressionRequestInterceptor()); builder.addInterceptorLast(new UseCompressionResponseInterceptor()); } for (final HttpRequestInterceptor iterceptor : httpRequestInterceptorList) { builder.addInterceptorLast(iterceptor); } init(builder.setConnectionManager(clientConnectionManager) .setDefaultRequestConfig(requestConfigBuilder.build()).build()); }
From source file:com.basistech.rosette.api.HttpRosetteAPI.java
private void initClient(String key, List<Header> additionalHeaders) { HttpClientBuilder builder = HttpClients.custom(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(connectionConcurrency); builder.setConnectionManager(cm); initHeaders(key, additionalHeaders); builder.setDefaultHeaders(this.additionalHeaders); httpClient = builder.build();/*from ww w . j a v a 2 s .c o m*/ this.additionalHeaders = new ArrayList<>(); }
From source file:com.esri.geoevent.datastore.GeoEventDataStoreProxy.java
private CloseableHttpClient createHttpClient(ServerInfo serverInfo) { HttpClientBuilder builder = (useBuiltinWindowsAuthentication(serverInfo)) ? WinHttpClients.custom() : HttpClients.custom();//from w w w .j a va 2s.c o m HttpClientConnectionManager connMgr = createConnectionManagerIfNecessary(); if (connMgr != null) { builder.setConnectionManager(connMgr); } builder.setUserAgent(userAgent); builder.useSystemProperties(); return builder.build(); }
From source file:run.var.teamcity.cloud.docker.client.apcon.ApacheConnector.java
/** * Create the new Apache HTTP Client connector. * * @param client JAX-RS client instance for which the connector is being created. * @param config client configuration.//from w w w. j a v a 2 s.com */ ApacheConnector(final Client client, final Configuration config) { final Object connectionManager = config.getProperties().get(ApacheClientProperties.CONNECTION_MANAGER); if (connectionManager != null) { if (!(connectionManager instanceof HttpClientConnectionManager)) { LOGGER.log(Level.WARNING, LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(ApacheClientProperties.CONNECTION_MANAGER, connectionManager.getClass().getName(), HttpClientConnectionManager.class.getName())); } } Object reqConfig = config.getProperties().get(ApacheClientProperties.REQUEST_CONFIG); if (reqConfig != null) { if (!(reqConfig instanceof RequestConfig)) { LOGGER.log(Level.WARNING, LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(ApacheClientProperties.REQUEST_CONFIG, reqConfig.getClass().getName(), RequestConfig.class.getName())); reqConfig = null; } } final SSLContext sslContext = client.getSslContext(); final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(getConnectionManager(client, config, sslContext)); clientBuilder.setConnectionManagerShared(PropertiesHelper.getValue(config.getProperties(), ApacheClientProperties.CONNECTION_MANAGER_SHARED, false, null)); clientBuilder.setSslcontext(sslContext); final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); final Object credentialsProvider = config.getProperty(ApacheClientProperties.CREDENTIALS_PROVIDER); if (credentialsProvider != null && (credentialsProvider instanceof CredentialsProvider)) { clientBuilder.setDefaultCredentialsProvider((CredentialsProvider) credentialsProvider); } final Object proxyUri; proxyUri = config.getProperty(ClientProperties.PROXY_URI); if (proxyUri != null) { final URI u = getProxyUri(proxyUri); final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); final String userName; userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); if (userName != null) { final String password; password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); if (password != null) { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(u.getHost(), u.getPort()), new UsernamePasswordCredentials(userName, password)); clientBuilder.setDefaultCredentialsProvider(credsProvider); } } clientBuilder.setProxy(proxy); } final Boolean preemptiveBasicAuthProperty = (Boolean) config.getProperties() .get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION); this.preemptiveBasicAuth = (preemptiveBasicAuthProperty != null) ? preemptiveBasicAuthProperty : false; final boolean ignoreCookies = PropertiesHelper.isProperty(config.getProperties(), ApacheClientProperties.DISABLE_COOKIES); if (reqConfig != null) { final RequestConfig.Builder reqConfigBuilder = RequestConfig.copy((RequestConfig) reqConfig); if (ignoreCookies) { reqConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); } requestConfig = reqConfigBuilder.build(); } else { if (ignoreCookies) { requestConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); } requestConfig = requestConfigBuilder.build(); } if (requestConfig.getCookieSpec() == null || !requestConfig.getCookieSpec().equals(CookieSpecs.IGNORE_COOKIES)) { this.cookieStore = new BasicCookieStore(); clientBuilder.setDefaultCookieStore(cookieStore); } else { this.cookieStore = null; } clientBuilder.setDefaultRequestConfig(requestConfig); /* DK_CLD: Add our connection reuse strategy. */ clientBuilder.setConnectionReuseStrategy(new UpgradeAwareConnectionReuseStrategy()); clientBuilder.setRequestExecutor(new HttpRequestExecutor() { protected HttpResponse doReceiveResponse(final HttpRequest request, final org.apache.http.HttpClientConnection conn, final HttpContext context) throws HttpException, IOException { Args.notNull(request, "HTTP request"); Args.notNull(conn, "Client connection"); Args.notNull(context, "HTTP context"); HttpResponse response = null; int statusCode = 0; while (response == null || (statusCode < HttpStatus.SC_OK // DK_CLD: the original implementation provided this loop to retry the HTTP request as long as // an intermediate response is returned (1xx status). This is however not suitable for the // status code 101 returned from Docker (and more generally, WebSockets) to notify that the // connection has been upgraded to raw TCP streaming. In such case the server ultimate response // is not HTTP anymore. && statusCode != HttpStatus.SC_SWITCHING_PROTOCOLS)) { response = conn.receiveResponseHeader(); if (canResponseHaveBody(request, response)) { conn.receiveResponseEntity(response); } statusCode = response.getStatusLine().getStatusCode(); } // while intermediate response return response; } @Override protected boolean canResponseHaveBody(HttpRequest request, HttpResponse response) { boolean canResponseHaveBody = super.canResponseHaveBody(request, response); return canResponseHaveBody || response.getStatusLine().getStatusCode() == HttpStatus.SC_SWITCHING_PROTOCOLS; } }); this.client = clientBuilder.build(); }
From source file:hello.MyPostHTTP.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectionRequestTimeout( context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setConnectTimeout( context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setRedirectsEnabled(false); requestConfigBuilder// w ww. j ava 2s. c om .setSocketTimeout(context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); final RequestConfig requestConfig = requestConfigBuilder.build(); final StreamThrottler throttler = throttlerRef.get(); final ProcessorLog logger = getLogger(); String lastUrl = null; long bytesToSend = 0L; final List<FlowFile> toSend = new ArrayList<>(); CloseableHttpClient client = null; final String transactionId = UUID.randomUUID().toString(); final ObjectHolder<String> dnHolder = new ObjectHolder<>("none"); while (true) { FlowFile flowFile = session.get(); if (flowFile == null) { break; } final String url = context.getProperty(URL).evaluateAttributeExpressions(flowFile).getValue(); try { new java.net.URL(url); } catch (final MalformedURLException e) { logger.error( "After substituting attribute values for {}, URL is {}; this is not a valid URL, so routing to failure", new Object[] { flowFile, url }); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); continue; } // If this FlowFile doesn't have the same url, throw it back on the queue and stop grabbing FlowFiles if (lastUrl != null && !lastUrl.equals(url)) { session.transfer(flowFile); break; } lastUrl = url; toSend.add(flowFile); if (client == null) { final Config config = getConfig(url, context); final HttpClientConnectionManager conMan = config.getConnectionManager(); final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(conMan); clientBuilder.addInterceptorFirst(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException { final HttpCoreContext coreContext = HttpCoreContext.adapt(httpContext); final ManagedHttpClientConnection conn = coreContext .getConnection(ManagedHttpClientConnection.class); if (!conn.isOpen()) { return; } final SSLSession sslSession = conn.getSSLSession(); if (sslSession != null) { final X509Certificate[] certChain = sslSession.getPeerCertificateChain(); if (certChain == null || certChain.length == 0) { throw new SSLPeerUnverifiedException("No certificates found"); } final X509Certificate cert = certChain[0]; dnHolder.set(cert.getSubjectDN().getName().trim()); } } }); clientBuilder.disableAutomaticRetries(); clientBuilder.disableContentCompression(); client = clientBuilder.build(); } bytesToSend += flowFile.getSize(); break; } if (toSend.isEmpty()) { return; } final String url = lastUrl; final HttpPost post = new HttpPost(url); final List<FlowFile> flowFileList = toSend; String userName = "Chris"; String password = "password"; final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("userName", userName); builder.addTextBody("password", password); for (final FlowFile flowFile : flowFileList) { session.read(flowFile, new InputStreamCallback() { @Override public void process(final InputStream rawIn) throws IOException { InputStream in = new ByteArrayInputStream(IOUtils.toByteArray(rawIn)); builder.addBinaryBody("file", in, ContentType.DEFAULT_BINARY, "filename"); } }); } final HttpEntity entity2 = builder.build(); post.setEntity(entity2); post.setConfig(requestConfig); final String contentType; contentType = DEFAULT_CONTENT_TYPE; post.setHeader(CONTENT_TYPE_HEADER, contentType); post.setHeader(FLOWFILE_CONFIRMATION_HEADER, "true"); post.setHeader(PROTOCOL_VERSION_HEADER, PROTOCOL_VERSION); post.setHeader(TRANSACTION_ID_HEADER, transactionId); // Do the actual POST final String flowFileDescription = toSend.size() <= 10 ? toSend.toString() : toSend.size() + " FlowFiles"; final String uploadDataRate; final long uploadMillis; CloseableHttpResponse response = null; try { final StopWatch stopWatch = new StopWatch(true); response = client.execute(post); // consume input stream entirely, ignoring its contents. If we // don't do this, the Connection will not be returned to the pool EntityUtils.consume(response.getEntity()); stopWatch.stop(); uploadDataRate = stopWatch.calculateDataRate(bytesToSend); uploadMillis = stopWatch.getDuration(TimeUnit.MILLISECONDS); } catch (final IOException e) { logger.error("Failed to Post {} due to {}; transferring to failure", new Object[] { flowFileDescription, e }); context.yield(); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } finally { if (response != null) { try { response.close(); } catch (final IOException e) { getLogger().warn("Failed to close HTTP Response due to {}", new Object[] { e }); } } } // If we get a 'SEE OTHER' status code and an HTTP header that indicates that the intent // of the Location URI is a flowfile hold, we will store this holdUri. This prevents us // from posting to some other webservice and then attempting to delete some resource to which // we are redirected final int responseCode = response.getStatusLine().getStatusCode(); final String responseReason = response.getStatusLine().getReasonPhrase(); String holdUri = null; if (responseCode == HttpServletResponse.SC_SEE_OTHER) { final Header locationUriHeader = response.getFirstHeader(LOCATION_URI_INTENT_NAME); if (locationUriHeader != null) { if (LOCATION_URI_INTENT_VALUE.equals(locationUriHeader.getValue())) { final Header holdUriHeader = response.getFirstHeader(LOCATION_HEADER_NAME); if (holdUriHeader != null) { holdUri = holdUriHeader.getValue(); } } } if (holdUri == null) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error( "Failed to Post {} to {}: sent content and received status code {}:{} but no Hold URI", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } return; } } if (holdUri == null) { if (responseCode == HttpServletResponse.SC_SERVICE_UNAVAILABLE) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error( "Failed to Post {} to {}: response code was {}:{}; will yield processing, " + "since the destination is temporarily unavailable", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } context.yield(); return; } if (responseCode >= 300) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error("Failed to Post {} to {}: response code was {}:{}", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } return; } logger.info("Successfully Posted {} to {} in {} at a rate of {}", new Object[] { flowFileDescription, url, FormatUtils.formatMinutesSeconds(uploadMillis, TimeUnit.MILLISECONDS), uploadDataRate }); for (final FlowFile flowFile : toSend) { session.getProvenanceReporter().send(flowFile, url, "Remote DN=" + dnHolder.get(), uploadMillis, true); session.transfer(flowFile, REL_SUCCESS); } return; } // // the response indicated a Hold URI; delete the Hold. // // determine the full URI of the Flow File's Hold; Unfortunately, the responses that are returned have // changed over the past, so we have to take into account a few different possibilities. String fullHoldUri = holdUri; if (holdUri.startsWith("/contentListener")) { // If the Hold URI that we get starts with /contentListener, it may not really be /contentListener, // as this really indicates that it should be whatever we posted to -- if posting directly to the // ListenHTTP component, it will be /contentListener, but if posting to a proxy/load balancer, we may // be posting to some other URL. fullHoldUri = url + holdUri.substring(16); } else if (holdUri.startsWith("/")) { // URL indicates the full path but not hostname or port; use the same hostname & port that we posted // to but use the full path indicated by the response. int firstSlash = url.indexOf("/", 8); if (firstSlash < 0) { firstSlash = url.length(); } final String beforeSlash = url.substring(0, firstSlash); fullHoldUri = beforeSlash + holdUri; } else if (!holdUri.startsWith("http")) { // Absolute URL fullHoldUri = url + (url.endsWith("/") ? "" : "/") + holdUri; } final HttpDelete delete = new HttpDelete(fullHoldUri); delete.setHeader(TRANSACTION_ID_HEADER, transactionId); while (true) { try { final HttpResponse holdResponse = client.execute(delete); EntityUtils.consume(holdResponse.getEntity()); final int holdStatusCode = holdResponse.getStatusLine().getStatusCode(); final String holdReason = holdResponse.getStatusLine().getReasonPhrase(); if (holdStatusCode >= 300) { logger.error( "Failed to delete Hold that destination placed on {}: got response code {}:{}; routing to failure", new Object[] { flowFileDescription, holdStatusCode, holdReason }); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } logger.info("Successfully Posted {} to {} in {} milliseconds at a rate of {}", new Object[] { flowFileDescription, url, uploadMillis, uploadDataRate }); for (final FlowFile flowFile : toSend) { session.getProvenanceReporter().send(flowFile, url); session.transfer(flowFile, REL_SUCCESS); } return; } catch (final IOException e) { logger.warn("Failed to delete Hold that destination placed on {} due to {}", new Object[] { flowFileDescription, e }); } if (!isScheduled()) { context.yield(); logger.warn( "Failed to delete Hold that destination placed on {}; Processor has been stopped so routing FlowFile(s) to failure", new Object[] { flowFileDescription }); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } } }