Search in sources :

Example 1 with IResourceStreamWriter

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

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Bytes (org.apache.wicket.util.lang.Bytes)1 IResourceStream (org.apache.wicket.util.resource.IResourceStream)1 IResourceStreamWriter (org.apache.wicket.util.resource.IResourceStreamWriter)1 ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)1 Time (org.apache.wicket.util.time.Time)1