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