Search in sources :

Example 1 with ResourceReferenceHandlerException

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) + ")");
    }
}
Also used : ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) Request(org.xwiki.container.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) Job(org.xwiki.job.Job)

Example 2 with ResourceReferenceHandlerException

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);
    }
}
Also used : ServletException(javax.servlet.ServletException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Type(java.lang.reflect.Type) ResourceType(org.xwiki.resource.ResourceType) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 3 with ResourceReferenceHandlerException

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;
        }
    }
}
Also used : ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) ResourceReferenceHandlingEvent(org.xwiki.resource.events.ResourceReferenceHandlingEvent) ResourceReferenceHandledEvent(org.xwiki.resource.events.ResourceReferenceHandledEvent)

Example 4 with ResourceReferenceHandlerException

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));
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) ResourceReferenceHandlerChain(org.xwiki.resource.ResourceReferenceHandlerChain) Test(org.junit.Test)

Example 5 with ResourceReferenceHandlerException

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);
}
Also used : ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) 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