Added more headers.
This commit is contained in:
parent
8073efd69f
commit
a8e8382034
|
|
@ -61,6 +61,8 @@ public class HttpHeaders implements MultiValueMap<String, String> {
|
||||||
|
|
||||||
private static final String ALLOW = "Allow";
|
private static final String ALLOW = "Allow";
|
||||||
|
|
||||||
|
private static final String CACHE_CONTROL = "Cache-Control";
|
||||||
|
|
||||||
private static final String CONTENT_LENGTH = "Content-Length";
|
private static final String CONTENT_LENGTH = "Content-Length";
|
||||||
|
|
||||||
private static final String CONTENT_TYPE = "Content-Type";
|
private static final String CONTENT_TYPE = "Content-Type";
|
||||||
|
|
@ -71,12 +73,16 @@ public class HttpHeaders implements MultiValueMap<String, String> {
|
||||||
|
|
||||||
private static final String EXPIRES = "Expires";
|
private static final String EXPIRES = "Expires";
|
||||||
|
|
||||||
|
private static final String IF_MODIFIED_SINCE = "If-Modified-Since";
|
||||||
|
|
||||||
private static final String IF_NONE_MATCH = "If-None-Match";
|
private static final String IF_NONE_MATCH = "If-None-Match";
|
||||||
|
|
||||||
private static final String LAST_MODIFIED = "Last-Modified";
|
private static final String LAST_MODIFIED = "Last-Modified";
|
||||||
|
|
||||||
private static final String LOCATION = "Location";
|
private static final String LOCATION = "Location";
|
||||||
|
|
||||||
|
private static final String PRAGMA = "Pragma";
|
||||||
|
|
||||||
|
|
||||||
private static final String[] DATE_FORMATS = new String[] {
|
private static final String[] DATE_FORMATS = new String[] {
|
||||||
"EEE, dd MMM yyyy HH:mm:ss zzz",
|
"EEE, dd MMM yyyy HH:mm:ss zzz",
|
||||||
|
|
@ -175,6 +181,22 @@ public class HttpHeaders implements MultiValueMap<String, String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the (new) value of the {@code Cache-Control} header.
|
||||||
|
* @param cacheControl the value of the header
|
||||||
|
*/
|
||||||
|
public void setCacheControl(String cacheControl) {
|
||||||
|
set(CACHE_CONTROL, cacheControl);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of the {@code Cache-Control} header.
|
||||||
|
* @return the value of the header
|
||||||
|
*/
|
||||||
|
public String getCacheControl() {
|
||||||
|
return getFirst(CACHE_CONTROL);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the length of the body in bytes, as specified by the {@code Content-Length} header.
|
* Set the length of the body in bytes, as specified by the {@code Content-Length} header.
|
||||||
* @param contentLength the content length
|
* @param contentLength the content length
|
||||||
|
|
@ -266,6 +288,24 @@ public class HttpHeaders implements MultiValueMap<String, String> {
|
||||||
return getFirstDate(EXPIRES);
|
return getFirstDate(EXPIRES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the (new) value of the {@code If-Modified-Since} header.
|
||||||
|
* <p>The date should be specified as the number of milliseconds since January 1, 1970 GMT.
|
||||||
|
* @param ifModifiedSince the new value of the header
|
||||||
|
*/
|
||||||
|
public void setIfModifiedSince(long ifModifiedSince) {
|
||||||
|
setDate(IF_MODIFIED_SINCE, ifModifiedSince);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of the {@code IfModifiedSince} header.
|
||||||
|
* <p>The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
|
||||||
|
* @return the header value
|
||||||
|
*/
|
||||||
|
public long getIfNotModifiedSince() {
|
||||||
|
return getFirstDate(IF_MODIFIED_SINCE);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the (new) value of the {@code If-None-Match} header.
|
* Sets the (new) value of the {@code If-None-Match} header.
|
||||||
* @param ifNoneMatch the new value of the header
|
* @param ifNoneMatch the new value of the header
|
||||||
|
|
@ -343,6 +383,22 @@ public class HttpHeaders implements MultiValueMap<String, String> {
|
||||||
return (value != null ? URI.create(value) : null);
|
return (value != null ? URI.create(value) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the (new) value of the {@code Pragma} header.
|
||||||
|
* @param pragma the value of the header
|
||||||
|
*/
|
||||||
|
public void setPragma(String pragma) {
|
||||||
|
set(PRAGMA, pragma);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of the {@code Pragma} header.
|
||||||
|
* @return the value of the header
|
||||||
|
*/
|
||||||
|
public String getPragma() {
|
||||||
|
return getFirst(PRAGMA);
|
||||||
|
}
|
||||||
|
|
||||||
// Utility methods
|
// Utility methods
|
||||||
|
|
||||||
private String quote(String s) {
|
private String quote(String s) {
|
||||||
|
|
|
||||||
|
|
@ -167,4 +167,32 @@ public class HttpHeadersTests {
|
||||||
assertEquals("Invalid Expires header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("expires"));
|
assertEquals("Invalid Expires header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("expires"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifModifiedSince() {
|
||||||
|
Calendar calendar = new GregorianCalendar(2008, 11, 18, 11, 20);
|
||||||
|
calendar.setTimeZone(TimeZone.getTimeZone("CET"));
|
||||||
|
long date = calendar.getTimeInMillis();
|
||||||
|
headers.setIfModifiedSince(date);
|
||||||
|
assertEquals("Invalid If-Modified-Since header", date, headers.getIfNotModifiedSince());
|
||||||
|
assertEquals("Invalid If-Modified-Since header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("if-modified-since"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void pragma() {
|
||||||
|
String pragma = "no-cache";
|
||||||
|
headers.setPragma(pragma);
|
||||||
|
assertEquals("Invalid Pragma header", pragma, headers.getPragma());
|
||||||
|
assertEquals("Invalid Pragma header", "no-cache", headers.getFirst("pragma"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void cacheControl() {
|
||||||
|
String cacheControl = "no-cache";
|
||||||
|
headers.setCacheControl(cacheControl);
|
||||||
|
assertEquals("Invalid Cache-Control header", cacheControl, headers.getCacheControl());
|
||||||
|
assertEquals("Invalid Cache-Control header", "no-cache", headers.getFirst("cache-control"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue