Reapply changes to HttpHeaders without indenting problems so that we can

make sure of additions.
This commit is contained in:
vibul 2012-04-27 09:48:15 +10:00
parent 66b4735acd
commit e8b016461c

View File

@ -21,10 +21,10 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
/** /**
* Provides the constants for the standard HTTP header names and values and * Provides the constants for the standard HTTP header names and values and
* commonly used utility methods that accesses an {@link HttpMessage}. * commonly used utility methods that accesses an {@link HttpMessage}.
*
* @apiviz.landmark * @apiviz.landmark
* @apiviz.stereotype static * @apiviz.stereotype static
*/ */
@ -32,7 +32,6 @@ public class HttpHeaders {
/** /**
* Standard HTTP header names. * Standard HTTP header names.
*
* @apiviz.stereotype static * @apiviz.stereotype static
*/ */
public static final class Names { public static final class Names {
@ -304,7 +303,6 @@ public class HttpHeaders {
/** /**
* Standard HTTP header values. * Standard HTTP header values.
*
* @apiviz.stereotype static * @apiviz.stereotype static
*/ */
public static final class Values { public static final class Values {
@ -323,7 +321,7 @@ public class HttpHeaders {
/** /**
* {@code "boundary"} * {@code "boundary"}
*/ */
static final String BOUNDARY = "boundary"; static final String BOUNDARY = "boundary";
/** /**
* {@code "bytes"} * {@code "bytes"}
*/ */
@ -347,7 +345,7 @@ public class HttpHeaders {
/** /**
* {@code "100-continue"} * {@code "100-continue"}
*/ */
public static final String CONTINUE = "100-continue"; public static final String CONTINUE = "100-continue";
/** /**
* {@code "deflate"} * {@code "deflate"}
*/ */
@ -379,7 +377,7 @@ public class HttpHeaders {
/** /**
* {@code "multipart/form-data"} * {@code "multipart/form-data"}
*/ */
static final String MULTIPART_FORM_DATA = "multipart/form-data"; static final String MULTIPART_FORM_DATA = "multipart/form-data";
/** /**
* {@code "must-revalidate"} * {@code "must-revalidate"}
*/ */
@ -442,9 +440,10 @@ public class HttpHeaders {
} }
} }
/** /**
* Returns {@code true} if and only if the connection can remain open and * Returns {@code true} if and only if the connection can remain open and
* thus 'kept alive'. This methods respects the value of the * thus 'kept alive'. This methods respects the value of the
* {@code "Connection"} header first and then the return value of * {@code "Connection"} header first and then the return value of
* {@link HttpVersion#isKeepAliveDefault()}. * {@link HttpVersion#isKeepAliveDefault()}.
*/ */
@ -463,23 +462,21 @@ public class HttpHeaders {
/** /**
* Sets the value of the {@code "Connection"} header depending on the * Sets the value of the {@code "Connection"} header depending on the
* protocol version of the specified message. This method sets or removes * protocol version of the specified message. This method sets or removes
* the {@code "Connection"} header depending on what the default keep alive * the {@code "Connection"} header depending on what the default keep alive
* mode of the message's protocol version is, as specified by * mode of the message's protocol version is, as specified by
* {@link HttpVersion#isKeepAliveDefault()}. * {@link HttpVersion#isKeepAliveDefault()}.
* <ul> * <ul>
* <li>If the connection is kept alive by default: * <li>If the connection is kept alive by default:
* <ul> * <ul>
* <li>set to {@code "close"} if {@code keepAlive} is {@code false}.</li> * <li>set to {@code "close"} if {@code keepAlive} is {@code false}.</li>
* <li>remove otherwise.</li> * <li>remove otherwise.</li>
* </ul> * </ul></li>
* </li>
* <li>If the connection is closed by default: * <li>If the connection is closed by default:
* <ul> * <ul>
* <li>set to {@code "keep-alive"} if {@code keepAlive} is {@code true}.</li> * <li>set to {@code "keep-alive"} if {@code keepAlive} is {@code true}.</li>
* <li>remove otherwise.</li> * <li>remove otherwise.</li>
* </ul> * </ul></li>
* </li>
* </ul> * </ul>
*/ */
public static void setKeepAlive(HttpMessage message, boolean keepAlive) { public static void setKeepAlive(HttpMessage message, boolean keepAlive) {
@ -499,10 +496,10 @@ public class HttpHeaders {
} }
/** /**
* Returns the header value with the specified header name. If there are * Returns the header value with the specified header name. If there are
* more than one header value for the specified header name, the first value * more than one header value for the specified header name, the first
* is returned. * value is returned.
* *
* @return the header value or {@code null} if there is no such header * @return the header value or {@code null} if there is no such header
*/ */
public static String getHeader(HttpMessage message, String name) { public static String getHeader(HttpMessage message, String name) {
@ -510,15 +507,14 @@ public class HttpHeaders {
} }
/** /**
* Returns the header value with the specified header name. If there are * Returns the header value with the specified header name. If there are
* more than one header value for the specified header name, the first value * more than one header value for the specified header name, the first
* is returned. * value is returned.
* *
* @return the header value or the {@code defaultValue} if there is no such * @return the header value or the {@code defaultValue} if there is no such
* header * header
*/ */
public static String getHeader(HttpMessage message, String name, public static String getHeader(HttpMessage message, String name, String defaultValue) {
String defaultValue) {
String value = message.getHeader(name); String value = message.getHeader(name);
if (value == null) { if (value == null) {
return defaultValue; return defaultValue;
@ -527,7 +523,7 @@ 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.
*/ */
public static void setHeader(HttpMessage message, String name, Object value) { public static void setHeader(HttpMessage message, String name, Object value) {
@ -535,11 +531,10 @@ 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.
*/ */
public static void setHeader(HttpMessage message, String name, public static void setHeader(HttpMessage message, String name, Iterable<?> values) {
Iterable<?> values) {
message.setHeader(name, values); message.setHeader(name, values);
} }
@ -551,14 +546,13 @@ public class HttpHeaders {
} }
/** /**
* Returns the integer header value with the specified header name. If there * Returns the integer header value with the specified header name. If
* are more than one header value for the specified header name, the first * there are more than one header value for the specified header name, the
* value is returned. * first value is returned.
* *
* @return the header value * @return the header value
* @throws NumberFormatException * @throws NumberFormatException
* if there is no such header or the header value is not a * if there is no such header or the header value is not a number
* number
*/ */
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);
@ -569,15 +563,14 @@ public class HttpHeaders {
} }
/** /**
* Returns the integer header value with the specified header name. If there * Returns the integer header value with the specified header name. If
* are more than one header value for the specified header name, the first * there are more than one header value for the specified header name, the
* value is returned. * first value is returned.
* *
* @return the header value or the {@code defaultValue} if there is no such * @return the header value or the {@code defaultValue} if there is no such
* header or the header value is not a number * header or the header value is not a number
*/ */
public static int getIntHeader(HttpMessage message, String name, public static int getIntHeader(HttpMessage message, String name, int defaultValue) {
int defaultValue) {
String value = getHeader(message, name); String value = getHeader(message, name);
if (value == null) { if (value == null) {
return defaultValue; return defaultValue;
@ -591,19 +584,18 @@ public class HttpHeaders {
} }
/** /**
* Sets a new integer header with the specified name and value. If there is * Sets a new integer header with the specified name and value. If there
* an existing header with the same name, the existing header is removed. * is an existing header with the same name, the existing header is removed.
*/ */
public static void setIntHeader(HttpMessage message, String name, int value) { public static void setIntHeader(HttpMessage message, String name, int value) {
message.setHeader(name, value); message.setHeader(name, value);
} }
/** /**
* Sets a new integer header with the specified name and values. If there is * Sets a new integer header with the specified name and values. If there
* an existing header with the same name, the existing header is removed. * is an existing header with the same name, the existing header is removed.
*/ */
public static void setIntHeader(HttpMessage message, String name, public static void setIntHeader(HttpMessage message, String name, Iterable<Integer> values) {
Iterable<Integer> values) {
message.setHeader(name, values); message.setHeader(name, values);
} }
@ -615,24 +607,24 @@ public class HttpHeaders {
} }
/** /**
* Returns the length of the content. Please note that this value is not * Returns the length of the content. Please note that this value is
* 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 the * @return the content length or {@code 0} if this message does not have
* {@code "Content-Length"} header * the {@code "Content-Length"} header
*/ */
public static long getContentLength(HttpMessage message) { public static long getContentLength(HttpMessage message) {
return getContentLength(message, 0L); return getContentLength(message, 0L);
} }
/** /**
* Returns the length of the content. Please note that this value is not * Returns the length of the content. Please note that this value is
* 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 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
*/ */
@ -645,16 +637,16 @@ public class HttpHeaders {
// 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;
if (HttpMethod.GET.equals(req.getMethod()) if (HttpMethod.GET.equals(req.getMethod()) &&
&& req.containsHeader(Names.SEC_WEBSOCKET_KEY1) req.containsHeader(Names.SEC_WEBSOCKET_KEY1) &&
&& req.containsHeader(Names.SEC_WEBSOCKET_KEY2)) { req.containsHeader(Names.SEC_WEBSOCKET_KEY2)) {
return 8; return 8;
} }
} else if (message instanceof HttpResponse) { } else if (message instanceof HttpResponse) {
HttpResponse res = (HttpResponse) message; HttpResponse res = (HttpResponse) message;
if (res.getStatus().getCode() == 101 if (res.getStatus().getCode() == 101 &&
&& res.containsHeader(Names.SEC_WEBSOCKET_ORIGIN) res.containsHeader(Names.SEC_WEBSOCKET_ORIGIN) &&
&& res.containsHeader(Names.SEC_WEBSOCKET_LOCATION)) { res.containsHeader(Names.SEC_WEBSOCKET_LOCATION)) {
return 16; return 16;
} }
} }
@ -677,7 +669,7 @@ public class HttpHeaders {
} }
/** /**
* Returns the value of the {@code "Host"} header. If there is no such * Returns the value of the {@code "Host"} header. If there is no such
* header, the {@code defaultValue} is returned. * header, the {@code defaultValue} is returned.
*/ */
public static String getHost(HttpMessage message, String defaultValue) { public static String getHost(HttpMessage message, String defaultValue) {
@ -715,8 +707,8 @@ public class HttpHeaders {
return true; return true;
} }
// Multiple 'Expect' headers. Search through them. // Multiple 'Expect' headers. Search through them.
for (String v : message.getHeaders(Names.EXPECT)) { for (String v: message.getHeaders(Names.EXPECT)) {
if (Values.CONTINUE.equalsIgnoreCase(v)) { if (Values.CONTINUE.equalsIgnoreCase(v)) {
return true; return true;
} }
@ -735,9 +727,9 @@ public class HttpHeaders {
/** /**
* Sets or removes the {@code "Expect: 100-continue"} header to / from the * Sets or removes the {@code "Expect: 100-continue"} header to / from the
* specified message. If the specified {@code value} is {@code true}, the * specified message. If the specified {@code value} is {@code true},
* {@code "Expect: 100-continue"} header is set and all other previous * the {@code "Expect: 100-continue"} header is set and all other previous
* {@code "Expect"} headers are removed. Otherwise, all {@code "Expect"} * {@code "Expect"} headers are removed. Otherwise, all {@code "Expect"}
* headers are removed completely. * headers are removed completely.
*/ */
public static void set100ContinueExpected(HttpMessage message, boolean set) { public static void set100ContinueExpected(HttpMessage message, boolean set) {
@ -752,7 +744,7 @@ public class HttpHeaders {
private static int hash(String name) { private static int hash(String name) {
int h = 0; int h = 0;
for (int i = name.length() - 1; i >= 0; i--) { for (int i = name.length() - 1; i >= 0; i --) {
char c = name.charAt(i); char c = name.charAt(i);
if (c >= 'A' && c <= 'Z') { if (c >= 'A' && c <= 'Z') {
c += 32; c += 32;
@ -775,7 +767,7 @@ public class HttpHeaders {
return false; return false;
} }
for (int i = nameLen - 1; i >= 0; i--) { for (int i = nameLen - 1; i >= 0; i --) {
char c1 = name1.charAt(i); char c1 = name1.charAt(i);
char c2 = name2.charAt(i); char c2 = name2.charAt(i);
if (c1 != c2) { if (c1 != c2) {
@ -894,7 +886,7 @@ public class HttpHeaders {
int i = index(h); int i = index(h);
removeHeader0(h, i, name); removeHeader0(h, i, name);
for (Object v : values) { for (Object v: values) {
if (v == null) { if (v == null) {
break; break;
} }
@ -905,7 +897,7 @@ public class HttpHeaders {
} }
void clearHeaders() { void clearHeaders() {
for (int i = 0; i < entries.length; i++) { for (int i = 0; i < entries.length; i ++) {
entries[i] = null; entries[i] = null;
} }
head.before = head.after = head; head.before = head.after = head;
@ -949,7 +941,8 @@ public class HttpHeaders {
} }
List<Map.Entry<String, String>> getHeaders() { List<Map.Entry<String, String>> getHeaders() {
List<Map.Entry<String, String>> all = new LinkedList<Map.Entry<String, String>>(); List<Map.Entry<String, String>> all =
new LinkedList<Map.Entry<String, String>>();
Entry e = head.after; Entry e = head.after;
while (e != head) { while (e != head) {
@ -964,7 +957,8 @@ public class HttpHeaders {
} }
Set<String> getHeaderNames() { Set<String> getHeaderNames() {
Set<String> names = new TreeSet<String>(CaseIgnoringComparator.INSTANCE); Set<String> names =
new TreeSet<String>(CaseIgnoringComparator.INSTANCE);
Entry e = head.after; Entry e = head.after;
while (e != head) { while (e != head) {
@ -1000,7 +994,7 @@ public class HttpHeaders {
} }
void addBefore(Entry e) { void addBefore(Entry e) {
after = e; after = e;
before = e.before; before = e.before;
before.after = this; before.after = this;
after.before = this; after.before = this;