From e50e46425a09598b0c11fba05b24e23d4f129dc4 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 26 Jan 2010 05:17:52 +0000 Subject: [PATCH] Improved DefaultHttpMessage.toString() for easier debugging --- .../codec/http/DefaultHttpMessage.java | 29 +++++++++++++++++++ .../codec/http/DefaultHttpRequest.java | 22 +++++++++++++- .../codec/http/DefaultHttpResponse.java | 20 ++++++++++++- .../jboss/netty/util/internal/StringUtil.java | 16 ++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java index 7051bab7d8..ac324366b2 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java @@ -23,6 +23,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.handler.codec.http.HttpHeaders.Names; import org.jboss.netty.handler.codec.http.HttpHeaders.Values; +import org.jboss.netty.util.internal.StringUtil; /** * The default {@link HttpMessage} implementation. @@ -176,4 +177,32 @@ public class DefaultHttpMessage implements HttpMessage { public ChannelBuffer getContent() { return content; } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append(getClass().getSimpleName()); + buf.append("(version: "); + buf.append(getProtocolVersion().getText()); + buf.append(", keepAlive: "); + buf.append(isKeepAlive()); + buf.append(", chunked: "); + buf.append(isChunked()); + buf.append(')'); + buf.append(StringUtil.NEWLINE); + appendHeaders(buf); + + // Remove the last newline. + buf.setLength(buf.length() - StringUtil.NEWLINE.length()); + return buf.toString(); + } + + void appendHeaders(StringBuilder buf) { + for (Map.Entry e: getHeaders()) { + buf.append(e.getKey()); + buf.append(": "); + buf.append(e.getValue()); + buf.append(StringUtil.NEWLINE); + } + } } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpRequest.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpRequest.java index 45b97ba7af..53971163b7 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpRequest.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpRequest.java @@ -15,6 +15,8 @@ */ package org.jboss.netty.handler.codec.http; +import org.jboss.netty.util.internal.StringUtil; + /** * The default {@link HttpRequest} implementation. * @@ -65,6 +67,24 @@ public class DefaultHttpRequest extends DefaultHttpMessage implements HttpReques @Override public String toString() { - return getMethod().toString() + ' ' + getUri() + ' ' + getProtocolVersion().getText(); + StringBuilder buf = new StringBuilder(); + buf.append(getClass().getSimpleName()); + buf.append("(keepAlive: "); + buf.append(isKeepAlive()); + buf.append(", chunked: "); + buf.append(isChunked()); + buf.append(')'); + buf.append(StringUtil.NEWLINE); + buf.append(getMethod().toString()); + buf.append(' '); + buf.append(getUri()); + buf.append(' '); + buf.append(getProtocolVersion().getText()); + buf.append(StringUtil.NEWLINE); + appendHeaders(buf); + + // Remove the last newline. + buf.setLength(buf.length() - StringUtil.NEWLINE.length()); + return buf.toString(); } } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpResponse.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpResponse.java index 28e33dcfad..aa67c04d02 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpResponse.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpResponse.java @@ -15,6 +15,8 @@ */ package org.jboss.netty.handler.codec.http; +import org.jboss.netty.util.internal.StringUtil; + /** * The default {@link HttpResponse} implementation. * @@ -51,6 +53,22 @@ public class DefaultHttpResponse extends DefaultHttpMessage implements HttpRespo @Override public String toString() { - return getProtocolVersion().getText() + ' ' + getStatus().toString(); + StringBuilder buf = new StringBuilder(); + buf.append(getClass().getSimpleName()); + buf.append("(keepAlive: "); + buf.append(isKeepAlive()); + buf.append(", chunked: "); + buf.append(isChunked()); + buf.append(')'); + buf.append(StringUtil.NEWLINE); + buf.append(getProtocolVersion().getText()); + buf.append(' '); + buf.append(getStatus().toString()); + buf.append(StringUtil.NEWLINE); + appendHeaders(buf); + + // Remove the last newline. + buf.setLength(buf.length() - StringUtil.NEWLINE.length()); + return buf.toString(); } } diff --git a/src/main/java/org/jboss/netty/util/internal/StringUtil.java b/src/main/java/org/jboss/netty/util/internal/StringUtil.java index 73fe8aa258..e691c2fcb0 100644 --- a/src/main/java/org/jboss/netty/util/internal/StringUtil.java +++ b/src/main/java/org/jboss/netty/util/internal/StringUtil.java @@ -15,6 +15,8 @@ */ package org.jboss.netty.util.internal; +import java.util.Formatter; + /** * String utility class. * @@ -28,6 +30,20 @@ public class StringUtil { // Unused. } + public static final String NEWLINE; + + static { + String newLine = null; + + try { + newLine = new Formatter().format("%n").toString(); + } catch (Exception e) { + newLine = "\n"; + } + + NEWLINE = newLine; + } + /** * Strip an Object of it's ISO control characters. *