org.apache.hc.core5.http.message.BasicHeader Java Examples

The following examples show how to use org.apache.hc.core5.http.message.BasicHeader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Http5FileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Create an {@link HttpClientBuilder} object. Invoked by {@link #createHttpClient(Http5FileSystemConfigBuilder, GenericFileName, FileSystemOptions)}.
 *
 * @param builder Configuration options builder for HTTP4 provider
 * @param rootName The root path
 * @param fileSystemOptions The FileSystem options
 * @return an {@link HttpClientBuilder} object
 * @throws FileSystemException if an error occurs
 */
protected HttpClientBuilder createHttpClientBuilder(final Http5FileSystemConfigBuilder builder, final GenericFileName rootName,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final List<Header> defaultHeaders = new ArrayList<>();
    defaultHeaders.add(new BasicHeader(HttpHeaders.USER_AGENT, builder.getUserAgent(fileSystemOptions)));

    final ConnectionReuseStrategy connectionReuseStrategy = builder.isKeepAlive(fileSystemOptions)
            ? DefaultConnectionReuseStrategy.INSTANCE
            : (request, response, context) -> false;

    final HttpClientBuilder httpClientBuilder =
            HttpClients.custom()
            .setRoutePlanner(createHttpRoutePlanner(builder, fileSystemOptions))
            .setConnectionManager(createConnectionManager(builder, fileSystemOptions))
            .setConnectionReuseStrategy(connectionReuseStrategy)
            .setDefaultRequestConfig(createDefaultRequestConfig(builder, fileSystemOptions))
            .setDefaultHeaders(defaultHeaders)
            .setDefaultCookieStore(createDefaultCookieStore(builder, fileSystemOptions));

    if (!builder.getFollowRedirect(fileSystemOptions)) {
        httpClientBuilder.disableRedirectHandling();
    }

    return httpClientBuilder;
}
 
Example #2
Source File: AbstractRequest.java    From spotify-web-api-java with MIT License 5 votes vote down vote up
public <X> BT setHeader(final String name, final X value) {
  assert (name != null);
  assert (!name.equals(""));
  assert (value != null);
  listAddOnce(this.headers, new BasicHeader(name, String.valueOf(value)));
  return self();
}
 
Example #3
Source File: ChannelHttp2.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean open() throws Exception {
    Config config = Config.getConfigByName("http2.properties");

    String hostname = this.getRemoteHost();
    int port = this.getRemotePort();
    int timeout = Integer.parseInt(config.getString("SOCKET_TIMEOUT", "30"));
    int initialWindowSize = Integer.parseInt(config.getString("client.INITIAL_WINDOW_SIZE", "1073741824"));
    int maxConcurrentStreams = Integer.parseInt(config.getString("client.MAX_CONCURRENT_STREAMS", "100"));

    IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
            .setSoTimeout(timeout, TimeUnit.SECONDS)
            .build();

    H2Config h2Config = H2Config.custom()
            .setInitialWindowSize(initialWindowSize)
            .setPushEnabled(false)
            .setMaxConcurrentStreams(maxConcurrentStreams)
            .build();

    if(streamId != null){
        StreamIdGenerator.ODD.nextStreamId(Integer.parseInt(streamId));
    }

    MyH2RequesterBootstrap bootstrap = MyH2RequesterBootstrap
            .bootstrap()
            .setIOReactorConfig(ioReactorConfig)
            .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
            .setHttpProcessor(HttpProcessorBuilder.create().addAll(
                    (HttpRequestInterceptor) (request, entity, context) -> {
                        // transform legacy "Host" header (if present) to authority pseudo-header
                        Optional.ofNullable(request.getHeader(HttpHeaders.HOST)).ifPresent(header -> {
                            request.setAuthority(new URIAuthority(header.getValue()));
                            request.removeHeader(header);
                        });
                        
                        // fix Content-Length header if present and if there is any content
                        Optional.ofNullable(request.getHeader(HttpHeaders.CONTENT_LENGTH)).ifPresent(header -> {
                            request.removeHeader(header);
                            long contentLength = entity.getContentLength();
                            if(contentLength > 0 ){
                                request.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, contentLength));
                            }
                        });
                    }).build())
            .setExceptionCallback(e -> GlobalLogger.instance().getApplicationLogger().error(TextEvent.Topic.PROTOCOL, e, "Error in HTTP channel " + ChannelHttp2.this.getName()))
            .setH2Config(h2Config)
            .setStreamIdGenerator(StreamIdGenerator.ODD);


    if (secure) {
        bootstrap.setTlsStrategy(new H2ClientTlsStrategy(StackHttp2.createClientSSLContext()));
    }

    requester = bootstrap.create();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            requester.close(CloseMode.IMMEDIATE);
        }
    });

    requester.start();

    if (secure) {
        target = new HttpHost("https", hostname, port);
    } else {
        target = new HttpHost(hostname, port);
    }

    clientEndpoint = requester.connect(target, Timeout.ofSeconds(timeout)).get();

    return true;
}