use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.
the class QuestionJobResourceReferenceHandler method handle.
@Override
public void handle(ParentResourceReference reference) throws ResourceReferenceHandlerException {
List<String> jobId = reference.getPathSegments();
Job job = this.executor.getJob(jobId);
if (job == null) {
throw new ResourceReferenceHandlerException("Cannot find any running job with id " + jobId);
}
Object question = job.getStatus().getQuestion();
if (question == null) {
throw new ResourceReferenceHandlerException("The job with id " + jobId + " does not have any question");
}
Request request = this.container.getRequest();
String prefix = "question/";
String contentType = "text/html; charset=utf-8";
// POST request means answer
if (request instanceof ServletRequest) {
HttpServletRequest httpRequest = ((ServletRequest) request).getHttpServletRequest();
if (httpRequest.getMethod().equals("POST")) {
String token = httpRequest.getParameter("form_token");
// TODO: should probably move this check in some filter triggered by an annotation
if (this.csrf.isTokenValid(token)) {
answer(httpRequest, job, jobId, question);
prefix = "answer/";
contentType = "application/json";
} else {
// TODO: Throw some exception
}
}
}
String jobType = job.getType();
// Provide informations about the job to the template
this.scriptContextManager.getCurrentScriptContext().setAttribute("job", job, ScriptContext.ENGINE_SCOPE);
String[] templates = getTemplates(question.getClass(), jobType, prefix);
if (!tryTemplates(contentType, templates)) {
throw new ResourceReferenceHandlerException("Cannot find any template for the job with id" + jobId + " (" + Arrays.toString(templates) + ")");
}
}
use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.
the class ResourceReferenceHandlerServlet method handleResourceReference.
private void handleResourceReference(ResourceReference resourceReference) throws ServletException {
ResourceReferenceHandlerManager<?> resourceReferenceHandlerManager;
try {
Type role = new DefaultParameterizedType(null, ResourceReferenceHandlerManager.class, ResourceType.class);
resourceReferenceHandlerManager = this.rootComponentManager.getInstance(role);
} catch (ComponentLookupException e) {
// Should not happen since a Resource Reference Handler should always exist on the system.
throw new ServletException("Failed to locate a Resource Reference Handler Manager component", e);
}
try {
resourceReferenceHandlerManager.handle(resourceReference);
} catch (ResourceReferenceHandlerException e) {
throw new ServletException(String.format("Failed to handle Resource Reference [%s]", resourceReference), e);
}
}
use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.
the class DefaultResourceReferenceHandlerChain method handleNext.
@Override
public void handleNext(ResourceReference reference) throws ResourceReferenceHandlerException {
if (!this.handlerStack.isEmpty()) {
ResourceReferenceHandler<?> handler = this.handlerStack.poll();
if (this.observation != null) {
this.observation.notify(new ResourceReferenceHandlingEvent(reference), handler);
}
ResourceReferenceHandlerException exception = null;
try {
handler.handle(reference, this);
} catch (ResourceReferenceHandlerException e) {
exception = e;
} finally {
if (this.observation != null) {
this.observation.notify(new ResourceReferenceHandledEvent(reference), handler, exception);
}
}
// Throw the exception if any
if (exception != null) {
throw exception;
}
}
}
use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.
the class VfsResourceReferenceHandlerTest method handleWhenNoGenericPermissionForScheme.
@Test
public void handleWhenNoGenericPermissionForScheme() throws Exception {
setUp("customscheme", "wiki3", "space3", "page3", "test.zip", Arrays.asList("test.txt"));
// Don't allow permission for "customscheme"
VfsPermissionChecker checker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading");
doThrow(new VfsException("no permission")).when(checker).checkPermission(this.reference);
try {
this.mocker.getComponentUnderTest().handle(this.reference, mock(ResourceReferenceHandlerChain.class));
fail("Should have thrown exception here");
} catch (ResourceReferenceHandlerException expected) {
assertEquals("VfsException: no permission", ExceptionUtils.getRootCauseMessage(expected));
}
}
use of org.xwiki.resource.ResourceReferenceHandlerException in project xwiki-platform by xwiki.
the class WebJarsResourceReferenceHandler method filterResource.
@Override
protected InputStream filterResource(WebJarsResourceReference resourceReference, InputStream resourceStream) throws ResourceReferenceHandlerException {
if (!isResourceCacheable(resourceReference)) {
String resourceName = getResourceName(resourceReference);
try {
// Evaluates the given resource using Velocity.
StringWriter writer = new StringWriter();
this.velocityManager.getVelocityEngine().evaluate(this.velocityManager.getVelocityContext(), writer, resourceName, new InputStreamReader(resourceStream, UTF8));
return new ByteArrayInputStream(writer.toString().getBytes(UTF8));
} catch (Exception e) {
throw new ResourceReferenceHandlerException(String.format("Failed to evaluate the Velocity code from WebJar resource [%s]", resourceName), e);
}
}
return super.filterResource(resourceReference, resourceStream);
}
Aggregations