BAEL-4061: Removed request mirroring (#9448)

* BAEL-4061: Removed request mirroring

* BAEL-4061: Code refactoring

* BAEL-4061: more refactoring

* BAEL-4061: renamed methods
This commit is contained in:
Sampada 2020-06-11 21:27:04 +05:30 committed by GitHub
parent 1bd8a0b90c
commit adb04983e7
3 changed files with 13 additions and 69 deletions

View File

@ -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<Object> {
@ -45,22 +40,20 @@ public class CustomHttpServerHandler extends SimpleChannelInboundHandler<Object>
}
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<Object>
.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
String cookieString = request.headers()
.get(HttpHeaderNames.COOKIE);
if (cookieString != null) {
Set<Cookie> 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) {

View File

@ -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<String, List<String>> params = queryStringDecoder.parameters();
@ -42,9 +25,9 @@ class ResponseBuilder {
List<String> 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<String, String> 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");

View File

@ -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<String, String> headers = response.getHeaders();
String cookies = headers.get("set-cookie");
assertTrue(cookies.contains("my-cookie"));
}
@After