Search in sources :

Example 1 with TrackableComponentEventCallback

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

the class ComponentEventRequestHandlerImpl method handle.

public void handle(ComponentEventRequestParameters parameters) throws IOException {
    Page activePage = cache.get(parameters.getActivePageName());
    if (pageActivator.activatePage(activePage.getRootElement().getComponentResources(), parameters.getPageActivationContext(), resultProcessor)) {
        return;
    }
    Page containerPage = cache.get(parameters.getContainingPageName());
    TrackableComponentEventCallback callback = new ComponentResultProcessorWrapper(resultProcessor);
    environment.push(ComponentEventResultProcessor.class, resultProcessor);
    environment.push(TrackableComponentEventCallback.class, callback);
    ComponentPageElement element = containerPage.getComponentElementByNestedId(parameters.getNestedComponentId());
    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 (callback.isAborted()) {
        callback.rethrow();
        return;
    }
    if (!response.isCommitted()) {
        resultProcessor.processResultValue(activePage.getName());
    }
}
Also used : ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) TrackableComponentEventCallback(org.apache.tapestry5.TrackableComponentEventCallback) Page(org.apache.tapestry5.internal.structure.Page) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 2 with TrackableComponentEventCallback

use of org.apache.tapestry5.TrackableComponentEventCallback 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 TrackableComponentEventCallback

use of org.apache.tapestry5.TrackableComponentEventCallback 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)

Aggregations

TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)3 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)2 Page (org.apache.tapestry5.internal.structure.Page)2 JSONObject (org.apache.tapestry5.json.JSONObject)1 ComponentEventResultProcessor (org.apache.tapestry5.services.ComponentEventResultProcessor)1