Improved DefaultHttpMessage.toString() for easier debugging

This commit is contained in:
Trustin Lee 2010-01-26 05:17:52 +00:00
parent d0e886c344
commit e50e46425a
4 changed files with 85 additions and 2 deletions

View File

@ -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<String, String> e: getHeaders()) {
buf.append(e.getKey());
buf.append(": ");
buf.append(e.getValue());
buf.append(StringUtil.NEWLINE);
}
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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.
*