Search in sources :

Example 16 with EventContext

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

the class AjaxFormLoop method onInjectRow.

Object onInjectRow(EventContext context) {
    ComponentEventCallback callback = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            value = result;
            return true;
        }
    };
    resources.triggerContextEvent(EventConstants.ADD_ROW, context, callback);
    if (value == null)
        throw new IllegalArgumentException(String.format("Event handler for event 'addRow' from %s should have returned a non-null value.", resources.getCompleteId()));
    ajaxResponseRenderer.addFilter(new PartialMarkupRendererFilter() {

        public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer) {
            pushContext();
            renderer.renderMarkup(writer, reply);
            popContext();
        }
    });
    return ajaxResponse;
}
Also used : PartialMarkupRendererFilter(org.apache.tapestry5.services.PartialMarkupRendererFilter) JSONObject(org.apache.tapestry5.json.JSONObject) PartialMarkupRenderer(org.apache.tapestry5.services.PartialMarkupRenderer) JSONObject(org.apache.tapestry5.json.JSONObject)

Example 17 with EventContext

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

the class TapestryModule method contributeObjectRenderer.

/**
 * Contributes a default object renderer for type Object, plus specialized
 * renderers for {@link org.apache.tapestry5.http.services.Request}, {@link org.apache.tapestry5.commons.Location},
 * {@link org.apache.tapestry5.ComponentResources}, {@link org.apache.tapestry5.EventContext},
 * {@link AvailableValues},
 * List, and Object[].
 */
@SuppressWarnings("unchecked")
public void contributeObjectRenderer(MappedConfiguration<Class, ObjectRenderer> configuration, @InjectService("LocationRenderer") ObjectRenderer locationRenderer, final TypeCoercer typeCoercer) {
    configuration.add(Object.class, new DefaultObjectRenderer());
    configuration.addInstance(Request.class, RequestRenderer.class);
    configuration.add(Location.class, locationRenderer);
    ObjectRenderer preformatted = new ObjectRenderer<Object>() {

        public void render(Object object, MarkupWriter writer) {
            writer.element("pre");
            writer.write(typeCoercer.coerce(object, String.class));
            writer.end();
        }
    };
    configuration.addInstance(List.class, ListRenderer.class);
    configuration.addInstance(Object[].class, ObjectArrayRenderer.class);
    configuration.addInstance(ComponentResources.class, ComponentResourcesRenderer.class);
    configuration.addInstance(EventContext.class, EventContextRenderer.class);
    configuration.add(AvailableValues.class, new AvailableValuesRenderer());
}
Also used : AvailableValuesRenderer(org.apache.tapestry5.internal.renderers.AvailableValuesRenderer) ObjectRenderer(org.apache.tapestry5.services.ObjectRenderer) DefaultObjectRenderer(org.apache.tapestry5.services.DefaultObjectRenderer) DefaultObjectRenderer(org.apache.tapestry5.services.DefaultObjectRenderer) JSONObject(org.apache.tapestry5.json.JSONObject) MarkupWriter(org.apache.tapestry5.MarkupWriter)

Example 18 with EventContext

use of org.apache.tapestry5.EventContext 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 19 with EventContext

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

the class ComponentEventImplTest method unable_to_coerce.

@Test
public void unable_to_coerce() {
    ComponentEventCallback handler = mockComponentEventHandler();
    EventContext context = mockEventContext();
    ComponentPageElementResources resources = mockComponentPageElementResources();
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, true);
    logger.debug(eq(TapestryMarkers.EVENT_HANDLER_METHOD), isA(String.class));
    train_toClass(resources, Integer.class.getName(), Integer.class);
    train_getCount(context, 1);
    expect(context.get(Integer.class, 0)).andThrow(new NumberFormatException("Not so easy, is it?"));
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, resources, false, model, logger);
    event.setMethodDescription("foo.Bar.baz()");
    try {
        event.coerceContext(0, "java.lang.Integer");
        unreachable();
    } catch (IllegalArgumentException ex) {
        // Different JVMs will report the conversion error slightly differently,
        // so we don't try to check that part of the error message.
        assertTrue(ex.getMessage().startsWith("Exception in method foo.Bar.baz(), parameter #1:"));
    }
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 20 with EventContext

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

the class ComponentEventImplTest method event_type_match_is_case_insensitive.

@Test
public void event_type_match_is_case_insensitive() {
    ComponentEventCallback handler = mockComponentEventHandler();
    EventContext context = mockEventContext();
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, false);
    train_getCount(context, 0);
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, null, false, model, logger);
    assertTrue(event.matches("EVENTTYPE", "someid", 0));
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Aggregations

EventContext (org.apache.tapestry5.EventContext)16 Test (org.testng.annotations.Test)15 Logger (org.slf4j.Logger)9 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)8 Link (org.apache.tapestry5.http.Link)8 ComponentModel (org.apache.tapestry5.model.ComponentModel)8 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)8 Page (org.apache.tapestry5.internal.structure.Page)5 ComponentEventRequestParameters (org.apache.tapestry5.services.ComponentEventRequestParameters)5 EmptyEventContext (org.apache.tapestry5.internal.EmptyEventContext)4 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)4 AbstractEventContext (org.apache.tapestry5.internal.AbstractEventContext)3 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)3 JSONObject (org.apache.tapestry5.json.JSONObject)3 ComponentClassResolver (org.apache.tapestry5.services.ComponentClassResolver)3 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)3 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)3 Request (org.apache.tapestry5.http.services.Request)2 PageRenderLinkSource (org.apache.tapestry5.services.PageRenderLinkSource)2 IOException (java.io.IOException)1