Skip to content

Commit

Permalink
XP-230 http clinet 4 -> 5
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiravka committed Sep 11, 2024
1 parent 3369c25 commit 217ed55
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 71 deletions.
22 changes: 11 additions & 11 deletions oap-http/oap-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${oap.deps.apache.httpcomponents.version}</version>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>${oap.deps.apache.httpcore5.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${oap.deps.apache.httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-nio</artifactId>
<version>${oap.deps.apache.httpcomponents.version}</version>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${oap.deps.apache.httpclient5.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpcore-nio</artifactId>-->
<!-- <version>${oap.deps.apache.httpcore-nio.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
Expand Down
107 changes: 51 additions & 56 deletions oap-http/oap-http/src/main/java/oap/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,44 @@
import oap.util.function.Try;
import oap.util.function.Try.ThrowingRunnable;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.cookie.StandardCookieSpec;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
import org.apache.hc.client5.http.psl.PublicSuffixMatcherLoader;
import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.message.BasicNameValuePair;
import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.http.nio.conn.NoopIOSessionStrategy;
import org.apache.http.nio.conn.SchemeIOSessionStrategy;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.nio.reactor.IOReactorException;
import org.apache.http.ssl.SSLContexts;
import org.apache.hc.core5.reactor.IOReactorShutdownException;
import org.apache.hc.core5.ssl.SSLContexts;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -96,7 +96,6 @@
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.file.Path;
import java.util.Date;
Expand Down Expand Up @@ -128,9 +127,9 @@ public final class Client implements Closeable, AutoCloseable {
.onTimeout( c -> log.error( "timeout" ) )
.build();
public static final String NO_RESPONSE = "no response";
private static final FutureCallback<org.apache.http.HttpResponse> FUTURE_CALLBACK = new FutureCallback<>() {
private static final FutureCallback<HttpResponse> FUTURE_CALLBACK = new FutureCallback<>() {
@Override
public void completed( org.apache.http.HttpResponse result ) {
public void completed( HttpResponse result ) {
}

@Override
Expand Down Expand Up @@ -161,8 +160,8 @@ public static ClientBuilder custom() {
return new ClientBuilder( null, null, Dates.m( 1 ), Dates.m( 5 ) );
}

private static List<Pair<String, String>> headers( org.apache.http.HttpResponse response ) {
return Stream.of( response.getAllHeaders() )
private static List<Pair<String, String>> headers( HttpResponse response ) {
return Stream.of( response.getHeaders() )
.map( h -> Pair.__( h.getName(), h.getValue() ) )
.toList();
}
Expand Down Expand Up @@ -228,17 +227,13 @@ public Result<Response, Throwable> post( String uri, Map<String, Object> params,
}

public Result<Response, Throwable> post( String uri, Map<String, Object> params, Map<String, Object> headers, long timeout ) {
try {
var request = new HttpPost( uri );
request.setEntity( new UrlEncodedFormEntity( Stream.of( params.entrySet() )
.<NameValuePair>map( e -> new BasicNameValuePair( e.getKey(),
e.getValue() == null ? "" : e.getValue().toString() ) )
.toList()
) );
return getResponse( request, Math.max( builder.timeout, timeout ), execute( request, headers ) );
} catch( UnsupportedEncodingException e ) {
throw new UncheckedIOException( e );
}
var request = new HttpPost( uri );
request.setEntity( new UrlEncodedFormEntity( Stream.of( params.entrySet() )
.<NameValuePair>map( e -> new BasicNameValuePair( e.getKey(),
e.getValue() == null ? "" : e.getValue().toString() ) )
.toList()
) );
return getResponse( request, Math.max( builder.timeout, timeout ), execute( request, headers ) );
}

public Response post( String uri, String content, String contentType ) {
Expand Down Expand Up @@ -319,11 +314,11 @@ public Response post( String uri, byte[] content, String contentType, Map<String
.orElseThrow( Throwables::propagate );
}

private Result<Response, Throwable> getResponse( HttpRequestBase request, long timeout, CompletableFuture<Response> future ) {
private Result<Response, Throwable> getResponse( HttpUriRequestBase request, long timeout, CompletableFuture<Response> future ) {
try {
return Result.success( timeout == 0 ? future.get() : future.get( timeout, MILLISECONDS ) );
} catch( ExecutionException e ) {
var newEx = new UncheckedIOException( request.getURI().toString(), new IOException( e.getCause().getMessage(), e.getCause() ) );
var newEx = new UncheckedIOException( request.getRequestUri(), new IOException( e.getCause().getMessage(), e.getCause() ) );
builder.onError.accept( this, newEx );
return Result.failure( e.getCause() );
} catch( TimeoutException e ) {
Expand Down Expand Up @@ -696,7 +691,7 @@ public static class ClientBuilder extends AsyncCallbacks<ClientBuilder, Client>
private int maxConnTotal = 10000;
private int maxConnPerRoute = 1000;
private boolean redirectsEnabled = false;
private String cookieSpec = CookieSpecs.STANDARD;
private String cookieSpec = StandardCookieSpec.RELAXED;

public ClientBuilder( Path certificateLocation, String certificatePassword, long connectTimeout, long timeout ) {
cookieStore = new BasicCookieStore();
Expand Down Expand Up @@ -748,7 +743,7 @@ private HttpAsyncClientBuilder initialize() {
.setCookieSpec( cookieSpec )
.build() )
.setDefaultCookieStore( cookieStore );
} catch( IOReactorException e ) {
} catch( IOReactorShutdownException e ) {
throw new UncheckedIOException( e );
}
}
Expand Down Expand Up @@ -796,12 +791,12 @@ public Client build() {

public class OutputStreamWithResponse extends OutputStream implements Closeable, AutoCloseable {
private final CompletableFuture<Response> completableFuture;
private final HttpRequestBase request;
private final HttpUriRequestBase request;
private final long timeout;
private PipedOutputStream pos;
private Response response;

public OutputStreamWithResponse( PipedOutputStream pos, CompletableFuture<Response> completableFuture, HttpRequestBase request, long timeout ) {
public OutputStreamWithResponse( PipedOutputStream pos, CompletableFuture<Response> completableFuture, HttpUriRequestBase request, long timeout ) {
this.pos = pos;
this.completableFuture = completableFuture;
this.request = request;
Expand Down
2 changes: 1 addition & 1 deletion oap-http/oap-http/src/main/java/oap/http/Uri.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import lombok.extern.slf4j.Slf4j;
import oap.util.Maps;
import oap.util.Pair;
import org.apache.http.client.utils.URIBuilder;
import org.apache.hc.core5.net.URIBuilder;
import org.yaml.snakeyaml.util.UriEncoder;

import java.net.URI;
Expand Down
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@
</distributionManagement>

<properties>
<oap.project.version>22.4.18</oap.project.version>
<oap.project.version>23.0.0</oap.project.version>

<oap.deps.config.version>21.0.0</oap.deps.config.version>
<oap.deps.oap-teamcity.version>21.0.1</oap.deps.oap-teamcity.version>

<oap.deps.mockito.version>5.11.0</oap.deps.mockito.version>

<oap.deps.apache.httpcomponents.version>4.4.16</oap.deps.apache.httpcomponents.version>
<oap.deps.apache.httpclient.version>4.5.14</oap.deps.apache.httpclient.version>
<oap.deps.apache.httpclient5.version>5.3.1</oap.deps.apache.httpclient5.version>
<oap.deps.apache.httpcore5.version>5.2.5</oap.deps.apache.httpcore5.version>
<oap.deps.apache.httpcore-nio.version>4.4.16</oap.deps.apache.httpcore-nio.version>
<oap.deps.apache.httpasyncclient.version>4.1.5</oap.deps.apache.httpasyncclient.version>
<oap.deps.apache.httpmime.version>4.5.14</oap.deps.apache.httpmime.version>

Expand Down

0 comments on commit 217ed55

Please sign in to comment.