Search in sources :

Example 16 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class BeanEditor method doPrepare.

/**
 * Used to initialize the model if necessary, to instantiate the object being edited if necessary, and to push the
 * BeanEditContext into the environment.
 */
void doPrepare() {
    if (model == null) {
        Class type = resources.getBoundType("object");
        model = modelSource.createEditModel(type, overrides.getOverrideMessages());
        BeanModelUtils.modify(model, add, include, exclude, reorder);
    }
    if (object == null) {
        try {
            object = model.newInstance();
        } catch (Exception ex) {
            String message = String.format("Exception instantiating instance of %s (for component '%s'): %s", PlasticUtils.toTypeName(model.getBeanType()), resources.getCompleteId(), ex);
            throw new TapestryException(message, resources.getLocation(), ex);
        }
    }
    BeanEditContext context = new BeanEditContextImpl(model.getBeanType());
    cachedObject = object;
    environment.push(BeanEditContext.class, context);
    // TAP5-2101: Always provide a new BeanValidationContext
    environment.push(BeanValidationContext.class, new BeanValidationContextImpl(object));
}
Also used : BeanEditContextImpl(org.apache.tapestry5.internal.BeanEditContextImpl) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) BeanEditContext(org.apache.tapestry5.services.BeanEditContext) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

Example 17 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class AbstractPropertyOutputTest method test_null_pointer_exception_message.

@Test
public // Tests TAPESTRY-2182.
void test_null_pointer_exception_message() {
    final PropertyConduit conduit = mockPropertyConduit();
    final PropertyModel model = mockPropertyModel();
    final Object object = new Object();
    ComponentResources resources = mockComponentResources();
    Location location = mockLocation();
    propertyOutputFixture.inject(model, object, resources);
    expect(model.getConduit()).andReturn(conduit);
    expect(conduit.get(object)).andThrow(new NullPointerException());
    expect(model.getPropertyName()).andReturn("wilma.occupation.address");
    expect(resources.getLocation()).andReturn(location);
    replay();
    try {
        propertyOutputFixture.readPropertyForObject();
        fail("Expected a NullPointerException to be thrown.");
    } catch (final TapestryException ex) {
        assertEquals(ex.getMessage(), "Property 'wilma.occupation.address' contains a null value in the path.");
        assertSame(ex.getLocation(), location);
        assertTrue(ex.getCause() instanceof NullPointerException);
    }
    verify();
}
Also used : PropertyModel(org.apache.tapestry5.beanmodel.PropertyModel) PropertyConduit(org.apache.tapestry5.beanmodel.PropertyConduit) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) ComponentResources(org.apache.tapestry5.ComponentResources) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 18 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException 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 19 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class ComponentAssemblerImpl method createEmbeddedAssembler.

public EmbeddedComponentAssembler createEmbeddedAssembler(String embeddedId, String componentClassName, EmbeddedComponentModel embeddedModel, String mixins, Location location) {
    try {
        if (InternalUtils.isBlank(componentClassName)) {
            throw new TapestryException("You must specify the type via t:type, the element, or @Component annotation.", location, null);
        }
        EmbeddedComponentAssemblerImpl embedded = new EmbeddedComponentAssemblerImpl(assemblerSource, instantiatorSource, componentClassResolver, componentClassName, getSelector(), embeddedModel, mixins, location, strictMixinParameters);
        if (embeddedIdToAssembler == null)
            embeddedIdToAssembler = CollectionFactory.newMap();
        embeddedIdToAssembler.put(embeddedId, embedded);
        if (embeddedModel != null) {
            for (String publishedParameterName : embeddedModel.getPublishedParameters()) {
                if (publishedParameterToEmbeddedId == null)
                    publishedParameterToEmbeddedId = CollectionFactory.newCaseInsensitiveMap();
                String existingEmbeddedId = publishedParameterToEmbeddedId.get(publishedParameterName);
                if (existingEmbeddedId != null) {
                    throw new TapestryException(String.format("Parameter '%s' of embedded component '%s' can not be published as a parameter of component %s, as it has previously been published by embedded component '%s'.", publishedParameterName, embeddedId, instantiator.getModel().getComponentClassName(), existingEmbeddedId), location, null);
                }
                publishedParameterToEmbeddedId.put(publishedParameterName, embeddedId);
            }
        }
        return embedded;
    } catch (Exception ex) {
        throw new TapestryException(String.format("Failure creating embedded component '%s' of %s: %s", embeddedId, instantiator.getModel().getComponentClassName(), ExceptionUtils.toMessage(ex)), location, ex);
    }
}
Also used : TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 20 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class EmbeddedComponentAssemblerImpl method addMixin.

private void addMixin(String className, String... order) {
    Instantiator mixinInstantiator = instantiatorSource.getInstantiator(className);
    String mixinId = InternalUtils.lastTerm(className);
    if (mixinIdToInstantiator.containsKey(mixinId))
        throw new TapestryException(String.format("Mixins applied to a component must be unique. Mixin '%s' has already been applied.", mixinId), location, null);
    mixinIdToInstantiator.put(mixinId, mixinInstantiator);
    mixinsIdToOrderConstraints.put(mixinId, order);
}
Also used : Instantiator(org.apache.tapestry5.internal.services.Instantiator) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Aggregations

TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)37 Location (org.apache.tapestry5.commons.Location)12 Test (org.testng.annotations.Test)12 ComponentResources (org.apache.tapestry5.ComponentResources)11 Binding (org.apache.tapestry5.Binding)7 BindingFactory (org.apache.tapestry5.services.BindingFactory)4 IOException (java.io.IOException)3 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)3 Component (org.apache.tapestry5.runtime.Component)3 PropertyOverrides (org.apache.tapestry5.PropertyOverrides)2 TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)2 PropertyModel (org.apache.tapestry5.beanmodel.PropertyModel)2 Messages (org.apache.tapestry5.commons.Messages)2 Link (org.apache.tapestry5.http.Link)2 Instantiator (org.apache.tapestry5.internal.services.Instantiator)2 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)2 Page (org.apache.tapestry5.internal.structure.Page)2 OperationException (org.apache.tapestry5.ioc.internal.OperationException)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 BindingSource (org.apache.tapestry5.services.BindingSource)2