From a8e83820340ea2c6ffc443035ec98fbab3fe3bd7 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 14 Jul 2009 09:24:15 +0000 Subject: [PATCH] Added more headers. --- .../org/springframework/http/HttpHeaders.java | 56 +++++++++++++++++++ .../http/HttpHeadersTests.java | 28 ++++++++++ 2 files changed, 84 insertions(+) diff --git a/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java b/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java index 3f33359f580..ce6022f1579 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java @@ -61,6 +61,8 @@ public class HttpHeaders implements MultiValueMap { 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_TYPE = "Content-Type"; @@ -71,12 +73,16 @@ public class HttpHeaders implements MultiValueMap { 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 LAST_MODIFIED = "Last-Modified"; private static final String LOCATION = "Location"; + private static final String PRAGMA = "Pragma"; + private static final String[] DATE_FORMATS = new String[] { "EEE, dd MMM yyyy HH:mm:ss zzz", @@ -175,6 +181,22 @@ public class HttpHeaders implements MultiValueMap { } } + /** + * 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. * @param contentLength the content length @@ -266,6 +288,24 @@ public class HttpHeaders implements MultiValueMap { return getFirstDate(EXPIRES); } + /** + * Sets the (new) value of the {@code If-Modified-Since} header. + *

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. + *

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. * @param ifNoneMatch the new value of the header @@ -343,6 +383,22 @@ public class HttpHeaders implements MultiValueMap { 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 private String quote(String s) { diff --git a/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java b/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java index 440c83f35b1..4a55eab6a01 100644 --- a/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java @@ -167,4 +167,32 @@ public class HttpHeadersTests { 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")); + } + + + }