NETTY-295 HTTP header getters/setters for date headers
* Added get/setDateHeader() methods that works with custom date headers * Removed get/setDateInMilliseconds() methods because they can be simply represented with a chained getTime() call * Fixed the inconsistent behavior & contract of HttpHeaders.getContentLength() which does not throw an exception when the header is missing or ill-formatted unless a default value is specified * Added clearHeaders() and removeHeader() to HttpHeaders * Tabs to spaces
This commit is contained in:
parent
94cff9d041
commit
9db6bba913
@ -109,7 +109,7 @@ class AcceptedServerChannelRequestDispatch extends SimpleChannelUpstreamHandler
|
|||||||
LOG.debug("send data request received for tunnel " + tunnelId);
|
LOG.debug("send data request received for tunnel " + tunnelId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (HttpHeaders.getContentLength(request) == 0 ||
|
if (HttpHeaders.getContentLength(request, 0) == 0 ||
|
||||||
request.getContent() == null ||
|
request.getContent() == null ||
|
||||||
request.getContent().readableBytes() == 0) {
|
request.getContent().readableBytes() == 0) {
|
||||||
respondWithRejection(ctx, request,
|
respondWithRejection(ctx, request,
|
||||||
|
@ -241,7 +241,7 @@ public class HttpTunnelMessageUtils {
|
|||||||
public static boolean hasContents(HttpResponse response,
|
public static boolean hasContents(HttpResponse response,
|
||||||
byte[] expectedContents) {
|
byte[] expectedContents) {
|
||||||
if (response.getContent() != null &&
|
if (response.getContent() != null &&
|
||||||
HttpHeaders.getContentLength(response) == expectedContents.length &&
|
HttpHeaders.getContentLength(response, 0) == expectedContents.length &&
|
||||||
response.getContent().readableBytes() == expectedContents.length) {
|
response.getContent().readableBytes() == expectedContents.length) {
|
||||||
byte[] compareBytes = new byte[expectedContents.length];
|
byte[] compareBytes = new byte[expectedContents.length];
|
||||||
response.getContent().readBytes(compareBytes);
|
response.getContent().readBytes(compareBytes);
|
||||||
@ -300,7 +300,7 @@ public class HttpTunnelMessageUtils {
|
|||||||
|
|
||||||
public static Object extractErrorMessage(HttpResponse response) {
|
public static Object extractErrorMessage(HttpResponse response) {
|
||||||
if (response.getContent() == null ||
|
if (response.getContent() == null ||
|
||||||
HttpHeaders.getContentLength(response) == 0) {
|
HttpHeaders.getContentLength(response, 0) == 0) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.handler.codec.http;
|
package org.jboss.netty.handler.codec.http;
|
||||||
|
|
||||||
import java.text.DateFormat;
|
|
||||||
import java.text.ParsePosition;
|
import java.text.ParsePosition;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -31,61 +30,63 @@ import java.util.TimeZone;
|
|||||||
* <li>Sun, 06 Nov 1994 08:49:37 GMT: obsolete specification</li>
|
* <li>Sun, 06 Nov 1994 08:49:37 GMT: obsolete specification</li>
|
||||||
* <li>Sun Nov 6 08:49:37 1994: obsolete specification</li>
|
* <li>Sun Nov 6 08:49:37 1994: obsolete specification</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||||
* @author <a href="http://www.rogiel.com/">Rogiel Josias Sulzbach</a>
|
* @author <a href="http://www.rogiel.com/">Rogiel Josias Sulzbach</a>
|
||||||
* @version $Rev$, $Date$
|
* @version $Rev$, $Date$
|
||||||
*/
|
*/
|
||||||
final class HttpHeaderDateFormat extends SimpleDateFormat {
|
final class HttpHeaderDateFormat extends SimpleDateFormat {
|
||||||
private static final long serialVersionUID = -925286159755905325L;
|
private static final long serialVersionUID = -925286159755905325L;
|
||||||
|
|
||||||
private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1();
|
private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1();
|
||||||
private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2();
|
private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Standard date format<p>
|
* Standard date format<p>
|
||||||
* Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z
|
* Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z
|
||||||
*/
|
*/
|
||||||
HttpHeaderDateFormat() {
|
HttpHeaderDateFormat() {
|
||||||
super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
|
super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
|
||||||
setTimeZone(TimeZone.getTimeZone("GMT"));
|
setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Date parse(String text, ParsePosition pos) {
|
public Date parse(String text, ParsePosition pos) {
|
||||||
Date date = super.parse(text, pos);
|
Date date = super.parse(text, pos);
|
||||||
if (date == null)
|
if (date == null) {
|
||||||
date = format1.parse(text, pos);
|
date = format1.parse(text, pos);
|
||||||
if (date == null)
|
}
|
||||||
date = format2.parse(text, pos);
|
if (date == null) {
|
||||||
return date;
|
date = format2.parse(text, pos);
|
||||||
}
|
}
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* First obsolete format<p>
|
* First obsolete format<p>
|
||||||
* Sunday, 06-Nov-94 08:49:37 GMT -> E, d-MMM-y HH:mm:ss z
|
* Sunday, 06-Nov-94 08:49:37 GMT -> E, d-MMM-y HH:mm:ss z
|
||||||
*/
|
*/
|
||||||
private final class HttpHeaderDateFormatObsolete1 extends SimpleDateFormat {
|
private final class HttpHeaderDateFormatObsolete1 extends SimpleDateFormat {
|
||||||
private static final long serialVersionUID = -3178072504225114298L;
|
private static final long serialVersionUID = -3178072504225114298L;
|
||||||
|
|
||||||
HttpHeaderDateFormatObsolete1() {
|
HttpHeaderDateFormatObsolete1() {
|
||||||
super("E, dd-MMM-y HH:mm:ss z", Locale.ENGLISH);
|
super("E, dd-MMM-y HH:mm:ss z", Locale.ENGLISH);
|
||||||
setTimeZone(TimeZone.getTimeZone("GMT"));
|
setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Second obsolete format
|
* Second obsolete format
|
||||||
* <p>
|
* <p>
|
||||||
* Sun Nov 6 08:49:37 1994 -> EEE, MMM d HH:mm:ss yyyy
|
* Sun Nov 6 08:49:37 1994 -> EEE, MMM d HH:mm:ss yyyy
|
||||||
*/
|
*/
|
||||||
private final class HttpHeaderDateFormatObsolete2 extends SimpleDateFormat {
|
private final class HttpHeaderDateFormatObsolete2 extends SimpleDateFormat {
|
||||||
private static final long serialVersionUID = 3010674519968303714L;
|
private static final long serialVersionUID = 3010674519968303714L;
|
||||||
|
|
||||||
HttpHeaderDateFormatObsolete2() {
|
HttpHeaderDateFormatObsolete2() {
|
||||||
super("E MMM d HH:mm:ss yyyy", Locale.ENGLISH);
|
super("E MMM d HH:mm:ss yyyy", Locale.ENGLISH);
|
||||||
setTimeZone(TimeZone.getTimeZone("GMT"));
|
setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package org.jboss.netty.handler.codec.http;
|
package org.jboss.netty.handler.codec.http;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -36,7 +37,6 @@ import java.util.TreeSet;
|
|||||||
* @apiviz.stereotype static
|
* @apiviz.stereotype static
|
||||||
*/
|
*/
|
||||||
public class HttpHeaders {
|
public class HttpHeaders {
|
||||||
private static final HttpHeaderDateFormat dateFormat = new HttpHeaderDateFormat();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Standard HTTP header names.
|
* Standard HTTP header names.
|
||||||
@ -532,6 +532,10 @@ public class HttpHeaders {
|
|||||||
/**
|
/**
|
||||||
* Sets a new header with the specified name and value. If there is an
|
* Sets a new header with the specified name and value. If there is an
|
||||||
* existing header with the same name, the existing header is removed.
|
* existing header with the same name, the existing header is removed.
|
||||||
|
* If the specified value is not a {@link String}, it is converted into a
|
||||||
|
* {@link String} by {@link Object#toString()}, except for {@link Date}
|
||||||
|
* and {@link Calendar} which are formatted to the date format defined in
|
||||||
|
* <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a>.
|
||||||
*/
|
*/
|
||||||
public static void setHeader(HttpMessage message, String name, Object value) {
|
public static void setHeader(HttpMessage message, String name, Object value) {
|
||||||
message.setHeader(name, value);
|
message.setHeader(name, value);
|
||||||
@ -540,6 +544,16 @@ public class HttpHeaders {
|
|||||||
/**
|
/**
|
||||||
* Sets a new header with the specified name and values. If there is an
|
* Sets a new header with the specified name and values. If there is an
|
||||||
* existing header with the same name, the existing header is removed.
|
* existing header with the same name, the existing header is removed.
|
||||||
|
* This method can be represented approximately as the following code:
|
||||||
|
* <pre>
|
||||||
|
* removeHeader(message, name);
|
||||||
|
* for (Object v: values) {
|
||||||
|
* if (v == null) {
|
||||||
|
* break;
|
||||||
|
* }
|
||||||
|
* addHeader(message, name, v);
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public static void setHeader(HttpMessage message, String name, Iterable<?> values) {
|
public static void setHeader(HttpMessage message, String name, Iterable<?> values) {
|
||||||
message.setHeader(name, values);
|
message.setHeader(name, values);
|
||||||
@ -547,11 +561,29 @@ public class HttpHeaders {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new header with the specified name and value.
|
* Adds a new header with the specified name and value.
|
||||||
|
* If the specified value is not a {@link String}, it is converted into a
|
||||||
|
* {@link String} by {@link Object#toString()}, except for {@link Date}
|
||||||
|
* and {@link Calendar} which are formatted to the date format defined in
|
||||||
|
* <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a>.
|
||||||
*/
|
*/
|
||||||
public static void addHeader(HttpMessage message, String name, Object value) {
|
public static void addHeader(HttpMessage message, String name, Object value) {
|
||||||
message.addHeader(name, value);
|
message.addHeader(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the header with the specified name.
|
||||||
|
*/
|
||||||
|
public static void removeHeader(HttpMessage message, String name) {
|
||||||
|
message.removeHeader(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all headers from the specified message.
|
||||||
|
*/
|
||||||
|
public static void clearHeaders(HttpMessage message) {
|
||||||
|
message.clearHeaders();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the integer header value with the specified header name. If
|
* Returns the integer header value with the specified header name. If
|
||||||
* there are more than one header value for the specified header name, the
|
* there are more than one header value for the specified header name, the
|
||||||
@ -564,7 +596,7 @@ public class HttpHeaders {
|
|||||||
public static int getIntHeader(HttpMessage message, String name) {
|
public static int getIntHeader(HttpMessage message, String name) {
|
||||||
String value = getHeader(message, name);
|
String value = getHeader(message, name);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
throw new NumberFormatException("null");
|
throw new NumberFormatException("header not found: " + name);
|
||||||
}
|
}
|
||||||
return Integer.parseInt(value);
|
return Integer.parseInt(value);
|
||||||
}
|
}
|
||||||
@ -613,17 +645,104 @@ public class HttpHeaders {
|
|||||||
message.addHeader(name, value);
|
message.addHeader(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the date header value with the specified header name. If
|
||||||
|
* there are more than one header value for the specified header name, the
|
||||||
|
* first value is returned.
|
||||||
|
*
|
||||||
|
* @return the header value
|
||||||
|
* @throws ParseException
|
||||||
|
* if there is no such header or the header value is not a formatted date
|
||||||
|
*/
|
||||||
|
public static Date getDateHeader(HttpMessage message, String name) throws ParseException {
|
||||||
|
String value = getHeader(message, name);
|
||||||
|
if (value == null) {
|
||||||
|
throw new ParseException("header not found: " + name, 0);
|
||||||
|
}
|
||||||
|
return new HttpHeaderDateFormat().parse(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the date header value with the specified header name. If
|
||||||
|
* there are more than one header value for the specified header name, the
|
||||||
|
* first value is returned.
|
||||||
|
*
|
||||||
|
* @return the header value or the {@code defaultValue} if there is no such
|
||||||
|
* header or the header value is not a formatted date
|
||||||
|
*/
|
||||||
|
public static Date getDateHeader(HttpMessage message, String name, Date defaultValue) {
|
||||||
|
final String value = getHeader(message, name);
|
||||||
|
if (value == null) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return new HttpHeaderDateFormat().parse(value);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a new date header with the specified name and value. If there
|
||||||
|
* is an existing header with the same name, the existing header is removed.
|
||||||
|
* The specified value is formatted as defined in
|
||||||
|
* <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a>
|
||||||
|
*/
|
||||||
|
public static void setDateHeader(HttpMessage message, String name, Date value) {
|
||||||
|
if (value != null) {
|
||||||
|
message.setHeader(name, new HttpHeaderDateFormat().format(value));
|
||||||
|
} else {
|
||||||
|
message.setHeader(name, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a new date header with the specified name and values. If there
|
||||||
|
* is an existing header with the same name, the existing header is removed.
|
||||||
|
* The specified values are formatted as defined in
|
||||||
|
* <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a>
|
||||||
|
*/
|
||||||
|
public static void setDateHeader(HttpMessage message, String name, Iterable<Date> values) {
|
||||||
|
message.setHeader(name, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new date header with the specified name and value. The specified
|
||||||
|
* value is formatted as defined in <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a>
|
||||||
|
*/
|
||||||
|
public static void addDateHeader(HttpMessage message, String name, Date value) {
|
||||||
|
message.addHeader(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the length of the content. Please note that this value is
|
* Returns the length of the content. Please note that this value is
|
||||||
* not retrieved from {@link HttpMessage#getContent()} but from the
|
* not retrieved from {@link HttpMessage#getContent()} but from the
|
||||||
* {@code "Content-Length"} header, and thus they are independent from each
|
* {@code "Content-Length"} header, and thus they are independent from each
|
||||||
* other.
|
* other.
|
||||||
*
|
*
|
||||||
* @return the content length or {@code 0} if this message does not have
|
* @return the content length
|
||||||
* the {@code "Content-Length"} header
|
*
|
||||||
|
* @throws NumberFormatException
|
||||||
|
* if the message does not have the {@code "Content-Length"} header
|
||||||
|
* or its value is not a number
|
||||||
*/
|
*/
|
||||||
public static long getContentLength(HttpMessage message) {
|
public static long getContentLength(HttpMessage message) {
|
||||||
return getContentLength(message, 0L);
|
String value = getHeader(message, Names.CONTENT_LENGTH);
|
||||||
|
if (value != null) {
|
||||||
|
return Long.parseLong(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// We know the content length if it's a Web Socket message even if
|
||||||
|
// Content-Length header is missing.
|
||||||
|
long webSocketContentLength = getWebSocketContentLength(message);
|
||||||
|
if (webSocketContentLength >= 0) {
|
||||||
|
return webSocketContentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise we don't.
|
||||||
|
throw new NumberFormatException("header not found: " + Names.CONTENT_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -633,14 +752,35 @@ public class HttpHeaders {
|
|||||||
* other.
|
* other.
|
||||||
*
|
*
|
||||||
* @return the content length or {@code defaultValue} if this message does
|
* @return the content length or {@code defaultValue} if this message does
|
||||||
* not have the {@code "Content-Length"} header
|
* not have the {@code "Content-Length"} header or its value is not
|
||||||
|
* a number
|
||||||
*/
|
*/
|
||||||
public static long getContentLength(HttpMessage message, long defaultValue) {
|
public static long getContentLength(HttpMessage message, long defaultValue) {
|
||||||
String contentLength = message.getHeader(Names.CONTENT_LENGTH);
|
String contentLength = message.getHeader(Names.CONTENT_LENGTH);
|
||||||
if (contentLength != null) {
|
if (contentLength != null) {
|
||||||
return Long.parseLong(contentLength);
|
try {
|
||||||
|
return Long.parseLong(contentLength);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We know the content length if it's a Web Socket message even if
|
||||||
|
// Content-Length header is missing.
|
||||||
|
long webSocketContentLength = getWebSocketContentLength(message);
|
||||||
|
if (webSocketContentLength >= 0) {
|
||||||
|
return webSocketContentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise we don't.
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the content length of the specified web socket message. If the
|
||||||
|
* specified message is not a web socket message, {@code -1} is returned.
|
||||||
|
*/
|
||||||
|
private static int getWebSocketContentLength(HttpMessage message) {
|
||||||
// WebSockset messages have constant content-lengths.
|
// WebSockset messages have constant content-lengths.
|
||||||
if (message instanceof HttpRequest) {
|
if (message instanceof HttpRequest) {
|
||||||
HttpRequest req = (HttpRequest) message;
|
HttpRequest req = (HttpRequest) message;
|
||||||
@ -658,7 +798,8 @@ public class HttpHeaders {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultValue;
|
// Not a web socket message
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -689,79 +830,37 @@ public class HttpHeaders {
|
|||||||
public static void setHost(HttpMessage message, String value) {
|
public static void setHost(HttpMessage message, String value) {
|
||||||
message.setHeader(Names.HOST, value);
|
message.setHeader(Names.HOST, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the value of the {@code "Date"} header.
|
|
||||||
*/
|
|
||||||
public static Date getDate(HttpMessage message) {
|
|
||||||
return getDate(message, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value of the {@code "Date"} header. If there is no such
|
* Returns the value of the {@code "Date"} header.
|
||||||
* header, the {@code defaultValue} is returned.
|
*
|
||||||
*/
|
* @throws ParseException
|
||||||
public static Date getDate(HttpMessage message, Date defaultValue) {
|
* if there is no such header or the header value is not a formatted date
|
||||||
final String dateString = message.getHeader(Names.DATE);
|
*/
|
||||||
if (dateString == null) {
|
public static Date getDate(HttpMessage message) throws ParseException {
|
||||||
return defaultValue;
|
return getDateHeader(message, Names.DATE);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
return dateFormat.parse(dateString);
|
|
||||||
} catch (ParseException e) {
|
|
||||||
// is that correct?
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the {@code "Date"} header.
|
* Returns the value of the {@code "Date"} header. If there is no such
|
||||||
*/
|
* header or the header is not a formatted date, the {@code defaultValue}
|
||||||
public static void setDate(HttpMessage message, Date value) {
|
* is returned.
|
||||||
if (value != null) {
|
*/
|
||||||
message.setHeader(Names.DATE, dateFormat.format(value));
|
public static Date getDate(HttpMessage message, Date defaultValue) {
|
||||||
} else {
|
return getDateHeader(message, Names.DATE, defaultValue);
|
||||||
message.setHeader(Names.DATE, null);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value of the {@code "Date"} header.
|
* Sets the {@code "Date"} header.
|
||||||
*/
|
*/
|
||||||
public static long getDateInMilliseconds(HttpMessage message) {
|
public static void setDate(HttpMessage message, Date value) {
|
||||||
return getDate(message, null).getTime();
|
if (value != null) {
|
||||||
}
|
message.setHeader(Names.DATE, new HttpHeaderDateFormat().format(value));
|
||||||
|
} else {
|
||||||
/**
|
message.setHeader(Names.DATE, null);
|
||||||
* Returns the value of the {@code "Date"} header. If there is no such
|
}
|
||||||
* header, the {@code defaultValue} is returned.
|
}
|
||||||
*/
|
|
||||||
public static long getDateInMilliseconds(HttpMessage message,
|
|
||||||
long defaultValue) {
|
|
||||||
Date date = getDate(message, null);
|
|
||||||
if (date == null) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
return date.getTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the {@code "Date"} header.
|
|
||||||
*
|
|
||||||
* @param message
|
|
||||||
* the {@link HttpMessage}
|
|
||||||
* @param date
|
|
||||||
* the date in milliseconds
|
|
||||||
*/
|
|
||||||
public static void setDateInMilliseconds(HttpMessage message, long date) {
|
|
||||||
final Date value = new Date(date);
|
|
||||||
if (date > 0) {
|
|
||||||
message.setHeader(Names.DATE, dateFormat.format(value));
|
|
||||||
} else {
|
|
||||||
message.setHeader(Names.DATE, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns {@code true} if and only if the specified message contains the
|
* Returns {@code true} if and only if the specified message contains the
|
||||||
@ -1052,6 +1151,18 @@ public class HttpHeaders {
|
|||||||
if (value == null) {
|
if (value == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (value instanceof String) {
|
||||||
|
return (String) value;
|
||||||
|
}
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return value.toString();
|
||||||
|
}
|
||||||
|
if (value instanceof Date) {
|
||||||
|
return new HttpHeaderDateFormat().format((Date) value);
|
||||||
|
}
|
||||||
|
if (value instanceof Calendar) {
|
||||||
|
return new HttpHeaderDateFormat().format(((Calendar) value).getTime());
|
||||||
|
}
|
||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.handler.codec.http;
|
package org.jboss.netty.handler.codec.http;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -100,18 +102,36 @@ public interface HttpMessage {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new header with the specified name and value.
|
* Adds a new header with the specified name and value.
|
||||||
|
* If the specified value is not a {@link String}, it is converted into a
|
||||||
|
* {@link String} by {@link Object#toString()}, except for {@link Date}
|
||||||
|
* and {@link Calendar} which are formatted to the date format defined in
|
||||||
|
* <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a>.
|
||||||
*/
|
*/
|
||||||
void addHeader(String name, Object value);
|
void addHeader(String name, Object value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a new header with the specified name and value. If there is an
|
* Sets a new header with the specified name and value. If there is an
|
||||||
* existing header with the same name, the existing header is removed.
|
* existing header with the same name, the existing header is removed.
|
||||||
|
* If the specified value is not a {@link String}, it is converted into a
|
||||||
|
* {@link String} by {@link Object#toString()}, except for {@link Date}
|
||||||
|
* and {@link Calendar} which are formatted to the date format defined in
|
||||||
|
* <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a>.
|
||||||
*/
|
*/
|
||||||
void setHeader(String name, Object value);
|
void setHeader(String name, Object value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a new header with the specified name and values. If there is an
|
* Sets a new header with the specified name and values. If there is an
|
||||||
* existing header with the same name, the existing header is removed.
|
* existing header with the same name, the existing header is removed.
|
||||||
|
* This method can be represented approximately as the following code:
|
||||||
|
* <pre>
|
||||||
|
* m.removeHeader(name);
|
||||||
|
* for (Object v: values) {
|
||||||
|
* if (v == null) {
|
||||||
|
* break;
|
||||||
|
* }
|
||||||
|
* m.addHeader(name, v);
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
*/
|
*/
|
||||||
void setHeader(String name, Iterable<?> values);
|
void setHeader(String name, Iterable<?> values);
|
||||||
|
|
||||||
|
@ -8,51 +8,51 @@ import junit.framework.Assert;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class HttpHeaderDateFormatTest {
|
public class HttpHeaderDateFormatTest {
|
||||||
/**
|
/**
|
||||||
* This date is set at "06 Nov 1994 08:49:37 GMT" (same used in example in
|
* This date is set at "06 Nov 1994 08:49:37 GMT" (same used in example in
|
||||||
* RFC documentation)
|
* RFC documentation)
|
||||||
* <p>
|
* <p>
|
||||||
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
|
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
|
||||||
*/
|
*/
|
||||||
private static final Date DATE = new Date(784111777000L);
|
private static final Date DATE = new Date(784111777000L);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse() throws ParseException {
|
public void testParse() throws ParseException {
|
||||||
HttpHeaderDateFormat format = new HttpHeaderDateFormat();
|
HttpHeaderDateFormat format = new HttpHeaderDateFormat();
|
||||||
|
|
||||||
{
|
{
|
||||||
final Date parsed = format.parse("Sun, 6 Nov 1994 08:49:37 GMT");
|
final Date parsed = format.parse("Sun, 6 Nov 1994 08:49:37 GMT");
|
||||||
Assert.assertNotNull(parsed);
|
Assert.assertNotNull(parsed);
|
||||||
Assert.assertEquals(DATE, parsed);
|
Assert.assertEquals(DATE, parsed);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
final Date parsed = format.parse("Sun, 06 Nov 1994 08:49:37 GMT");
|
final Date parsed = format.parse("Sun, 06 Nov 1994 08:49:37 GMT");
|
||||||
Assert.assertNotNull(parsed);
|
Assert.assertNotNull(parsed);
|
||||||
Assert.assertEquals(DATE, parsed);
|
Assert.assertEquals(DATE, parsed);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
final Date parsed = format.parse("Sunday, 06-Nov-94 08:49:37 GMT");
|
final Date parsed = format.parse("Sunday, 06-Nov-94 08:49:37 GMT");
|
||||||
Assert.assertNotNull(parsed);
|
Assert.assertNotNull(parsed);
|
||||||
Assert.assertEquals(DATE, parsed);
|
Assert.assertEquals(DATE, parsed);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
final Date parsed = format.parse("Sunday, 6-Nov-94 08:49:37 GMT");
|
final Date parsed = format.parse("Sunday, 6-Nov-94 08:49:37 GMT");
|
||||||
Assert.assertNotNull(parsed);
|
Assert.assertNotNull(parsed);
|
||||||
Assert.assertEquals(DATE, parsed);
|
Assert.assertEquals(DATE, parsed);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
final Date parsed = format.parse("Sun Nov 6 08:49:37 1994");
|
final Date parsed = format.parse("Sun Nov 6 08:49:37 1994");
|
||||||
Assert.assertNotNull(parsed);
|
Assert.assertNotNull(parsed);
|
||||||
Assert.assertEquals(DATE, parsed);
|
Assert.assertEquals(DATE, parsed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFormat() {
|
public void testFormat() {
|
||||||
HttpHeaderDateFormat format = new HttpHeaderDateFormat();
|
HttpHeaderDateFormat format = new HttpHeaderDateFormat();
|
||||||
|
|
||||||
final String formatted = format.format(DATE);
|
final String formatted = format.format(DATE);
|
||||||
Assert.assertNotNull(formatted);
|
Assert.assertNotNull(formatted);
|
||||||
Assert.assertEquals("Sun, 06 Nov 1994 08:49:37 GMT", formatted);
|
Assert.assertEquals("Sun, 06 Nov 1994 08:49:37 GMT", formatted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user