Search in sources :

Example 26 with Event

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

the class PageActivationContextCollectorImpl method collectPageActivationContext.

public Object[] collectPageActivationContext(String pageName) {
    Boolean hasHandler = cache.get(pageName);
    if (hasHandler == null) {
        ComponentModel model = modelSource.getPageModel(pageName);
        hasHandler = model.handlesEvent(EventConstants.PASSIVATE);
        cache.put(pageName, hasHandler);
    }
    if (!hasHandler)
        return EMPTY;
    // Get or create a page instance and trigger the event.
    Page page = requestPageCache.get(pageName);
    ComponentPageElement element = page.getRootElement();
    final Holder<Object[]> holder = Holder.create();
    ComponentEventCallback callback = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            holder.put(typeCoercer.coerce(result, Object[].class));
            return true;
        }
    };
    element.triggerEvent(EventConstants.PASSIVATE, null, callback);
    if (!holder.hasValue())
        return EMPTY;
    return holder.get();
}
Also used : ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) ComponentModel(org.apache.tapestry5.model.ComponentModel) Page(org.apache.tapestry5.internal.structure.Page) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback)

Example 27 with Event

use of org.apache.tapestry5.runtime.Event 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 28 with Event

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

the class ComponentEventLinkEncoderImplTest method decode_compoent_event_request_with_slash_in_context_path.

@Test
public void decode_compoent_event_request_with_slash_in_context_path() throws Exception {
    ComponentClassResolver resolver = mockComponentClassResolver();
    Request request = mockRequest();
    Response response = mockResponse();
    LocalizationSetter ls = mockLocalizationSetter();
    MetaDataLocator metaDataLocator = neverWhitelistProtected();
    expect(ls.isSupportedLocaleName("page.component:event")).andReturn(false);
    train_getParameter(request, InternalConstants.PAGE_CONTEXT_NAME, null);
    train_getParameter(request, InternalConstants.CONTAINER_PAGE_NAME, null);
    train_getLocale(request, Locale.ENGLISH);
    ls.setNonPersistentLocaleFromLocaleName("en");
    train_getPath(request, "/foo/bar/page.component:event");
    train_isPageName(resolver, "page", true);
    train_canonicalizePageName(resolver, "page", "Page");
    replay();
    ComponentEventLinkEncoderImpl linkEncoder = new ComponentEventLinkEncoderImpl(resolver, contextPathEncoder, ls, response, null, null, null, true, null, "foo/bar", metaDataLocator, null);
    ComponentEventRequestParameters parameters = linkEncoder.decodeComponentEventRequest(request);
    assertNotNull(parameters);
    assertEquals(parameters.getActivePageName(), "Page");
    assertEquals(parameters.getEventType(), "event");
    verify();
}
Also used : Response(org.apache.tapestry5.http.services.Response) ComponentEventRequestParameters(org.apache.tapestry5.services.ComponentEventRequestParameters) ComponentClassResolver(org.apache.tapestry5.services.ComponentClassResolver) Request(org.apache.tapestry5.http.services.Request) LocalizationSetter(org.apache.tapestry5.services.LocalizationSetter) MetaDataLocator(org.apache.tapestry5.services.MetaDataLocator) Test(org.testng.annotations.Test)

Example 29 with Event

use of org.apache.tapestry5.runtime.Event 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 30 with Event

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

Aggregations

ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)19 ComponentModel (org.apache.tapestry5.model.ComponentModel)14 Logger (org.slf4j.Logger)14 Test (org.testng.annotations.Test)14 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)13 JSONObject (org.apache.tapestry5.json.JSONObject)11 EventContext (org.apache.tapestry5.EventContext)9 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)6 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)6 Component (org.apache.tapestry5.runtime.Component)6 ComponentResources (org.apache.tapestry5.ComponentResources)4 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)4 JSONArray (org.apache.tapestry5.json.JSONArray)4 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)4 TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)3 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)3 Page (org.apache.tapestry5.internal.structure.Page)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Link (org.apache.tapestry5.Link)2