Search in sources :

Example 11 with ResourceStreamNotFoundException

use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.

the class ConcatBundleResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    final ResourceResponse resourceResponse = new ResourceResponse();
    if (resourceResponse.dataNeedsToBeWritten(attributes)) {
        try {
            List<IResourceStream> resources = collectResourceStreams();
            if (resources == null)
                return sendResourceError(resourceResponse, HttpServletResponse.SC_NOT_FOUND, "Unable to find resource");
            resourceResponse.setContentType(findContentType(resources));
            // add Last-Modified header (to support HEAD requests and If-Modified-Since)
            final Time lastModified = findLastModified(resources);
            if (lastModified != null)
                resourceResponse.setLastModified(lastModified);
            // read resource data
            final byte[] bytes = readAllResources(resources);
            // send Content-Length header
            resourceResponse.setContentLength(bytes.length);
            // send response body with resource data
            resourceResponse.setWriteCallback(new WriteCallback() {

                @Override
                public void writeData(Attributes attributes) {
                    attributes.getResponse().write(bytes);
                }
            });
        } catch (IOException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(resourceResponse, 500, "Unable to read resource stream");
        } catch (ResourceStreamNotFoundException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(resourceResponse, 500, "Unable to open resource stream");
        }
    }
    return resourceResponse;
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) Time(org.apache.wicket.util.time.Time) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

Example 12 with ResourceStreamNotFoundException

use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.

the class Initializer method getVelocityProperties.

private Properties getVelocityProperties(final Application application) {
    String velocityPropertiesFile = "velocity.properties";
    if (application instanceof WebApplication) {
        WebApplication webapp = (WebApplication) application;
        ServletContext servletContext = webapp.getServletContext();
        String propertiesFolder = servletContext.getInitParameter("velocityPropertiesFolder");
        String propsFile = servletContext.getInitParameter("velocity.properties");
        if (null != propsFile) {
            velocityPropertiesFile = propsFile;
        }
        if (null != propertiesFolder) {
            WebApplicationPath webPath = new WebApplicationPath(servletContext, propertiesFolder);
            IResourceStream stream = webPath.find(Initializer.class, velocityPropertiesFile);
            InputStream is;
            try {
                is = stream.getInputStream();
                Properties props = new Properties();
                props.load(is);
                return props;
            } catch (IOException | ResourceStreamNotFoundException e) {
                throw new WicketRuntimeException(e);
            } finally {
                try {
                    IOUtils.close(stream);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
    // if it's not a web app, load from the package
    InputStream is = Initializer.class.getResourceAsStream("velocity.properties");
    try {
        Properties props = new Properties();
        props.load(is);
        return props;
    } catch (Exception e) {
        throw new WicketRuntimeException(e);
    } finally {
        try {
            IOUtils.close(is);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) InputStream(java.io.InputStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ServletContext(javax.servlet.ServletContext) WebApplicationPath(org.apache.wicket.core.util.file.WebApplicationPath) IOException(java.io.IOException) WebApplication(org.apache.wicket.protocol.http.WebApplication) Properties(java.util.Properties) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

Example 13 with ResourceStreamNotFoundException

use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.

the class WebExternalResourceStream method getInputStream.

@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
    final ServletContext context = ((WebApplication) Application.get()).getServletContext();
    in = context.getResourceAsStream(url);
    if (in == null) {
        throw new ResourceStreamNotFoundException("The requested resource was not found: " + url);
    }
    return in;
}
Also used : ServletContext(javax.servlet.ServletContext) WebApplication(org.apache.wicket.protocol.http.WebApplication) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

Example 14 with ResourceStreamNotFoundException

use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.

the class PackageResource method newResourceResponse.

/**
 * creates a new resource response based on the request attributes
 *
 * @param attributes
 *            current request attributes from client
 * @return resource response for answering request
 */
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    final ResourceResponse resourceResponse = new ResourceResponse();
    final IResourceStream resourceStream = getResourceStream();
    // bail out if resource stream could not be found
    if (resourceStream == null) {
        return sendResourceError(resourceResponse, HttpServletResponse.SC_NOT_FOUND, "Unable to find resource");
    }
    // add Last-Modified header (to support HEAD requests and If-Modified-Since)
    final Time lastModified = resourceStream.lastModifiedTime();
    resourceResponse.setLastModified(lastModified);
    if (resourceResponse.dataNeedsToBeWritten(attributes)) {
        String contentType = resourceStream.getContentType();
        if (contentType == null && Application.exists()) {
            contentType = Application.get().getMimeType(path);
        }
        // set Content-Type (may be null)
        resourceResponse.setContentType(contentType);
        // set content encoding (may be null)
        resourceResponse.setTextEncoding(getTextEncoding());
        // supports accept range
        resourceResponse.setAcceptRange(ContentRangeType.BYTES);
        try {
            // read resource data to get the content length
            InputStream inputStream = resourceStream.getInputStream();
            byte[] bytes = null;
            // send Content-Length header
            if (readBuffered) {
                bytes = IOUtils.toByteArray(inputStream);
                resourceResponse.setContentLength(bytes.length);
            } else {
                resourceResponse.setContentLength(resourceStream.length().bytes());
            }
            // get content range information
            RequestCycle cycle = RequestCycle.get();
            Long startbyte = cycle.getMetaData(CONTENT_RANGE_STARTBYTE);
            Long endbyte = cycle.getMetaData(CONTENT_RANGE_ENDBYTE);
            // send response body with resource data
            PartWriterCallback partWriterCallback = new PartWriterCallback(bytes != null ? new ByteArrayInputStream(bytes) : inputStream, resourceResponse.getContentLength(), startbyte, endbyte);
            // If read buffered is set to false ensure the part writer callback is going to
            // close the input stream
            resourceResponse.setWriteCallback(partWriterCallback.setClose(!readBuffered));
        } catch (IOException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(resourceResponse, 500, "Unable to read resource stream");
        } catch (ResourceStreamNotFoundException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(resourceResponse, 500, "Unable to open resource stream");
        } finally {
            try {
                if (readBuffered) {
                    IOUtils.close(resourceStream);
                }
            } catch (IOException e) {
                log.warn("Unable to close the resource stream", e);
            }
        }
    }
    return resourceResponse;
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Time(org.apache.wicket.util.time.Time) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

Example 15 with ResourceStreamNotFoundException

use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.

the class ContextRelativeResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    final ResourceResponse resourceResponse = new ResourceResponse();
    final WebExternalResourceStream webExternalResourceStream = new WebExternalResourceStream(path);
    resourceResponse.setContentType(webExternalResourceStream.getContentType());
    resourceResponse.setLastModified(webExternalResourceStream.lastModifiedTime());
    resourceResponse.setFileName(path);
    resourceResponse.setWriteCallback(new WriteCallback() {

        @Override
        public void writeData(final Attributes attributes) throws IOException {
            try {
                InputStream inputStream = webExternalResourceStream.getInputStream();
                try {
                    Streams.copy(inputStream, attributes.getResponse().getOutputStream());
                } finally {
                    IOUtils.closeQuietly(inputStream);
                }
            } catch (ResourceStreamNotFoundException rsnfx) {
                throw new WicketRuntimeException(rsnfx);
            }
        }
    });
    return resourceResponse;
}
Also used : WebExternalResourceStream(org.apache.wicket.core.util.resource.WebExternalResourceStream) InputStream(java.io.InputStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

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