Search in sources :

Example 1 with ComponentEventResultProcessor

use of org.apache.tapestry5.services.ComponentEventResultProcessor in project tapestry-5 by apache.

the class PageRenderRequestHandlerImplTest method loopback_request.

@Test
public void loopback_request() throws Exception {
    RequestPageCache cache = mockRequestPageCache();
    ComponentEventResultProcessor processor = mockComponentEventResultProcessor();
    PageResponseRenderer renderer = mockPageResponseRenderer();
    Page page = mockPage();
    EventContext context = mockEventContext();
    ComponentPageElement root = mockComponentPageElement();
    InternalComponentResources pageResources = mockInternalComponentResources();
    PageActivator activator = newMock(PageActivator.class);
    Request request = mockRequest();
    train_getAttribute(request, InternalConstants.BYPASS_ACTIVATION, null);
    train_get(cache, "foo/Bar", page);
    train_getRootElement(page, root);
    train_getComponentResources(root, pageResources);
    expect(activator.activatePage(pageResources, context, processor)).andReturn(false);
    // Skips the pageReset()
    renderer.renderPageResponse(page);
    replay();
    PageRenderRequestHandler handler = new PageRenderRequestHandlerImpl(cache, processor, renderer, activator, request);
    PageRenderRequestParameters parameters = new PageRenderRequestParameters("foo/Bar", context, true);
    handler.handle(parameters);
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) ComponentEventResultProcessor(org.apache.tapestry5.services.ComponentEventResultProcessor) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) Request(org.apache.tapestry5.http.services.Request) PageRenderRequestHandler(org.apache.tapestry5.services.PageRenderRequestHandler) Page(org.apache.tapestry5.internal.structure.Page) Test(org.testng.annotations.Test)

Example 2 with ComponentEventResultProcessor

use of org.apache.tapestry5.services.ComponentEventResultProcessor in project tapestry-5 by apache.

the class AjaxComponentEventRequestHandler method handle.

public void handle(ComponentEventRequestParameters parameters) throws IOException {
    Page activePage = cache.get(parameters.getActivePageName());
    final Holder<Boolean> resultProcessorInvoked = Holder.create();
    resultProcessorInvoked.put(false);
    ComponentEventResultProcessor interceptor = new ComponentEventResultProcessor() {

        public void processResultValue(Object value) throws IOException {
            resultProcessorInvoked.put(true);
            resultProcessor.processResultValue(value);
        }
    };
    // If we end up doing a partial render, the page render queue service needs to know the
    // page that will be rendered (for logging purposes, if nothing else).
    queue.setRenderingPage(activePage);
    request.setAttribute(InternalConstants.PAGE_NAME_ATTRIBUTE_NAME, parameters.getActivePageName());
    if (pageActivator.activatePage(activePage.getRootElement().getComponentResources(), parameters.getPageActivationContext(), interceptor))
        return;
    Page containerPage = cache.get(parameters.getContainingPageName());
    ComponentPageElement element = containerPage.getComponentElementByNestedId(parameters.getNestedComponentId());
    // In many cases, the triggered element is a Form that needs to be able to
    // pass its event handler return values to the correct result processor.
    // This is certainly the case for forms.
    TrackableComponentEventCallback callback = new ComponentResultProcessorWrapper(interceptor);
    environment.push(ComponentEventResultProcessor.class, interceptor);
    environment.push(TrackableComponentEventCallback.class, callback);
    boolean handled = element.triggerContextEvent(parameters.getEventType(), parameters.getEventContext(), callback);
    if (!handled)
        throw new TapestryException(String.format("Request event '%s' (on component %s) was not handled; you must provide a matching event handler method in the component or in one of its containers.", parameters.getEventType(), element.getCompleteId()), element, null);
    environment.pop(TrackableComponentEventCallback.class);
    environment.pop(ComponentEventResultProcessor.class);
    // If the result processor was passed a value, then it will already have rendered. Otherwise it was not passed a value,
    // but it's still possible that we still want to do a partial page render ... if filters were added to the render queue.
    // In that event, run the partial page render now and return.
    boolean wasInvoked = resultProcessorInvoked.get();
    if ((!wasInvoked) && queue.isPartialRenderInitialized()) {
        partialRenderer.renderPartialPageMarkup();
        return;
    }
    if (wasInvoked) {
        return;
    }
    // Send an empty JSON reply if no value was returned from the component event handler method.
    // This is the typical behavior when an Ajax component event handler returns null. It still
    // will go through a pipeline that will add information related to partial page rendering.
    resultProcessor.processResultValue(new JSONObject());
}
Also used : ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) ComponentEventResultProcessor(org.apache.tapestry5.services.ComponentEventResultProcessor) JSONObject(org.apache.tapestry5.json.JSONObject) TrackableComponentEventCallback(org.apache.tapestry5.TrackableComponentEventCallback) Page(org.apache.tapestry5.internal.structure.Page) JSONObject(org.apache.tapestry5.json.JSONObject) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 3 with ComponentEventResultProcessor

use of org.apache.tapestry5.services.ComponentEventResultProcessor in project tapestry-5 by apache.

the class PageActivatorImpl method activatePage.

