Search in sources :

Example 6 with Time

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
    }
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) Time(org.apache.wicket.util.time.Time) Test(org.junit.Test)

Example 7 with Time

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");
}
Also used : Time(org.apache.wicket.util.time.Time)

Example 8 with Time

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;
    }
}
Also used : Time(org.apache.wicket.util.time.Time) IOException(java.io.IOException)

Example 9 with Time

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;
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) Time(org.apache.wicket.util.time.Time) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

Example 10 with Time

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();
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) Time(org.apache.wicket.util.time.Time)

Aggregations

Time (org.apache.wicket.util.time.Time)21 Test (org.junit.Test)6 IOException (java.io.IOException)5 IResourceStream (org.apache.wicket.util.resource.IResourceStream)5 ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)4 Duration (org.apache.wicket.util.time.Duration)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 LocalDateTime (java.time.LocalDateTime)2 File (java.io.File)1 Locale (java.util.Locale)1 Random (java.util.Random)1 TimeZone (java.util.TimeZone)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpHeaderCollection (org.apache.wicket.request.HttpHeaderCollection)1 Response (org.apache.wicket.request.Response)1 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)1