Implement toString() for all HttpMessage implementations

Related: #3019

Motivation:

We have multiple (Full)HttpRequest/Response implementations and only
some of them implements toString() properly.

Modifications:

- Add the reusable string converter for HttpMessages to HttpMessageUtil
- Implement toString() of (Full)HttpRequest/Response implementations
  properly using HttpMessageUtil

Result:

Prettier string representation is returned by HttpMessage
implementations.
This commit is contained in:
Trustin Lee 2014-12-31 18:32:48 +09:00
parent 63536c3d73
commit f819b24f1c
7 changed files with 136 additions and 71 deletions

View File

@ -117,4 +117,9 @@ public class DefaultFullHttpRequest extends DefaultHttpRequest implements FullHt
duplicate.trailingHeaders().set(trailingHeaders()); duplicate.trailingHeaders().set(trailingHeaders());
return duplicate; return duplicate;
} }
@Override
public String toString() {
return HttpMessageUtil.appendFullRequest(new StringBuilder(256), this).toString();
}
} }

View File

@ -113,4 +113,9 @@ public class DefaultFullHttpResponse extends DefaultHttpResponse implements Full
duplicate.trailingHeaders().set(trailingHeaders()); duplicate.trailingHeaders().set(trailingHeaders());
return duplicate; return duplicate;
} }
@Override
public String toString() {
return HttpMessageUtil.appendFullResponse(new StringBuilder(256), this).toString();
}
} }

View File

@ -15,10 +15,6 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.util.internal.StringUtil;
import java.util.Map;
/** /**
* The default {@link HttpMessage} implementation. * The default {@link HttpMessage} implementation.
*/ */
@ -52,24 +48,6 @@ public abstract class DefaultHttpMessage extends DefaultHttpObject implements Ht
return version; return version;
} }
@Override
public String toString() {
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append("(version: ")
.append(getProtocolVersion().text())
.append(", keepAlive: ")
.append(HttpHeaders.isKeepAlive(this))
.append(')')
.append(StringUtil.NEWLINE);
appendHeaders(buf);
// Remove the last newline.
buf.setLength(buf.length() - StringUtil.NEWLINE.length());
return buf.toString();
}
@Override @Override
public HttpMessage setProtocolVersion(HttpVersion version) { public HttpMessage setProtocolVersion(HttpVersion version) {
if (version == null) { if (version == null) {
@ -78,17 +56,4 @@ public abstract class DefaultHttpMessage extends DefaultHttpObject implements Ht
this.version = version; this.version = version;
return this; return this;
} }
void appendHeaders(StringBuilder buf) {
appendHeaders(buf, headers());
}
void appendHeaders(StringBuilder buf, HttpHeaders headers) {
for (Map.Entry<String, String> e: headers) {
buf.append(e.getKey())
.append(": ")
.append(e.getValue())
.append(StringUtil.NEWLINE);
}
}
} }

View File

@ -15,8 +15,6 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.util.internal.StringUtil;
/** /**
* The default {@link HttpRequest} implementation. * The default {@link HttpRequest} implementation.
*/ */
@ -92,22 +90,6 @@ public class DefaultHttpRequest extends DefaultHttpMessage implements HttpReques
@Override @Override
public String toString() { public String toString() {
StringBuilder buf = new StringBuilder(); return HttpMessageUtil.appendRequest(new StringBuilder(256), this).toString();
buf.append(StringUtil.simpleClassName(this));
buf.append("(decodeResult: ");
buf.append(getDecoderResult());
buf.append(')');
buf.append(StringUtil.NEWLINE);
buf.append(getMethod());
buf.append(' ');
buf.append(getUri());
buf.append(' ');
buf.append(getProtocolVersion().text());
buf.append(StringUtil.NEWLINE);
appendHeaders(buf);
// Remove the last newline.
buf.setLength(buf.length() - StringUtil.NEWLINE.length());
return buf.toString();
} }
} }

View File

