From a3c68b3f30f02d33ad6e063aa01e40ee2ae4ce37 Mon Sep 17 00:00:00 2001 From: Anton Smolich Date: Thu, 29 Mar 2018 12:13:53 -0700 Subject: [PATCH] Deferred print of headers until all Interceptors are done --- .../jersey/logging/ClientLoggingFilter.java | 15 ++++++++---- .../jersey/logging/LoggingInterceptor.java | 24 +++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/core-common/src/main/java/org/glassfish/jersey/logging/ClientLoggingFilter.java b/core-common/src/main/java/org/glassfish/jersey/logging/ClientLoggingFilter.java index 30fb3a95c1..97d64d2479 100644 --- a/core-common/src/main/java/org/glassfish/jersey/logging/ClientLoggingFilter.java +++ b/core-common/src/main/java/org/glassfish/jersey/logging/ClientLoggingFilter.java @@ -42,6 +42,7 @@ import java.io.IOException; import java.io.OutputStream; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; @@ -99,18 +100,22 @@ public void filter(final ClientRequestContext context) throws IOException { final long id = _id.incrementAndGet(); context.setProperty(LOGGING_ID_PROPERTY, id); - final StringBuilder b = new StringBuilder(); + Supplier sbs = () -> { + final StringBuilder b = new StringBuilder(); + + printRequestLine(b, "Sending client request", id, context.getMethod(), context.getUri()); + printPrefixedHeaders(b, id, REQUEST_PREFIX, context::getStringHeaders); - printRequestLine(b, "Sending client request", id, context.getMethod(), context.getUri()); - printPrefixedHeaders(b, id, REQUEST_PREFIX, context.getStringHeaders()); + return b; + }; if (context.hasEntity() && printEntity(verbosity, context.getMediaType())) { - final OutputStream stream = new LoggingStream(b, context.getEntityStream()); + final OutputStream stream = new LoggingStream(sbs, context.getEntityStream()); context.setEntityStream(stream); context.setProperty(ENTITY_LOGGER_PROPERTY, stream); // not calling log(b) here - it will be called by the interceptor } else { - log(b); + log(sbs.get()); } } diff --git a/core-common/src/main/java/org/glassfish/jersey/logging/LoggingInterceptor.java b/core-common/src/main/java/org/glassfish/jersey/logging/LoggingInterceptor.java index f9a255fab4..231330357d 100644 --- a/core-common/src/main/java/org/glassfish/jersey/logging/LoggingInterceptor.java +++ b/core-common/src/main/java/org/glassfish/jersey/logging/LoggingInterceptor.java @@ -55,6 +55,7 @@ import java.util.Set; import java.util.TreeSet; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; @@ -197,6 +198,14 @@ void printPrefixedHeaders(final StringBuilder b, } } + void printPrefixedHeaders(final StringBuilder b, + final long id, + final String prefix, + final Supplier> lazyHeaders) { + MultivaluedMap headers = lazyHeaders.get(); + printPrefixedHeaders(b, id, prefix, headers); + } + Set>> getSortedHeaders(final Set>> headers) { final TreeSet>> sortedHeaders = new TreeSet>>(COMPARATOR); sortedHeaders.addAll(headers); @@ -266,7 +275,7 @@ static boolean printEntity(Verbosity verbosity, MediaType mediaType) { */ class LoggingStream extends FilterOutputStream { - private final StringBuilder b; + private final Supplier sbs; private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); /** @@ -276,15 +285,26 @@ class LoggingStream extends FilterOutputStream { * @param inner the underlying output stream. */ LoggingStream(final StringBuilder b, final OutputStream inner) { + this(() -> b, inner); + } + + /** + * Creates {@code LoggingStream} with the entity supplier and the underlying output stream as parameters. + * + * @param sbs contains the entity supplier to log. + * @param inner the underlying output stream. + */ + LoggingStream(final Supplier sbs, final OutputStream inner) { super(inner); - this.b = b; + this.sbs = sbs; } StringBuilder getStringBuilder(final Charset charset) { // write entity to the builder final byte[] entity = baos.toByteArray(); + StringBuilder b = sbs.get(); b.append(new String(entity, 0, Math.min(entity.length, maxEntitySize), charset)); if (entity.length > maxEntitySize) { b.append("...more...");