@Override protected void onEntityEnclosed(final HttpEntity entity, final ContentType contentType) throws IOException { long len = entity.getContentLength(); if (len > Integer.MAX_VALUE) { throw new ContentTooLongException("Entity content is too long: " + len); } if (len < 0) { len = BUFFER_SIZE; } this.buf = new SimpleInputBuffer((int) len, new HeapByteBufferAllocator()); this.httpResponse.setEntity(new ContentBufferEntity(entity, this.buf)); }
@Override protected void onEntityEnclosed(final HttpEntity entity, final ContentType contentType) throws IOException { long len = entity.getContentLength(); if (len > Integer.MAX_VALUE) { throw new ContentTooLongException("Entity content is too long: " + len); } if (len < 0) { len = 4096; } this.buf = new SimpleInputBuffer((int) len, new HeapByteBufferAllocator()); this.response.setEntity(new ContentBufferEntity(entity, this.buf)); }
protected ExecCallbackAsyncREST<HttpResponse> buildAsyncClient(RESTPool pool) throws IOException { SSLContext sslContext; try { sslContext = SSLContext.getDefault(); } catch (NoSuchAlgorithmException e) { throw new IOException(e); } Registry<SchemeIOSessionStrategy> socketRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create() .register("http", NoopIOSessionStrategy.INSTANCE) .register("https", new SSLIOSessionStrategy(sslContext, NoopHostnameVerifier.INSTANCE)) .build(); IOReactorConfig socketConfig = IOReactorConfig.custom() .setIoThreadCount(pool.getReactorThreadCount()) .setSoTimeout(new Long(pool.getSocketTimeout()).intValue()) .setTcpNoDelay(true) .setSoKeepAlive(true) .setSelectInterval(REACTOR_SELECT_INTERVAL) .build(); ConnectionConfig connectionConfig = ConnectionConfig.custom() .setCharset(StandardCharsets.UTF_8) .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .build(); RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(new Long(pool.getMaxPoolWait()).intValue()) .setConnectTimeout(new Long(pool.getConnectionTimeout()).intValue()) .setExpectContinueEnabled(pool.expectContinue()) .setRedirectsEnabled(false) .setStaleConnectionCheckEnabled(pool.getValidationOnInactivity() >= 0) .build(); NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory = new ManagedNHttpClientConnectionFactory( new org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory(), new org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory(), HeapByteBufferAllocator.INSTANCE ); //TODO set validateAfterInactivity when supported PoolingNHttpClientConnectionManager ccm = new PoolingNHttpClientConnectionManager( new DefaultConnectingIOReactor(socketConfig), connFactory, socketRegistry, new SystemDefaultDnsResolver() ); ccm.setMaxTotal(pool.getMaxTotal()); ccm.setDefaultMaxPerRoute(pool.getMaxPerRoute()); ccm.setDefaultConnectionConfig(connectionConfig); HttpAsyncClientBuilder builder = HttpAsyncClients.custom() .setConnectionManager(ccm) .setDefaultRequestConfig(requestConfig) .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE) .disableCookieManagement(); IdleAsyncConnectionEvictor evictor = new IdleAsyncConnectionEvictor(ccm, pool.getEvictorSleep(), TimeUnit.MILLISECONDS, pool.getMaxIdleTime(), TimeUnit.MILLISECONDS); addProxy(pool, builder); handleRedirects(pool, builder); CloseableHttpAsyncClient servClient = builder.build(); servClient.start(); HTTPCClientMonitor monitor = pool.hasConnectionMetrics() ? new HTTPCAsyncClientMonitor(pool.getName(), ccm) : null; return new HTTPCAsyncClient(servClient, evictor, monitor); }
/** * Returns the instance of {@link ByteBufferAllocator} to use for content buffering. * Allows to plug in any {@link ByteBufferAllocator} implementation. */ protected ByteBufferAllocator getByteBufferAllocator() { return HeapByteBufferAllocator.INSTANCE; }