use of org.apache.wicket.util.resource.IResourceStreamWriter 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;
}
Aggregations