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
f398f2f7b5
commit
20d818ccec
@ -129,4 +129,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();
|
||||
}
|
||||
}
|
||||
|
@ -125,4 +125,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.
|
||||
*/
|
||||
@ -61,23 +57,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(protocolVersion().text())
|
||||
.append(", keepAlive: ")
|
||||
.append(HttpHeaderUtil.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) {
|
||||
@ -86,17 +65,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -104,22 +104,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(decoderResult());
|
||||
buf.append(')');
|
||||
buf.append(StringUtil.NEWLINE);
|
||||
buf.append(method());
|
||||
buf.append(' ');
|
||||
buf.append(uri());
|
||||
buf.append(' ');
|
||||
buf.append(protocolVersion().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();
|
||||
}
|
||||
}
|
||||
|
@ -77,20 +77,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(decoderResult());
|
||||
buf.append(')');
|
||||
buf.append(StringUtil.NEWLINE);
|
||||
buf.append(protocolVersion().text());
|
||||
buf.append(' ');
|
||||
buf.append(status());
|
||||
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.decoderResult());
|
||||
buf.append(", version: ");
|
||||
buf.append(msg.protocolVersion());
|
||||
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.decoderResult());
|
||||
buf.append(", version: ");
|
||||
buf.append(msg.protocolVersion());
|
||||
buf.append(", content: ");
|
||||
buf.append(msg.content());
|
||||
buf.append(')');
|
||||
buf.append(StringUtil.NEWLINE);
|
||||
}
|
||||
|
||||
private static void appendInitialLine(StringBuilder buf, HttpRequest req) {
|
||||
buf.append(req.method());
|
||||
buf.append(' ');
|
||||
buf.append(req.uri());
|
||||
buf.append(' ');
|
||||
buf.append(req.protocolVersion());
|
||||
buf.append(StringUtil.NEWLINE);
|
||||
}
|
||||
|
||||
private static void appendInitialLine(StringBuilder buf, HttpResponse res) {
|
||||
buf.append(res.protocolVersion());
|
||||
buf.append(' ');
|
||||
buf.append(res.status());
|
||||
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() { }
|
||||
}
|
@ -170,7 +170,7 @@ public class HttpObjectAggregator
|
||||
// If the client started to send data already, close because it's impossible to recover.
|
||||
// If keep-alive is off and 'Expect: 100-continue' is missing, no need to leave the connection open.
|
||||
if (oversized instanceof FullHttpMessage ||
|
||||
(!HttpHeaderUtil.is100ContinueExpected(oversized) && !HttpHeaderUtil.isKeepAlive(oversized))) {
|
||||
!HttpHeaderUtil.is100ContinueExpected(oversized) && !HttpHeaderUtil.isKeepAlive(oversized)) {
|
||||
future.addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
@ -358,6 +358,11 @@ public class HttpObjectAggregator
|
||||
super.setProtocolVersion(version);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpMessageUtil.appendFullRequest(new StringBuilder(256), this).toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AggregatedFullHttpResponse extends AggregatedFullHttpMessage
|
||||
@ -429,5 +434,10 @@ public class HttpObjectAggregator
|
||||
super.touch();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpMessageUtil.appendFullResponse(new StringBuilder(256), this).toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user