Search in sources :

Example 6 with Context

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

the class BlockInjectionProvider method createConduit.

private FieldConduit<Object> createConduit(PlasticField field, final String blockId) {
    final String className = field.getPlasticClass().getClassName();
    final String fieldName = field.getName();
    return new ReadOnlyComponentFieldConduit(className, fieldName) {

        public Object get(Object instance, InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            return resources.getBlock(blockId);
        }
    };
}
Also used : ReadOnlyComponentFieldConduit(org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 7 with Context

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

the class AssetInjectionProvider method provideInjection.

public boolean provideInjection(PlasticField field, ObjectLocator locator, MutableComponentModel componentModel) {
    Path path = field.getAnnotation(Path.class);
    if (path == null) {
        return false;
    }
    final String assetPath = path.value();
    final String libraryName = componentModel.getLibraryName();
    ComputedValue<Asset> computedAsset = new ComputedValue<Asset>() {

        public Asset get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            // a different library name than the subclass).
            return assetSource.getComponentAsset(resources, assetPath, libraryName);
        }
    };
    field.injectComputed(computedAsset);
    return true;
}
Also used : Path(org.apache.tapestry5.annotations.Path) ComputedValue(org.apache.tapestry5.plastic.ComputedValue) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) Asset(org.apache.tapestry5.Asset) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 8 with Context

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

the class PageLoaderImpl method addParameterBindingAction.

private void addParameterBindingAction(AssemblerContext context, final EmbeddedComponentAssembler embeddedAssembler, final String parameterName, final String parameterValue, final String metaDefaultBindingPrefix, final Location location, final boolean ignoreUnmatchedFormal) {
    if (embeddedAssembler.isBound(parameterName))
        return;
    embeddedAssembler.setBound(parameterName);
    if (parameterValue.startsWith(InternalConstants.INHERIT_BINDING_PREFIX)) {
        String containerParameterName = parameterValue.substring(InternalConstants.INHERIT_BINDING_PREFIX.length());
        addInheritedBindingAction(context, parameterName, containerParameterName);
        return;
    }
    context.add(new PageAssemblyAction() {

        public void execute(PageAssembly pageAssembly) {
            // Because of published parameters, we have to wait until page assembly time to throw out
            // informal parameters bound to components that don't support informal parameters ...
            // otherwise we'd throw out (sometimes!) published parameters.
            final ParameterBinder binder = embeddedAssembler.createParameterBinder(parameterName);
            if (binder == null) {
                if (ignoreUnmatchedFormal) {
                    return;
                }
                throw new UnknownValueException(String.format("Component %s does not include a formal parameter '%s' (and does not support informal parameters).", pageAssembly.createdElement.peek().getCompleteId(), parameterName), null, null, new AvailableValues("Formal parameters", embeddedAssembler.getFormalParameterNames()));
            }
            final String defaultBindingPrefix = binder.getDefaultBindingPrefix(metaDefaultBindingPrefix);
            InternalComponentResources containerResources = pageAssembly.activeElement.peek().getComponentResources();
            ComponentPageElement embeddedElement = pageAssembly.createdElement.peek();
            InternalComponentResources embeddedResources = embeddedElement.getComponentResources();
            Binding binding = elementFactory.newBinding(parameterName, containerResources, embeddedResources, defaultBindingPrefix, parameterValue, location);
            binder.bind(embeddedElement, binding);
        }
    });
}
Also used : LiteralBinding(org.apache.tapestry5.internal.bindings.LiteralBinding) Binding(org.apache.tapestry5.Binding) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues)

Example 9 with Context

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

the class PageLoaderImpl method parameter.

private void parameter(AssemblerContext context) {
    final ParameterToken token = context.next(ParameterToken.class);
    context.add(new PageAssemblyAction() {

        public void execute(PageAssembly pageAssembly) {
            String parameterName = token.name;
            ComponentPageElement element = pageAssembly.createdElement.peek();
            Location location = token.getLocation();
            BlockImpl block = new BlockImpl(location, interner.format("Parameter %s of %s", parameterName, element.getCompleteId()));
            Binding binding = new LiteralBinding(location, "block parameter " + parameterName, block);
            EmbeddedComponentAssembler embeddedAssembler = pageAssembly.embeddedAssembler.peek();
            ParameterBinder binder = embeddedAssembler.createParameterBinder(parameterName);
            if (binder == null) {
                throw new UnknownValueException(String.format("Component %s does not include a formal parameter '%s' (and does not support informal parameters).", element.getCompleteId(), parameterName), location, null, new AvailableValues("Formal parameters", embeddedAssembler.getFormalParameterNames()));
            }
            binder.bind(pageAssembly.createdElement.peek(), binding);
            pageAssembly.bodyElement.push(block);
        }
    });
    consumeToEndElementAndPopBodyElement(context);
}
Also used : LiteralBinding(org.apache.tapestry5.internal.bindings.LiteralBinding) Binding(org.apache.tapestry5.Binding) LiteralBinding(org.apache.tapestry5.internal.bindings.LiteralBinding) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues) Location(org.apache.tapestry5.commons.Location)

