Search in sources :

Example 21 with Link

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

the class DefaultModuleDefImpl method bind.

/**
 * See if the build class defined a bind method and invoke it.
 *
 * @param remainingMethods
 *         set of methods as yet unaccounted for
 * @param modulePreventsServiceDecoration
 *         true if {@link org.apache.tapestry5.ioc.annotations.PreventServiceDecoration} on
 *         module
 *         class
 */
private void bind(Set<Method> remainingMethods, boolean modulePreventsServiceDecoration) {
    Throwable failure;
    Method bindMethod = null;
    try {
        bindMethod = moduleClass.getMethod("bind", ServiceBinder.class);
        if (!Modifier.isStatic(bindMethod.getModifiers()))
            throw new RuntimeException(IOCMessages.bindMethodMustBeStatic(toString(bindMethod)));
        ServiceBinderImpl binder = new ServiceBinderImpl(this, bindMethod, proxyFactory, defaultMarkers, modulePreventsServiceDecoration);
        bindMethod.invoke(null, binder);
        binder.finish();
        remainingMethods.remove(bindMethod);
        return;
    } catch (NoSuchMethodException ex) {
        return;
    } catch (IllegalArgumentException ex) {
        failure = ex;
    } catch (IllegalAccessException ex) {
        failure = ex;
    } catch (InvocationTargetException ex) {
        failure = ex.getTargetException();
    }
    String methodId = toString(bindMethod);
    throw new RuntimeException(IOCMessages.errorInBindMethod(methodId, failure), failure);
}
Also used : ServiceBinder(org.apache.tapestry5.ioc.ServiceBinder) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 22 with Link

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

the class RegistryBuilder method build.

/**
 * Constructs and returns the registry; this may only be done once. The caller is responsible for invoking
 * {@link org.apache.tapestry5.ioc.Registry#performRegistryStartup()}.
 */
public Registry build() {
    lock.lock();
    PerThreadOperationTracker tracker = new PerThreadOperationTracker(loggerSource.getLogger(Registry.class));
    RegistryImpl registry = new RegistryImpl(modules, proxyFactory, loggerSource, tracker);
    return new RegistryWrapper(registry);
}
Also used : RegistryImpl(org.apache.tapestry5.ioc.internal.RegistryImpl) PerThreadOperationTracker(org.apache.tapestry5.ioc.internal.PerThreadOperationTracker) RegistryWrapper(org.apache.tapestry5.ioc.internal.RegistryWrapper)

Example 23 with Link

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

the class Form method beginRender.

void beginRender(MarkupWriter writer) {
    Link link = resources.createFormEventLink(EventConstants.ACTION, context);
    String actionURL = secure && secureEnabled ? link.toAbsoluteURI(true) : link.toURI();
    actionSink = new ComponentActionSink(logger, clientDataEncoder);
    clientId = javascriptSupport.allocateClientId(resources);
    // Pre-register some names, to prevent client-side collisions with function names
    // attached to the JS Form object.
    IdAllocator allocator = new IdAllocator();
    preallocateNames(allocator);
    formSupport = createRenderTimeFormSupport(clientId, actionSink, allocator);
    environment.push(FormSupport.class, formSupport);
    environment.push(ValidationTracker.class, tracker);
    if (autofocus) {
        ValidationDecorator autofocusDecorator = new AutofocusValidationDecorator(environment.peek(ValidationDecorator.class), tracker, javascriptSupport);
        environment.push(ValidationDecorator.class, autofocusDecorator);
    }
    // Now that the environment is setup, inform the component or other
    // listeners that the form
    // is about to render.
    resources.triggerEvent(EventConstants.PREPARE_FOR_RENDER, context, null);
    resources.triggerEvent(EventConstants.PREPARE, context, null);
    // Push BeanValidationContext only after the container had a chance to prepare
    environment.push(BeanValidationContext.class, new BeanValidationContextImpl(validate));
    // Save the form element for later, in case we want to write an encoding
    // type attribute.
    form = writer.element("form", "id", clientId, "method", "post", "action", actionURL, "data-update-zone", zone, DATA_ATTRIBUTE, DATA_ATTRIBUTE_VALUE);
    if (clientValidation != ClientValidation.NONE) {
        writer.attributes("data-validate", "submit");
    }
    if (async) {
        javascriptSupport.require("t5/core/zone");
        writer.attributes("data-async-trigger", true);
    }
    resources.renderInformalParameters(writer);
    div = writer.element("div");
    for (String parameterName : link.getParameterNames()) {
        String[] values = link.getParameterValues(parameterName);
        for (String value : values) {
            // but the input value shouldn't be encoded.
            try {
                value = URLDecoder.decode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                logger.error("Enable to decode parameter value for parameter {} in form {}", parameterName, form.getName(), e);
            }
            writer.element("input", "type", "hidden", "name", parameterName, "value", value);
            writer.end();
        }
    }
    // div
    writer.end();
    environment.peek(Heartbeat.class).begin();
}
Also used : ComponentActionSink(org.apache.tapestry5.corelib.internal.ComponentActionSink) IdAllocator(org.apache.tapestry5.ioc.util.IdAllocator) AutofocusValidationDecorator(org.apache.tapestry5.internal.util.AutofocusValidationDecorator) Heartbeat(org.apache.tapestry5.services.Heartbeat) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AutofocusValidationDecorator(org.apache.tapestry5.internal.util.AutofocusValidationDecorator) Link(org.apache.tapestry5.http.Link) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

