From adb04983e7b505b2b827f4cd50a1ebd2da566a35 Mon Sep 17 00:00:00 2001 From: Sampada <46674082+sampada07@users.noreply.github.com> Date: Thu, 11 Jun 2020 21:27:04 +0530 Subject: [PATCH] BAEL-4061: Removed request mirroring (#9448) * BAEL-4061: Removed request mirroring * BAEL-4061: Code refactoring * BAEL-4061: more refactoring * BAEL-4061: renamed methods --- .../http/server/CustomHttpServerHandler.java | 29 ++--------- ...ResponseBuilder.java => RequestUtils.java} | 48 +++---------------- .../http/server/HttpServerLiveTest.java | 5 +- 3 files changed, 13 insertions(+), 69 deletions(-) rename netty/src/main/java/com/baeldung/http/server/{ResponseBuilder.java => RequestUtils.java} (61%) diff --git a/netty/src/main/java/com/baeldung/http/server/CustomHttpServerHandler.java b/netty/src/main/java/com/baeldung/http/server/CustomHttpServerHandler.java index 038f559329..abefbed0d9 100644 --- a/netty/src/main/java/com/baeldung/http/server/CustomHttpServerHandler.java +++ b/netty/src/main/java/com/baeldung/http/server/CustomHttpServerHandler.java @@ -5,8 +5,6 @@ import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE; import static io.netty.handler.codec.http.HttpResponseStatus.OK; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; -import java.util.Set; - import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; @@ -20,9 +18,6 @@ import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpUtil; import io.netty.handler.codec.http.LastHttpContent; -import io.netty.handler.codec.http.cookie.Cookie; -import io.netty.handler.codec.http.cookie.ServerCookieDecoder; -import io.netty.handler.codec.http.cookie.ServerCookieEncoder; import io.netty.util.CharsetUtil; public class CustomHttpServerHandler extends SimpleChannelInboundHandler { @@ -45,22 +40,20 @@ public class CustomHttpServerHandler extends SimpleChannelInboundHandler } responseData.setLength(0); - responseData.append(ResponseBuilder.addRequestAttributes(request)); - responseData.append(ResponseBuilder.addHeaders(request)); - responseData.append(ResponseBuilder.addParams(request)); + responseData.append(RequestUtils.formatParams(request)); } - responseData.append(ResponseBuilder.addDecoderResult(request)); + responseData.append(RequestUtils.evaluateDecoderResult(request)); if (msg instanceof HttpContent) { HttpContent httpContent = (HttpContent) msg; - responseData.append(ResponseBuilder.addBody(httpContent)); - responseData.append(ResponseBuilder.addDecoderResult(request)); + responseData.append(RequestUtils.formatBody(httpContent)); + responseData.append(RequestUtils.evaluateDecoderResult(request)); if (msg instanceof LastHttpContent) { LastHttpContent trailer = (LastHttpContent) msg; - responseData.append(ResponseBuilder.addLastResponse(request, trailer)); + responseData.append(RequestUtils.prepareLastResponse(request, trailer)); writeResponse(ctx, trailer, responseData); } } @@ -88,18 +81,6 @@ public class CustomHttpServerHandler extends SimpleChannelInboundHandler .set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } - String cookieString = request.headers() - .get(HttpHeaderNames.COOKIE); - if (cookieString != null) { - Set cookies = ServerCookieDecoder.STRICT.decode(cookieString); - if (!cookies.isEmpty()) { - for (Cookie cookie : cookies) { - httpResponse.headers() - .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie)); - } - } - } - ctx.write(httpResponse); if (!keepAlive) { diff --git a/netty/src/main/java/com/baeldung/http/server/ResponseBuilder.java b/netty/src/main/java/com/baeldung/http/server/RequestUtils.java similarity index 61% rename from netty/src/main/java/com/baeldung/http/server/ResponseBuilder.java rename to netty/src/main/java/com/baeldung/http/server/RequestUtils.java index 6d4e7845da..92ec2242f3 100644 --- a/netty/src/main/java/com/baeldung/http/server/ResponseBuilder.java +++ b/netty/src/main/java/com/baeldung/http/server/RequestUtils.java @@ -7,32 +7,15 @@ import java.util.Map.Entry; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.http.HttpContent; -import io.netty.handler.codec.http.HttpHeaderNames; -import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.LastHttpContent; import io.netty.handler.codec.http.QueryStringDecoder; import io.netty.util.CharsetUtil; -class ResponseBuilder { +class RequestUtils { - static StringBuilder addRequestAttributes(HttpRequest request) { - StringBuilder responseData = new StringBuilder(); - responseData.append("Version: ") - .append(request.protocolVersion()) - .append("\r\n"); - responseData.append("Host: ") - .append(request.headers() - .get(HttpHeaderNames.HOST, "unknown")) - .append("\r\n"); - responseData.append("URI: ") - .append(request.uri()) - .append("\r\n\r\n"); - return responseData; - } - - static StringBuilder addParams(HttpRequest request) { + static StringBuilder formatParams(HttpRequest request) { StringBuilder responseData = new StringBuilder(); QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri()); Map> params = queryStringDecoder.parameters(); @@ -42,9 +25,9 @@ class ResponseBuilder { List vals = p.getValue(); for (String val : vals) { responseData.append("Parameter: ") - .append(key) + .append(key.toUpperCase()) .append(" = ") - .append(val) + .append(val.toUpperCase()) .append("\r\n"); } } @@ -53,24 +36,7 @@ class ResponseBuilder { return responseData; } - static StringBuilder addHeaders(HttpRequest request) { - StringBuilder responseData = new StringBuilder(); - HttpHeaders headers = request.headers(); - if (!headers.isEmpty()) { - for (Map.Entry header : headers) { - CharSequence key = header.getKey(); - CharSequence value = header.getValue(); - responseData.append(key) - .append(" = ") - .append(value) - .append("\r\n"); - } - responseData.append("\r\n"); - } - return responseData; - } - - static StringBuilder addBody(HttpContent httpContent) { + static StringBuilder formatBody(HttpContent httpContent) { StringBuilder responseData = new StringBuilder(); ByteBuf content = httpContent.content(); if (content.isReadable()) { @@ -81,7 +47,7 @@ class ResponseBuilder { return responseData; } - static StringBuilder addDecoderResult(HttpObject o) { + static StringBuilder evaluateDecoderResult(HttpObject o) { StringBuilder responseData = new StringBuilder(); DecoderResult result = o.decoderResult(); @@ -94,7 +60,7 @@ class ResponseBuilder { return responseData; } - static StringBuilder addLastResponse(HttpRequest request, LastHttpContent trailer) { + static StringBuilder prepareLastResponse(HttpRequest request, LastHttpContent trailer) { StringBuilder responseData = new StringBuilder(); responseData.append("Good Bye!\r\n"); diff --git a/netty/src/test/java/com/baeldung/http/server/HttpServerLiveTest.java b/netty/src/test/java/com/baeldung/http/server/HttpServerLiveTest.java index 7a0f884724..77f1288838 100644 --- a/netty/src/test/java/com/baeldung/http/server/HttpServerLiveTest.java +++ b/netty/src/test/java/com/baeldung/http/server/HttpServerLiveTest.java @@ -89,7 +89,7 @@ public class HttpServerLiveTest { } @Test - public void whenGetSent_thenCookieReceivedInResponse() throws Exception { + public void whenGetSent_thenResponseOK() throws Exception { DefaultFullHttpRequest request = createRequest(null); channel.writeAndFlush(request); @@ -98,9 +98,6 @@ public class HttpServerLiveTest { assertEquals(200, response.getStatus()); assertEquals("HTTP/1.1", response.getVersion()); - Map headers = response.getHeaders(); - String cookies = headers.get("set-cookie"); - assertTrue(cookies.contains("my-cookie")); } @After