Deprecated HttpClient functionality now provided out of HttpCore

This commit is contained in:
Oleg Kalnichevski 2019-06-06 16:55:38 +02:00
parent b535a2812b
commit 8e42474a6c
31 changed files with 148 additions and 671 deletions

View File

@ -31,7 +31,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpAsyncCacheInvalidator;
import org.apache.hc.client5.http.cache.HttpAsyncCacheStorage;
@ -48,6 +47,7 @@ import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.message.RequestLine;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.util.ByteArrayBuffer;
@ -100,7 +100,7 @@ class BasicHttpAsyncCache implements HttpAsyncCache {
if (log.isDebugEnabled()) {
log.debug("Flush cache entries: " + host + "; " + new RequestLine(request));
}
if (!StandardMethods.isSafe(request.getMethod())) {
if (!Methods.isSafe(request.getMethod())) {
final String cacheKey = cacheKeyGenerator.generateKey(host, request);
return storage.removeEntry(cacheKey, new FutureCallback<Boolean>() {
@ -147,7 +147,7 @@ class BasicHttpAsyncCache implements HttpAsyncCache {
if (log.isDebugEnabled()) {
log.debug("Flush cache entries invalidated by exchange: " + host + "; " + new RequestLine(request) + " -> " + new StatusLine(response));
}
if (!StandardMethods.isSafe(request.getMethod())) {
if (!Methods.isSafe(request.getMethod())) {
return cacheInvalidator.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyGenerator, storage, callback);
}
callback.completed(Boolean.TRUE);

View File

@ -30,7 +30,6 @@ import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
@ -43,6 +42,7 @@ import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.message.RequestLine;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.util.ByteArrayBuffer;
@ -102,7 +102,7 @@ class BasicHttpCache implements HttpCache {
if (log.isDebugEnabled()) {
log.debug("Flush cache entries: " + host + "; " + new RequestLine(request));
}
if (!StandardMethods.isSafe(request.getMethod())) {
if (!Methods.isSafe(request.getMethod())) {
final String cacheKey = cacheKeyGenerator.generateKey(host, request);
try {
storage.removeEntry(cacheKey);
@ -127,7 +127,7 @@ class BasicHttpCache implements HttpCache {
if (log.isDebugEnabled()) {
log.debug("Flush cache entries invalidated by exchange: " + host + "; " + new RequestLine(request) + " -> " + new StatusLine(response));
}
if (!StandardMethods.isSafe(request.getMethod())) {
if (!Methods.isSafe(request.getMethod())) {
cacheInvalidator.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyGenerator, storage);
}
}

View File

@ -41,7 +41,6 @@ import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.classic.methods.ClassicHttpRequests;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.Configurable;
@ -56,6 +55,7 @@ import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.FileEntity;
@ -81,7 +81,7 @@ public class Request {
private SimpleDateFormat dateFormatter;
public static Request create(final StandardMethods method, final URI uri) {
public static Request create(final Methods method, final URI uri) {
return new Request(new HttpUriRequestBase(method.name(), uri));
}

View File

@ -36,7 +36,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.hc.client5.http.async.methods.AsyncRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
@ -46,8 +45,11 @@ import org.apache.hc.core5.http.ContentType;
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.Methods;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
@ -96,9 +98,8 @@ public abstract class AbstractHttpAsyncFundamentalsTest<T extends CloseableHttpA
final Random rnd = new Random(System.currentTimeMillis());
rnd.nextBytes(b1);
final Future<Message<HttpResponse, byte[]>> future = httpclient.execute(
AsyncRequestBuilder.post(target, "/echo/")
.setEntity(b1, ContentType.APPLICATION_OCTET_STREAM)
.build(),
new BasicRequestProducer(Methods.GET, target, "/echo/",
AsyncEntityProducers.create(b1, ContentType.APPLICATION_OCTET_STREAM)),
new BasicResponseConsumer<>(new BasicAsyncEntityConsumer()), HttpClientContext.create(), null);
final Message<HttpResponse, byte[]> responseMessage = future.get();
Assert.assertThat(responseMessage, CoreMatchers.notNullValue());
@ -121,9 +122,8 @@ public abstract class AbstractHttpAsyncFundamentalsTest<T extends CloseableHttpA
final Queue<Future<Message<HttpResponse, byte[]>>> queue = new LinkedList<>();
for (int i = 0; i < reqCount; i++) {
final Future<Message<HttpResponse, byte[]>> future = httpclient.execute(
AsyncRequestBuilder.post(target, "/echo/")
.setEntity(b1, ContentType.APPLICATION_OCTET_STREAM)
.build(),
new BasicRequestProducer(Methods.POST, target, "/echo/",
AsyncEntityProducers.create(b1, ContentType.APPLICATION_OCTET_STREAM)),
new BasicResponseConsumer<>(new BasicAsyncEntityConsumer()), HttpClientContext.create(), null);
queue.add(future);
}

View File

@ -34,7 +34,6 @@ import java.util.Random;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.apache.hc.client5.http.async.methods.AsyncRequestBuilder;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
@ -47,10 +46,13 @@ import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.Message;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.config.Http1Config;
import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.http2.config.H2Config;
@ -122,9 +124,8 @@ public class TestHttpAsyncMinimal extends AbstractHttpAsyncFundamentalsTest<Mini
final Queue<Future<Message<HttpResponse, byte[]>>> queue = new LinkedList<>();
for (int i = 0; i < reqCount; i++) {
final Future<Message<HttpResponse, byte[]>> future = endpoint.execute(
AsyncRequestBuilder.post(target, "/echo/")
.setEntity(b1, ContentType.APPLICATION_OCTET_STREAM)
.build(),
new BasicRequestProducer(Methods.GET, target, "/echo/",
AsyncEntityProducers.create(b1, ContentType.APPLICATION_OCTET_STREAM)),
new BasicResponseConsumer<>(new BasicAsyncEntityConsumer()), HttpClientContext.create(), null);
queue.add(future);
}

View File

@ -33,7 +33,10 @@ import java.util.Locale;
* Common HTTP methods defined by the HTTP spec.
*
* @since 5.0
*
* @deprecated Use {@link org.apache.hc.core5.http.Methods}
*/
@Deprecated
public enum StandardMethods {
GET(true, true),

View File

@ -35,11 +35,11 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.message.BasicHeader;
@ -66,7 +66,10 @@ import org.apache.hc.core5.util.TextUtils;
* </p>
*
* @since 5.0
*
* @deprecated Use {@link org.apache.hc.core5.http.nio.support.AsyncRequestBuilder}
*/
@Deprecated
public class AsyncRequestBuilder {
private HttpHost host;
@ -88,7 +91,7 @@ public class AsyncRequestBuilder {
this.method = method;
}
AsyncRequestBuilder(final StandardMethods method) {
AsyncRequestBuilder(final Methods method) {
this(method.name());
}
@ -98,18 +101,18 @@ public class AsyncRequestBuilder {
this.uri = uri;
}
AsyncRequestBuilder(final StandardMethods method, final HttpHost host, final String path) {
AsyncRequestBuilder(final Methods method, final HttpHost host, final String path) {
super();
this.method = method.name();
this.host = host;
this.path = path;
}
AsyncRequestBuilder(final StandardMethods method, final URI uri) {
AsyncRequestBuilder(final Methods method, final URI uri) {
this(method.name(), uri);
}
AsyncRequestBuilder(final StandardMethods method, final String uri) {
AsyncRequestBuilder(final Methods method, final String uri) {
this(method.name(), uri != null ? URI.create(uri) : null);
}
@ -123,131 +126,131 @@ public class AsyncRequestBuilder {
}
public static AsyncRequestBuilder get() {
return new AsyncRequestBuilder(StandardMethods.GET);
return new AsyncRequestBuilder(Methods.GET);
}
public static AsyncRequestBuilder get(final URI uri) {
return new AsyncRequestBuilder(StandardMethods.GET, uri);
return new AsyncRequestBuilder(Methods.GET, uri);
}
public static AsyncRequestBuilder get(final String uri) {
return new AsyncRequestBuilder(StandardMethods.GET, uri);
return new AsyncRequestBuilder(Methods.GET, uri);
}
public static AsyncRequestBuilder get(final HttpHost host, final String path) {
return new AsyncRequestBuilder(StandardMethods.GET, host, path);
return new AsyncRequestBuilder(Methods.GET, host, path);
}
public static AsyncRequestBuilder head() {
return new AsyncRequestBuilder(StandardMethods.HEAD);
return new AsyncRequestBuilder(Methods.HEAD);
}
public static AsyncRequestBuilder head(final URI uri) {
return new AsyncRequestBuilder(StandardMethods.HEAD, uri);
return new AsyncRequestBuilder(Methods.HEAD, uri);
}
public static AsyncRequestBuilder head(final String uri) {
return new AsyncRequestBuilder(StandardMethods.HEAD, uri);
return new AsyncRequestBuilder(Methods.HEAD, uri);
}
public static AsyncRequestBuilder head(final HttpHost host, final String path) {
return new AsyncRequestBuilder(StandardMethods.HEAD, host, path);
return new AsyncRequestBuilder(Methods.HEAD, host, path);
}
public static AsyncRequestBuilder patch() {
return new AsyncRequestBuilder(StandardMethods.PATCH);
return new AsyncRequestBuilder(Methods.PATCH);
}
public static AsyncRequestBuilder patch(final URI uri) {
return new AsyncRequestBuilder(StandardMethods.PATCH, uri);
return new AsyncRequestBuilder(Methods.PATCH, uri);
}
public static AsyncRequestBuilder patch(final String uri) {
return new AsyncRequestBuilder(StandardMethods.PATCH, uri);
return new AsyncRequestBuilder(Methods.PATCH, uri);
}
public static AsyncRequestBuilder patch(final HttpHost host, final String path) {
return new AsyncRequestBuilder(StandardMethods.PATCH, host, path);
return new AsyncRequestBuilder(Methods.PATCH, host, path);
}
public static AsyncRequestBuilder post() {
return new AsyncRequestBuilder(StandardMethods.POST);
return new AsyncRequestBuilder(Methods.POST);
}
public static AsyncRequestBuilder post(final URI uri) {
return new AsyncRequestBuilder(StandardMethods.POST, uri);
return new AsyncRequestBuilder(Methods.POST, uri);
}
public static AsyncRequestBuilder post(final String uri) {
return new AsyncRequestBuilder(StandardMethods.POST, uri);
return new AsyncRequestBuilder(Methods.POST, uri);
}
public static AsyncRequestBuilder post(final HttpHost host, final String path) {
return new AsyncRequestBuilder(StandardMethods.POST, host, path);
return new AsyncRequestBuilder(Methods.POST, host, path);
}
public static AsyncRequestBuilder put() {
return new AsyncRequestBuilder(StandardMethods.PUT);
return new AsyncRequestBuilder(Methods.PUT);
}
public static AsyncRequestBuilder put(final URI uri) {
return new AsyncRequestBuilder(StandardMethods.PUT, uri);
return new AsyncRequestBuilder(Methods.PUT, uri);
}
public static AsyncRequestBuilder put(final String uri) {
return new AsyncRequestBuilder(StandardMethods.PUT, uri);
return new AsyncRequestBuilder(Methods.PUT, uri);
}
public static AsyncRequestBuilder put(final HttpHost host, final String path) {
return new AsyncRequestBuilder(StandardMethods.PUT, host, path);
return new AsyncRequestBuilder(Methods.PUT, host, path);
}
public static AsyncRequestBuilder delete() {
return new AsyncRequestBuilder(StandardMethods.DELETE);
return new AsyncRequestBuilder(Methods.DELETE);
}
public static AsyncRequestBuilder delete(final URI uri) {
return new AsyncRequestBuilder(StandardMethods.DELETE, uri);
return new AsyncRequestBuilder(Methods.DELETE, uri);
}
public static AsyncRequestBuilder delete(final String uri) {
return new AsyncRequestBuilder(StandardMethods.DELETE, uri);
return new AsyncRequestBuilder(Methods.DELETE, uri);
}
public static AsyncRequestBuilder delete(final HttpHost host, final String path) {
return new AsyncRequestBuilder(StandardMethods.DELETE, host, path);
return new AsyncRequestBuilder(Methods.DELETE, host, path);
}
public static AsyncRequestBuilder trace() {
return new AsyncRequestBuilder(StandardMethods.TRACE);
return new AsyncRequestBuilder(Methods.TRACE);
}
public static AsyncRequestBuilder trace(final URI uri) {
return new AsyncRequestBuilder(StandardMethods.TRACE, uri);
return new AsyncRequestBuilder(Methods.TRACE, uri);
}
public static AsyncRequestBuilder trace(final String uri) {
return new AsyncRequestBuilder(StandardMethods.TRACE, uri);
return new AsyncRequestBuilder(Methods.TRACE, uri);
}
public static AsyncRequestBuilder trace(final HttpHost host, final String path) {
return new AsyncRequestBuilder(StandardMethods.TRACE, host, path);
return new AsyncRequestBuilder(Methods.TRACE, host, path);
}
public static AsyncRequestBuilder options() {
return new AsyncRequestBuilder(StandardMethods.OPTIONS);
return new AsyncRequestBuilder(Methods.OPTIONS);
}
public static AsyncRequestBuilder options(final URI uri) {
return new AsyncRequestBuilder(StandardMethods.OPTIONS, uri);
return new AsyncRequestBuilder(Methods.OPTIONS, uri);
}
public static AsyncRequestBuilder options(final String uri) {
return new AsyncRequestBuilder(StandardMethods.OPTIONS, uri);
return new AsyncRequestBuilder(Methods.OPTIONS, uri);
}
public static AsyncRequestBuilder options(final HttpHost host, final String path) {
return new AsyncRequestBuilder(StandardMethods.OPTIONS, host, path);
return new AsyncRequestBuilder(Methods.OPTIONS, host, path);
}
public AsyncRequestBuilder setCharset(final Charset charset) {
@ -411,8 +414,7 @@ public class AsyncRequestBuilder {
public AsyncRequestProducer build() {
AsyncEntityProducer entityProducerCopy = this.entityProducer;
if (parameters != null && !parameters.isEmpty()) {
if (entityProducerCopy == null && (StandardMethods.POST.name().equalsIgnoreCase(method)
|| StandardMethods.PUT.name().equalsIgnoreCase(method))) {
if (entityProducerCopy == null && (Methods.POST.isSame(method) || Methods.PUT.isSame(method))) {
final String content = URLEncodedUtils.format(
parameters,
charset != null ? charset : ContentType.APPLICATION_FORM_URLENCODED.getCharset());
@ -431,8 +433,8 @@ public class AsyncRequestBuilder {
}
}
if (entityProducerCopy != null && StandardMethods.TRACE.name().equalsIgnoreCase(method)) {
throw new IllegalStateException(StandardMethods.TRACE.name() + " requests may not include an entity.");
if (entityProducerCopy != null && Methods.isSafe(method)) {
throw new IllegalStateException(Methods.TRACE + " requests may not include an entity.");
}
final ConfigurableHttpRequest request = host != null ?

View File

@ -29,8 +29,8 @@ package org.apache.hc.client5.http.async.methods;
import java.net.URI;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.message.BasicHttpRequest;
/**
@ -43,96 +43,96 @@ public enum HttpRequests {
DELETE {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(StandardMethods.DELETE.name(), uri);
return new BasicHttpRequest(Methods.DELETE, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(StandardMethods.DELETE.name(), host, path);
return new BasicHttpRequest(Methods.DELETE, host, path);
}
},
GET {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(StandardMethods.GET.name(), uri);
return new BasicHttpRequest(Methods.GET, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(StandardMethods.GET.name(), host, path);
return new BasicHttpRequest(Methods.GET, host, path);
}
},
HEAD {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(StandardMethods.HEAD.name(), uri);
return new BasicHttpRequest(Methods.HEAD, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(StandardMethods.HEAD.name(), host, path);
return new BasicHttpRequest(Methods.HEAD, host, path);
}
},
OPTIONS {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(StandardMethods.OPTIONS.name(), uri);
return new BasicHttpRequest(Methods.OPTIONS, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(StandardMethods.OPTIONS.name(), host, path);
return new BasicHttpRequest(Methods.OPTIONS, host, path);
}
},
PATCH {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(StandardMethods.PATCH.name(), uri);
return new BasicHttpRequest(Methods.PATCH, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(StandardMethods.PATCH.name(), host, path);
return new BasicHttpRequest(Methods.PATCH, host, path);
}
},
POST {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(StandardMethods.POST.name(), uri);
return new BasicHttpRequest(Methods.POST, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(StandardMethods.POST.name(), host, path);
return new BasicHttpRequest(Methods.POST, host, path);
}
},
PUT {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(StandardMethods.PUT.name(), uri);
return new BasicHttpRequest(Methods.PUT, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(StandardMethods.PUT.name(), host, path);
return new BasicHttpRequest(Methods.PUT, host, path);
}
},
TRACE {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(StandardMethods.TRACE.name(), uri);
return new BasicHttpRequest(Methods.TRACE, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(StandardMethods.TRACE.name(), host, path);
return new BasicHttpRequest(Methods.TRACE, host, path);
}
};

View File

@ -30,11 +30,11 @@ package org.apache.hc.client5.http.async.methods;
import java.net.URI;
import java.util.Iterator;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.util.Args;
/**
@ -73,11 +73,11 @@ public final class SimpleHttpRequest extends ConfigurableHttpRequest {
super(method, requestUri);
}
SimpleHttpRequest(final StandardMethods method, final URI requestUri) {
SimpleHttpRequest(final Methods method, final URI requestUri) {
this(method.name(), requestUri);
}
SimpleHttpRequest(final StandardMethods method, final HttpHost host, final String path) {
SimpleHttpRequest(final Methods method, final HttpHost host, final String path) {
this(method.name(), host, path);
}

View File

@ -29,8 +29,8 @@ package org.apache.hc.client5.http.async.methods;
import java.net.URI;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Methods;
/**
* Common HTTP methods using {@link SimpleHttpRequest} as a HTTP request message representation.
@ -42,96 +42,96 @@ public enum SimpleHttpRequests {
DELETE {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(StandardMethods.DELETE.name(), uri);
return new SimpleHttpRequest(Methods.DELETE, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(StandardMethods.DELETE.name(), host, path);
return new SimpleHttpRequest(Methods.DELETE, host, path);
}
},
GET {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(StandardMethods.GET.name(), uri);
return new SimpleHttpRequest(Methods.GET, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(StandardMethods.GET.name(), host, path);
return new SimpleHttpRequest(Methods.GET, host, path);
}
},
HEAD {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(StandardMethods.HEAD.name(), uri);
return new SimpleHttpRequest(Methods.HEAD, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(StandardMethods.HEAD.name(), host, path);
return new SimpleHttpRequest(Methods.HEAD, host, path);
}
},
OPTIONS {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(StandardMethods.OPTIONS.name(), uri);
return new SimpleHttpRequest(Methods.OPTIONS, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(StandardMethods.OPTIONS.name(), host, path);
return new SimpleHttpRequest(Methods.OPTIONS, host, path);
}
},
PATCH {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(StandardMethods.PATCH.name(), uri);
return new SimpleHttpRequest(Methods.PATCH, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(StandardMethods.PATCH.name(), host, path);
return new SimpleHttpRequest(Methods.PATCH, host, path);
}
},
POST {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(StandardMethods.POST.name(), uri);
return new SimpleHttpRequest(Methods.POST, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(StandardMethods.POST.name(), host, path);
return new SimpleHttpRequest(Methods.POST, host, path);
}
},
PUT {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(StandardMethods.PUT.name(), uri);
return new SimpleHttpRequest(Methods.PUT, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(StandardMethods.PUT.name(), host, path);
return new SimpleHttpRequest(Methods.PUT, host, path);
}
},
TRACE {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(StandardMethods.TRACE.name(), uri);
return new SimpleHttpRequest(Methods.TRACE, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(StandardMethods.TRACE.name(), host, path);
return new SimpleHttpRequest(Methods.TRACE, host, path);
}
};

View File

@ -67,7 +67,10 @@ import org.apache.hc.core5.util.Args;
* </p>
*
* @since 4.3
*
* @deprecated Use {@link org.apache.hc.core5.http.io.support.ClassicRequestBuilder}
*/
@Deprecated
public class RequestBuilder {
private String method;

View File

@ -38,12 +38,12 @@ import java.util.Set;
import javax.net.ssl.SSLException;
import org.apache.hc.client5.http.HttpRequestRetryHandler;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.concurrent.CancellableDependency;
import org.apache.hc.core5.http.ConnectionClosedException;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.util.Args;
@ -163,7 +163,7 @@ public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
* @since 4.2
*/
protected boolean handleAsIdempotent(final HttpRequest request) {
return StandardMethods.isIdempotent(request.getMethod());
return Methods.isIdempotent(request.getMethod());
}
}

View File

@ -34,7 +34,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.async.AsyncExecCallback;
import org.apache.hc.client5.http.async.AsyncExecChain;
import org.apache.hc.client5.http.async.AsyncExecChainHandler;
@ -58,6 +57,7 @@ import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
@ -181,7 +181,7 @@ public final class AsyncProtocolExec implements AsyncExecChainHandler {
clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
httpProcessor.process(response, entityDetails, clientContext);
if (request.getMethod().equalsIgnoreCase(StandardMethods.TRACE.name())) {
if (Methods.TRACE.isSame(request.getMethod())) {
// Do not perform authentication for TRACE request
return asyncExecCallback.handleResponse(response, entityDetails);
}

View File

@ -32,7 +32,6 @@ import java.net.URI;
import org.apache.hc.client5.http.CircularRedirectException;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.RedirectException;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.async.AsyncExecCallback;
import org.apache.hc.client5.http.async.AsyncExecChain;
import org.apache.hc.client5.http.async.AsyncExecChainHandler;
@ -52,6 +51,7 @@ import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
@ -142,8 +142,8 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler {
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_SEE_OTHER:
if (!StandardMethods.isSafe(request.getMethod())) {
final HttpRequest httpGet = new BasicHttpRequest(StandardMethods.GET.name(), redirectUri);
if (!Methods.isSafe(request.getMethod())) {
final HttpRequest httpGet = new BasicHttpRequest(Methods.GET, redirectUri);
httpGet.setHeaders(scope.originalRequest.getHeaders());
state.currentRequest = httpGet;
state.currentEntityProducer = null;

View File

@ -34,7 +34,6 @@ import java.util.Iterator;
import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.auth.AuthExchange;
import org.apache.hc.client5.http.auth.ChallengeType;
import org.apache.hc.client5.http.auth.CredentialsProvider;
@ -58,6 +57,7 @@ import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
@ -167,7 +167,7 @@ public final class ProtocolExec implements ExecChainHandler {
context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
httpProcessor.process(response, response.getEntity(), context);
if (request.getMethod().equalsIgnoreCase(StandardMethods.TRACE.name())) {
if (Methods.TRACE.isSame(request.getMethod())) {
// Do not perform authentication for TRACE request
return response;
}

View File

@ -33,12 +33,10 @@ import java.net.URI;
import org.apache.hc.client5.http.CircularRedirectException;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.RedirectException;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.auth.AuthExchange;
import org.apache.hc.client5.http.classic.ExecChain;
import org.apache.hc.client5.http.classic.ExecChainHandler;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.RequestBuilder;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.protocol.RedirectLocations;
@ -54,8 +52,10 @@ import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.LangUtils;
import org.slf4j.Logger;
@ -146,7 +146,7 @@ public final class RedirectExec implements ExecChainHandler {
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_SEE_OTHER:
if (!StandardMethods.isSafe(request.getMethod())) {
if (!Methods.isSafe(request.getMethod())) {
final HttpGet httpGet = new HttpGet(redirectUri);
httpGet.setHeaders(originalRequest.getHeaders());
redirect = httpGet;
@ -155,7 +155,8 @@ public final class RedirectExec implements ExecChainHandler {
}
}
if (redirect == null) {
redirect = RequestBuilder.copy(originalRequest).setUri(redirectUri).build();
redirect = new BasicClassicHttpRequest(originalRequest.getMethod(), redirectUri);
redirect.setEntity(originalRequest.getEntity());
}
final HttpHost newTarget = URIUtils.extractHost(redirectUri);

View File

@ -33,6 +33,7 @@ import javax.net.ssl.SSLEngine;
import org.apache.hc.core5.function.Factory;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
import org.apache.hc.core5.reactor.ssl.TlsDetails;
import org.apache.hc.core5.ssl.SSLContexts;

View File

@ -30,6 +30,7 @@ package org.apache.hc.client5.http.ssl;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.ssl.SSLContexts;
/**

View File

@ -29,13 +29,15 @@ package org.apache.hc.client5.http.ssl;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.message.ParserCursor;
/**
* Supported {@code TLS} protocol versions.
*
* @since 5.0
*
* @deprecated Use {@link org.apache.hc.core5.http.ssl.TLS}
*/
@Deprecated
public enum TLS {
V_1_0("TLSv1", new ProtocolVersion("TLS", 1, 0)),
@ -68,11 +70,7 @@ public enum TLS {
}
public static ProtocolVersion parse(final String s) throws ParseException {
if (s == null) {
return null;
}
final ParserCursor cursor = new ParserCursor(0, s.length());
return TlsVersionParser.INSTANCE.parse(s, cursor, null);
return org.apache.hc.core5.http.ssl.TLS.parse(s);
}
}

View File

@ -41,6 +41,7 @@ import javax.security.auth.x500.X500Principal;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.ssl.TLS;
import org.slf4j.Logger;
final class TlsSessionValidator {

View File

@ -1,105 +0,0 @@
/*
* ====================================================================
* 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.client5.http.ssl;
import java.util.BitSet;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.message.ParserCursor;
import org.apache.hc.core5.http.message.TokenParser;
final class TlsVersionParser {
public final static TlsVersionParser INSTANCE = new TlsVersionParser();
private final TokenParser tokenParser;
TlsVersionParser() {
this.tokenParser = TokenParser.INSTANCE;
}
ProtocolVersion parse(
final CharSequence buffer,
final ParserCursor cursor,
final BitSet delimiters) throws ParseException {
final int lowerBound = cursor.getLowerBound();
final int upperBound = cursor.getUpperBound();
int pos = cursor.getPos();
if (pos + 4 > cursor.getUpperBound()) {
throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
}
if (buffer.charAt(pos) != 'T' || buffer.charAt(pos + 1) != 'L' || buffer.charAt(pos + 2) != 'S'
|| buffer.charAt(pos + 3) != 'v') {
throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
}
pos = pos + 4;
cursor.updatePos(pos);
if (cursor.atEnd()) {
throw new ParseException("Invalid TLS version", buffer, lowerBound, upperBound, pos);
}
final String s = this.tokenParser.parseToken(buffer, cursor, delimiters);
final int idx = s.indexOf('.');
if (idx == -1) {
final int major;
try {
major = Integer.parseInt(s);
} catch (final NumberFormatException e) {
throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos);
}
return new ProtocolVersion("TLS", major, 0);
} else {
final String s1 = s.substring(0, idx);
final int major;
try {
major = Integer.parseInt(s1);
} catch (final NumberFormatException e) {
throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos);
}
final String s2 = s.substring(idx + 1);
final int minor;
try {
minor = Integer.parseInt(s2);
} catch (final NumberFormatException e) {
throw new ParseException("Invalid TLS minor version", buffer, lowerBound, upperBound, pos);
}
return new ProtocolVersion("TLS", major, minor);
}
}
ProtocolVersion parse(final String s) throws ParseException {
if (s == null) {
return null;
}
final ParserCursor cursor = new ParserCursor(0, s.length());
return parse(s, cursor, null);
}
}

View File

@ -1,42 +0,0 @@
/*
* ====================================================================
* 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.client5.http.async.methods;
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
import org.junit.Test;
public class TestAsyncRequestBuilder {
@Test(expected = IllegalStateException.class)
public void testBuildTraceWithEntity() {
final AsyncRequestBuilder builder = AsyncRequestBuilder.create("TRACE").setUri("/path");
builder.setEntity(new BasicAsyncEntityProducer("stuff"));
builder.build();
}
}

View File

@ -1,296 +0,0 @@
/*
* ====================================================================
* 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.client5.http.classic.methods;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.hc.client5.http.config.Configurable;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.junit.Assert;
import org.junit.Test;
public class TestRequestBuilder {
@Test
public void testBasicGet() throws Exception {
final ClassicHttpRequest request = RequestBuilder.get().build();
Assert.assertNotNull(request);
Assert.assertEquals("GET", request.getMethod());
Assert.assertEquals(URI.create("/"), request.getUri());
}
@Test
public void testArbitraryMethod() throws Exception {
final ClassicHttpRequest request = RequestBuilder.create("Whatever").build();
Assert.assertNotNull(request);
Assert.assertEquals("Whatever", request.getMethod());
Assert.assertEquals(URI.create("/"), request.getUri());
}
@Test
public void testBasicWithEntity() throws Exception {
final HttpEntity entity = new StringEntity("whatever");
final ClassicHttpRequest request = RequestBuilder.post().setEntity(entity).build();
Assert.assertNotNull(request);
Assert.assertEquals("POST", request.getMethod());
Assert.assertEquals(URI.create("/"), request.getUri());
Assert.assertSame(entity, request.getEntity());
}
@Test
public void testGetWithEntity() throws Exception {
final HttpEntity entity = new StringEntity("whatever");
final ClassicHttpRequest request = RequestBuilder.get().setEntity(entity).build();
Assert.assertNotNull(request);
Assert.assertEquals("GET", request.getMethod());
Assert.assertEquals(URI.create("/"), request.getUri());
Assert.assertSame(entity, request.getEntity());
}
@Test
public void testAddParameters1() throws Exception {
final ClassicHttpRequest request = RequestBuilder.get()
.addParameter("p1", "this")
.addParameter("p2", "that")
.build();
Assert.assertEquals(new URI("/?p1=this&p2=that"), request.getUri());
}
@Test
public void testAddParameters2() throws Exception {
final ClassicHttpRequest request = RequestBuilder.get()
.addParameter("p1", "this")
.addParameters(new BasicNameValuePair("p2", "that"))
.build();
Assert.assertEquals(new URI("/?p1=this&p2=that"), request.getUri());
}
@Test
public void testAddParameters3() throws Exception {
final ClassicHttpRequest request = RequestBuilder.post()
.addParameter("p1", "this")
.addParameter("p2", "that")
.build();
final HttpEntity entity = request.getEntity();
Assert.assertNotNull(entity);
Assert.assertEquals(new URI("/"), request.getUri());
Assert.assertEquals("p1=this&p2=that", EntityUtils.toString(entity));
}
@Test
public void testAddParameters4() throws Exception {
final ClassicHttpRequest request = RequestBuilder.post()
.setUri("http://targethost/?blah")
.addParameter("p1", "this")
.addParameter("p2", "that")
.setEntity(new StringEntity("blah"))
.build();
Assert.assertEquals(new URI("http://targethost/?blah&p1=this&p2=that"), request.getUri());
}
@Test
public void testCopy() throws Exception {
final HttpEntity entity = new StringEntity("stuff");
final RequestConfig config = RequestConfig.custom().build();
final ClassicHttpRequest request = RequestBuilder.put()
.setUri(URI.create("/stuff"))
.setVersion(HttpVersion.HTTP_1_0)
.addHeader("header1", "stuff")
.setHeader("header2", "more stuff")
.setEntity(entity)
.setConfig(config)
.build();
Assert.assertNotNull(request);
Assert.assertEquals("PUT", request.getMethod());
Assert.assertEquals(URI.create("/stuff"), request.getUri());
Assert.assertEquals(HttpVersion.HTTP_1_0, request.getVersion());
final ClassicHttpRequest copy = RequestBuilder.copy(request).setUri("/other-stuff").build();
Assert.assertEquals("PUT", copy.getMethod());
Assert.assertEquals(URI.create("/other-stuff"), copy.getUri());
Assert.assertSame(entity, copy.getEntity());
Assert.assertTrue(copy instanceof Configurable);
Assert.assertSame(config, ((Configurable) copy).getConfig());
}
@Test
public void testCopyWithQueryParams() throws Exception {
final HttpGet get = new HttpGet("/stuff?p1=this&p2=that");
final RequestBuilder builder = RequestBuilder.copy(get).setEntity(new StringEntity(""));
final List<NameValuePair> parameters = builder.getParameters();
Assert.assertNotNull(parameters);
Assert.assertEquals(0, parameters.size());
Assert.assertEquals(new URI("/stuff?p1=this&p2=that"), builder.getUri());
}
@Test
public void testCopyWithFormParams() throws Exception {
final HttpPost post = new HttpPost("/stuff?p1=wtf");
post.setEntity(new StringEntity("p1=this&p2=that", ContentType.APPLICATION_FORM_URLENCODED));
final RequestBuilder builder = RequestBuilder.copy(post);
final List<NameValuePair> parameters = builder.getParameters();
Assert.assertNotNull(parameters);
Assert.assertEquals(2, parameters.size());
assertNameValuePair(new BasicNameValuePair("p1", "this"), parameters.get(0));
assertNameValuePair(new BasicNameValuePair("p2", "that"), parameters.get(1));
Assert.assertEquals(new URI("/stuff?p1=wtf"), builder.getUri());
Assert.assertNull(builder.getEntity());
}
private static void assertNameValuePair (
final NameValuePair expected,
final NameValuePair result) {
Assert.assertNotNull(result);
Assert.assertEquals(expected.getName(), result.getName());
Assert.assertEquals(expected.getValue(), result.getValue());
}
@Test
public void testCopyWithStringEntity() throws Exception {
final HttpPost post = new HttpPost("/stuff?p1=wtf");
final HttpEntity entity = new StringEntity("p1=this&p2=that", ContentType.TEXT_PLAIN);
post.setEntity(entity);
final RequestBuilder builder = RequestBuilder.copy(post);
final List<NameValuePair> parameters = builder.getParameters();
Assert.assertNotNull(parameters);
Assert.assertEquals(0, parameters.size());
Assert.assertEquals(new URI("/stuff?p1=wtf"), builder.getUri());
Assert.assertSame(entity, builder.getEntity());
}
@Test
public void testCopyAndSetUri() throws Exception {
final URI uri1 = URI.create("http://host1.com/path?param=something");
final URI uri2 = URI.create("http://host2.com/path?param=somethingdifferent");
final HttpGet request1 = new HttpGet(uri1);
final ClassicHttpRequest request2 = RequestBuilder.copy(request1).setUri(uri2).build();
Assert.assertEquals(request2.getUri(), uri2);
}
@Test
public void testGettersAndMutators() throws Exception {
final HttpEntity entity = new StringEntity("stuff");
final RequestConfig config = RequestConfig.custom().build();
final Header h1 = new BasicHeader("header1", "stuff");
final Header h2 = new BasicHeader("header1", "more-stuff");
final RequestBuilder builder = RequestBuilder.put()
.setUri("/stuff")
.setVersion(HttpVersion.HTTP_1_0)
.addHeader(h1)
.addHeader(h2)
.setEntity(entity)
.setConfig(config);
Assert.assertEquals("PUT", builder.getMethod());
Assert.assertEquals(URI.create("/stuff"), builder.getUri());
Assert.assertEquals(HttpVersion.HTTP_1_0, builder.getVersion());
Assert.assertSame(h1, builder.getFirstHeader("header1"));
Assert.assertSame(h2, builder.getLastHeader("header1"));
Assert.assertEquals(2, builder.getHeaders("header1").length);
Assert.assertSame(entity, builder.getEntity());
Assert.assertSame(config, builder.getConfig());
builder.setUri((String) null)
.setVersion(null)
.removeHeader(h1)
.removeHeaders("header1")
.removeHeader(h2)
.setEntity(null)
.setConfig(null);
Assert.assertEquals(null, builder.getUri());
Assert.assertEquals(null, builder.getVersion());
Assert.assertSame(null, builder.getFirstHeader("header1"));
Assert.assertSame(null, builder.getLastHeader("header1"));
Assert.assertEquals(0, builder.getHeaders("header1").length);
Assert.assertSame(null, builder.getEntity());
Assert.assertSame(null, builder.getConfig());
builder.setHeader(h2)
.setHeader("header1", "a-lot-more-stuff");
Assert.assertSame("a-lot-more-stuff", builder.getLastHeader("header1").getValue());
Assert.assertEquals(1, builder.getHeaders("header1").length);
}
@Test(expected=IllegalArgumentException.class)
public void testCopyNull() throws Exception {
RequestBuilder.copy(null);
}
@Test
public void testBuildGETwithUTF8() throws Exception {
assertBuild(StandardCharsets.UTF_8);
}
@Test
public void testBuildGETwithISO88591() throws Exception {
assertBuild(StandardCharsets.ISO_8859_1);
}
@Test(expected = IllegalStateException.class)
public void testBuildTraceWithEntity() {
final RequestBuilder requestBuilder = RequestBuilder.create("TRACE").setUri("/path");
requestBuilder.setEntity(new StringEntity("foo"));
requestBuilder.build();
}
private void assertBuild(final Charset charset) throws Exception {
final RequestBuilder requestBuilder = RequestBuilder.create("GET").setCharset(charset);
requestBuilder.setUri("https://somehost.com/stuff");
requestBuilder.addParameters(createParameters());
final String encodedData1 = URLEncoder.encode("\"1\u00aa position\"", charset.displayName());
final String encodedData2 = URLEncoder.encode("Jos\u00e9 Abra\u00e3o", charset.displayName());
final String uriExpected = String.format("https://somehost.com/stuff?parameter1=value1&parameter2=%s&parameter3=%s", encodedData1, encodedData2);
final ClassicHttpRequest request = requestBuilder.build();
Assert.assertEquals(uriExpected, request.getUri().toString());
}
private NameValuePair[] createParameters() {
final NameValuePair parameters[] = new NameValuePair[3];
parameters[0] = new BasicNameValuePair("parameter1", "value1");
parameters[1] = new BasicNameValuePair("parameter2", "\"1\u00aa position\"");
parameters[2] = new BasicNameValuePair("parameter3", "Jos\u00e9 Abra\u00e3o");
return parameters;
}
}

View File

@ -33,7 +33,6 @@ import java.util.concurrent.Future;
import org.apache.hc.client5.http.async.methods.AbstractBinPushConsumer;
import org.apache.hc.client5.http.async.methods.AbstractCharResponseConsumer;
import org.apache.hc.client5.http.async.methods.AsyncRequestBuilder;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.core5.function.Supplier;
@ -42,8 +41,10 @@ import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.http2.config.H2Config;
import org.apache.hc.core5.io.CloseMode;
@ -117,7 +118,7 @@ public class AsyncClientHttp2ServerPush {
final HttpHost target = new HttpHost("nghttp2.org");
final String requestURI = "/httpbin/";
final Future<Void> future = client.execute(
AsyncRequestBuilder.get(target, requestURI).build(),
new BasicRequestProducer(Methods.GET, target, requestURI),
new AbstractCharResponseConsumer<Void>() {
@Override

View File

@ -31,14 +31,15 @@ import java.nio.CharBuffer;
import java.util.concurrent.Future;
import org.apache.hc.client5.http.async.methods.AbstractCharResponseConsumer;
import org.apache.hc.client5.http.async.methods.AsyncRequestBuilder;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.util.Timeout;
@ -65,7 +66,7 @@ public class AsyncClientHttpExchangeStreaming {
for (final String requestUri: requestUris) {
final Future<Void> future = client.execute(
AsyncRequestBuilder.get(target, requestUri).build(),
new BasicRequestProducer(Methods.GET, target, requestUri),
new AbstractCharResponseConsumer<Void>() {
@Override

View File

@ -33,7 +33,6 @@ import java.util.concurrent.Future;
import org.apache.hc.client5.http.async.AsyncExecCallback;
import org.apache.hc.client5.http.async.AsyncExecChain;
import org.apache.hc.client5.http.async.AsyncExecChainHandler;
import org.apache.hc.client5.http.async.methods.AsyncRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
import org.apache.hc.client5.http.impl.ChainElements;
@ -47,6 +46,7 @@ import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
import org.apache.hc.core5.http.nio.entity.DigestingEntityProducer;
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.util.Timeout;

View File

@ -34,7 +34,6 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import org.apache.hc.client5.http.async.methods.AbstractCharResponseConsumer;
import org.apache.hc.client5.http.async.methods.AsyncRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
@ -45,6 +44,7 @@ import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
public class AsyncQuickStart {

View File

@ -41,8 +41,8 @@ import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.client5.http.ssl.TLS;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.hc.core5.ssl.TrustStrategy;

View File

@ -30,7 +30,6 @@ import java.net.URI;
import java.util.List;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.RequestBuilder;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
@ -39,6 +38,7 @@ import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
/**
* A example that demonstrates how HttpClient APIs can be used to perform
@ -69,7 +69,7 @@ public class ClientFormLogin {
}
}
final ClassicHttpRequest login = RequestBuilder.post()
final ClassicHttpRequest login = ClassicRequestBuilder.post()
.setUri(new URI("https://someportal/"))
.addParameter("IDToken1", "username")
.addParameter("IDToken2", "password")

View File

@ -27,7 +27,6 @@
package org.apache.hc.client5.http.impl.classic;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.client5.http.classic.ExecChain;
import org.apache.hc.client5.http.classic.ExecRuntime;
import org.apache.hc.client5.http.config.RequestConfig;
@ -40,6 +39,7 @@ import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Methods;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
@ -77,7 +77,7 @@ public class TestContentCompressionExec {
@Test
public void testContentEncodingNoEntity() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
@ -90,7 +90,7 @@ public class TestContentCompressionExec {
@Test
public void testNoContentEncoding() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final StringEntity original = new StringEntity("plain stuff");
response.setEntity(original);
@ -106,7 +106,7 @@ public class TestContentCompressionExec {
@Test
public void testGzipContentEncoding() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("GZip").build();
response.setEntity(original);
@ -122,7 +122,7 @@ public class TestContentCompressionExec {
@Test
public void testGzipContentEncodingZeroLength() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final HttpEntity original = EntityBuilder.create().setText("").setContentEncoding("GZip").build();
response.setEntity(original);
@ -138,7 +138,7 @@ public class TestContentCompressionExec {
@Test
public void testXGzipContentEncoding() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("x-gzip").build();
response.setEntity(original);
@ -154,7 +154,7 @@ public class TestContentCompressionExec {
@Test
public void testDeflateContentEncoding() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("deFlaTe").build();
response.setEntity(original);
@ -170,7 +170,7 @@ public class TestContentCompressionExec {
@Test
public void testIdentityContentEncoding() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("identity").build();
response.setEntity(original);
@ -186,7 +186,7 @@ public class TestContentCompressionExec {
@Test(expected=HttpException.class)
public void testUnknownContentEncoding() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("whatever").build();
response.setEntity(original);
@ -200,7 +200,7 @@ public class TestContentCompressionExec {
@Test
public void testContentEncodingRequestParameter() throws Exception {
final ClassicHttpRequest request = new BasicClassicHttpRequest(StandardMethods.GET.name(), host, "/");
final ClassicHttpRequest request = new BasicClassicHttpRequest(Methods.GET, host, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("GZip").build();
response.setEntity(original);

View File

@ -1,93 +0,0 @@
/*
* ====================================================================
* 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.client5.http.ssl;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.message.ParserCursor;
import org.apache.hc.core5.http.message.TokenParser;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Unit tests for {@link TlsVersionParser}.
*/
public class TestTlsVersionParser {
private TlsVersionParser impl;
@Before
public void setup() {
impl = new TlsVersionParser();
}
@Test
public void testParseBasic() throws Exception {
Assert.assertThat(impl.parse("TLSv1"), CoreMatchers.equalTo(TLS.V_1_0.version));
Assert.assertThat(impl.parse("TLSv1.1"), CoreMatchers.equalTo(TLS.V_1_1.version));
Assert.assertThat(impl.parse("TLSv1.2"), CoreMatchers.equalTo(TLS.V_1_2.version));
Assert.assertThat(impl.parse("TLSv1.3"), CoreMatchers.equalTo(TLS.V_1_3.version));
Assert.assertThat(impl.parse("TLSv22.356"), CoreMatchers.equalTo(new ProtocolVersion("TLS", 22, 356)));
}
@Test
public void testParseBuffer() throws Exception {
final ParserCursor cursor = new ParserCursor(1, 13);
Assert.assertThat(impl.parse(" TLSv1.2,0000", cursor, TokenParser.INIT_BITSET(',')),
CoreMatchers.equalTo(TLS.V_1_2.version));
Assert.assertThat(cursor.getPos(), CoreMatchers.equalTo(8));
}
@Test(expected = ParseException.class)
public void testParseFailure1() throws Exception {
impl.parse("Tlsv1");
}
@Test(expected = ParseException.class)
public void testParseFailure2() throws Exception {
impl.parse("TLSV1");
}
@Test(expected = ParseException.class)
public void testParseFailure3() throws Exception {
impl.parse("TLSv");
}
@Test(expected = ParseException.class)
public void testParseFailure4() throws Exception {
impl.parse("TLSv1A");
}
@Test(expected = ParseException.class)
public void testParseFailure5() throws Exception {
impl.parse("TLSv1.A");
}
}