Search in sources :

Example 6 with ResourceReferenceHandlerException

use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.

the class AbstractContentResourceReferenceHandler method serveResource.

protected void serveResource(String resourceName, InputStream resourceStream) throws ResourceReferenceHandlerException {
    // Make sure the resource stream supports mark & reset which is needed in order be able to detect the
    // content type without affecting the stream (Tika may need to read a few bytes from the start of the
    // stream, in which case it will mark & reset the stream).
    // 
    // Note that even though the stream returned by TrueVFS returns true for markSupported() in practice it
    // doesn't! Thus we need to wrap the stream to make it support mark and reset.
    InputStream markResetSupportingStream = new BufferedInputStream(resourceStream);
    try {
        Response response = this.container.getResponse();
        response.setContentType(TikaUtils.detect(markResetSupportingStream, resourceName));
        IOUtils.copy(markResetSupportingStream, response.getOutputStream());
    } catch (Exception e) {
        throw new ResourceReferenceHandlerException(String.format("Failed to read resource [%s]", resourceName), e);
    } finally {
        IOUtils.closeQuietly(markResetSupportingStream);
    }
}
Also used : Response(org.xwiki.container.Response) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException)

Example 7 with ResourceReferenceHandlerException

use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.

the class VfsResourceReferenceHandler method handle.

@Override
public void handle(ResourceReference resourceReference, ResourceReferenceHandlerChain chain) throws ResourceReferenceHandlerException {
    // This code only handles VFS Resource References.
    VfsResourceReference vfsResourceReference = (VfsResourceReference) resourceReference;
    try {
        // Verify that the user has the permission for the specified VFS scheme and for the VFS URI
        this.permissionChecker.checkPermission(vfsResourceReference);
        // Extract the asked resource from inside the zip and return its content for display.
        // We need to convert the VFS Resource Reference into a hierarchical URI supported by TrueVFS
        URI trueVFSURI = convertResourceReference(vfsResourceReference);
        // We use TrueVFS. This line will automatically use the VFS Driver that matches the scheme passed in the URI
        Path path = new TPath(trueVFSURI);
        try (InputStream in = Files.newInputStream(path)) {
            List<String> pathSegments = vfsResourceReference.getPathSegments();
            serveResource(pathSegments.get(pathSegments.size() - 1), in);
        }
    } catch (Exception e) {
        throw new ResourceReferenceHandlerException(String.format("Failed to extract resource [%s]", vfsResourceReference), e);
    }
    // Be a good citizen, continue the chain, in case some lower-priority Handler has something to do for this
    // Resource Reference.
    chain.handleNext(vfsResourceReference);
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) InputStream(java.io.InputStream) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) TPath(net.java.truevfs.access.TPath) URI(java.net.URI) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException)

Example 8 with ResourceReferenceHandlerException

use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.

the class JobRootResourceReferenceHandler method handleChild.

private void handleChild(ParentResourceReference reference) throws ResourceReferenceHandlerException {
    if (StringUtils.isNotEmpty(reference.getChild())) {
        ComponentManager componentManager = this.componentManagerProvider.get();
        if (componentManager.hasComponent(JobResourceReferenceHandler.class, reference.getChild())) {
            JobResourceReferenceHandler child;
            try {
                child = componentManager.getInstance(JobResourceReferenceHandler.class, reference.getChild());
            } catch (ComponentLookupException e) {
                throw new ResourceReferenceHandlerException("Failed to initialize job resource handler with hint [" + reference.getChild() + "]");
            }
            child.handle(reference);
        } else {
            throw new ResourceReferenceHandlerException("Unknow job resource handler with hint [" + reference.getChild() + "]");
        }
    } else {
    // TODO: put some explanation about the various services provided by the job resource handler
    }
}
Also used : ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 9 with ResourceReferenceHandlerException

use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.

the class AbstractTemplateJobResourceReferenceHandler method tryTemplate.

protected boolean tryTemplate(String defaultContentType, String templateName) throws ResourceReferenceHandlerException {
    Template template = this.templates.getTemplate("job/" + templateName);
    if (template == null) {
        return false;
    }
    Response response = this.container.getResponse();
    try {
        // Set default content type (can be overwritten by the template itself)
        if (defaultContentType != null) {
            response.setContentType(defaultContentType);
        }
        Writer writer = new StringWriter();
        this.templates.render(template, writer);
        sendContent(writer.toString());
    } catch (Exception e) {
        throw new ResourceReferenceHandlerException("Failed to execute template [" + templateName + "]", e);
    }
    return true;
}
Also used : Response(org.xwiki.container.Response) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) IOException(java.io.IOException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) Template(org.xwiki.template.Template)

Example 10 with ResourceReferenceHandlerException

use of org.xwiki.resource.ResourceReferenceHandlerException 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

ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)10 InputStream (java.io.InputStream)3 Response (org.xwiki.container.Response)3 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Writer (java.io.Writer)1 Type (java.lang.reflect.Type)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 TPath (net.java.truevfs.access.TPath)1 Test (org.junit.Test)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1