@SuppressWarnings("rawtypes")
public boolean activatePage(ComponentResources pageResources, EventContext activationContext, ComponentEventResultProcessor resultProcessor) throws IOException {
    TrackableComponentEventCallback callback = new ComponentResultProcessorWrapper(resultProcessor);
    boolean handled = pageResources.triggerContextEvent(EventConstants.ACTIVATE, activationContext, callback);
    boolean acceptEmpty = !pageResources.getComponentModel().handlesEvent(EventConstants.ACTIVATE) && activationContext.getCount() == 0;
    boolean checkUnknown = metaDataLocator.findMeta(MetaDataConstants.UNKNOWN_ACTIVATION_CONTEXT_CHECK, pageResources, Boolean.class);
    if (!handled && !acceptEmpty && checkUnknown && !pageResources.getComponentModel().handleActivationEventContext()) {
        logger.info("Page {} required an exact activation context, let's handle this", pageResources.getPageName());
        unknownActivationContextHandler.handleUnknownContext(pageResources, activationContext);
        return true;
    }
    if (callback.isAborted()) {
        callback.rethrow();
        return true;
    } else {
        if (InternalConstants.TRUE.equals(pageResources.getComponentModel().getMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHOD_PRESENT))) {
            callback = new ComponentResultProcessorWrapper(resultProcessor);
            handled = pageResources.triggerContextEvent(InternalConstants.HTTP_METHOD_EVENT_PREFIX + request.getMethod(), activationContext, callback);
            if (callback.isAborted()) {
                callback.rethrow();
                return true;
            } else {
                throw new RestEndpointNotFoundException(String.format("Page %s (%s) has at least one REST endpoint event handler method " + "but none handled %s for this request", pageResources.getPageName(), pageResources.getPage().getClass().getName(), request.getMethod()));
            }
        }
    }
    return false;
}
Also used : TrackableComponentEventCallback(org.apache.tapestry5.TrackableComponentEventCallback)

Example 4 with ComponentEventResultProcessor

use of org.apache.tapestry5.services.ComponentEventResultProcessor in project tapestry-5 by apache.

the class ComponentInstanceResultProcessorTest method warning_for_component_is_not_root_component.

@Test
public void warning_for_component_is_not_root_component() throws Exception {
    Component value = mockComponent();
    Component containerResources = mockComponent();
    ComponentResources valueResources = mockComponentResources();
    Logger logger = mockLogger();
    ComponentEventResultProcessor primary = mockComponentEventResultProcessor();
    train_getComponentResources(value, valueResources);
    train_getContainer(valueResources, containerResources);
    String completeId = PAGE_NAME + ":child";
    train_getCompleteId(valueResources, completeId);
    logger.warn("Component {} was returned from an event handler method, but is not a page component. The page containing the component will render the client response.", completeId);
    train_getPageName(valueResources, PAGE_NAME);
    primary.processResultValue(PAGE_NAME);
    replay();
    ComponentEventResultProcessor<Component> processor = new ComponentInstanceResultProcessor(logger, primary);
    processor.processResultValue(value);
    verify();
}
Also used : ComponentEventResultProcessor(org.apache.tapestry5.services.ComponentEventResultProcessor) Component(org.apache.tapestry5.runtime.Component) Logger(org.slf4j.Logger) ComponentResources(org.apache.tapestry5.ComponentResources) Test(org.testng.annotations.Test)

Example 5 with ComponentEventResultProcessor

use of org.apache.tapestry5.services.ComponentEventResultProcessor in project tapestry-5 by apache.

the class ComponentInstanceResultProcessorTest method result_is_root_component.

@Test
public void result_is_root_component() throws Exception {
    Component result = mockComponent();
    ComponentResources resources = mockComponentResources();
    Logger logger = mockLogger();
    ComponentEventResultProcessor primary = mockComponentEventResultProcessor();
    train_getComponentResources(result, resources);
    train_getContainer(resources, null);
    train_getPageName(resources, PAGE_NAME);
    primary.processResultValue(PAGE_NAME);
    replay();
    ComponentEventResultProcessor<Component> processor = new ComponentInstanceResultProcessor(logger, primary);
    processor.processResultValue(result);
    verify();
}
Also used : ComponentEventResultProcessor(org.apache.tapestry5.services.ComponentEventResultProcessor) Component(org.apache.tapestry5.runtime.Component) Logger(org.slf4j.Logger) ComponentResources(org.apache.tapestry5.ComponentResources) Test(org.testng.annotations.Test)

Aggregations

ComponentEventResultProcessor (org.apache.tapestry5.services.ComponentEventResultProcessor)6 Test (org.testng.annotations.Test)5 ComponentResources (org.apache.tapestry5.ComponentResources)4 Component (org.apache.tapestry5.runtime.Component)4 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)3 Page (org.apache.tapestry5.internal.structure.Page)3 TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)2 Logger (org.slf4j.Logger)2 EventContext (org.apache.tapestry5.EventContext)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 Request (org.apache.tapestry5.http.services.Request)1 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)1 JSONObject (org.apache.tapestry5.json.JSONObject)1 PageRenderRequestHandler (org.apache.tapestry5.services.PageRenderRequestHandler)1 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)1