Example 24 with Link

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

the class FormFragment method beginRender.

/**
 * Renders a &lt;div&gt; tag and provides an override of the {@link org.apache.tapestry5.services.FormSupport}
 * environmental.
 */
void beginRender(MarkupWriter writer) {
    FormSupport formSupport = environment.peekRequired(FormSupport.class);
    String clientId = getClientId();
    hiddenFieldPositioner = new HiddenFieldPositioner(writer, rules);
    Element element = writer.element(this.element, "id", clientId, "data-component-type", "core/FormFragment");
    if (alwaysSubmit) {
        element.attribute("data-always-submit", "true");
    }
    resources.renderInformalParameters(writer);
    if (!visible) {
        element.attribute("style", "display: none;");
        if (!alwaysSubmit) {
            javascriptSupport.require("t5/core/form-fragment").invoke("hide").with(clientId);
        }
    }
    componentActions = new ComponentActionSink(logger, clientDataEncoder);
    // Here's the magic of environmentals ... we can create a wrapper around
    // the normal FormSupport environmental that intercepts some of the behavior.
    // Here we're setting aside all the actions inside the FormFragment so that we
    // can control whether those actions occur when the form is submitted.
    FormSupport override = new FormSupportAdapter(formSupport) {

        @Override
        public <T> void store(T component, ComponentAction<T> action) {
            componentActions.store(component, action);
        }

        @Override
        public <T> void storeCancel(T component, ComponentAction<T> action) {
            componentActions.storeCancel(component, action);
        }

        @Override
        public <T> void storeAndExecute(T component, ComponentAction<T> action) {
            componentActions.store(component, action);
            action.execute(component);
        }
    };
    // Tada! Now all the enclosed components will use our override of FormSupport,
    // until we pop it off.
    environment.push(FormSupport.class, override);
}
Also used : ComponentActionSink(org.apache.tapestry5.corelib.internal.ComponentActionSink) HiddenFieldPositioner(org.apache.tapestry5.corelib.internal.HiddenFieldPositioner) Element(org.apache.tapestry5.dom.Element) FormSupportAdapter(org.apache.tapestry5.corelib.internal.FormSupportAdapter) FormSupport(org.apache.tapestry5.services.FormSupport)

Example 25 with Link

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

the class PropertyEditor method setupEnvironment.

/**
 * Creates a {@link org.apache.tapestry5.services.PropertyEditContext} and pushes it onto the {@link
 * org.apache.tapestry5.services.Environment} stack.
 */
void setupEnvironment(final String propertyName) {
    propertyModel = model.get(propertyName);
    PropertyEditContext context = new PropertyEditContext() {

        public Messages getContainerMessages() {
            return overrides.getOverrideMessages();
        }

        public String getLabel() {
            return propertyModel.getLabel();
        }

        public String getPropertyId() {
            return propertyModel.getId();
        }

        public Class getPropertyType() {
            return propertyModel.getPropertyType();
        }

        public Object getPropertyValue() {
            return propertyModel.getConduit().get(object);
        }

        public FieldTranslator getTranslator(Field field) {
            return fieldTranslatorSource.createDefaultTranslator(field, propertyName, overrides.getOverrideMessages(), locale, propertyModel.getPropertyType(), propertyModel.getConduit());
        }

        public FieldValidator getValidator(Field field) {
            return fieldValidatorDefaultSource.createDefaultValidator(field, propertyName, overrides.getOverrideMessages(), locale, propertyModel.getPropertyType(), propertyModel.getConduit());
        }

        public void setPropertyValue(Object value) {
            propertyModel.getConduit().set(object, value);
        }

        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            return propertyModel.getAnnotation(annotationClass);
        }
    };
    environment.push(PropertyEditContext.class, context);
    BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class);
    if (beanValidationContext != null) {
        beanValidationContext.setCurrentProperty(propertyName);
    }
    heartbeat.begin();
}
Also used : PropertyEditContext(org.apache.tapestry5.services.PropertyEditContext) BeanValidationContext(org.apache.tapestry5.internal.BeanValidationContext) Annotation(java.lang.annotation.Annotation)

Aggregations

Link (org.apache.tapestry5.http.Link)66 Test (org.testng.annotations.Test)37 Response (org.apache.tapestry5.http.services.Response)19 MarkupWriter (org.apache.tapestry5.MarkupWriter)10 JSONObject (org.apache.tapestry5.json.JSONObject)10 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)10 Request (org.apache.tapestry5.http.services.Request)8 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)8 Element (org.apache.tapestry5.dom.Element)7 Contribute (org.apache.tapestry5.ioc.annotations.Contribute)7 Link (org.apache.tapestry5.Link)6 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)6 EventContext (org.apache.tapestry5.EventContext)5 ComponentClassResolver (org.apache.tapestry5.services.ComponentClassResolver)5 IOException (java.io.IOException)4 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)4 BaseURLSource (org.apache.tapestry5.http.services.BaseURLSource)4 Page (org.apache.tapestry5.internal.structure.Page)4 List (java.util.List)3 ComponentResources (org.apache.tapestry5.ComponentResources)3