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();
}
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;
}
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();
}
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();
}
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();
}
Aggregations