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:
parent
63536c3d73
commit
f819b24f1c
@ -117,4 +117,9 @@ public class DefaultFullHttpRequest extends DefaultHttpRequest implements FullHt
|
||||
duplicate.trailingHeaders().set(trailingHeaders());
|
||||
return duplicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpMessageUtil.appendFullRequest(new StringBuilder(256), this).toString();
|
||||
}
|
||||
}
|
||||
|
@ -113,4 +113,9 @@ public class DefaultFullHttpResponse extends DefaultHttpResponse implements Full
|
||||
duplicate.trailingHeaders().set(trailingHeaders());
|
||||
return duplicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpMessageUtil.appendFullResponse(new StringBuilder(256), this).toString();
|
||||
}
|
||||
}
|
||||
|
@ -15,10 +15,6 @@
|
||||
*/
|
||||
package io.netty.handler.codec.http;
|
||||
|
||||
import io.netty.util.internal.StringUtil;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The default {@link HttpMessage} implementation.
|
||||
*/
|
||||
@ -52,24 +48,6 @@ public abstract class DefaultHttpMessage extends DefaultHttpObject implements Ht
|
||||
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
|
||||
public HttpMessage setProtocolVersion(HttpVersion version) {
|
||||
if (version == null) {
|
||||
@ -78,17 +56,4 @@ public abstract class DefaultHttpMessage extends DefaultHttpObject implements Ht
|
||||
this.version = version;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,8 +15,6 @@
|
||||
*/
|
||||
package io.netty.handler.codec.http;
|
||||
|
||||
import io.netty.util.internal.StringUtil;
|
||||
|
||||
/**
|
||||
* The default {@link HttpRequest} implementation.
|
||||
*/
|
||||
@ -92,22 +90,6 @@ public class DefaultHttpRequest extends DefaultHttpMessage implements HttpReques
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
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();
|
||||
return HttpMessageUtil.appendRequest(new StringBuilder(256), this).toString();
|
||||
}
|
||||
}
|
||||
|
@ -15,8 +15,6 @@
|
||||
*/
|
||||
package io.netty.handler.codec.http;
|
||||
|
||||
import io.netty.util.internal.StringUtil;
|
||||
|
||||
/**
|
||||
* The default {@link HttpResponse} implementation.
|
||||
*/
|
||||
@ -71,20 +69,6 @@ public class DefaultHttpResponse extends DefaultHttpMessage implements HttpRespo
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
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();
|
||||
return HttpMessageUtil.appendResponse(new StringBuilder(256), this).toString();
|
||||
}
|
||||
}
|
||||
|
@ -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() { }
|
||||
}
|
@ -403,6 +403,11 @@ public class HttpObjectAggregator extends MessageToMessageDecoder<HttpObject> {
|
||||
super.setProtocolVersion(version);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpMessageUtil.appendFullRequest(new StringBuilder(256), this).toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AggregatedFullHttpResponse extends AggregatedFullHttpMessage
|
||||
@ -457,5 +462,10 @@ public class HttpObjectAggregator extends MessageToMessageDecoder<HttpObject> {
|
||||
super.retain();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpMessageUtil.appendFullResponse(new StringBuilder(256), this).toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user