@ -15,8 +15,6 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.util.internal.StringUtil;
/** /**
* The default {@link HttpResponse} implementation. * The default {@link HttpResponse} implementation.
*/ */
@ -71,20 +69,6 @@ public class DefaultHttpResponse extends DefaultHttpMessage implements HttpRespo
@Override @Override
public String toString() { public String toString() {
StringBuilder buf = new StringBuilder(); return HttpMessageUtil.appendResponse(new StringBuilder(256), this).toString();
buf.append(StringUtil.simpleClassName(this));
buf.append("(decodeResult: ");
buf.append(getDecoderResult());
buf.append(')');
buf.append(StringUtil.NEWLINE);
buf.append(getProtocolVersion().text());
buf.append(' ');
buf.append(getStatus());
buf.append(StringUtil.NEWLINE);
appendHeaders(buf);
// Remove the last newline.
buf.setLength(buf.length() - StringUtil.NEWLINE.length());
return buf.toString();
} }
} }

View File

@ -0,0 +1,114 @@
/*
* Copyright 2014 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.http;
import io.netty.util.internal.StringUtil;
import java.util.Map;
/**
* Provides some utility methods for HTTP message implementations.
*/
final class HttpMessageUtil {
static StringBuilder appendRequest(StringBuilder buf, HttpRequest req) {
appendCommon(buf, req);
appendInitialLine(buf, req);
appendHeaders(buf, req.headers());
removeLastNewLine(buf);
return buf;
}
static StringBuilder appendResponse(StringBuilder buf, HttpResponse res) {
appendCommon(buf, res);
appendInitialLine(buf, res);
appendHeaders(buf, res.headers());
removeLastNewLine(buf);
return buf;
}
private static void appendCommon(StringBuilder buf, HttpMessage msg) {
buf.append(StringUtil.simpleClassName(msg));
buf.append("(decodeResult: ");
buf.append(msg.getDecoderResult());
buf.append(", version: ");
buf.append(msg.getProtocolVersion());
buf.append(')');
buf.append(StringUtil.NEWLINE);
}
static StringBuilder appendFullRequest(StringBuilder buf, FullHttpRequest req) {
appendFullCommon(buf, req);
appendInitialLine(buf, req);
appendHeaders(buf, req.headers());
appendHeaders(buf, req.trailingHeaders());
removeLastNewLine(buf);
return buf;
}
static StringBuilder appendFullResponse(StringBuilder buf, FullHttpResponse res) {
appendFullCommon(buf, res);
appendInitialLine(buf, res);
appendHeaders(buf, res.headers());
appendHeaders(buf, res.trailingHeaders());
removeLastNewLine(buf);
return buf;
}
private static void appendFullCommon(StringBuilder buf, FullHttpMessage msg) {
buf.append(StringUtil.simpleClassName(msg));
buf.append("(decodeResult: ");
buf.append(msg.getDecoderResult());
buf.append(", version: ");
buf.append(msg.getProtocolVersion());
buf.append(", content: ");
buf.append(msg.content());
buf.append(')');
buf.append(StringUtil.NEWLINE);
}
private static void appendInitialLine(StringBuilder buf, HttpRequest req) {
buf.append(req.getMethod());
buf.append(' ');
buf.append(req.getUri());
buf.append(' ');
buf.append(req.getProtocolVersion());
buf.append(StringUtil.NEWLINE);
}
private static void appendInitialLine(StringBuilder buf, HttpResponse res) {
buf.append(res.getProtocolVersion());
buf.append(' ');
buf.append(res.getStatus());
buf.append(StringUtil.NEWLINE);
}
private static void appendHeaders(StringBuilder buf, HttpHeaders headers) {
for (Map.Entry<String, String> e: headers) {
buf.append(e.getKey());
buf.append(": ");
buf.append(e.getValue());
buf.append(StringUtil.NEWLINE);
}
}
private static void removeLastNewLine(StringBuilder buf) {
buf.setLength(buf.length() - StringUtil.NEWLINE.length());
}
private HttpMessageUtil() { }
}

View File

@ -403,6 +403,11 @@ public class HttpObjectAggregator extends MessageToMessageDecoder<HttpObject> {
super.setProtocolVersion(version); super.setProtocolVersion(version);
return this; return this;
} }
@Override
public String toString() {
return HttpMessageUtil.appendFullRequest(new StringBuilder(256), this).toString();
}
} }
private static final class AggregatedFullHttpResponse extends AggregatedFullHttpMessage private static final class AggregatedFullHttpResponse extends AggregatedFullHttpMessage
@ -457,5 +462,10 @@ public class HttpObjectAggregator extends MessageToMessageDecoder<HttpObject> {
super.retain(); super.retain();
return this; return this;
} }
@Override
public String toString() {
return HttpMessageUtil.appendFullResponse(new StringBuilder(256), this).toString();
}
} }
} }