Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<inceptionYear>2005</inceptionYear>
<packaging>pom</packaging>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<scm>
<connection>scm:git:https://gitbox.apache.org/repos/asf/httpcomponents-website.git</connection>
<developerConnection>scm:git:https://gitbox.apache.org/repos/asf/httpcomponents-website.git</developerConnection>
Expand Down
27 changes: 12 additions & 15 deletions samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@
<packaging>jar</packaging>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<hc4.client.version>4.5.14</hc4.client.version>
<hc5.client.version>5.6</hc5.client.version>
<jackson.version>2.20.1</jackson.version>
<asyncjson.version>0.4.0</asyncjson.version>
<hc5.core.version>5.5-beta1</hc5.core.version>
<hc5.client.version>5.6.1</hc5.client.version>
</properties>

<dependencies>
Expand All @@ -57,6 +54,16 @@
<artifactId>fluent-hc</artifactId>
<version>${hc4.client.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5-jackson2</artifactId>
<version>${hc5.core.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5-testing</artifactId>
<version>${hc5.core.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
Expand All @@ -67,16 +74,6 @@
<artifactId>httpclient5-fluent</artifactId>
<version>${hc5.client.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.github.ok2c.hc5</groupId>
<artifactId>hc5-async-json</artifactId>
<version>${asyncjson.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class HttpClient4Example {
public static void main(String... args) throws Exception {
CloseableHttpClient client = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(
SSLContexts.createSystemDefault(),
SSLContexts.createDefault(),
new String[] { "TLSv1.2" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class HttpClient5AsyncSimpleExample {
public static void main(String... args) throws Exception {
PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
.setTlsStrategy(ClientTlsStrategyBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setSslContext(SSLContexts.createDefault())
.setTlsVersions(TLS.V_1_3)
.buildAsync())
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ok2c.hc5.json.http.JsonRequestProducers;
import com.ok2c.hc5.json.http.JsonResponseConsumers;

import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.config.ConnectionConfig;
Expand All @@ -46,6 +44,8 @@
import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.jackson2.http.JsonRequestProducers;
import org.apache.hc.core5.jackson2.http.JsonResponseConsumers;
import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
import org.apache.hc.core5.pool.PoolReusePolicy;
import org.apache.hc.core5.reactor.IOReactorConfig;
Expand All @@ -58,7 +58,7 @@ public class HttpClient5AsyncStreamExample {
public static void main(String... args) throws Exception {
PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
.setTlsStrategy(ClientTlsStrategyBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setSslContext(SSLContexts.createDefault())
.setTlsVersions(TLS.V_1_3)
.buildAsync())
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
Expand Down Expand Up @@ -107,7 +107,7 @@ public static void main(String... args) throws Exception {
new org.apache.http.message.BasicNameValuePair("name2", "value2")),
objectMapper),
JsonResponseConsumers.create(jsonFactory),
new FutureCallback<Message<HttpResponse, JsonNode>>() {
new FutureCallback<>() {

@Override
public void completed(Message<HttpResponse, JsonNode> message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

package org.apache.hc.client5.migration.examples;

import java.util.Arrays;
import java.util.concurrent.Future;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ok2c.hc5.json.http.JsonRequestProducers;
import com.ok2c.hc5.json.http.JsonResponseConsumers;

import java.util.Arrays;
import java.util.concurrent.Future;
import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
Expand All @@ -43,6 +39,8 @@
import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.jackson2.http.JsonRequestProducers;
import org.apache.hc.core5.jackson2.http.JsonResponseConsumers;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.hc.core5.util.TimeValue;
Expand All @@ -53,7 +51,7 @@ public class HttpClient5AsyncStreamHttp2Example {
public static void main(String... args) throws Exception {
CloseableHttpAsyncClient client = HttpAsyncClients.customHttp2()
.setTlsStrategy(ClientTlsStrategyBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setSslContext(SSLContexts.createDefault())
.setTlsVersions(TLS.V_1_3)
.buildAsync())
.setIOReactorConfig(IOReactorConfig.custom()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class HttpClient5ClassicExample {
public static void main(String... args) throws Exception {
PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
.setTlsSocketStrategy(ClientTlsStrategyBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setSslContext(SSLContexts.createDefault())
.setTlsVersions(TLS.V_1_3)
.buildClassic())
.setDefaultSocketConfig(SocketConfig.custom()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.core5.http.examples;

import java.util.concurrent.CountDownLatch;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Message;
import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.http2.impl.nio.bootstrap.H2RequesterBootstrap;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.jackson2.http.AsyncJsonClientPipeline;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.testing.classic.LoggingConnPoolListener;
import org.apache.hc.core5.testing.nio.LoggingExceptionCallback;
import org.apache.hc.core5.testing.nio.LoggingH2StreamListener;
import org.apache.hc.core5.testing.nio.LoggingHttp1StreamListener;
import org.apache.hc.core5.testing.nio.LoggingIOSessionDecorator;
import org.apache.hc.core5.testing.nio.LoggingIOSessionListener;
import org.apache.hc.core5.testing.nio.LoggingReactorMetricsListener;
import org.apache.hc.core5.util.Timeout;

public class AsyncRequesterBootstrapExample {

public static void main(final String[] args) throws Exception {

final HttpAsyncRequester requester = H2RequesterBootstrap.bootstrap()
.setIOReactorConfig(IOReactorConfig.custom()
.setSoTimeout(Timeout.ofMinutes(1))
.build())
.setVersionPolicy(HttpVersionPolicy.NEGOTIATE)

// Pluggable logging
.setStreamListener(LoggingHttp1StreamListener.INSTANCE_CLIENT)
.setStreamListener(LoggingH2StreamListener.INSTANCE)
.setConnPoolListener(LoggingConnPoolListener.INSTANCE)
.setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE)
.setExceptionCallback(LoggingExceptionCallback.INSTANCE)
.setIOSessionListener(LoggingIOSessionListener.INSTANCE)
.setIOReactorMetricsListener(LoggingReactorMetricsListener.INSTANCE)

.create();
requester.start();

final HttpHost target = new HttpHost("httpbin.org");
final HttpCoreContext context = HttpCoreContext.create();

final ObjectMapper objectMapper = new ObjectMapper();

final CountDownLatch latch = new CountDownLatch(1);
requester.execute(
AsyncJsonClientPipeline.assemble(objectMapper)
.request()
.post(target, "/post")
.asObject(new BasicNameValuePair("name", "value"))
.response()
.asObject(RequestData.class)
.result(new FutureCallback<>() {

@Override
public void completed(final Message<HttpResponse, RequestData> m) {
final HttpResponse response = m.head();
final RequestData requestData = m.body();
System.out.println(target + "->" + response.getCode());
System.out.println(requestData);
latch.countDown();
}

@Override
public void failed(final Exception ex) {
ex.printStackTrace(System.out);
latch.countDown();
}

@Override
public void cancelled() {
latch.countDown();
}

})
.create(),
Timeout.ofMinutes(1),
context);

latch.await();

requester.close(CloseMode.GRACEFUL);
}

}
Loading