diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index 5e9fdd9fb4..70771f6832 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-5-reactive-client spring-5-reactive-client diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java index bb88502132..d5f1ef77a0 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java @@ -116,7 +116,9 @@ public class WebClientLoggingIntegrationTest { reactor.netty.http.client.HttpClient httpClient = HttpClient .create() - .tcpConfiguration(tcpClient -> tcpClient.bootstrap(b -> BootstrapHandlers.updateLogSupport(b, new CustomLogger(HttpClient.class)))); + .tcpConfiguration( + tc -> tc.bootstrap( + b -> BootstrapHandlers.updateLogSupport(b, new CustomLogger(HttpClient.class)))); WebClient .builder() .clientConnector(new ReactorClientHttpConnector(httpClient)) diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/filters/LogFilters.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/filters/LogFilters.java index 8bb4c2aecd..c1c3d3e895 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/filters/LogFilters.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/filters/LogFilters.java @@ -8,26 +8,47 @@ import reactor.core.publisher.Mono; @Slf4j public class LogFilters { - public static List prepareFilters(){ - return Arrays.asList(ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { - if ( log.isDebugEnabled()) { - StringBuilder sb = new StringBuilder("Request: \n").append(clientRequest.method()).append(" ").append(clientRequest.url()); + public static List prepareFilters() { + return Arrays.asList(logRequest(), logResponse()); + } + + private static ExchangeFilterFunction logRequest() { + return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { + if (log.isDebugEnabled()) { + StringBuilder sb = new StringBuilder("Request: \n") + .append(clientRequest.method()) + .append(" ") + .append(clientRequest.url()); clientRequest .headers() - .forEach((name, values) -> values.forEach(value -> sb.append("\n").append(name).append(":").append(value))); + .forEach((name, values) -> values.forEach(value -> sb + .append("\n") + .append(name) + .append(":") + .append(value))); log.debug(sb.toString()); } return Mono.just(clientRequest); - }), ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { - if ( log.isDebugEnabled()) { - StringBuilder sb = new StringBuilder("Response: \n").append("Status: ").append(clientResponse.rawStatusCode()); + }); + } + + private static ExchangeFilterFunction logResponse() { + return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { + if (log.isDebugEnabled()) { + StringBuilder sb = new StringBuilder("Response: \n") + .append("Status: ") + .append(clientResponse.rawStatusCode()); clientResponse .headers() .asHttpHeaders() - .forEach((key, value1) -> value1.forEach(value -> sb.append("\n").append(key).append(":").append(value))); + .forEach((key, value1) -> value1.forEach(value -> sb + .append("\n") + .append(key) + .append(":") + .append(value))); log.debug(sb.toString()); } return Mono.just(clientResponse); - })); + }); } } diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java index 334faf2c91..ac333feb6c 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java @@ -27,7 +27,9 @@ public class RequestLogEnhancer { .append(header) .append("\n"); }); - request.onRequestContent((theRequest, content) -> group.append(toString(content, getCharset(theRequest.getHeaders())))); + request.onRequestContent((theRequest, content) -> { + group.append(toString(content, getCharset(theRequest.getHeaders()))); + }); request.onRequestSuccess(theRequest -> { log.debug(group.toString()); group.delete(0, group.length()); @@ -35,13 +37,15 @@ public class RequestLogEnhancer { group.append("\n"); request.onResponseBegin(theResponse -> { group - .append("Response \n ") + .append("Response \n") .append(theResponse.getVersion()) .append(" ") .append(theResponse.getStatus()); - if (theResponse.getReason() != null) group - .append(" ") - .append(theResponse.getReason()); + if (theResponse.getReason() != null) { + group + .append(" ") + .append(theResponse.getReason()); + } group.append("\n"); }); request.onResponseHeaders(theResponse -> { @@ -50,39 +54,39 @@ public class RequestLogEnhancer { .append(header) .append("\n"); }); - request.onResponseContent((theResponse, content) -> group.append(toString(content, getCharset(theResponse.getHeaders())))); + request.onResponseContent((theResponse, content) -> { + group.append(toString(content, getCharset(theResponse.getHeaders()))); + }); request.onResponseSuccess(theResponse -> { log.debug(group.toString()); }); return request; } - - private String toString(ByteBuffer buffer, Charset charset) - { + private String toString(ByteBuffer buffer, Charset charset) { byte[] bytes; if (buffer.hasArray()) { bytes = new byte[buffer.capacity()]; - System.arraycopy(buffer.array(), 0, bytes, 0, buffer.capacity() ); - } - else - { + System.arraycopy(buffer.array(), 0, bytes, 0, buffer.capacity()); + } else { bytes = new byte[buffer.remaining()]; buffer.get(bytes, 0, bytes.length); } return new String(bytes, charset); } - private Charset getCharset(HttpFields headers) { String contentType = headers.get(HttpHeader.CONTENT_TYPE); - if (contentType == null) return StandardCharsets.UTF_8; - String[] tokens = contentType - .toLowerCase(Locale.US) - .split("charset="); - if (tokens.length != 2) return StandardCharsets.UTF_8; - String encoding = tokens[1].replaceAll("[;\"]", ""); - return Charset.forName(encoding); + if (contentType != null) { + String[] tokens = contentType + .toLowerCase(Locale.US) + .split("charset="); + if (tokens.length == 2) { + String encoding = tokens[1].replaceAll("[;\"]", ""); + return Charset.forName(encoding); + } + } + return StandardCharsets.UTF_8; } } diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java index df7c65f33f..529549f99b 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java @@ -3,35 +3,40 @@ package com.baeldung.reactive.logging.netty; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.logging.LoggingHandler; -import io.netty.util.internal.PlatformDependent; import java.nio.charset.Charset; +import static io.netty.util.internal.PlatformDependent.allocateUninitializedArray; +import static java.lang.Math.max; +import static java.nio.charset.Charset.defaultCharset; + public class CustomLogger extends LoggingHandler { public CustomLogger(Class clazz) { super(clazz); } @Override - protected String format(ChannelHandlerContext ctx, String eventName, Object arg) { + protected String format(ChannelHandlerContext ctx, String event, Object arg) { if (arg instanceof ByteBuf) { ByteBuf msg = (ByteBuf) arg; - return decodeString(msg, msg.readerIndex(), msg.readableBytes(), Charset.defaultCharset()); + return decode(msg, msg.readerIndex(), msg.readableBytes(), defaultCharset()); } return ""; } - private String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) { - if (len == 0) return ""; - byte[] array; - int offset; - if (src.hasArray()) { - array = src.array(); - offset = src.arrayOffset() + readerIndex; - } else { - array = PlatformDependent.allocateUninitializedArray(Math.max(len, 1024)); - offset = 0; - src.getBytes(readerIndex, array, 0, len); + private String decode(ByteBuf src, int readerIndex, int len, Charset charset) { + if (len != 0) { + byte[] array; + int offset; + if (src.hasArray()) { + array = src.array(); + offset = src.arrayOffset() + readerIndex; + } else { + array = allocateUninitializedArray(max(len, 1024)); + offset = 0; + src.getBytes(readerIndex, array, 0, len); + } + return new String(array, offset, len, charset); } - return new String(array, offset, len, charset); + return ""; } }