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;
}
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");
}
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);
}
}
}
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;
}
}
}
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);
}
}
Aggregations