add our HttpResponse in the client API, REST client module, fix dependencies with compile/compileTest

This commit is contained in:
Jörg Prante 2019-08-07 18:50:42 +02:00
parent 7848b063e7
commit 28cdfcfb57
45 changed files with 366 additions and 282 deletions

View file

@ -1,6 +1,6 @@
group = org.xbib
name = netty-http
version = 4.1.38.0
version = 4.1.38.1
# main packages
netty.version = 4.1.38.Final

View file

@ -0,0 +1,3 @@
dependencies {
compile project(':netty-http-client')
}

View file

@ -1,12 +1,12 @@
package org.xbib.netty.http.client.rest;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpMethod;
import org.xbib.net.URL;
import org.xbib.netty.http.client.Client;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.common.HttpResponse;
import java.io.IOException;
import java.nio.charset.Charset;
@ -16,16 +16,16 @@ public class RestClient {
private static final Client client = new Client();
private FullHttpResponse response;
private HttpResponse response;
private RestClient() {
}
public void setResponse(FullHttpResponse response) {
this.response = response.copy();
public void setResponse(HttpResponse response) {
this.response = response;
}
public FullHttpResponse getResponse() {
public HttpResponse getResponse() {
return response;
}
@ -34,14 +34,8 @@ public class RestClient {
}
public String asString(Charset charset) {
ByteBuf byteBuf = response != null ? response.content() : null;
try {
return byteBuf != null && byteBuf.isReadable() ? response.content().toString(charset) : null;
} finally {
if (byteBuf != null) {
byteBuf.release();
}
}
ByteBuf byteBuf = response != null ? response.getBody() : null;
return byteBuf != null && byteBuf.isReadable() ? byteBuf.toString(charset) : null;
}
public void close() throws IOException {

View file

@ -1,7 +1,6 @@
package org.xbib.netty.http.client.test.rest;
package org.xbib.netty.http.client.rest;
import org.junit.jupiter.api.Test;
import org.xbib.netty.http.client.rest.RestClient;
import java.io.IOException;
import java.util.logging.Logger;

View file

@ -1,12 +1,10 @@
dependencies {
implementation project(":netty-http-common")
implementation "io.netty:netty-handler-proxy:${project.property('netty.version')}"
implementation "io.netty:netty-transport-native-epoll:${project.property('netty.version')}"
implementation "io.netty:netty-codec-http2:${project.property('netty.version')}"
implementation "org.xbib:net-url:${project.property('xbib-net-url.version')}"
testImplementation "io.netty:netty-tcnative-boringssl-static:${project.property('tcnative.version')}"
testImplementation "org.bouncycastle:bcpkix-jdk15on:${project.property('bouncycastle.version')}"
testImplementation "org.conscrypt:conscrypt-openjdk-uber:${project.property('conscrypt.version')}"
testImplementation "com.fasterxml.jackson.core:jackson-databind:${project.property('jackson.version')}"
compile project(":netty-http-common")
compile "io.netty:netty-handler-proxy:${project.property('netty.version')}"
compile "io.netty:netty-transport-native-epoll:${project.property('netty.version')}"
testCompile "io.netty:netty-tcnative-boringssl-static:${project.property('tcnative.version')}"
testCompile "org.bouncycastle:bcpkix-jdk15on:${project.property('bouncycastle.version')}"
testCompile "org.conscrypt:conscrypt-openjdk-uber:${project.property('conscrypt.version')}"
testCompile "com.fasterxml.jackson.core:jackson-databind:${project.property('jackson.version')}"
}

View file

@ -13,7 +13,6 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.pool.ChannelPoolHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http2.Http2Settings;
import io.netty.handler.proxy.HttpProxyHandler;
@ -33,6 +32,7 @@ import org.xbib.netty.http.client.transport.Http2Transport;
import org.xbib.netty.http.client.transport.HttpTransport;
import org.xbib.netty.http.client.transport.Transport;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.common.NetworkUtils;
import org.xbib.netty.http.common.security.SecurityUtil;
@ -261,7 +261,7 @@ public final class Client {
}
public <T> CompletableFuture<T> execute(Request request,
Function<FullHttpResponse, T> supplier) throws IOException {
Function<HttpResponse, T> supplier) throws IOException {
return newTransport(HttpAddress.of(request.url(), request.httpVersion()))
.execute(request, supplier);
}

View file

@ -25,6 +25,7 @@ import org.xbib.netty.http.client.listener.StatusListener;
import org.xbib.netty.http.client.retry.BackOff;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.common.HttpParameters;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.common.cookie.Cookie;
import java.nio.charset.MalformedInputException;
@ -73,7 +74,7 @@ public class Request {
private CompletableFuture<?> completableFuture;
private ResponseListener responseListener;
private ResponseListener<HttpResponse> responseListener;
private CookieListener cookieListener;
@ -208,12 +209,12 @@ public class Request {
return statusListener;
}
public Request setResponseListener(ResponseListener responseListener) {
public Request setResponseListener(ResponseListener<HttpResponse> responseListener) {
this.responseListener = responseListener;
return this;
}
public ResponseListener getResponseListener() {
public ResponseListener<HttpResponse> getResponseListener() {
return responseListener;
}

View file

@ -5,6 +5,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpResponse;
import org.xbib.netty.http.client.transport.Transport;
import org.xbib.netty.http.common.DefaultHttpResponse;
@ChannelHandler.Sharable
public class HttpResponseHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
@ -12,7 +13,8 @@ public class HttpResponseHandler extends SimpleChannelInboundHandler<FullHttpRes
@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpResponse httpResponse) throws Exception {
Transport transport = ctx.channel().attr(Transport.TRANSPORT_ATTRIBUTE_KEY).get();
transport.responseReceived(ctx.channel(),null, httpResponse);
transport.responseReceived(ctx.channel(),null,
new DefaultHttpResponse(transport.getHttpAddress(), httpResponse.retain()));
}
@Override

View file

@ -6,6 +6,7 @@ import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http2.HttpConversionUtil;
import org.xbib.netty.http.client.transport.Transport;
import org.xbib.netty.http.common.DefaultHttpResponse;
@ChannelHandler.Sharable
public class Http2ResponseHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
@ -14,7 +15,8 @@ public class Http2ResponseHandler extends SimpleChannelInboundHandler<FullHttpRe
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse httpResponse) throws Exception {
Transport transport = ctx.channel().attr(Transport.TRANSPORT_ATTRIBUTE_KEY).get();
Integer streamId = httpResponse.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
transport.responseReceived(ctx.channel(), streamId, httpResponse);
transport.responseReceived(ctx.channel(), streamId,
new DefaultHttpResponse(transport.getHttpAddress(), httpResponse.retain()));
}
@Override

View file

@ -1,9 +1,9 @@
package org.xbib.netty.http.client.listener;
import io.netty.handler.codec.http.FullHttpResponse;
import org.xbib.netty.http.common.HttpResponse;
@FunctionalInterface
public interface ResponseListener {
public interface ResponseListener<R extends HttpResponse> {
void onResponse(FullHttpResponse fullHttpResponse);
void onResponse(R response);
}

View file

@ -1,9 +1,10 @@
package org.xbib.netty.http.client.listener;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.xbib.netty.http.common.HttpStatus;
@FunctionalInterface
public interface StatusListener {
void onStatus(HttpResponseStatus httpResponseStatus);
void onStatus(HttpStatus httpStatus);
}

View file

@ -1,7 +1,6 @@
package org.xbib.netty.http.client.transport;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.ssl.SslHandler;
@ -12,6 +11,7 @@ import org.xbib.netty.http.client.Client;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.client.retry.BackOff;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.common.cookie.Cookie;
import org.xbib.netty.http.common.cookie.CookieBox;
@ -65,6 +65,11 @@ abstract class BaseTransport implements Transport {
this.requests = new ConcurrentSkipListMap<>();
}
@Override
public HttpAddress getHttpAddress() {
return httpAddress;
}
/**
* Experimental method for executing in a wrapping completable future.
* @param request request
@ -74,7 +79,7 @@ abstract class BaseTransport implements Transport {
*/
@Override
public <T> CompletableFuture<T> execute(Request request,
Function<FullHttpResponse, T> supplier) throws IOException {
Function<HttpResponse, T> supplier) throws IOException {
Objects.requireNonNull(supplier);
final CompletableFuture<T> completableFuture = new CompletableFuture<>();
request.setResponseListener(response -> {
@ -237,7 +242,7 @@ abstract class BaseTransport implements Transport {
return channel;
}
protected Request continuation(Request request, FullHttpResponse httpResponse) throws URLSyntaxException {
protected Request continuation(Request request, HttpResponse httpResponse) throws URLSyntaxException {
if (httpResponse == null) {
return null;
}
@ -247,7 +252,7 @@ abstract class BaseTransport implements Transport {
}
try {
if (request.canRedirect()) {
int status = httpResponse.status().code();
int status = httpResponse.getStatus().getCode();
switch (status) {
case 300:
case 301:
@ -256,12 +261,12 @@ abstract class BaseTransport implements Transport {
case 305:
case 307:
case 308:
String location = httpResponse.headers().get(HttpHeaderNames.LOCATION);
String location = httpResponse.getHeaders().getHeader(HttpHeaderNames.LOCATION);
location = new PercentDecoder(StandardCharsets.UTF_8.newDecoder()).decode(location);
if (location != null) {
logger.log(Level.FINE, "found redirect location: " + location);
URL redirUrl = URL.base(request.url()).resolve(location);
HttpMethod method = httpResponse.status().code() == 303 ? HttpMethod.GET : request.httpMethod();
HttpMethod method = httpResponse.getStatus().getCode() == 303 ? HttpMethod.GET : request.httpMethod();
Request.Builder newHttpRequestBuilder = Request.builder(method)
.url(redirUrl)
.setVersion(request.httpVersion())
@ -297,7 +302,7 @@ abstract class BaseTransport implements Transport {
return null;
}
protected Request retry(Request request, FullHttpResponse httpResponse) {
protected Request retry(Request request, HttpResponse httpResponse) {
if (httpResponse == null) {
return null;
}
@ -308,7 +313,7 @@ abstract class BaseTransport implements Transport {
if (request.isBackOff()) {
BackOff backOff = request.getBackOff() != null ? request.getBackOff() :
client.getClientConfig().getBackOff();
int status = httpResponse.status().code();
int status = httpResponse.getStatus ().getCode();
switch (status) {
case 403:
case 404:

View file

@ -3,7 +3,6 @@ package org.xbib.netty.http.client.transport;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http2.DefaultHttp2DataFrame;
@ -26,6 +25,7 @@ import org.xbib.netty.http.client.listener.StatusListener;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.client.listener.ResponseListener;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.common.cookie.Cookie;
import java.io.IOException;
@ -134,13 +134,13 @@ public class Http2Transport extends BaseTransport {
}
@Override
public void responseReceived(Channel channel, Integer streamId, FullHttpResponse fullHttpResponse) {
public void responseReceived(Channel channel, Integer streamId, HttpResponse httpResponse) {
if (throwable != null) {
logger.log(Level.WARNING, "throwable not null for response " + fullHttpResponse, throwable);
logger.log(Level.WARNING, "throwable not null for response " + httpResponse, throwable);
return;
}
if (streamId == null) {
logger.log(Level.WARNING, "stream ID is null for response " + fullHttpResponse);
logger.log(Level.WARNING, "stream ID is null for response " + httpResponse);
return;
}
// format of childchan channel ID is <parent channel ID> "/" <substream ID>
@ -160,9 +160,9 @@ public class Http2Transport extends BaseTransport {
} else {
StatusListener statusListener = request.getStatusListener();
if (statusListener != null) {
statusListener.onStatus(fullHttpResponse.status());
statusListener.onStatus(httpResponse.getStatus());
}
for (String cookieString : fullHttpResponse.headers().getAll(HttpHeaderNames.SET_COOKIE)) {
for (String cookieString : httpResponse.getHeaders().getAllHeaders(HttpHeaderNames.SET_COOKIE)) {
Cookie cookie = ClientCookieDecoder.STRICT.decode(cookieString);
addCookie(cookie);
CookieListener cookieListener = request.getCookieListener();
@ -170,17 +170,17 @@ public class Http2Transport extends BaseTransport {
cookieListener.onCookie(cookie);
}
}
ResponseListener responseListener = request.getResponseListener();
ResponseListener<HttpResponse> responseListener = request.getResponseListener();
if (responseListener != null) {
responseListener.onResponse(fullHttpResponse);
responseListener.onResponse(httpResponse);
}
try {
Request retryRequest = retry(request, fullHttpResponse);
Request retryRequest = retry(request, httpResponse);
if (retryRequest != null) {
// retry transport, wait for completion
client.retry(this, retryRequest);
} else {
Request continueRequest = continuation(request, fullHttpResponse);
Request continueRequest = continuation(request, httpResponse);
if (continueRequest != null) {
// continue with new transport, synchronous call here, wait for completion
client.continuation(this, continueRequest);

View file

@ -3,7 +3,6 @@ package org.xbib.netty.http.client.transport;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.handler.codec.http2.Http2Settings;
@ -17,6 +16,7 @@ import org.xbib.netty.http.client.listener.StatusListener;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.client.listener.ResponseListener;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.common.cookie.Cookie;
import java.io.IOException;
@ -75,9 +75,9 @@ public class HttpTransport extends BaseTransport {
}
@Override
public void responseReceived(Channel channel, Integer streamId, FullHttpResponse fullHttpResponse) {
public void responseReceived(Channel channel, Integer streamId, HttpResponse httpResponse) {
if (throwable != null) {
logger.log(Level.WARNING, "throwable not null for response " + fullHttpResponse, throwable);
logger.log(Level.WARNING, "throwable not null for response " + httpResponse, throwable);
return;
}
if (requests.isEmpty()) {
@ -89,9 +89,9 @@ public class HttpTransport extends BaseTransport {
if (request != null) {
StatusListener statusListener = request.getStatusListener();
if (statusListener != null) {
statusListener.onStatus(fullHttpResponse.status());
statusListener.onStatus(httpResponse.getStatus());
}
for (String cookieString : fullHttpResponse.headers().getAll(HttpHeaderNames.SET_COOKIE)) {
for (String cookieString : httpResponse.getHeaders().getAllHeaders(HttpHeaderNames.SET_COOKIE)) {
Cookie cookie = ClientCookieDecoder.STRICT.decode(cookieString);
addCookie(cookie);
CookieListener cookieListener = request.getCookieListener();
@ -99,18 +99,18 @@ public class HttpTransport extends BaseTransport {
cookieListener.onCookie(cookie);
}
}
ResponseListener responseListener = request.getResponseListener();
ResponseListener<HttpResponse> responseListener = request.getResponseListener();
if (responseListener != null) {
responseListener.onResponse(fullHttpResponse);
responseListener.onResponse(httpResponse);
}
}
try {
Request retryRequest = retry(request, fullHttpResponse);
Request retryRequest = retry(request, httpResponse);
if (retryRequest != null) {
// retry transport, wait for completion
client.retry(this, retryRequest);
} else {
Request continueRequest = continuation(request, fullHttpResponse);
Request continueRequest = continuation(request, httpResponse);
if (continueRequest != null) {
// continue with new transport, synchronous call here, wait for completion
client.continuation(this, continueRequest);

View file

@ -1,11 +1,12 @@
package org.xbib.netty.http.client.transport;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.handler.codec.http2.Http2Settings;
import io.netty.util.AttributeKey;
import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.common.cookie.CookieBox;
import javax.net.ssl.SSLSession;
@ -18,15 +19,17 @@ public interface Transport {
AttributeKey<Transport> TRANSPORT_ATTRIBUTE_KEY = AttributeKey.valueOf("transport");
HttpAddress getHttpAddress();
Transport execute(Request request) throws IOException;
<T> CompletableFuture<T> execute(Request request, Function<FullHttpResponse, T> supplier) throws IOException;
<T> CompletableFuture<T> execute(Request request, Function<HttpResponse, T> supplier) throws IOException;
void waitForSettings();
void settingsReceived(Http2Settings http2Settings) throws IOException;
void responseReceived(Channel channel, Integer streamId, FullHttpResponse fullHttpResponse) throws IOException;
void responseReceived(Channel channel, Integer streamId, HttpResponse fullHttpResponse) throws IOException;
void pushPromiseReceived(Channel channel, Integer streamId, Integer promisedStreamId, Http2Headers headers);

View file

@ -1,9 +1,9 @@
package org.xbib.netty.http.client.test;
import io.netty.handler.codec.http.FullHttpResponse;
import org.junit.jupiter.api.Test;
import org.xbib.netty.http.client.Client;
import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.common.HttpResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@ -23,12 +23,12 @@ class CompletableFutureTest {
void testComposeCompletableFutures() throws IOException {
Client client = Client.builder().build();
try {
final Function<FullHttpResponse, String> httpResponseStringFunction = response ->
response.content().toString(StandardCharsets.UTF_8);
final Function<HttpResponse, String> stringFunction = response ->
response.getBodyAsString(StandardCharsets.UTF_8);
Request request = Request.get()
.url("http://repo.maven.apache.org/maven2/org/xbib/netty-http-client/maven-metadata.xml.sha1")
.build();
CompletableFuture<String> completableFuture = client.execute(request, httpResponseStringFunction)
CompletableFuture<String> completableFuture = client.execute(request, stringFunction)
.exceptionally(Throwable::getMessage)
.thenCompose(content -> {
logger.log(Level.INFO, content);
@ -37,7 +37,7 @@ class CompletableFutureTest {
return client.execute(Request.post()
.url("http://google.com/")
.addParameter("query", content)
.build(), httpResponseStringFunction);
.build(), stringFunction);
} catch (IOException e) {
logger.log(Level.WARNING, e.getMessage(), e);
return null;

View file

@ -28,10 +28,9 @@ class ConscryptTest {
.url("https://google.com")
.setVersion("HTTP/1.1")
.build()
.setResponseListener(fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "status = " + fullHttpResponse.status()
+ " response body = " + response);
.setResponseListener(resp -> {
logger.log(Level.INFO, "status = " + resp.getStatus()
+ " response body = " + resp.getBodyAsString(StandardCharsets.UTF_8));
});
client.execute(request).get();
} finally {

View file

@ -36,9 +36,9 @@ class CookieSetterHttpBinTest {
.url("http://httpbin.org/cookies/set?name=value")
.build()
.setCookieListener(cookie -> logger.log(Level.INFO, "this is the cookie: " + cookie.toString()))
.setResponseListener(fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
.setResponseListener(resp -> {
logger.log(Level.INFO, "status = " + resp.getStatus() +
" response body = " + resp.getBodyAsString(StandardCharsets.UTF_8));
});
client.execute(request).get();
} finally {

View file

@ -22,10 +22,10 @@ class Http1Test {
.build();
try {
Request request = Request.get().url("http://xbib.org").build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
msg.content().toString(StandardCharsets.UTF_8) +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO,
"got response: " + resp.getHeaders() +
resp.getBodyAsString(StandardCharsets.UTF_8) +
" status=" + resp.getStatus()));
client.execute(request).get();
} finally {
client.shutdownGracefully();
@ -38,13 +38,13 @@ class Http1Test {
.build();
try {
Request request1 = Request.get().url("http://xbib.org").build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.content().toString(StandardCharsets.UTF_8)));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getBodyAsString(StandardCharsets.UTF_8)));
client.execute(request1).get();
Request request2 = Request.get().url("http://google.com").setVersion("HTTP/1.1").build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.content().toString(StandardCharsets.UTF_8)));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getBodyAsString(StandardCharsets.UTF_8)));
client.execute(request2).get();
} finally {
client.shutdownGracefully();
@ -59,17 +59,13 @@ class Http1Test {
Request request1 = Request.builder(HttpMethod.GET)
.url("http://xbib.org").setVersion("HTTP/1.1")
.build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
//msg.content().toString(StandardCharsets.UTF_8) +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getHeaders() + " status=" +resp.getStatus()));
Request request2 = Request.builder(HttpMethod.GET)
.url("http://xbib.org").setVersion("HTTP/1.1")
.build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
//msg.content().toString(StandardCharsets.UTF_8) +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getHeaders() + " status=" +resp.getStatus()));
for (int i = 0; i < 10; i++) {
client.execute(request1);

View file

@ -22,10 +22,9 @@ class SecureHttpTest {
.build();
try {
Request request = Request.get().url("https://www.google.com/").build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
msg.content().toString(StandardCharsets.UTF_8) +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getHeaders() + resp.getBodyAsString(StandardCharsets.UTF_8) +
" status=" + resp.getStatus()));
client.execute(request).get();
} finally {
client.shutdownGracefully();
@ -38,13 +37,13 @@ class SecureHttpTest {
.build();
try {
Request request1 = Request.get().url("https://google.com").build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.content().toString(StandardCharsets.UTF_8)));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getBodyAsString(StandardCharsets.UTF_8)));
client.execute(request1).get();
Request request2 = Request.get().url("https://google.com").setVersion("HTTP/2.0").build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.content().toString(StandardCharsets.UTF_8)));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getBodyAsString(StandardCharsets.UTF_8)));
client.execute(request2).get();
} finally {
client.shutdownGracefully();
@ -59,15 +58,15 @@ class SecureHttpTest {
Request request1 = Request.builder(HttpMethod.GET)
.url("https://google.com").setVersion("HTTP/1.1")
.build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getHeaders() +
" status=" + resp.getStatus()));
Request request2 = Request.builder(HttpMethod.GET)
.url("https://google.com").setVersion("HTTP/1.1")
.build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getHeaders() +
" status=" + resp.getStatus()));
for (int i = 0; i < 10; i++) {
client.execute(request1);

View file

@ -1,10 +1,10 @@
package org.xbib.netty.http.client.test;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.proxy.HttpProxyHandler;
import org.junit.jupiter.api.Test;
import org.xbib.netty.http.client.Client;
import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.common.HttpResponse;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -24,9 +24,9 @@ class XbibTest {
try {
Request request = Request.get().url("http://xbib.org")
.build()
.setResponseListener(fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
.setResponseListener(resp -> {
logger.log(Level.INFO, "status = " + resp.getStatus() +
" response = " + resp.getBodyAsString(StandardCharsets.UTF_8));
});
client.execute(request);
} finally {
@ -40,18 +40,18 @@ class XbibTest {
.setTcpNodelay(true)
.build();
try {
final Function<FullHttpResponse, String> httpResponseStringFunction =
response -> response.content().toString(StandardCharsets.UTF_8);
final Function<HttpResponse, String> stringFunction =
response -> response.getBodyAsString(StandardCharsets.UTF_8);
Request request = Request.get().url("http://xbib.org")
.build();
final CompletableFuture<String> completableFuture = httpClient.execute(request, httpResponseStringFunction)
final CompletableFuture<String> completableFuture = httpClient.execute(request, stringFunction)
.exceptionally(Throwable::getMessage)
.thenCompose(content -> {
try {
return httpClient.execute(Request.post()
.url("http://google.de")
.addParameter("query", content.substring(0, 15))
.build(), httpResponseStringFunction);
.build(), stringFunction);
} catch (IOException e) {
logger.log(Level.WARNING, e.getMessage(), e);
return null;
@ -75,10 +75,8 @@ class XbibTest {
httpClient.execute(Request.get()
.url("http://xbib.org")
.build()
.setResponseListener(fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
}))
.setResponseListener(resp -> logger.log(Level.INFO, "status = " + resp.getStatus() +
" response body = " + resp.getBodyAsString(StandardCharsets.UTF_8))))
.get();
} finally {
httpClient.shutdownGracefully();
@ -94,10 +92,10 @@ class XbibTest {
.url("http://xbib.org")
.setTimeoutInMillis(10)
.build()
.setResponseListener(fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
}))
.setResponseListener(resp ->
logger.log(Level.INFO, "status = " + resp.getStatus() +
" response body = " + resp.getBodyAsString(StandardCharsets.UTF_8))
))
.get();
} finally {
httpClient.shutdownGracefully();
@ -112,9 +110,9 @@ class XbibTest {
.setVersion("HTTP/1.1")
.url("http://xbib.org")
.build()
.setResponseListener(fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
.setResponseListener(resp -> {
logger.log(Level.INFO, "status = " + resp.getStatus() +
" response body = " + resp.getBodyAsString(StandardCharsets.UTF_8));
}))
.get();
@ -122,9 +120,9 @@ class XbibTest {
.setVersion("HTTP/1.1")
.url("http://xbib.org")
.build()
.setResponseListener(fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
.setResponseListener(resp -> {
logger.log(Level.INFO, "status = " + resp.getStatus() +
" response body = " + resp.getBodyAsString(StandardCharsets.UTF_8));
}))
.get();
} finally {

View file

@ -39,10 +39,9 @@ public class AkamaiTest {
//.url("https://http2.akamai.com/")
.setVersion("HTTP/2.0")
.build()
.setResponseListener(msg -> {
String response = msg.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "status = " + msg.status() +
msg.headers().entries() + " " + response);
.setResponseListener(resp -> {
logger.log(Level.INFO, "status = " + resp.getStatus().getCode() +
resp.getHeaders() + " " + resp.getBodyAsString(StandardCharsets.UTF_8));
});
client.execute(request).get();
} finally {

View file

@ -26,10 +26,8 @@ class Http2PushTest {
Request request = Request.builder(HttpMethod.GET)
.url(url).setVersion("HTTP/2.0")
.build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
//msg.content().toString(StandardCharsets.UTF_8) +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO,
"got response: " + resp.getHeaders() + " status=" + resp.getStatus()));
client.execute(request).get();
} finally {

View file

@ -35,9 +35,8 @@ class PooledClientTest {
.setPoolNodeConnectionLimit(threads)
.build();
AtomicInteger count = new AtomicInteger();
ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
ResponseListener responseListener = resp -> {
String response = resp.getBodyAsString(StandardCharsets.UTF_8);
count.getAndIncrement();
};
try {
@ -49,7 +48,6 @@ class PooledClientTest {
for (int i = 0; i < loop; i++) {
Request request = Request.get().setVersion(httpAddress.getVersion())
.url(url.toString())
//.setTimeoutInMillis(25000L)
.build()
.setResponseListener(responseListener);
client.newTransport().execute(request).get();

View file

@ -36,18 +36,14 @@ class WebtideTest {
Request request1 = Request.builder(HttpMethod.GET)
.url("https://webtide.com").setVersion("HTTP/2.0")
.build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
//msg.content().toString(StandardCharsets.UTF_8) +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getHeaders() + " status=" + resp.getStatus()));
Request request2 = Request.builder(HttpMethod.GET)
.url("https://webtide.com/why-choose-jetty/").setVersion("HTTP/2.0")
.build()
.setResponseListener(msg -> logger.log(Level.INFO, "got response: " +
msg.headers().entries() +
//msg.content().toString(StandardCharsets.UTF_8) +
" status=" + msg.status().code()));
.setResponseListener(resp -> logger.log(Level.INFO, "got response: " +
resp.getHeaders() + " status=" +resp.getStatus()));
client.execute(request1).execute(request2);
} finally {

View file

@ -0,0 +1,34 @@
package org.xbib.netty.http.common;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class DefaultHttpHeaders implements HttpHeaders {
private final io.netty.handler.codec.http.HttpHeaders httpHeaders;
public DefaultHttpHeaders(io.netty.handler.codec.http.HttpHeaders headers) {
this.httpHeaders = headers;
}
@Override
public String getHeader(CharSequence header) {
return httpHeaders.get(header);
}
@Override
public List<String> getAllHeaders(CharSequence header) {
return httpHeaders.getAll(header);
}
@Override
public Iterator<Map.Entry<CharSequence, CharSequence>> iterator() {
return httpHeaders.iteratorCharSequence();
}
@Override
public String toString() {
return httpHeaders.entries().toString();
}
}

View file

@ -0,0 +1,56 @@
package org.xbib.netty.http.common;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.handler.codec.http.FullHttpResponse;
import java.io.InputStream;
import java.nio.charset.Charset;
public class DefaultHttpResponse implements HttpResponse {
private final HttpAddress httpAddress;
private final FullHttpResponse fullHttpResponse;
private final HttpStatus httpStatus;
private final HttpHeaders httpHeaders;
public DefaultHttpResponse(HttpAddress httpAddress, FullHttpResponse fullHttpResponse) {
this.httpAddress = httpAddress;
this.fullHttpResponse = fullHttpResponse;
this.httpStatus = new HttpStatus(fullHttpResponse.status());
this.httpHeaders = new DefaultHttpHeaders(fullHttpResponse.headers());
}
@Override
public HttpAddress getAddress() {
return httpAddress;
}
@Override
public HttpStatus getStatus() {
return httpStatus;
}
@Override
public HttpHeaders getHeaders() {
return httpHeaders;
}
@Override
public ByteBuf getBody() {
return fullHttpResponse.content().asReadOnly();
}
@Override
public InputStream getBodyAsStream() {
return new ByteBufInputStream(getBody());
}
@Override
public String getBodyAsString(Charset charset) {
return getBody().toString(charset);
}
}

View file

@ -0,0 +1,14 @@
package org.xbib.netty.http.common;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public interface HttpHeaders {
String getHeader(CharSequence header);
List<String> getAllHeaders(CharSequence header);
Iterator<Map.Entry<CharSequence, CharSequence>> iterator();
}

View file

@ -1,6 +1,5 @@
package org.xbib.netty.http.common;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
@ -21,7 +20,7 @@ public interface HttpRequest {
Map<String, String> getHeaders();
InputStream getContent() throws IOException;
InputStream getContent();
String getContentType();

View file

@ -1,13 +1,21 @@
package org.xbib.netty.http.common;
import java.io.IOException;
import io.netty.buffer.ByteBuf;
import java.io.InputStream;
import java.nio.charset.Charset;
public interface HttpResponse {
int getStatusCode() throws IOException;
HttpAddress getAddress();
String getReason() throws Exception;
HttpStatus getStatus();
InputStream getContent() throws IOException;
HttpHeaders getHeaders();
ByteBuf getBody();
InputStream getBodyAsStream();
String getBodyAsString(Charset charset);
}

View file

@ -0,0 +1,30 @@
package org.xbib.netty.http.common;
import io.netty.handler.codec.http.HttpResponseStatus;
public class HttpStatus {
private final HttpResponseStatus httpResponseStatus;
public HttpStatus(HttpResponseStatus httpResponseStatus) {
this.httpResponseStatus = httpResponseStatus;
}
public int getCode() {
return httpResponseStatus.code();
}
public String getMessage() {
return httpResponseStatus.codeAsText().toString();
}
public String getReasonPhrase() {
return httpResponseStatus.reasonPhrase();
}
@Override
public String toString() {
return httpResponseStatus.toString();
}
}

View file

@ -1,7 +1,7 @@
dependencies {
compile project(':netty-http-server')
implementation "org.reactivestreams:reactive-streams:${project.property('reactivestreams.version')}"
testImplementation("org.reactivestreams:reactive-streams-tck:${project.property('reactivestreams.version')}") {
compile "org.reactivestreams:reactive-streams:${project.property('reactivestreams.version')}"
testCompile("org.reactivestreams:reactive-streams-tck:${project.property('reactivestreams.version')}") {
exclude module: 'testng'
}
}

View file

@ -1,6 +1,4 @@
dependencies {
implementation project(":netty-http-server")
implementation "io.netty:netty-codec-http2:${project.property('netty.version')}"
implementation "org.xbib:net-url:${project.property('xbib-net-url.version')}"
implementation "org.xbib:guice:${project.property('xbib-guice.version')}"
compile project(":netty-http-server")
compile "org.xbib:guice:${project.property('xbib-guice.version')}"
}

View file

@ -1,9 +1,7 @@
dependencies {
compile project(":netty-http-common")
compile "io.netty:netty-handler:${project.property('netty.version')}"
compile "io.netty:netty-transport-native-epoll:${project.property('netty.version')}"
compile "io.netty:netty-tcnative-boringssl-static:${project.property('tcnative.version')}"
compile "io.netty:netty-codec-http2:${project.property('netty.version')}"
compile "org.bouncycastle:bcpkix-jdk15on:${project.property('bouncycastle.version')}"
testImplementation project(":netty-http-client")
testCompile project(":netty-http-client")
}

View file

@ -43,9 +43,9 @@ class ClassloaderServiceTest {
Request request = Request.get().setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/classloader/test.txt"))
.build()
.setResponseListener(r -> {
if (r.status().equals(HttpResponseStatus.OK)) {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
count.incrementAndGet();
}
});

View file

@ -9,10 +9,10 @@ import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.client.listener.ResponseListener;
import org.xbib.netty.http.client.transport.Transport;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.server.Server;
import org.xbib.netty.http.server.Domain;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@ -41,10 +41,8 @@ class CleartextHttp1Test {
Client client = Client.builder()
.build();
AtomicInteger counter = new AtomicInteger();
final ResponseListener responseListener = fullHttpResponse -> {
if (fullHttpResponse.status().equals(HttpResponseStatus.OK)) {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
final ResponseListener<HttpResponse> responseListener = resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
counter.incrementAndGet();
}
};
@ -79,10 +77,8 @@ class CleartextHttp1Test {
.setPoolNodeConnectionLimit(2)
.build();
AtomicInteger counter = new AtomicInteger();
final ResponseListener responseListener = fullHttpResponse -> {
if (fullHttpResponse.status().equals(HttpResponseStatus.OK)) {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
final ResponseListener<HttpResponse> responseListener = resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
counter.incrementAndGet();
}
};
@ -126,11 +122,8 @@ class CleartextHttp1Test {
.setPoolNodeConnectionLimit(threads)
.build();
AtomicInteger counter = new AtomicInteger();
final ResponseListener responseListener = fullHttpResponse -> {
if (fullHttpResponse.status().equals(HttpResponseStatus.OK)) {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "status = " + fullHttpResponse.status() +
// " response=" + response + " payload=" + payload);
final ResponseListener<HttpResponse> responseListener = resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
counter.incrementAndGet();
}
};

View file

@ -8,6 +8,7 @@ import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.client.listener.ResponseListener;
import org.xbib.netty.http.client.transport.Transport;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.server.Server;
import org.xbib.netty.http.server.ServerResponse;
import org.xbib.netty.http.server.Domain;
@ -43,10 +44,9 @@ class CleartextHttp2Test {
.build();
AtomicInteger counter = new AtomicInteger();
// a single instance of HTTP/2 response listener, always receives responses out-of-order
ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "response listener: headers = " + fullHttpResponse.headers().entries() +
" response body = " + response);
ResponseListener<HttpResponse> responseListener = resp -> {
logger.log(Level.INFO, "response listener: headers = " + resp.getHeaders() +
" response body = " + resp.getBodyAsString(StandardCharsets.UTF_8));
counter.incrementAndGet();
};
try {
@ -87,17 +87,12 @@ class CleartextHttp2Test {
.build();
AtomicInteger counter = new AtomicInteger();
// a single instance of HTTP/2 response listener, always receives responses out-of-order
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "response listener: headers = " + fullHttpResponse.headers().entries() +
// " response body = " + response);
counter.incrementAndGet();
};
final ResponseListener<HttpResponse> responseListener = resp -> counter.incrementAndGet();
try {
// single transport, single thread
Transport transport = client.newTransport();
for (int i = 0; i < loop; i++) {
String payload = Integer.toString(0) + "/" + Integer.toString(i);
String payload = 0 + "/" + i;
Request request = Request.get().setVersion("HTTP/2.0")
.url(server.getServerConfig().getAddress().base())
.content(payload, "text/plain")
@ -136,12 +131,7 @@ class CleartextHttp2Test {
.build();
AtomicInteger counter = new AtomicInteger();
// a HTTP/2 listener always receives responses out-of-order
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "response listener: headers = " + fullHttpResponse.headers().entries() +
// " response body = " + response);
counter.incrementAndGet();
};
final ResponseListener<HttpResponse> responseListener = resp -> counter.incrementAndGet();
try {
// note: for HTTP/2 only, we can use a single shared transport
final Transport transport = client.newTransport();
@ -214,12 +204,7 @@ class CleartextHttp2Test {
.build();
AtomicInteger counter = new AtomicInteger();
// a single instance of HTTP/2 response listener, always receives responses out-of-order
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "response listener: headers = " + fullHttpResponse.headers().entries() +
// " response body = " + response);
counter.incrementAndGet();
};
final ResponseListener<HttpResponse> responseListener = resp -> counter.incrementAndGet();
try {
// note: for HTTP/2 only, we can use a single shared transport
final Transport transport = client.newTransport();

View file

@ -59,8 +59,8 @@ class EndpointTest {
Request request = Request.get().setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/test.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
});
client.execute(request).get();
@ -99,8 +99,8 @@ class EndpointTest {
Request request = Request.get().setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/test.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
});
client.execute(request).get();
@ -146,24 +146,24 @@ class EndpointTest {
Request request = Request.get().setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/static/test.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
});
client.execute(request).get();
Request request1 = Request.get().setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/static1/test1.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg 1", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg 1",resp.getBodyAsString(StandardCharsets.UTF_8));
success1.set(true);
});
client.execute(request1).get();
Request request2 = Request.get().setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/static2/test2.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg 2", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg 2", resp.getBodyAsString(StandardCharsets.UTF_8));
success2.set(true);
});
client.execute(request2).get();
@ -214,12 +214,12 @@ class EndpointTest {
.url(server.getServerConfig().getAddress().base().resolve("/static/test.txt"))
.addParameter("a", "b")
.build()
.setResponseListener(r -> {
if (r.status().equals(HttpResponseStatus.OK)) {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
} else {
logger.log(Level.WARNING, r.toString());
logger.log(Level.WARNING, resp.getStatus().getReasonPhrase());
}
});
client.execute(request).get();
@ -227,12 +227,12 @@ class EndpointTest {
.url(server.getServerConfig().getAddress().base()
.resolve("/static1/test1.txt").newBuilder().fragment("frag").build())
.build()
.setResponseListener(r -> {
if (r.status().equals(HttpResponseStatus.OK)) {
assertEquals("Hello Jörg 1", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
assertEquals("Hello Jörg 1", resp.getBodyAsString(StandardCharsets.UTF_8));
success1.set(true);
} else {
logger.log(Level.WARNING, r.toString());
logger.log(Level.WARNING, resp.getStatus().getReasonPhrase());
}
});
client.execute(request1).get();
@ -240,12 +240,12 @@ class EndpointTest {
.url(server.getServerConfig().getAddress().base().resolve("/static2/test2.txt"))
.content("{\"a\":\"b\"}","application/json")
.build()
.setResponseListener(r -> {
if (r.status().equals(HttpResponseStatus.OK)) {
assertEquals("Hello Jörg 2", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
assertEquals("Hello Jörg 2",resp.getBodyAsString(StandardCharsets.UTF_8));
success2.set(true);
} else {
logger.log(Level.WARNING, r.toString());
logger.log(Level.WARNING, resp.getStatus().getReasonPhrase());
}
});
client.execute(request2).get();
@ -288,11 +288,11 @@ class EndpointTest {
Request request = Request.get().setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/static/" + i + "/test.txt"))
.build()
.setResponseListener(r -> {
if (r.status().equals(HttpResponseStatus.OK)) {
.setResponseListener(resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
count.incrementAndGet();
} else {
logger.log(Level.WARNING, r.status().reasonPhrase());
logger.log(Level.WARNING, resp.getStatus().getReasonPhrase());
}
});
client.execute(request).get();

View file

@ -44,8 +44,8 @@ class FileServiceTest {
Request request = Request.get().setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/static/test.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
});
logger.log(Level.INFO, request.toString());
@ -79,8 +79,8 @@ class FileServiceTest {
.setVersion(HttpVersion.valueOf("HTTP/2.0"))
.url(server.getServerConfig().getAddress().base().resolve("/static/test.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
});
logger.log(Level.INFO, request.toString());

View file

@ -45,8 +45,8 @@ class PostTest {
.addParameter("a", "b")
.addFormParameter("name", "Jörg")
.build()
.setResponseListener(r -> {
if (r.status().equals(HttpResponseStatus.OK)) {
.setResponseListener(resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
success.set(true);
}
});
@ -83,8 +83,8 @@ class PostTest {
.addParameter("a", "b")
.addFormParameter("name", "Jörg")
.build()
.setResponseListener(r -> {
if (r.status().equals(HttpResponseStatus.OK)) {
.setResponseListener(resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
success.set(true);
}
});

View file

@ -53,8 +53,8 @@ class SecureFileServiceTest {
.setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/static/test.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
});
logger.log(Level.INFO, request.toString());
@ -94,8 +94,8 @@ class SecureFileServiceTest {
.setVersion(HttpVersion.valueOf("HTTP/2.0"))
.url(server.getServerConfig().getAddress().base().resolve("/static/test.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
});
logger.log(Level.INFO, request.toString());
@ -136,8 +136,8 @@ class SecureFileServiceTest {
.setVersion(HttpVersion.HTTP_1_1)
.url(server.getServerConfig().getAddress().base().resolve("/static/test.txt"))
.build()
.setResponseListener(r -> {
assertEquals("Hello Jörg", r.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
assertEquals("Hello Jörg", resp.getBodyAsString(StandardCharsets.UTF_8));
success.set(true);
});
logger.log(Level.INFO, request.toString());

View file

@ -9,11 +9,11 @@ import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.client.listener.ResponseListener;
import org.xbib.netty.http.client.transport.Transport;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.server.Server;
import org.xbib.netty.http.server.Domain;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@ -43,11 +43,7 @@ class SecureHttp1Test {
.trustInsecure()
.build();
AtomicInteger counter = new AtomicInteger();
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
counter.getAndIncrement();
};
final ResponseListener<HttpResponse> responseListener = resp -> counter.getAndIncrement();
try {
server.accept();
Request request = Request.get().setVersion(HttpVersion.HTTP_1_1)
@ -81,11 +77,7 @@ class SecureHttp1Test {
.setPoolNodeConnectionLimit(2)
.build();
AtomicInteger counter = new AtomicInteger();
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "status = " + fullHttpResponse.status() + " response body = " + response);
counter.incrementAndGet();
};
final ResponseListener<HttpResponse> responseListener = resp -> counter.incrementAndGet();
try {
for (int i = 0; i < loop; i++) {
Request request = Request.get().setVersion(HttpVersion.HTTP_1_1)
@ -129,12 +121,7 @@ class SecureHttp1Test {
.setPoolNodeConnectionLimit(threads)
.build();
AtomicInteger counter = new AtomicInteger();
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "response listener: headers = " + fullHttpResponse.headers().entries() +
// " response body = " + response);
counter.incrementAndGet();
};
final ResponseListener<HttpResponse> responseListener = resp -> counter.incrementAndGet();
try {
ExecutorService executorService = Executors.newFixedThreadPool(threads);
for (int n = 0; n < threads; n++) {

View file

@ -8,6 +8,7 @@ import org.xbib.netty.http.client.Request;
import org.xbib.netty.http.client.listener.ResponseListener;
import org.xbib.netty.http.client.transport.Transport;
import org.xbib.netty.http.common.HttpAddress;
import org.xbib.netty.http.common.HttpResponse;
import org.xbib.netty.http.server.Server;
import org.xbib.netty.http.server.Domain;
@ -44,10 +45,9 @@ class SecureHttp2Test {
.build();
AtomicInteger counter = new AtomicInteger();
// a single instance of HTTP/2 response listener, always receives responses out-of-order
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
logger.log(Level.INFO, "response listener: headers = " + fullHttpResponse.headers().entries() +
" response body = " + response);
final ResponseListener<HttpResponse> responseListener = resp -> {
logger.log(Level.INFO, "response listener: headers = " + resp.getHeaders() +
" response body = " + resp.getBodyAsString(StandardCharsets.UTF_8));
counter.incrementAndGet();
};
try {
@ -89,17 +89,12 @@ class SecureHttp2Test {
.build();
AtomicInteger counter = new AtomicInteger();
// a single instance of HTTP/2 response listener, always receives responses out-of-order
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "response listener: headers = " + fullHttpResponse.headers().entries() +
// " response body = " + response);
counter.incrementAndGet();
};
final ResponseListener<HttpResponse> responseListener = resp -> counter.incrementAndGet();
try {
// single transport, single thread
Transport transport = client.newTransport();
for (int i = 0; i < loop; i++) {
String payload = Integer.toString(0) + "/" + Integer.toString(i);
String payload = 0 + "/" + i;
Request request = Request.get().setVersion("HTTP/2.0")
.url(server.getServerConfig().getAddress().base())
.content(payload, "text/plain")
@ -141,12 +136,7 @@ class SecureHttp2Test {
.build();
AtomicInteger counter = new AtomicInteger();
// a HTTP/2 listener always receives responses out-of-order
final ResponseListener responseListener = fullHttpResponse -> {
String response = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
//logger.log(Level.INFO, "response listener: headers = " + fullHttpResponse.headers().entries() +
// " response body = " + response);
counter.incrementAndGet();
};
final ResponseListener<HttpResponse> responseListener = resp -> counter.incrementAndGet();
try {
// note: for HTTP/2 only, we can use a single shared transport
final Transport transport = client.newTransport();

View file

@ -47,9 +47,9 @@ class StreamTest {
.url(server.getServerConfig().getAddress().base().resolve("/"))
.content("my body parameter", "text/plain")
.build()
.setResponseListener(response -> {
if (response.status().equals(HttpResponseStatus.OK)) {
assertEquals("Hello World", response.content().toString(StandardCharsets.UTF_8));
.setResponseListener(resp -> {
if (resp.getStatus().getCode() == HttpResponseStatus.OK.code()) {
assertEquals("Hello World", resp.getBodyAsString(StandardCharsets.UTF_8));
count.incrementAndGet();
}
});

View file

@ -1,5 +1,6 @@
include 'netty-http-common'
include 'netty-http-client'
include 'netty-http-client-rest'
include 'netty-http-server'
include 'netty-http-server-reactive'
include 'netty-http-server-rest'