Search in sources :

Example 6 with Response

use of org.xwiki.container.Response in project xwiki-platform by xwiki.

the class AbstractServletResourceReferenceHandler method serveResource.

/**
 * Sends back the specified resource.
 *
 * @param resourceReference the reference of the requested resource
 * @param rawResourceStream the resource stream used to read the resource
 * @throws ResourceReferenceHandlerException if it fails to read the resource
 */
private void serveResource(R resourceReference, InputStream rawResourceStream) throws ResourceReferenceHandlerException {
    InputStream resourceStream = rawResourceStream;
    String resourceName = getResourceName(resourceReference);
    // stream, in which case it will mark & reset the stream).
    if (!resourceStream.markSupported()) {
        resourceStream = new BufferedInputStream(resourceStream);
    }
    try {
        Response response = this.container.getResponse();
        setResponseHeaders(response, resourceReference);
        response.setContentType(TikaUtils.detect(resourceStream, resourceName));
        IOUtils.copy(resourceStream, response.getOutputStream());
    } catch (Exception e) {
        throw new ResourceReferenceHandlerException(String.format("Failed to read resource [%s]", resourceName), e);
    } finally {
        IOUtils.closeQuietly(resourceStream);
    }
}
Also used : Response(org.xwiki.container.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(org.xwiki.container.servlet.ServletResponse) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException)

Aggregations

Response (org.xwiki.container.Response)6 ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)3 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ServletResponse (org.xwiki.container.servlet.ServletResponse)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 URI (java.net.URI)1 Date (java.util.Date)1 Provider (javax.inject.Provider)1 TArchiveDetector (net.java.truevfs.access.TArchiveDetector)1 TConfig (net.java.truevfs.access.TConfig)1 Test (org.junit.Test)1