use of org.apache.wicket.util.time.Time in project wicket by apache.
the class HttpHeaderCollectionTest method dateValues.
@Test
public void dateValues() {
final HttpHeaderCollection headers = new HttpHeaderCollection();
final Time time1 = Time.millis(1000000);
final Time time2 = Time.millis(2000000);
headers.setDateHeader("date", time1);
headers.addDateHeader("date", time2);
headers.addHeader("date", "not-a-date");
assertEquals(time1, headers.getDateHeader("date"));
assertEquals("Thu, 01 Jan 1970 00:16:40 GMT", headers.getHeader("date"));
// a change of the locale or timezone must not affect the date format
final Locale defaultLocale = Locale.getDefault();
final TimeZone defaultLocaleefaultTimezone = TimeZone.getDefault();
try {
final String expected = "Thu, 01 Jan 1970 00:16:40 GMT";
Locale.setDefault(Locale.CHINESE);
TimeZone.setDefault(TimeZone.getTimeZone("CET"));
assertEquals(expected, headers.getHeader("date"));
Locale.setDefault(Locale.US);
TimeZone.setDefault(TimeZone.getTimeZone("EST"));
assertEquals(expected, headers.getHeader("date"));
} finally {
Locale.setDefault(defaultLocale);
TimeZone.setDefault(defaultLocaleefaultTimezone);
}
assertArrayEquals(new String[] { "Thu, 01 Jan 1970 00:16:40 GMT", "Thu, 01 Jan 1970 00:33:20 GMT", "not-a-date" }, headers.getHeaderValues("date"));
headers.setHeader("date", "foobar");
try {
Time date = headers.getDateHeader("date");
fail();
} catch (IllegalStateException e) {
// ok
}
}
use of org.apache.wicket.util.time.Time in project wicket by apache.
the class WebResponse method enableCaching.
/**
* Make this response cacheable
* <p/>
* when trying to enable caching for web pages check this out: <a
* href="https://issues.apache.org/jira/browse/WICKET-4357">WICKET-4357</a>
*
* @param duration
* maximum duration before the response must be invalidated by any caches. It should
* not exceed one year, based on <a
* href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">RFC-2616</a>.
* @param scope
* controls which caches are allowed to cache the response
*
* @see WebResponse#MAX_CACHE_DURATION
*/
public void enableCaching(Duration duration, final WebResponse.CacheScope scope) {
Args.notNull(duration, "duration");
Args.notNull(scope, "scope");
// do not exceed the maximum recommended value from RFC-2616
if (duration.compareTo(MAX_CACHE_DURATION) > 0) {
duration = MAX_CACHE_DURATION;
}
// Get current time
Time now = Time.now();
// Time of message generation
setDateHeader("Date", now);
// Time for cache expiry = now + duration
setDateHeader("Expires", now.add(duration));
// Set cache scope
setHeader("Cache-Control", scope.cacheControl);
// Set maximum age for caching in seconds (rounded)
addHeader("Cache-Control", "max-age=" + Math.round(duration.seconds()));
// Though 'cache' is not an official value it will eliminate an eventual 'no-cache' header
setHeader("Pragma", "cache");
}
use of org.apache.wicket.util.time.Time in project wicket by apache.
the class UrlResourceStream method lastModifiedTime.
/**
* @see org.apache.wicket.util.watch.IModifiable#lastModifiedTime()
* @return The last time this resource was modified
*/
@Override
public Time lastModifiedTime() {
try {
// get url modification timestamp
final Time time = Connections.getLastModified(url);
// if timestamp changed: update content length and last modified date
if (Objects.equal(time, lastModified) == false) {
lastModified = time;
updateContentLength();
}
return lastModified;
} catch (IOException e) {
log.warn("getLastModified() for '{}' failed: {}", url, e.getMessage());
// allow modification watcher to detect the problem
return null;
}
}
use of org.apache.wicket.util.time.Time in project wicket by apache.
the class ConcatBundleResource method newResourceResponse.
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
final ResourceResponse resourceResponse = new ResourceResponse();
if (resourceResponse.dataNeedsToBeWritten(attributes)) {
try {
List<IResourceStream> resources = collectResourceStreams();
if (resources == null)
return sendResourceError(resourceResponse, HttpServletResponse.SC_NOT_FOUND, "Unable to find resource");
resourceResponse.setContentType(findContentType(resources));
// add Last-Modified header (to support HEAD requests and If-Modified-Since)
final Time lastModified = findLastModified(resources);
if (lastModified != null)
resourceResponse.setLastModified(lastModified);
// read resource data
final byte[] bytes = readAllResources(resources);
// send Content-Length header
resourceResponse.setContentLength(bytes.length);
// send response body with resource data
resourceResponse.setWriteCallback(new WriteCallback() {
@Override
public void writeData(Attributes attributes) {
attributes.getResponse().write(bytes);
}
});
} catch (IOException e) {
log.debug(e.getMessage(), e);
return sendResourceError(resourceResponse, 500, "Unable to read resource stream");
} catch (ResourceStreamNotFoundException e) {
log.debug(e.getMessage(), e);
return sendResourceError(resourceResponse, 500, "Unable to open resource stream");
}
}
return resourceResponse;
}
use of org.apache.wicket.util.time.Time in project wicket by apache.
the class LastModifiedResourceVersion method getVersion.
@Override
public String getVersion(IStaticCacheableResource resource) {
// get last modified timestamp of resource
IResourceStream stream = resource.getResourceStream();
// if resource stream can not be found do not cache
if (stream == null) {
return null;
}
final Time lastModified = stream.lastModifiedTime();
// if no timestamp is available we can not provide a version
if (lastModified == null) {
return null;
}
// version string = last modified timestamp converted to milliseconds
return String.valueOf(lastModified.getMilliseconds()).intern();
}
Aggregations