Search in sources :

Example 16 with ResourceStreamNotFoundException

use of org.apache.wicket.util.resource.ResourceStreamNotFoundException 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

ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)16 IOException (java.io.IOException)15 InputStream (java.io.InputStream)7 IResourceStream (org.apache.wicket.util.resource.IResourceStream)7 AbstractResourceStream (org.apache.wicket.util.resource.AbstractResourceStream)4 Time (org.apache.wicket.util.time.Time)4 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 ServletContext (javax.servlet.ServletContext)2 AjaxEventBehavior (org.apache.wicket.ajax.AjaxEventBehavior)2 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)2 WebApplication (org.apache.wicket.protocol.http.WebApplication)2 AjaxButton (com.googlecode.wicket.jquery.ui.form.button.AjaxButton)1 AgreementResult (de.tudarmstadt.ukp.clarin.webanno.curation.agreement.AgreementUtils.AgreementResult)1 AJAXDownload (de.tudarmstadt.ukp.clarin.webanno.support.AJAXDownload)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 AbstractMap (java.util.AbstractMap)1