Redesign of SimpleHttpRequest / SimpleHttpResponse APIs

This commit is contained in:
Oleg Kalnichevski 2017-09-28 15:41:06 +02:00
parent 5203c66928
commit e8972624ac
18 changed files with 507 additions and 179 deletions

View File

@ -0,0 +1,140 @@
/*
* ====================================================================
* 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.cache;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.util.Args;
public final class HttpCacheResponse implements EntityDetails {
private final int code;
private final String reasonPhrase;
private final byte[] body;
private final ContentType contentType;
public static HttpCacheResponse create(
final int code,
final String reasonPhrase,
final byte[] body,
final ContentType contentType) {
return new HttpCacheResponse(code, reasonPhrase, body, contentType);
}
public static HttpCacheResponse create(
final int code,
final byte[] body,
final ContentType contentType) {
return new HttpCacheResponse(code, null, body, contentType);
}
public static HttpCacheResponse create(
final int code,
final String reasonPhrase) {
return new HttpCacheResponse(code, reasonPhrase, null, null);
}
public static HttpCacheResponse create(final int code) {
return new HttpCacheResponse(code, null, null, null);
}
public static HttpCacheResponse create(
final int code,
final String reasonPhrase,
final String body,
final ContentType contentType) {
if (body != null) {
final Charset charset = contentType != null ? contentType.getCharset() : null;
final byte[] b = body.getBytes(charset != null ? charset : StandardCharsets.US_ASCII);
return new HttpCacheResponse(code, reasonPhrase, b, contentType);
} else {
return create(code, reasonPhrase);
}
}
public static HttpCacheResponse create(
final int code,
final String body,
final ContentType contentType) {
return create(code, null, body, contentType);
}
private HttpCacheResponse(
final int code,
final String reasonPhrase,
final byte[] body,
final ContentType contentType) {
this.code = Args.checkRange(code, 200, 599, "HTTP status");
this.reasonPhrase = reasonPhrase;
this.body = body;
this.contentType = contentType;
}
public int getCode() {
return code;
}
public String getReasonPhrase() {
return reasonPhrase;
}
public byte[] getBody() {
return body;
}
@Override
public long getContentLength() {
return body != null ? body.length : 0;
}
@Override
public String getContentType() {
return contentType != null ? contentType.toString() : null;
}
@Override
public String getContentEncoding() {
return null;
}
@Override
public boolean isChunked() {
return false;
}
@Override
public Set<String> getTrailerNames() {
return null;
}
}

View File

@ -28,15 +28,18 @@ package org.apache.hc.client5.testing.async;
import java.io.IOException; import java.io.IOException;
import org.apache.hc.client5.http.async.methods.SimpleBody;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpException; import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.AsyncRequestConsumer; import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
import org.apache.hc.core5.http.nio.AsyncServerRequestHandler; import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
import org.apache.hc.core5.http.nio.BasicResponseProducer; import org.apache.hc.core5.http.nio.BasicResponseProducer;
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer; import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer; import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
import org.apache.hc.core5.http.nio.support.AbstractAsyncRequesterConsumer; import org.apache.hc.core5.http.nio.support.AbstractAsyncRequesterConsumer;
import org.apache.hc.core5.http.nio.support.AbstractServerExchangeHandler; import org.apache.hc.core5.http.nio.support.AbstractServerExchangeHandler;
@ -51,14 +54,18 @@ public abstract class AbstractSimpleServerExchangeHandler extends AbstractServer
protected final AsyncRequestConsumer<SimpleHttpRequest> supplyConsumer( protected final AsyncRequestConsumer<SimpleHttpRequest> supplyConsumer(
final HttpRequest request, final HttpRequest request,
final HttpContext context) throws HttpException { final HttpContext context) throws HttpException {
return new AbstractAsyncRequesterConsumer<SimpleHttpRequest, String>(new StringAsyncEntityConsumer()) { return new AbstractAsyncRequesterConsumer<SimpleHttpRequest, byte[]>(new BasicAsyncEntityConsumer()) {
@Override @Override
protected SimpleHttpRequest buildResult( protected SimpleHttpRequest buildResult(
final HttpRequest request, final HttpRequest request,
final String entity, final byte[] body,
final ContentType contentType) { final ContentType contentType) {
return new SimpleHttpRequest(request, entity, contentType); final SimpleHttpRequest simpleRequest = SimpleHttpRequest.copy(request);
if (body != null) {
simpleRequest.setBodyBytes(body, contentType);
}
return simpleRequest;
} }
}; };
@ -70,9 +77,18 @@ public abstract class AbstractSimpleServerExchangeHandler extends AbstractServer
final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final AsyncServerRequestHandler.ResponseTrigger responseTrigger,
final HttpContext context) throws HttpException, IOException { final HttpContext context) throws HttpException, IOException {
final SimpleHttpResponse response = handle(request, HttpCoreContext.adapt(context)); final SimpleHttpResponse response = handle(request, HttpCoreContext.adapt(context));
responseTrigger.submitResponse(new BasicResponseProducer( final SimpleBody body = response.getBody();
response, final AsyncEntityProducer entityProducer;
response.getBody() != null ? new StringAsyncEntityProducer(response.getBody(), response.getContentType()) : null)); if (body != null) {
if (body.isText()) {
entityProducer = new StringAsyncEntityProducer(body.getBodyText(), body.getContentType());
} else {
entityProducer = new BasicAsyncEntityProducer(body.getBodyBytes(), body.getContentType());
}
} else {
entityProducer = null;
}
responseTrigger.submitResponse(new BasicResponseProducer(response, entityProducer));
} }

View File

@ -122,9 +122,11 @@ public class TestAsyncRedirects extends IntegrationTestBase {
} }
return response; return response;
} else if (path.equals("/newlocation/")) { } else if (path.equals("/newlocation/")) {
return new SimpleHttpResponse(HttpStatus.SC_OK, "Successful redirect", ContentType.TEXT_PLAIN); final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
response.setBodyText("Successful redirect", ContentType.TEXT_PLAIN);
return response;
} else { } else {
return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND, null, null); return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND);
} }
} catch (final URISyntaxException ex) { } catch (final URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex); throw new ProtocolException(ex.getMessage(), ex);
@ -154,7 +156,7 @@ public class TestAsyncRedirects extends IntegrationTestBase {
response.addHeader(new BasicHeader("Location", "/circular-oldlocation")); response.addHeader(new BasicHeader("Location", "/circular-oldlocation"));
return response; return response;
} else { } else {
return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND, null, null); return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND);
} }
} catch (final URISyntaxException ex) { } catch (final URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex); throw new ProtocolException(ex.getMessage(), ex);
@ -176,7 +178,9 @@ public class TestAsyncRedirects extends IntegrationTestBase {
response.addHeader(new BasicHeader("Location", "/relativelocation/")); response.addHeader(new BasicHeader("Location", "/relativelocation/"));
return response; return response;
} else if (path.equals("/relativelocation/")) { } else if (path.equals("/relativelocation/")) {
return new SimpleHttpResponse(HttpStatus.SC_OK, "Successful redirect", ContentType.TEXT_PLAIN); final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
response.setBodyText("Successful redirect", ContentType.TEXT_PLAIN);
return response;
} else { } else {
return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND); return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND);
} }
@ -199,7 +203,9 @@ public class TestAsyncRedirects extends IntegrationTestBase {
response.addHeader(new BasicHeader("Location", "relativelocation")); response.addHeader(new BasicHeader("Location", "relativelocation"));
return response; return response;
} else if (path.equals("/test/relativelocation")) { } else if (path.equals("/test/relativelocation")) {
return new SimpleHttpResponse(HttpStatus.SC_OK, "Successful redirect", ContentType.TEXT_PLAIN); final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
response.setBodyText("Successful redirect", ContentType.TEXT_PLAIN);
return response;
} else { } else {
return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND); return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND);
} }
@ -450,9 +456,8 @@ public class TestAsyncRedirects extends IntegrationTestBase {
.setMaxRedirects(5).build(); .setMaxRedirects(5).build();
try { try {
final Future<SimpleHttpResponse> future = httpclient.execute( final Future<SimpleHttpResponse> future = httpclient.execute(
new SimpleRequestProducer( SimpleRequestProducer.create(SimpleHttpRequest.get(target, "/circular-oldlocation/"), config),
SimpleHttpRequest.get(target, "/circular-oldlocation/"), config), SimpleResponseConsumer.create(), null);
new SimpleResponseConsumer(), null);
future.get(); future.get();
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof RedirectException); Assert.assertTrue(e.getCause() instanceof RedirectException);
@ -477,9 +482,8 @@ public class TestAsyncRedirects extends IntegrationTestBase {
.build(); .build();
try { try {
final Future<SimpleHttpResponse> future = httpclient.execute( final Future<SimpleHttpResponse> future = httpclient.execute(
new SimpleRequestProducer( SimpleRequestProducer.create(SimpleHttpRequest.get(target, "/circular-oldlocation/"), config),
SimpleHttpRequest.get(target, "/circular-oldlocation/"), config), SimpleResponseConsumer.create(), null);
new SimpleResponseConsumer(), null);
future.get(); future.get();
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof CircularRedirectException); Assert.assertTrue(e.getCause() instanceof CircularRedirectException);
@ -501,8 +505,9 @@ public class TestAsyncRedirects extends IntegrationTestBase {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute( final SimpleHttpRequest post = SimpleHttpRequest.post(target, "/oldlocation/");
SimpleHttpRequest.post(target, "/oldlocation/", "stuff", ContentType.TEXT_PLAIN), context, null); post.setBodyText("stuff", ContentType.TEXT_PLAIN);
final Future<SimpleHttpResponse> future = httpclient.execute(post, context, null);
final HttpResponse response = future.get(); final HttpResponse response = future.get();
Assert.assertNotNull(response); Assert.assertNotNull(response);
@ -585,7 +590,9 @@ public class TestAsyncRedirects extends IntegrationTestBase {
response.addHeader(new BasicHeader("Location", url)); response.addHeader(new BasicHeader("Location", url));
return response; return response;
} else if (path.equals("/relativelocation/")) { } else if (path.equals("/relativelocation/")) {
return new SimpleHttpResponse(HttpStatus.SC_OK, "Successful redirect", ContentType.TEXT_PLAIN); final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
response.setBodyText("Successful redirect", ContentType.TEXT_PLAIN);
return response;
} else { } else {
return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND); return new SimpleHttpResponse(HttpStatus.SC_NOT_FOUND);
} }
@ -793,7 +800,9 @@ public class TestAsyncRedirects extends IntegrationTestBase {
final URI requestURI = request.getUri(); final URI requestURI = request.getUri();
final String path = requestURI.getPath(); final String path = requestURI.getPath();
if (path.equals("/rome")) { if (path.equals("/rome")) {
return new SimpleHttpResponse(HttpStatus.SC_OK, "Successful redirect", ContentType.TEXT_PLAIN); final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
response.setBodyText("Successful redirect", ContentType.TEXT_PLAIN);
return response;
} else { } else {
final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY); final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY);
response.addHeader(new BasicHeader("Location", "/rome")); response.addHeader(new BasicHeader("Location", "/rome"));

View File

@ -62,7 +62,9 @@ public class TestAsyncStatefulConnManagement extends IntegrationTestBase {
protected SimpleHttpResponse handle( protected SimpleHttpResponse handle(
final SimpleHttpRequest request, final SimpleHttpRequest request,
final HttpCoreContext context) throws HttpException { final HttpCoreContext context) throws HttpException {
return new SimpleHttpResponse(HttpStatus.SC_OK, "Whatever", ContentType.TEXT_PLAIN); final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
response.setBodyText("Whatever", ContentType.TEXT_PLAIN);
return response;
} }
}; };
} }
@ -185,7 +187,9 @@ public class TestAsyncStatefulConnManagement extends IntegrationTestBase {
protected SimpleHttpResponse handle( protected SimpleHttpResponse handle(
final SimpleHttpRequest request, final SimpleHttpRequest request,
final HttpCoreContext context) throws HttpException { final HttpCoreContext context) throws HttpException {
return new SimpleHttpResponse(HttpStatus.SC_OK, "Whatever", ContentType.TEXT_PLAIN); final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
response.setBodyText("Whatever", ContentType.TEXT_PLAIN);
return response;
} }
}; };
} }

View File

@ -234,8 +234,9 @@ public class TestClientAuthentication extends IntegrationTestBase {
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);
final Future<SimpleHttpResponse> future = httpclient.execute( final SimpleHttpRequest put = SimpleHttpRequest.put(target, "/");
SimpleHttpRequest.put(target, "/", "Some important stuff", ContentType.TEXT_PLAIN), context, null); put.setBodyText("Some important stuff", ContentType.TEXT_PLAIN);
final Future<SimpleHttpResponse> future = httpclient.execute(put, context, null);
final HttpResponse response = future.get(); final HttpResponse response = future.get();
Assert.assertNotNull(response); Assert.assertNotNull(response);
@ -306,8 +307,9 @@ public class TestClientAuthentication extends IntegrationTestBase {
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);
context.setRequestConfig(RequestConfig.custom().setExpectContinueEnabled(true).build()); context.setRequestConfig(RequestConfig.custom().setExpectContinueEnabled(true).build());
final Future<SimpleHttpResponse> future = httpclient.execute( final SimpleHttpRequest put = SimpleHttpRequest.put(target, "/");
SimpleHttpRequest.put(target, "/", "Some important stuff", ContentType.TEXT_PLAIN), context, null); put.setBodyText("Some important stuff", ContentType.TEXT_PLAIN);
final Future<SimpleHttpResponse> future = httpclient.execute(put, context, null);
final HttpResponse response = future.get(); final HttpResponse response = future.get();
Assert.assertNotNull(response); Assert.assertNotNull(response);
@ -332,8 +334,9 @@ public class TestClientAuthentication extends IntegrationTestBase {
context.setCredentialsProvider(credsProvider); context.setCredentialsProvider(credsProvider);
context.setRequestConfig(RequestConfig.custom().setExpectContinueEnabled(true).build()); context.setRequestConfig(RequestConfig.custom().setExpectContinueEnabled(true).build());
final Future<SimpleHttpResponse> future = httpclient.execute( final SimpleHttpRequest put = SimpleHttpRequest.put(target, "/");
SimpleHttpRequest.put(target, "/", "Some important stuff", ContentType.TEXT_PLAIN), context, null); put.setBodyText("Some important stuff", ContentType.TEXT_PLAIN);
final Future<SimpleHttpResponse> future = httpclient.execute(put, context, null);
final HttpResponse response = future.get(); final HttpResponse response = future.get();
Assert.assertNotNull(response); Assert.assertNotNull(response);
@ -547,8 +550,8 @@ public class TestClientAuthentication extends IntegrationTestBase {
final HttpGet httpget = new HttpGet("/"); final HttpGet httpget = new HttpGet("/");
httpget.setConfig(config); httpget.setConfig(config);
final Future<SimpleHttpResponse> future = httpclient.execute( final Future<SimpleHttpResponse> future = httpclient.execute(
new SimpleRequestProducer(SimpleHttpRequest.get(target, "/"), config), SimpleRequestProducer.create(SimpleHttpRequest.get(target, "/"), config),
new SimpleResponseConsumer(), SimpleResponseConsumer.create(),
context, null); context, null);
final SimpleHttpResponse response = future.get(); final SimpleHttpResponse response = future.get();
Assert.assertNotNull(response); Assert.assertNotNull(response);

View File

@ -78,7 +78,7 @@ public class TestHttpAsync extends IntegrationTestBase {
final SimpleHttpResponse response = future.get(); final SimpleHttpResponse response = future.get();
Assert.assertThat(response, CoreMatchers.notNullValue()); Assert.assertThat(response, CoreMatchers.notNullValue());
Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200)); Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200));
final String body = response.getBody(); final String body = response.getBodyText();
Assert.assertThat(body, CoreMatchers.notNullValue()); Assert.assertThat(body, CoreMatchers.notNullValue());
Assert.assertThat(body.length(), CoreMatchers.equalTo(2048)); Assert.assertThat(body.length(), CoreMatchers.equalTo(2048));
} }
@ -93,7 +93,7 @@ public class TestHttpAsync extends IntegrationTestBase {
final SimpleHttpResponse response = future.get(); final SimpleHttpResponse response = future.get();
Assert.assertThat(response, CoreMatchers.notNullValue()); Assert.assertThat(response, CoreMatchers.notNullValue());
Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200)); Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200));
final String body = response.getBody(); final String body = response.getBodyText();
Assert.assertThat(body, CoreMatchers.nullValue()); Assert.assertThat(body, CoreMatchers.nullValue());
} }
} }
@ -108,7 +108,7 @@ public class TestHttpAsync extends IntegrationTestBase {
final SimpleHttpResponse response = future.get(); final SimpleHttpResponse response = future.get();
Assert.assertThat(response, CoreMatchers.notNullValue()); Assert.assertThat(response, CoreMatchers.notNullValue());
Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200)); Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200));
final String body = response.getBody(); final String body = response.getBodyText();
Assert.assertThat(body, CoreMatchers.notNullValue()); Assert.assertThat(body, CoreMatchers.notNullValue());
Assert.assertThat(body.length(), CoreMatchers.equalTo(2048)); Assert.assertThat(body.length(), CoreMatchers.equalTo(2048));
} }
@ -209,7 +209,7 @@ public class TestHttpAsync extends IntegrationTestBase {
final SimpleHttpResponse response1 = future1.get(); final SimpleHttpResponse response1 = future1.get();
Assert.assertThat(response1, CoreMatchers.notNullValue()); Assert.assertThat(response1, CoreMatchers.notNullValue());
Assert.assertThat(response1.getCode(), CoreMatchers.equalTo(200)); Assert.assertThat(response1.getCode(), CoreMatchers.equalTo(200));
final String body1 = response1.getBody(); final String body1 = response1.getBodyText();
Assert.assertThat(body1, CoreMatchers.notNullValue()); Assert.assertThat(body1, CoreMatchers.notNullValue());
Assert.assertThat(body1.length(), CoreMatchers.equalTo(2048)); Assert.assertThat(body1.length(), CoreMatchers.equalTo(2048));
@ -224,7 +224,7 @@ public class TestHttpAsync extends IntegrationTestBase {
final SimpleHttpResponse response2 = future2.get(); final SimpleHttpResponse response2 = future2.get();
Assert.assertThat(response2, CoreMatchers.notNullValue()); Assert.assertThat(response2, CoreMatchers.notNullValue());
Assert.assertThat(response2.getCode(), CoreMatchers.equalTo(200)); Assert.assertThat(response2.getCode(), CoreMatchers.equalTo(200));
final String body2 = response2.getBody(); final String body2 = response2.getBodyText();
Assert.assertThat(body2, CoreMatchers.notNullValue()); Assert.assertThat(body2, CoreMatchers.notNullValue());
Assert.assertThat(body2.length(), CoreMatchers.equalTo(2048)); Assert.assertThat(body2.length(), CoreMatchers.equalTo(2048));
} }
@ -234,7 +234,7 @@ public class TestHttpAsync extends IntegrationTestBase {
final SimpleHttpResponse response3 = future3.get(); final SimpleHttpResponse response3 = future3.get();
Assert.assertThat(response3, CoreMatchers.notNullValue()); Assert.assertThat(response3, CoreMatchers.notNullValue());
Assert.assertThat(response3.getCode(), CoreMatchers.equalTo(200)); Assert.assertThat(response3.getCode(), CoreMatchers.equalTo(200));
final String body3 = response3.getBody(); final String body3 = response3.getBodyText();
Assert.assertThat(body3, CoreMatchers.notNullValue()); Assert.assertThat(body3, CoreMatchers.notNullValue());
Assert.assertThat(body3.length(), CoreMatchers.equalTo(2048)); Assert.assertThat(body3.length(), CoreMatchers.equalTo(2048));
} }

View File

@ -189,7 +189,7 @@ public class TestHttpAsyncMinimal {
final SimpleHttpResponse response = future.get(); final SimpleHttpResponse response = future.get();
Assert.assertThat(response, CoreMatchers.notNullValue()); Assert.assertThat(response, CoreMatchers.notNullValue());
Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200)); Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200));
final String body = response.getBody(); final String body = response.getBodyText();
Assert.assertThat(body, CoreMatchers.notNullValue()); Assert.assertThat(body, CoreMatchers.notNullValue());
Assert.assertThat(body.length(), CoreMatchers.equalTo(2048)); Assert.assertThat(body.length(), CoreMatchers.equalTo(2048));
} }
@ -204,7 +204,7 @@ public class TestHttpAsyncMinimal {
final SimpleHttpResponse response = future.get(); final SimpleHttpResponse response = future.get();
Assert.assertThat(response, CoreMatchers.notNullValue()); Assert.assertThat(response, CoreMatchers.notNullValue());
Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200)); Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200));
final String body = response.getBody(); final String body = response.getBodyText();
Assert.assertThat(body, CoreMatchers.nullValue()); Assert.assertThat(body, CoreMatchers.nullValue());
} }
} }

View File

@ -66,8 +66,8 @@ public class AsyncClientConnectionEviction {
final SimpleHttpRequest request = SimpleHttpRequest.get(target, "/"); final SimpleHttpRequest request = SimpleHttpRequest.get(target, "/");
final Future<SimpleHttpResponse> future1 = client.execute( final Future<SimpleHttpResponse> future1 = client.execute(
new SimpleRequestProducer(request), SimpleRequestProducer.create(request, null),
new SimpleResponseConsumer(), SimpleResponseConsumer.create(),
new FutureCallback<SimpleHttpResponse>() { new FutureCallback<SimpleHttpResponse>() {
@Override @Override
@ -95,8 +95,8 @@ public class AsyncClientConnectionEviction {
// Previous connection should get evicted from the pool by now // Previous connection should get evicted from the pool by now
final Future<SimpleHttpResponse> future2 = client.execute( final Future<SimpleHttpResponse> future2 = client.execute(
new SimpleRequestProducer(request), SimpleRequestProducer.create(request, null),
new SimpleResponseConsumer(), SimpleResponseConsumer.create(),
new FutureCallback<SimpleHttpResponse>() { new FutureCallback<SimpleHttpResponse>() {
@Override @Override

View File

@ -105,8 +105,8 @@ public class AsyncClientCustomSSL {
final SimpleHttpRequest request = SimpleHttpRequest.get(target, requestUri); final SimpleHttpRequest request = SimpleHttpRequest.get(target, requestUri);
final Future<SimpleHttpResponse> future = client.execute( final Future<SimpleHttpResponse> future = client.execute(
new SimpleRequestProducer(request), SimpleRequestProducer.create(request, null),
new SimpleResponseConsumer(), SimpleResponseConsumer.create(),
clientContext, clientContext,
new FutureCallback<SimpleHttpResponse>() { new FutureCallback<SimpleHttpResponse>() {

View File

@ -69,8 +69,8 @@ public class AsyncClientHttp1Pipelining {
for (final String requestUri: requestUris) { for (final String requestUri: requestUris) {
final SimpleHttpRequest request = SimpleHttpRequest.get(target, requestUri); final SimpleHttpRequest request = SimpleHttpRequest.get(target, requestUri);
endpoint.execute( endpoint.execute(
new SimpleRequestProducer(request), SimpleRequestProducer.create(request, null),
new SimpleResponseConsumer(), SimpleResponseConsumer.create(),
new FutureCallback<SimpleHttpResponse>() { new FutureCallback<SimpleHttpResponse>() {
@Override @Override

View File

@ -70,8 +70,8 @@ public class AsyncClientHttp2Multiplexing {
for (final String requestUri: requestUris) { for (final String requestUri: requestUris) {
final SimpleHttpRequest request = SimpleHttpRequest.get(target, requestUri); final SimpleHttpRequest request = SimpleHttpRequest.get(target, requestUri);
endpoint.execute( endpoint.execute(
new SimpleRequestProducer(request), SimpleRequestProducer.create(request, null),
new SimpleResponseConsumer(), SimpleResponseConsumer.create(),
new FutureCallback<SimpleHttpResponse>() { new FutureCallback<SimpleHttpResponse>() {
@Override @Override

View File

@ -93,7 +93,7 @@ public class AsyncClientMessageTrailers {
.build(); .build();
final Future<SimpleHttpResponse> future = client.execute( final Future<SimpleHttpResponse> future = client.execute(
requestProducer, requestProducer,
new SimpleResponseConsumer(), SimpleResponseConsumer.create(),
new FutureCallback<SimpleHttpResponse>() { new FutureCallback<SimpleHttpResponse>() {
@Override @Override

View File

@ -0,0 +1,99 @@
/*
* ====================================================================
* 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 java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.util.Args;
public final class SimpleBody {
private final byte[] bodyAsBytes;
private final String bodyAsText;
private final ContentType contentType;
SimpleBody(final byte[] bodyAsBytes, final String bodyAsText, final ContentType contentType) {
this.bodyAsBytes = bodyAsBytes;
this.bodyAsText = bodyAsText;
this.contentType = contentType;
}
static SimpleBody create(final String body, final ContentType contentType) {
Args.notNull(body, "Body");
if (body.length() > 2048) {
return new SimpleBody(null, body, contentType);
} else {
final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
final byte[] bytes = body.getBytes(charset != null ? charset : StandardCharsets.US_ASCII);
return new SimpleBody(bytes, null, contentType);
}
}
static SimpleBody create(final byte[] body, final ContentType contentType) {
Args.notNull(body, "Body");
return new SimpleBody(body, null, contentType);
}
public ContentType getContentType() {
return contentType;
}
public byte[] getBodyBytes() {
if (bodyAsBytes != null) {
return bodyAsBytes;
} else if (bodyAsText != null) {
final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
return bodyAsText.getBytes(charset != null ? charset : StandardCharsets.US_ASCII);
} else {
return null;
}
}
public String getBodyText() {
if (bodyAsBytes != null) {
final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
return new String(bodyAsBytes, charset != null ? charset : StandardCharsets.US_ASCII);
} else if (bodyAsText != null) {
return bodyAsText;
} else {
return null;
}
}
public boolean isText() {
return bodyAsText != null;
}
public boolean isBytes() {
return bodyAsBytes != null;
}
}

View File

@ -28,174 +28,190 @@
package org.apache.hc.client5.http.async.methods; package org.apache.hc.client5.http.async.methods;
import java.net.URI; import java.net.URI;
import java.util.Iterator;
import org.apache.hc.client5.http.StandardMethods; import org.apache.hc.client5.http.StandardMethods;
import org.apache.hc.core5.http.ContentType; 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.HttpHost;
import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.message.BasicHttpRequest; import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.HttpRequestWrapper;
import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.Args;
public final class SimpleHttpRequest extends HttpRequestWrapper { public final class SimpleHttpRequest extends BasicHttpRequest {
private final String body; private SimpleBody body;
private final ContentType contentType;
public static SimpleHttpRequest get(final URI requestUri) { public static SimpleHttpRequest get(final URI requestUri) {
Args.notNull(requestUri, "Request URI"); Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.GET, requestUri, null, null); return new SimpleHttpRequest(StandardMethods.GET, requestUri);
} }
public static SimpleHttpRequest get(final String requestUri) { public static SimpleHttpRequest get(final String requestUri) {
return new SimpleHttpRequest(StandardMethods.GET, URI.create(requestUri), null, null); return new SimpleHttpRequest(StandardMethods.GET, URI.create(requestUri));
} }
public static SimpleHttpRequest get(final HttpHost host, final String path) { public static SimpleHttpRequest get(final HttpHost host, final String path) {
Args.notNull(host, "Host"); Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.GET, host, path, null, null); return new SimpleHttpRequest(StandardMethods.GET, host, path);
}
public static SimpleHttpRequest post(final URI requestUri) {
Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.POST, requestUri);
}
public static SimpleHttpRequest post(final String requestUri) {
return new SimpleHttpRequest(StandardMethods.POST, URI.create(requestUri));
}
public static SimpleHttpRequest post(final HttpHost host, final String path) {
Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.POST, host, path);
}
public static SimpleHttpRequest put(final URI requestUri) {
Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.PUT, requestUri);
}
public static SimpleHttpRequest put(final String requestUri) {
return new SimpleHttpRequest(StandardMethods.PUT, URI.create(requestUri));
}
public static SimpleHttpRequest put(final HttpHost host, final String path) {
Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.PUT, host, path);
} }
public static SimpleHttpRequest head(final URI requestUri) { public static SimpleHttpRequest head(final URI requestUri) {
Args.notNull(requestUri, "Request URI"); Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.HEAD, requestUri, null, null); return new SimpleHttpRequest(StandardMethods.HEAD, requestUri);
} }
public static SimpleHttpRequest head(final String requestUri) { public static SimpleHttpRequest head(final String requestUri) {
return new SimpleHttpRequest(StandardMethods.HEAD, URI.create(requestUri), null, null); return new SimpleHttpRequest(StandardMethods.HEAD, URI.create(requestUri));
} }
public static SimpleHttpRequest head(final HttpHost host, final String path) { public static SimpleHttpRequest head(final HttpHost host, final String path) {
Args.notNull(host, "Host"); Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.HEAD, host, path, null, null); return new SimpleHttpRequest(StandardMethods.HEAD, host, path);
}
public static SimpleHttpRequest post(final URI requestUri, final String body, final ContentType contentType) {
Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.POST, requestUri, body, contentType);
}
public static SimpleHttpRequest post(final String requestUri, final String body, final ContentType contentType) {
return new SimpleHttpRequest(StandardMethods.POST, URI.create(requestUri), body, contentType);
}
public static SimpleHttpRequest post(final HttpHost host, final String path, final String body, final ContentType contentType) {
Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.POST, host, path, body, contentType);
}
public static SimpleHttpRequest PUT(final URI requestUri, final String body, final ContentType contentType) {
Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.PUT, requestUri, body, contentType);
}
public static SimpleHttpRequest put(final String requestUri, final String body, final ContentType contentType) {
return new SimpleHttpRequest(StandardMethods.PUT, URI.create(requestUri), body, contentType);
}
public static SimpleHttpRequest put(final HttpHost host, final String path, final String body, final ContentType contentType) {
Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.PUT, host, path, body, contentType);
} }
public static SimpleHttpRequest delete(final URI requestUri) { public static SimpleHttpRequest delete(final URI requestUri) {
Args.notNull(requestUri, "Request URI"); Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.DELETE, requestUri, null, null); return new SimpleHttpRequest(StandardMethods.DELETE, requestUri);
} }
public static SimpleHttpRequest delete(final String requestUri) { public static SimpleHttpRequest delete(final String requestUri) {
return new SimpleHttpRequest(StandardMethods.DELETE, URI.create(requestUri), null, null); return new SimpleHttpRequest(StandardMethods.DELETE, URI.create(requestUri));
} }
public static SimpleHttpRequest delete(final HttpHost host, final String path) { public static SimpleHttpRequest delete(final HttpHost host, final String path) {
Args.notNull(host, "Host"); Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.DELETE, host, path, null, null); return new SimpleHttpRequest(StandardMethods.DELETE, host, path);
} }
public static SimpleHttpRequest trace(final URI requestUri) { public static SimpleHttpRequest trace(final URI requestUri) {
Args.notNull(requestUri, "Request URI"); Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.TRACE, requestUri, null, null); return new SimpleHttpRequest(StandardMethods.TRACE, requestUri);
} }
public static SimpleHttpRequest trace(final String requestUri) { public static SimpleHttpRequest trace(final String requestUri) {
return new SimpleHttpRequest(StandardMethods.TRACE, URI.create(requestUri), null, null); return new SimpleHttpRequest(StandardMethods.TRACE, URI.create(requestUri));
} }
public static SimpleHttpRequest trace(final HttpHost host, final String path) { public static SimpleHttpRequest trace(final HttpHost host, final String path) {
Args.notNull(host, "Host"); Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.TRACE, host, path, null, null); return new SimpleHttpRequest(StandardMethods.TRACE, host, path);
} }
public static SimpleHttpRequest options(final URI requestUri) { public static SimpleHttpRequest options(final URI requestUri) {
Args.notNull(requestUri, "Request URI"); Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.OPTIONS, requestUri, null, null); return new SimpleHttpRequest(StandardMethods.OPTIONS, requestUri);
} }
public static SimpleHttpRequest options(final String requestUri) { public static SimpleHttpRequest options(final String requestUri) {
return new SimpleHttpRequest(StandardMethods.OPTIONS, URI.create(requestUri), null, null); return new SimpleHttpRequest(StandardMethods.OPTIONS, URI.create(requestUri));
} }
public static SimpleHttpRequest options(final HttpHost host, final String path) { public static SimpleHttpRequest options(final HttpHost host, final String path) {
Args.notNull(host, "Host"); Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.OPTIONS, host, path, null, null); return new SimpleHttpRequest(StandardMethods.OPTIONS, host, path);
} }
public static SimpleHttpRequest patch(final URI requestUri, final String body, final ContentType contentType) { public static SimpleHttpRequest patch(final URI requestUri) {
Args.notNull(requestUri, "Request URI"); Args.notNull(requestUri, "Request URI");
return new SimpleHttpRequest(StandardMethods.PATCH, requestUri, body, contentType); return new SimpleHttpRequest(StandardMethods.PATCH, requestUri);
} }
public static SimpleHttpRequest patch(final String requestUri, final String body, final ContentType contentType) { public static SimpleHttpRequest patch(final String requestUri) {
return new SimpleHttpRequest(StandardMethods.PATCH, URI.create(requestUri), body, contentType); return new SimpleHttpRequest(StandardMethods.PATCH, URI.create(requestUri));
} }
public static SimpleHttpRequest patch(final HttpHost host, final String path, final String body, final ContentType contentType) { public static SimpleHttpRequest patch(final HttpHost host, final String path) {
Args.notNull(host, "Host"); Args.notNull(host, "Host");
return new SimpleHttpRequest(StandardMethods.PATCH, host, path, body, contentType); return new SimpleHttpRequest(StandardMethods.PATCH, host, path);
} }
public SimpleHttpRequest(final HttpRequest head, final String body, final ContentType contentType) { public static SimpleHttpRequest copy(final HttpRequest original) {
super(head); Args.notNull(original, "HTTP request");
final SimpleHttpRequest copy = new SimpleHttpRequest(original.getMethod(), original.getRequestUri());
copy.setVersion(original.getVersion());
for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
copy.addHeader(it.next());
}
copy.setScheme(original.getScheme());
copy.setAuthority(original.getAuthority());
return copy;
}
public SimpleHttpRequest(final String method, final String path) {
super(method, path);
}
public SimpleHttpRequest(final String method, final HttpHost host, final String path) {
super(method, host, path);
}
public SimpleHttpRequest(final String method, final URI requestUri) {
super(method, requestUri);
}
SimpleHttpRequest(final StandardMethods method, final URI requestUri) {
this(method.name(), requestUri);
}
SimpleHttpRequest(final StandardMethods method, final HttpHost host, final String path) {
this(method.name(), host, path);
}
public void setBody(final SimpleBody body) {
this.body = body; this.body = body;
this.contentType = contentType;
} }
public SimpleHttpRequest( public void setBodyBytes(final byte[] bodyBytes, final ContentType contentType) {
final String method, this.body = SimpleBody.create(bodyBytes, contentType);
final HttpHost host,
final String path,
final String body,
final ContentType contentType) {
super(new BasicHttpRequest(method, host, path));
this.body = body;
this.contentType = contentType;
} }
SimpleHttpRequest( public void setBodyText(final String bodyText, final ContentType contentType) {
final StandardMethods method, this.body = SimpleBody.create(bodyText, contentType);
final HttpHost host,
final String path,
final String body,
final ContentType contentType) {
this(method.name(), host, path, body, contentType);
} }
public SimpleHttpRequest(final String method, final URI requestUri, final String body, final ContentType contentType) { public SimpleBody getBody() {
super(new BasicHttpRequest(method, requestUri));
this.body = body;
this.contentType = contentType;
}
SimpleHttpRequest(final StandardMethods method, final URI requestUri, final String body, final ContentType contentType) {
this(method.name(), requestUri, body, contentType);
}
public String getBody() {
return body; return body;
} }
public ContentType getContentType() { public ContentType getContentType() {
return contentType; return body != null ? body.getContentType() : null;
}
public String getBodyText() {
return body != null ? body.getBodyText() : null;
}
public byte[] getBodyBytes() {
return body != null ? body.getBodyBytes() : null;
} }
} }

View File

@ -27,51 +27,58 @@
package org.apache.hc.client5.http.async.methods; package org.apache.hc.client5.http.async.methods;
import java.util.Iterator;
import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.message.BasicHttpResponse; import org.apache.hc.core5.http.message.BasicHttpResponse;
import org.apache.hc.core5.http.message.HttpResponseWrapper; import org.apache.hc.core5.util.Args;
public final class SimpleHttpResponse extends HttpResponseWrapper { public final class SimpleHttpResponse extends BasicHttpResponse {
private final String body; private SimpleBody body;
private final ContentType contentType;
public SimpleHttpResponse(
final HttpResponse head,
final String body,
final ContentType contentType) {
super(head);
this.body = body;
this.contentType = contentType;
}
public SimpleHttpResponse(
final int code,
final String reasonPhrase,
final String body,
final ContentType contentType) {
super(new BasicHttpResponse(code, reasonPhrase));
this.body = body;
this.contentType = contentType;
}
public SimpleHttpResponse(final int code, final String body, final ContentType contentType) {
super(new BasicHttpResponse(code));
this.body = body;
this.contentType = contentType;
}
public SimpleHttpResponse(final int code) { public SimpleHttpResponse(final int code) {
this(code, null, null); super(code);
} }
public String getBody() { public static SimpleHttpResponse copy(final HttpResponse original) {
Args.notNull(original, "HTTP response");
final SimpleHttpResponse copy = new SimpleHttpResponse(original.getCode());
copy.setVersion(original.getVersion());
for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
copy.addHeader(it.next());
}
return copy;
}
public void setBody(final SimpleBody body) {
this.body = body;
}
public void setBodyBytes(final byte[] bodyBytes, final ContentType contentType) {
this.body = SimpleBody.create(bodyBytes, contentType);
}
public void setBodyText(final String bodyText, final ContentType contentType) {
this.body = SimpleBody.create(bodyText, contentType);
}
public SimpleBody getBody() {
return body; return body;
} }
public ContentType getContentType() { public ContentType getContentType() {
return contentType; return body != null ? body.getContentType() : null;
}
public String getBodyText() {
return body != null ? body.getBodyText() : null;
}
public byte[] getBodyBytes() {
return body != null ? body.getBodyBytes() : null;
} }
} }

View File

@ -27,18 +27,41 @@
package org.apache.hc.client5.http.async.methods; package org.apache.hc.client5.http.async.methods;
import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer; import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.Args;
public final class SimpleRequestProducer extends DefaultAsyncRequestProducer { public final class SimpleRequestProducer extends DefaultAsyncRequestProducer {
public SimpleRequestProducer(final SimpleHttpRequest request, final RequestConfig requestConfig) { SimpleRequestProducer(final HttpRequest request, final AsyncEntityProducer entityProducer, final RequestConfig requestConfig) {
super(Args.notNull(request, "Request"), request.getBody() != null ? super(request, entityProducer, requestConfig);
new StringAsyncEntityProducer(request.getBody(), request.getContentType()) : null, requestConfig);
} }
public SimpleRequestProducer(final SimpleHttpRequest request) { public static SimpleRequestProducer create(final SimpleHttpRequest request, final RequestConfig requestConfig) {
this(request, null); Args.notNull(request, "Request");
final SimpleBody body = request.getBody();
final AsyncEntityProducer entityProducer;
if (body != null) {
if (body.isText()) {
entityProducer = new StringAsyncEntityProducer(body.getBodyText(), body.getContentType());
} else {
entityProducer = new BasicAsyncEntityProducer(body.getBodyBytes(), body.getContentType()) {
//TODO: return the actual content length once
// the entity producers made repeatable in HttpCore
@Override
public long getContentLength() {
return -1;
}
};
}
} else {
entityProducer = null;
}
return new SimpleRequestProducer(request, entityProducer, requestConfig);
} }
} }

View File

@ -28,18 +28,27 @@ package org.apache.hc.client5.http.async.methods;
import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer; import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
import org.apache.hc.core5.http.nio.support.AbstractAsyncResponseConsumer; import org.apache.hc.core5.http.nio.support.AbstractAsyncResponseConsumer;
public final class SimpleResponseConsumer extends AbstractAsyncResponseConsumer<SimpleHttpResponse, String> { public final class SimpleResponseConsumer extends AbstractAsyncResponseConsumer<SimpleHttpResponse, byte[]> {
public SimpleResponseConsumer() { SimpleResponseConsumer(final AsyncEntityConsumer<byte[]> entityConsumer) {
super(new StringAsyncEntityConsumer()); super(entityConsumer);
}
public static SimpleResponseConsumer create() {
return new SimpleResponseConsumer(new BasicAsyncEntityConsumer());
} }
@Override @Override
protected SimpleHttpResponse buildResult(final HttpResponse response, final String entity, final ContentType contentType) { protected SimpleHttpResponse buildResult(final HttpResponse response, final byte[] entity, final ContentType contentType) {
return new SimpleHttpResponse(response, entity, contentType); final SimpleHttpResponse simpleResponse = SimpleHttpResponse.copy(response);
if (entity != null) {
simpleResponse.setBodyBytes(entity, contentType);
}
return simpleResponse;
} }
} }

View File

@ -48,6 +48,7 @@ import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.io.ShutdownType; import org.apache.hc.core5.io.ShutdownType;
import org.apache.hc.core5.reactor.ExceptionEvent; import org.apache.hc.core5.reactor.ExceptionEvent;
import org.apache.hc.core5.reactor.IOReactorStatus; import org.apache.hc.core5.reactor.IOReactorStatus;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.TimeValue; import org.apache.hc.core5.util.TimeValue;
/** /**
@ -85,8 +86,9 @@ public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, Close
final SimpleHttpRequest request, final SimpleHttpRequest request,
final HttpContext context, final HttpContext context,
final FutureCallback<SimpleHttpResponse> callback) { final FutureCallback<SimpleHttpResponse> callback) {
Args.notNull(request, "Request");
final BasicFuture<SimpleHttpResponse> future = new BasicFuture<>(callback); final BasicFuture<SimpleHttpResponse> future = new BasicFuture<>(callback);
execute(new SimpleRequestProducer(request), new SimpleResponseConsumer(), context, new FutureCallback<SimpleHttpResponse>() { execute(SimpleRequestProducer.create(request, null), SimpleResponseConsumer.create(), context, new FutureCallback<SimpleHttpResponse>() {
@Override @Override
public void completed(final SimpleHttpResponse response) { public void completed(final SimpleHttpResponse response) {