use of org.apache.hc.core5.http.protocol.RequestUserAgent in project httpcomponents-core by apache.
the class TestStandardInterceptors method testRequestUserAgentNotGenerated.
@Test
public void testRequestUserAgentNotGenerated() throws Exception {
final HttpContext context = new BasicHttpContext(null);
final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
request.addHeader(new BasicHeader(HttpHeaders.USER_AGENT, "whatever"));
final RequestUserAgent interceptor = new RequestUserAgent("some agent");
interceptor.process(request, request.getEntity(), context);
final Header header = request.getFirstHeader(HttpHeaders.USER_AGENT);
Assertions.assertNotNull(header);
Assertions.assertEquals("whatever", header.getValue());
}
use of org.apache.hc.core5.http.protocol.RequestUserAgent in project californium by eclipse.
the class HttpClientFactory method createClient.
/**
* Create the pooled asynchronous http client.
*
* @param config configuration for the http client
* @return asynchronous http client
* @since 3.0 (changed parameter to Configuration)
*/
public static CloseableHttpAsyncClient createClient(Configuration config) {
int connectionIdleSecs = config.getTimeAsInt(Proxy2Config.HTTP_CONNECTION_IDLE_TIMEOUT, TimeUnit.SECONDS);
final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().disableCookieManagement().setDefaultRequestConfig(createCustomRequestConfig(config)).setConnectionManager(createPoolingConnManager(config)).setVersionPolicy(HttpVersionPolicy.NEGOTIATE).setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(connectionIdleSecs)).build()).addRequestInterceptorFirst(new RequestConnControl()).addRequestInterceptorFirst(new RequestDate()).addRequestInterceptorFirst(new RequestExpectContinue()).addRequestInterceptorFirst(new RequestTargetHost()).addRequestInterceptorFirst(new RequestUserAgent()).setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
@Override
public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) {
TimeValue keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == null || keepAlive.getDuration() < 0) {
// Keep connections alive if a keep-alive value
// has not be explicitly set by the server
keepAlive = KEEP_ALIVE;
}
return keepAlive;
}
}).build();
client.start();
return client;
}
use of org.apache.hc.core5.http.protocol.RequestUserAgent in project httpcomponents-core by apache.
the class TestStandardInterceptors method testRequestUserAgentGenerated.
@Test
public void testRequestUserAgentGenerated() throws Exception {
final HttpContext context = new BasicHttpContext(null);
final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
final RequestUserAgent interceptor = new RequestUserAgent("some agent");
interceptor.process(request, request.getEntity(), context);
final Header header = request.getFirstHeader(HttpHeaders.USER_AGENT);
Assertions.assertNotNull(header);
Assertions.assertEquals("some agent", header.getValue());
}
use of org.apache.hc.core5.http.protocol.RequestUserAgent in project httpcomponents-core by apache.
the class HttpBenchmark method execute.
public Results execute() throws Exception {
final HttpProcessorBuilder builder = HttpProcessorBuilder.create().addAll(new H2RequestContent(), new H2RequestTargetHost(), new H2RequestConnControl(), new RequestUserAgent("HttpCore-AB/5.0"));
if (this.config.isUseExpectContinue()) {
builder.add(new RequestExpectContinue());
}
final SSLContext sslContext;
if ("https".equals(config.getUri().getScheme())) {
final SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.setProtocol("SSL");
if (config.isDisableSSLVerification()) {
sslContextBuilder.loadTrustMaterial(null, (chain, authType) -> true);
} else if (config.getTrustStorePath() != null) {
sslContextBuilder.loadTrustMaterial(new File(config.getTrustStorePath()), config.getTrustStorePassword() != null ? config.getTrustStorePassword().toCharArray() : null);
}
if (config.getIdentityStorePath() != null) {
sslContextBuilder.loadKeyMaterial(Paths.get(config.getIdentityStorePath()), config.getIdentityStorePassword() != null ? config.getIdentityStorePassword().toCharArray() : null, config.getIdentityStorePassword() != null ? config.getIdentityStorePassword().toCharArray() : null);
}
sslContext = sslContextBuilder.build();
} else {
sslContext = SSLContexts.createSystemDefault();
}
final HttpVersionPolicy versionPolicy;
if (config.isForceHttp2()) {
versionPolicy = HttpVersionPolicy.FORCE_HTTP_2;
} else {
if (sslContext != null) {
versionPolicy = HttpVersionPolicy.NEGOTIATE;
} else {
versionPolicy = HttpVersionPolicy.FORCE_HTTP_1;
}
}
final Stats stats = new Stats();
try (final HttpAsyncRequester requester = H2RequesterBootstrap.bootstrap().setHttpProcessor(builder.build()).setTlsStrategy(new BasicClientTlsStrategy(sslContext)).setVersionPolicy(versionPolicy).setH2Config(H2Config.custom().setPushEnabled(false).build()).setIOSessionDecorator(ioSession -> new IOSession() {
@Override
public String getId() {
return ioSession.getId();
}
@Override
public Lock getLock() {
return ioSession.getLock();
}
@Override
public void enqueue(final Command command, final Command.Priority priority) {
ioSession.enqueue(command, priority);
}
@Override
public boolean hasCommands() {
return ioSession.hasCommands();
}
@Override
public Command poll() {
return ioSession.poll();
}
@Override
public ByteChannel channel() {
return ioSession.channel();
}
@Override
public SocketAddress getRemoteAddress() {
return ioSession.getRemoteAddress();
}
@Override
public SocketAddress getLocalAddress() {
return ioSession.getLocalAddress();
}
@Override
public int getEventMask() {
return ioSession.getEventMask();
}
@Override
public void setEventMask(final int ops) {
ioSession.setEventMask(ops);
}
@Override
public void setEvent(final int op) {
ioSession.setEvent(op);
}
@Override
public void clearEvent(final int op) {
ioSession.clearEvent(op);
}
@Override
public void close() {
ioSession.close();
}
@Override
public Status getStatus() {
return ioSession.getStatus();
}
@Override
public int read(final ByteBuffer dst) throws IOException {
final int bytesRead = ioSession.read(dst);
if (bytesRead > 0) {
stats.incTotalBytesRecv(bytesRead);
}
return bytesRead;
}
@Override
public int write(final ByteBuffer src) throws IOException {
final int bytesWritten = ioSession.write(src);
if (bytesWritten > 0) {
stats.incTotalBytesSent(bytesWritten);
}
return bytesWritten;
}
@Override
public boolean isOpen() {
return ioSession.isOpen();
}
@Override
public Timeout getSocketTimeout() {
return ioSession.getSocketTimeout();
}
@Override
public void setSocketTimeout(final Timeout timeout) {
ioSession.setSocketTimeout(timeout);
}
@Override
public long getLastReadTime() {
return ioSession.getLastReadTime();
}
@Override
public long getLastWriteTime() {
return ioSession.getLastWriteTime();
}
@Override
public long getLastEventTime() {
return ioSession.getLastEventTime();
}
@Override
public void updateReadTime() {
ioSession.updateReadTime();
}
@Override
public void updateWriteTime() {
ioSession.updateWriteTime();
}
@Override
public void close(final CloseMode closeMode) {
ioSession.close(closeMode);
}
@Override
public IOEventHandler getHandler() {
return ioSession.getHandler();
}
@Override
public void upgrade(final IOEventHandler handler) {
ioSession.upgrade(handler);
}
}).setStreamListener(new Http1StreamListener() {
@Override
public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
if (config.getVerbosity() >= 3) {
System.out.println(">> " + request.getMethod() + " " + request.getRequestUri());
final Header[] headers = request.getHeaders();
for (final Header header : headers) {
System.out.println(">> " + header);
}
System.out.println();
}
}
@Override
public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
if (config.getVerbosity() >= 3) {
System.out.println("<< " + response.getCode() + " " + response.getReasonPhrase());
final Header[] headers = response.getHeaders();
for (final Header header : headers) {
System.out.println("<< " + header);
}
System.out.println();
}
}
@Override
public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
}
}).setStreamListener(new H2StreamListener() {
private final FramePrinter framePrinter = new FramePrinter();
@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
if (config.getVerbosity() >= 3) {
for (final Header header : headers) {
System.out.println("<< " + header);
}
System.out.println();
}
}
@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
if (config.getVerbosity() >= 3) {
for (final Header header : headers) {
System.out.println(">> " + header);
}
System.out.println();
}
}
@Override
public void onFrameInput(final HttpConnection connection, final int streamId, final RawFrame frame) {
if (config.getVerbosity() >= 4) {
System.out.print("<< ");
try {
framePrinter.printFrameInfo(frame, System.out);
System.out.println();
if (!frame.isType(FrameType.DATA)) {
framePrinter.printPayload(frame, System.out);
System.out.println();
}
} catch (final IOException ignore) {
}
}
}
@Override
public void onFrameOutput(final HttpConnection connection, final int streamId, final RawFrame frame) {
if (config.getVerbosity() >= 4) {
System.out.print(">> ");
try {
framePrinter.printFrameInfo(frame, System.out);
System.out.println();
if (!frame.isType(FrameType.DATA)) {
framePrinter.printPayload(frame, System.out);
System.out.println();
}
} catch (final IOException ignore) {
}
}
}
@Override
public void onInputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
if (config.getVerbosity() >= 5) {
System.out.println("<< stream " + streamId + ": " + actualSize + " " + delta);
}
}
@Override
public void onOutputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
if (config.getVerbosity() >= 5) {
System.out.println(">> stream " + streamId + ": " + actualSize + " " + delta);
}
}
}).setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(config.getSocketTimeout()).build()).create()) {
requester.setDefaultMaxPerRoute(config.getConcurrencyLevel());
requester.setMaxTotal(config.getConcurrencyLevel() * 2);
requester.start();
return doExecute(requester, stats);
}
}
Aggregations