Example 10 with Context

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

the class PageLoaderImpl method startComponent.

private EmbeddedComponentAssembler startComponent(AssemblerContext context) {
    StartComponentToken token = context.next(StartComponentToken.class);
    ComponentAssembler assembler = context.assembler;
    String elementName = token.getElementName();
    // Initial guess: the type from the token (but this may be null in many cases).
    String embeddedType = token.getComponentType();
    // This may be null for an anonymous component.
    String embeddedId = token.getId();
    String embeddedComponentClassName = null;
    final EmbeddedComponentModel embeddedModel = embeddedId == null ? null : assembler.getModel().getEmbeddedComponentModel(embeddedId);
    if (embeddedId == null)
        embeddedId = assembler.generateEmbeddedId(embeddedType);
    if (embeddedModel != null) {
        String modelType = embeddedModel.getComponentType();
        if (InternalUtils.isNonBlank(modelType) && embeddedType != null) {
            throw new TapestryException(String.format("Embedded component '%s' provides a type attribute in the template ('%s') " + "as well as in the component class ('%s'). You should not provide a type attribute " + "in the template when defining an embedded component within the component class.", embeddedId, embeddedType, modelType), token, null);
        }
        embeddedType = modelType;
        embeddedComponentClassName = embeddedModel.getComponentClassName();
    }
    String componentClassName = embeddedComponentClassName;
    if (InternalUtils.isNonBlank(embeddedType)) {
        try {
            componentClassName = componentClassResolver.resolveComponentTypeToClassName(embeddedType);
        } catch (RuntimeException ex) {
            throw new TapestryException(ex.getMessage(), token, ex);
        }
    }
    // OK, now we can record an action to get it instantiated.
    EmbeddedComponentAssembler embeddedAssembler = assembler.createEmbeddedAssembler(embeddedId, componentClassName, embeddedModel, token.getMixins(), token.getLocation());
    addActionForEmbeddedComponent(context, embeddedAssembler, embeddedId, elementName, componentClassName);
    addParameterBindingActions(context, embeddedAssembler, embeddedModel);
    if (embeddedModel != null && embeddedModel.getInheritInformalParameters()) {
        // Another two-step: The first "captures" the container and embedded component. The second
        // occurs at the end of the page setup.
        assembler.add(new PageAssemblyAction() {

            public void execute(PageAssembly pageAssembly) {
                final ComponentPageElement container = pageAssembly.activeElement.peek();
                final ComponentPageElement embedded = pageAssembly.createdElement.peek();
                pageAssembly.deferred.add(new PageAssemblyAction() {

                    public void execute(PageAssembly pageAssembly) {
                        copyInformalParameters(container, embedded);
                    }
                });
            }
        });
    }
    return embeddedAssembler;
}
Also used : EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Aggregations

Test (org.testng.annotations.Test)29 Context (org.apache.tapestry5.http.services.Context)18 Link (org.apache.tapestry5.http.Link)16 EventContext (org.apache.tapestry5.EventContext)12 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)9 Request (org.apache.tapestry5.http.services.Request)9 Response (org.apache.tapestry5.http.services.Response)9 ComponentModel (org.apache.tapestry5.model.ComponentModel)9 Logger (org.slf4j.Logger)9 RequestFilter (org.apache.tapestry5.http.services.RequestFilter)8 RequestHandler (org.apache.tapestry5.http.services.RequestHandler)8 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)8 ServletContext (javax.servlet.ServletContext)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)7 ComponentResources (org.apache.tapestry5.ComponentResources)6 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)6 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)6 JSONObject (org.apache.tapestry5.json.JSONObject)6 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)5