Search in sources :

Example 16 with Time

use of org.apache.wicket.util.time.Time in project wicket by apache.

the class ResourceStreamResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    final IResourceStream resourceStream = internalGetResourceStream(attributes);
    ResourceResponse data = new ResourceResponse();
    Time lastModifiedTime = resourceStream.lastModifiedTime();
    if (lastModifiedTime != null) {
        data.setLastModified(lastModifiedTime);
    }
    if (cacheDuration != null) {
        data.setCacheDuration(cacheDuration);
    }
    // performance check; don't bother to do anything if the resource is still cached by client
    if (data.dataNeedsToBeWritten(attributes)) {
        InputStream inputStream = null;
        if (resourceStream instanceof IResourceStreamWriter == false) {
            try {
                inputStream = resourceStream.getInputStream();
            } catch (ResourceStreamNotFoundException e) {
                data.setError(HttpServletResponse.SC_NOT_FOUND);
                close(resourceStream);
            }
        }
        data.setContentDisposition(contentDisposition);
        Bytes length = resourceStream.length();
        if (length != null) {
            data.setContentLength(length.bytes());
        }
        data.setFileName(fileName);
        String contentType = resourceStream.getContentType();
        if (contentType == null && fileName != null && Application.exists()) {
            contentType = Application.get().getMimeType(fileName);
        }
        data.setContentType(contentType);
        data.setTextEncoding(textEncoding);
        if (resourceStream instanceof IResourceStreamWriter) {
            data.setWriteCallback(new WriteCallback() {

                @Override
                public void writeData(Attributes attributes) throws IOException {
                    ((IResourceStreamWriter) resourceStream).write(attributes.getResponse().getOutputStream());
                    close(resourceStream);
                }
            });
        } else {
            final InputStream s = inputStream;
            data.setWriteCallback(new WriteCallback() {

                @Override
                public void writeData(Attributes attributes) throws IOException {
                    try {
                        writeStream(attributes, s);
                    } finally {
                        close(resourceStream);
                    }
                }
            });
        }
    }
    return data;
}
Also used : Bytes(org.apache.wicket.util.lang.Bytes) IResourceStream(org.apache.wicket.util.resource.IResourceStream) InputStream(java.io.InputStream) Time(org.apache.wicket.util.time.Time) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) IResourceStreamWriter(org.apache.wicket.util.resource.IResourceStreamWriter)

Example 17 with Time

use of org.apache.wicket.util.time.Time in project wicket by apache.

the class PushHeaderItem method applyPageCacheHeader.

/**
 * Applies the cache header item to the response
 */
protected void applyPageCacheHeader() {
    // check modification of page html
    Time pageModificationTime = getPageModificationTime();
    // The date of the page is now
    pageWebResponse.setDateHeader("Date", Time.now());
    // Set the modification time so that the browser sends a "If-Modified-Since" header which
    // can be compared
    pageWebResponse.setLastModifiedTime(pageModificationTime);
    // Make the resource stale so that it gets revalidated even if a cache entry is set
    // (see http://stackoverflow.com/questions/11357430/http-expires-header-values-0-and-1)
    pageWebResponse.setHeader("Expires", "-1");
    // Set a cache but set it to max-age=0 / must-revalidate so that the request to the page is
    // done
    pageWebResponse.setHeader("Cache-Control", "max-age=0, public, must-revalidate, proxy-revalidate");
}
Also used : LocalDateTime(java.time.LocalDateTime) Time(org.apache.wicket.util.time.Time)

Example 18 with Time

use of org.apache.wicket.util.time.Time in project wicket by apache.

the class PushHeaderItem method render.

/**
 * Pushes the previously created URLs to the client
 */
@Override
public void render(Response response) {
    // applies the caching header to the actual page request
    applyPageCacheHeader();
    HttpServletRequest request = getContainerRequest(RequestCycle.get().getRequest());
    // Check if the protocol is http/2 or http/2.0 to only push the resources in this case
    if (isHttp2(request)) {
        Time pageModificationTime = getPageModificationTime();
        String ifModifiedSinceHeader = pageWebRequest.getHeader("If-Modified-Since");
        // Check if the if-modified-since header is set - if not push all resources
        if (ifModifiedSinceHeader != null) {
            // Try to parse RFC1123
            Time ifModifiedSinceFromRequestTime = parseIfModifiedSinceHeader(ifModifiedSinceHeader, headerDateFormat_RFC1123);
            // Try to parse ASCTIME
            if (ifModifiedSinceFromRequestTime == null) {
                ifModifiedSinceFromRequestTime = parseIfModifiedSinceHeader(ifModifiedSinceHeader, headerDateFormat_ASCTIME);
            }
            // Try to parse RFC1036 - because it is obsolete due to RFC 1036 check this last.
            if (ifModifiedSinceFromRequestTime == null) {
                ifModifiedSinceFromRequestTime = parseIfModifiedSinceHeader(ifModifiedSinceHeader, headerDateFormat_RFC1036);
            }
            // be parsed push it.
            if (ifModifiedSinceFromRequestTime == null || ifModifiedSinceFromRequestTime.before(pageModificationTime)) {
                // Some browsers like IE 9-11 or Chrome 39 that does not send right headers
                // receive the resource via push all the time
                push(request);
            }
        } else {
            // Push the resources if the "if-modified-since" is not available
            push(request);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LocalDateTime(java.time.LocalDateTime) Time(org.apache.wicket.util.time.Time)

Example 19 with Time

use of org.apache.wicket.util.time.Time in project wicket by apache.

the class ModificationWatcher method checkModified.

/**
 * Checks which IModifiables were modified and notifies their listeners
 */
protected void checkModified() {
    for (Entry entry : modifiableToEntry.values()) {
        // If the modifiable has been modified after the last known
        // modification time
        final Time modifiableLastModified = entry.modifiable.lastModifiedTime();
        if ((modifiableLastModified != null) && modifiableLastModified.after(entry.lastModifiedTime)) {
            // Notify all listeners that the modifiable was modified
            entry.listeners.notifyListeners(entry.modifiable);
            // Update timestamp
            entry.lastModifiedTime = modifiableLastModified;
        }
    }
}
Also used : Time(org.apache.wicket.util.time.Time)

Example 20 with Time

use of org.apache.wicket.util.time.Time in project wicket by apache.

the class ModificationWatcher method add.

@Override
public final boolean add(final IModifiable modifiable, final IChangeListener<IModifiable> listener) {
    // Look up entry for modifiable
    final Entry entry = modifiableToEntry.get(modifiable);
    // Found it?
    if (entry == null) {
        Time lastModifiedTime = modifiable.lastModifiedTime();
        if (lastModifiedTime != null) {
            // Construct new entry
            final Entry newEntry = new Entry();
            newEntry.modifiable = modifiable;
            newEntry.lastModifiedTime = lastModifiedTime;
            newEntry.listeners.add(listener);
            // Put in map
            modifiableToEntry.putIfAbsent(modifiable, newEntry);
        } else {
            // The IModifiable is not returning a valid lastModifiedTime
            log.info("Cannot track modifications to resource '{}'", modifiable);
        }
        return true;
    } else {
        // Add listener to existing entry
        return !entry.listeners.add(listener);
    }
}
Also used : 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