Search in sources :

Example 1 with Application

use of jakarta.faces.application.Application in project myfaces by apache.

the class FacesConfigurator method configureProtectedViews.

public void configureProtectedViews() {
    Application application = getApplication();
    FacesConfigData dispenser = getDispenser();
    // Protected Views
    ViewHandler viewHandler = application.getViewHandler();
    for (String urlPattern : dispenser.getProtectedViewUrlPatterns()) {
        viewHandler.addProtectedView(urlPattern);
    }
}
Also used : FacesConfigData(org.apache.myfaces.config.element.FacesConfigData) ViewHandler(jakarta.faces.application.ViewHandler) Application(jakarta.faces.application.Application)

Example 2 with Application

use of jakarta.faces.application.Application in project myfaces by apache.

the class FacesConfigurator method configureFlowHandler.

private void configureFlowHandler() {
    FacesContext facesContext = getFacesContext();
    Application application = getApplication();
    FacesConfigData dispenser = getDispenser();
    if (!dispenser.getFacesFlowDefinitions().isEmpty()) {
        // Faces Flow require client window enabled to work.
        FacesConfigurator.enableDefaultWindowMode(facesContext);
    }
    for (FacesFlowDefinition flowDefinition : dispenser.getFacesFlowDefinitions()) {
        FlowImpl flow = new FlowImpl();
        // TODO: configure flow object
        flow.setId(flowDefinition.getId());
        flow.setDefiningDocumentId(flowDefinition.getDefiningDocumentId());
        flow.setStartNodeId(flowDefinition.getStartNode());
        if (StringUtils.isNotEmpty(flowDefinition.getInitializer())) {
            flow.setInitializer(application.getExpressionFactory().createMethodExpression(facesContext.getELContext(), flowDefinition.getInitializer(), null, NO_PARAMETER_TYPES));
        }
        if (StringUtils.isNotEmpty(flowDefinition.getFinalizer())) {
            flow.setFinalizer(application.getExpressionFactory().createMethodExpression(facesContext.getELContext(), flowDefinition.getFinalizer(), null, NO_PARAMETER_TYPES));
        }
        for (FacesFlowCall call : flowDefinition.getFlowCallList()) {
            FlowCallNodeImpl node = new FlowCallNodeImpl(call.getId());
            if (call.getFlowReference() != null) {
                node.setCalledFlowId(call.getFlowReference().getFlowId());
                node.setCalledFlowDocumentId(call.getFlowReference().getFlowDocumentId());
            }
            for (FacesFlowParameter parameter : call.getOutboundParameterList()) {
                node.putOutboundParameter(parameter.getName(), new ParameterImpl(parameter.getName(), application.getExpressionFactory().createValueExpression(facesContext.getELContext(), parameter.getValue(), Object.class)));
            }
            flow.putFlowCall(node.getId(), node);
        }
        for (FacesFlowMethodCall methodCall : flowDefinition.getMethodCallList()) {
            MethodCallNodeImpl node = new MethodCallNodeImpl(methodCall.getId());
            if (StringUtils.isNotEmpty(methodCall.getMethod())) {
                node.setMethodExpression(application.getExpressionFactory().createMethodExpression(facesContext.getELContext(), methodCall.getMethod(), null, NO_PARAMETER_TYPES));
            }
            if (StringUtils.isNotEmpty(methodCall.getDefaultOutcome())) {
                node.setOutcome(application.getExpressionFactory().createValueExpression(facesContext.getELContext(), methodCall.getDefaultOutcome(), Object.class));
            }
            for (FacesFlowMethodParameter parameter : methodCall.getParameterList()) {
                node.addParameter(new ParameterImpl(parameter.getClassName(), application.getExpressionFactory().createValueExpression(facesContext.getELContext(), parameter.getValue(), Object.class)));
            }
            flow.addMethodCall(node);
        }
        for (FacesFlowParameter parameter : flowDefinition.getInboundParameterList()) {
            flow.putInboundParameter(parameter.getName(), new ParameterImpl(parameter.getName(), application.getExpressionFactory().createValueExpression(facesContext.getELContext(), parameter.getValue(), Object.class)));
        }
        for (NavigationRule rule : flowDefinition.getNavigationRuleList()) {
            flow.addNavigationCases(rule.getFromViewId(), NavigationUtils.convertNavigationCasesToAPI(rule));
        }
        for (FacesFlowSwitch flowSwitch : flowDefinition.getSwitchList()) {
            SwitchNodeImpl node = new SwitchNodeImpl(flowSwitch.getId());
            if (flowSwitch.getDefaultOutcome() != null && StringUtils.isNotEmpty(flowSwitch.getDefaultOutcome().getFromOutcome())) {
                if (ELText.isLiteral(flowSwitch.getDefaultOutcome().getFromOutcome())) {
                    node.setDefaultOutcome(flowSwitch.getDefaultOutcome().getFromOutcome());
                } else {
                    node.setDefaultOutcome(application.getExpressionFactory().createValueExpression(facesContext.getELContext(), flowSwitch.getDefaultOutcome().getFromOutcome(), Object.class));
                }
            }
            for (NavigationCase navCase : flowSwitch.getNavigationCaseList()) {
                SwitchCaseImpl nodeCase = new SwitchCaseImpl();
                nodeCase.setFromOutcome(navCase.getFromOutcome());
                if (StringUtils.isNotEmpty(navCase.getIf())) {
                    nodeCase.setCondition(application.getExpressionFactory().createValueExpression(facesContext.getELContext(), navCase.getIf(), Object.class));
                }
                node.addCase(nodeCase);
            }
            flow.putSwitch(node.getId(), node);
        }
        for (FacesFlowView view : flowDefinition.getViewList()) {
            ViewNodeImpl node = new ViewNodeImpl(view.getId(), view.getVdlDocument());
            flow.addView(node);
        }
        for (FacesFlowReturn flowReturn : flowDefinition.getReturnList()) {
            ReturnNodeImpl node = new ReturnNodeImpl(flowReturn.getId());
            if (flowReturn.getNavigationCase() != null && StringUtils.isNotEmpty(flowReturn.getNavigationCase().getFromOutcome())) {
                if (ELText.isLiteral(flowReturn.getNavigationCase().getFromOutcome())) {
                    node.setFromOutcome(flowReturn.getNavigationCase().getFromOutcome());
                } else {
                    node.setFromOutcome(application.getExpressionFactory().createValueExpression(facesContext.getELContext(), flowReturn.getNavigationCase().getFromOutcome(), Object.class));
                }
            }
            flow.putReturn(node.getId(), node);
        }
        flow.freeze();
        // Add the flow, so the config can be processed by the flow system and the
        // navigation system.
        application.getFlowHandler().addFlow(facesContext, flow);
    }
    configureAnnotatedFlows(facesContext);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) MethodCallNodeImpl(org.apache.myfaces.flow.MethodCallNodeImpl) FacesFlowReturn(org.apache.myfaces.config.element.FacesFlowReturn) FacesConfigData(org.apache.myfaces.config.element.FacesConfigData) FlowImpl(org.apache.myfaces.flow.FlowImpl) FlowCallNodeImpl(org.apache.myfaces.flow.FlowCallNodeImpl) FacesFlowMethodParameter(org.apache.myfaces.config.element.FacesFlowMethodParameter) NavigationRule(org.apache.myfaces.config.element.NavigationRule) ViewNodeImpl(org.apache.myfaces.flow.ViewNodeImpl) ReturnNodeImpl(org.apache.myfaces.flow.ReturnNodeImpl) FacesFlowCall(org.apache.myfaces.config.element.FacesFlowCall) ParameterImpl(org.apache.myfaces.flow.ParameterImpl) NavigationCase(org.apache.myfaces.config.element.NavigationCase) FacesFlowView(org.apache.myfaces.config.element.FacesFlowView) SwitchCaseImpl(org.apache.myfaces.flow.SwitchCaseImpl) FacesFlowParameter(org.apache.myfaces.config.element.FacesFlowParameter) FacesFlowSwitch(org.apache.myfaces.config.element.FacesFlowSwitch) FacesFlowMethodCall(org.apache.myfaces.config.element.FacesFlowMethodCall) Application(jakarta.faces.application.Application) SwitchNodeImpl(org.apache.myfaces.flow.SwitchNodeImpl) FacesFlowDefinition(org.apache.myfaces.config.element.FacesFlowDefinition)

Example 3 with Application

use of jakarta.faces.application.Application in project myfaces by apache.

the class ApplicationImpl method createComponent.

@Override
public UIComponent createComponent(FacesContext context, Resource componentResource) {
    Assert.notNull(context, "context");
    Assert.notNull(componentResource, "componentResource");
    UIComponent component = null;
    Resource resource;
    String fqcn;
    Class<? extends UIComponent> componentClass = null;
    /*
         * Obtain a reference to the ViewDeclarationLanguage for this Application instance by calling
         * ViewHandler.getViewDeclarationLanguage(jakarta.faces.context.FacesContext, java.lang.String), passing the
         * viewId found by calling UIViewRoot.getViewId() on the UIViewRoot in the argument FacesContext.
         */
    UIViewRoot view = context.getViewRoot();
    Application application = context.getApplication();
    ViewDeclarationLanguage vdl = application.getViewHandler().getViewDeclarationLanguage(context, view.getViewId());
    /*
         * Obtain a reference to the composite component metadata for this composite component by calling
         * ViewDeclarationLanguage.getComponentMetadata(jakarta.faces.context.FacesContext,
         * jakarta.faces.application.Resource), passing the facesContext and componentResource arguments to this method.
         * This version of JSF specification uses JavaBeans as the API to the component metadata.
         */
    BeanInfo metadata = vdl.getComponentMetadata(context, componentResource);
    if (metadata == null) {
        throw new FacesException("Could not get component metadata for " + componentResource.getResourceName() + ". Did you forget to specify <composite:interface>?");
    }
    /*
         * Determine if the component author declared a component-type for this component instance by obtaining the
         * BeanDescriptor from the component metadata and calling its getValue() method, passing
         * UIComponent.COMPOSITE_COMPONENT_TYPE_KEY as the argument. If non-null, the result must be a ValueExpression
         * whose value is the component-type of the UIComponent to be created for this Resource component. Call through
         * to createComponent(java.lang.String) to create the component.
         */
    BeanDescriptor descriptor = metadata.getBeanDescriptor();
    ValueExpression componentType = (ValueExpression) descriptor.getValue(UIComponent.COMPOSITE_COMPONENT_TYPE_KEY);
    boolean annotationsApplied = false;
    if (componentType != null) {
        component = application.createComponent((String) componentType.getValue(context.getELContext()));
        annotationsApplied = true;
    } else {
        /*
             * Otherwise, determine if a script based component for this Resource can be found by calling
             * ViewDeclarationLanguage.getScriptComponentResource(jakarta.faces.context.FacesContext,
             * jakarta.faces.application.Resource). If the result is non-null, and is a script written in one of the
             * languages listed in JSF 4.3 of the specification prose document, create a UIComponent instance from the
             * script resource.
             */
        resource = vdl.getScriptComponentResource(context, componentResource);
        if (resource != null) {
            String name = resource.getResourceName();
            String className = name.substring(0, name.lastIndexOf('.'));
            component = (UIComponent) ClassUtils.newInstance(className);
        } else {
            /*
                 * Otherwise, let library-name be the return from calling Resource.getLibraryName() on the argument
                 * componentResource and resource-name be the return from calling Resource.getResourceName() on the
                 * argument componentResource. Create a fully qualified Java class name by removing any file extension
                 * from resource-name and let fqcn be library-name + "." + resource-name. If a class with the name of
                 * fqcn cannot be found, take no action and continue to the next step. If any of 
                 * InstantiationException,
                 * IllegalAccessException, or ClassCastException are thrown, wrap the exception in a FacesException and
                 * re-throw it. If any other exception is thrown, log the exception and continue to the next step.
                 */
            boolean isProduction = FacesContext.getCurrentInstance().isProjectStage(ProjectStage.Production);
            String name = componentResource.getResourceName();
            String className = name.substring(0, name.lastIndexOf('.'));
            fqcn = componentResource.getLibraryName() + '.' + className;
            if (isProduction) {
                componentClass = (Class<? extends UIComponent>) _componentClassMap.get(fqcn);
            }
            if (componentClass == null) {
                try {
                    componentClass = ClassUtils.classForName(fqcn);
                    if (isProduction) {
                        _componentClassMap.put(fqcn, componentClass);
                    }
                } catch (ClassNotFoundException e) {
                    // Remember here that classForName did not find Class
                    if (isProduction) {
                        _componentClassMap.put(fqcn, NOTHING.getClass());
                    }
                }
            }
            if (componentClass != null && NOTHING.getClass() != componentClass) {
                try {
                    component = componentClass.newInstance();
                } catch (InstantiationException e) {
                    log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
                    throw new FacesException("Could not instantiate component class name = " + fqcn, e);
                } catch (IllegalAccessException e) {
                    log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
                    throw new FacesException("Could not instantiate component class name = " + fqcn, e);
                } catch (Exception e) {
                    log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
                }
            }
            /*
                 * If none of the previous steps have yielded a UIComponent instance, call
                 * createComponent(java.lang.String) passing "jakarta.faces.NamingContainer" as the argument.
                 */
            if (component == null) {
                component = application.createComponent(context, UINamingContainer.COMPONENT_TYPE, null);
                annotationsApplied = true;
            }
        }
    }
    /*
         * Call UIComponent.setRendererType(java.lang.String) on the UIComponent instance, passing
         * "jakarta.faces.Composite" as the argument.
         */
    component.setRendererType("jakarta.faces.Composite");
    /*
         * Store the argument Resource in the attributes Map of the UIComponent under the key,
         * Resource.COMPONENT_RESOURCE_KEY.
         */
    component.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
    /*
         * Store composite component metadata in the attributes Map of the UIComponent under the key,
         * UIComponent.BEANINFO_KEY.
         */
    component.getAttributes().put(UIComponent.BEANINFO_KEY, metadata);
    /*
         * Before the component instance is returned, it must be inspected for the presence of a 
         * ListenerFor annotation.
         * If this annotation is present, the action listed in ListenerFor must be taken on the component, 
         * before it is
         * returned from this method.
         */
    if (!annotationsApplied) {
        _handleAnnotations(context, component, component);
    }
    return component;
}
Also used : BeanInfo(java.beans.BeanInfo) UIComponent(jakarta.faces.component.UIComponent) Resource(jakarta.faces.application.Resource) ViewDeclarationLanguage(jakarta.faces.view.ViewDeclarationLanguage) FacesException(jakarta.faces.FacesException) MissingResourceException(java.util.MissingResourceException) NamingException(javax.naming.NamingException) ELException(jakarta.el.ELException) FacesException(jakarta.faces.FacesException) BeanDescriptor(java.beans.BeanDescriptor) ValueExpression(jakarta.el.ValueExpression) UIViewRoot(jakarta.faces.component.UIViewRoot) Application(jakarta.faces.application.Application)

Example 4 with Application

use of jakarta.faces.application.Application in project myfaces by apache.

the class RenderResponseExecutor method execute.

@Override
public boolean execute(FacesContext facesContext) {
    Application application = facesContext.getApplication();
    ViewHandler viewHandler = application.getViewHandler();
    UIViewRoot root;
    UIViewRoot previousRoot;
    String viewId;
    String newViewId;
    boolean isNotSameRoot;
    int loops = 0;
    int maxLoops = 15;
    if (facesContext.getViewRoot() == null) {
        throw new ViewNotFoundException("A view is required to execute " + facesContext.getCurrentPhaseId());
    }
    forceSessionCreation(facesContext);
    try {
        // do-while, because the view might change in PreRenderViewEvent-listeners
        do {
            root = facesContext.getViewRoot();
            previousRoot = root;
            viewId = root.getViewId();
            ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(facesContext, viewId);
            if (vdl != null) {
                vdl.buildView(facesContext, root);
            }
            // publish a PreRenderViewEvent: note that the event listeners
            // of this event can change the view, so we have to perform the algorithm
            // until the viewId does not change when publishing this event.
            application.publishEvent(facesContext, PreRenderViewEvent.class, root);
            // was the response marked as complete by an event listener?
            if (facesContext.getResponseComplete()) {
                return false;
            }
            root = facesContext.getViewRoot();
            newViewId = root.getViewId();
            isNotSameRoot = !((newViewId == null ? newViewId == viewId : newViewId.equals(viewId)) && previousRoot.equals(root));
            loops++;
        } while ((newViewId == null && viewId != null) || (newViewId != null && (!newViewId.equals(viewId) || isNotSameRoot)) && loops < maxLoops);
        if (loops == maxLoops) {
            // PreRenderView reach maxLoops - probably a infinitive recursion:
            Level level = facesContext.isProjectStage(ProjectStage.Production) ? Level.FINE : Level.WARNING;
            if (log.isLoggable(level)) {
                log.log(level, "Cicle over buildView-PreRenderViewEvent on RENDER_RESPONSE phase " + "reaches maximal limit, please check listeners for infinite recursion.");
            }
        }
        viewHandler.renderView(facesContext, root);
        application.publishEvent(facesContext, PostRenderViewEvent.class, root);
        // log all unhandled FacesMessages, don't swallow them
        // perf: org.apache.myfaces.context.servlet.FacesContextImpl.getMessageList() creates
        // new Collections.unmodifiableList with every invocation->  call it only once
        // and messageList is RandomAccess -> use index based loop
        List<FacesMessage> messageList = facesContext.getMessageList();
        if (!messageList.isEmpty()) {
            StringBuilder builder = new StringBuilder();
            boolean shouldLog = false;
            for (int i = 0, size = messageList.size(); i < size; i++) {
                FacesMessage message = messageList.get(i);
                if (!message.isRendered()) {
                    builder.append("\n- ");
                    builder.append(message.getDetail());
                    shouldLog = true;
                }
            }
            if (shouldLog) {
                log.log(Level.WARNING, "There are some unhandled FacesMessages, " + "this means not every FacesMessage had a chance to be rendered.\n" + "These unhandled FacesMessages are: " + builder.toString());
            }
        }
    } catch (IOException e) {
        throw new FacesException(e.getMessage(), e);
    }
    return false;
}
Also used : ViewHandler(jakarta.faces.application.ViewHandler) ViewDeclarationLanguage(jakarta.faces.view.ViewDeclarationLanguage) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException) Level(java.util.logging.Level) Application(jakarta.faces.application.Application) UIViewRoot(jakarta.faces.component.UIViewRoot) FacesMessage(jakarta.faces.application.FacesMessage)

Example 5 with Application

use of jakarta.faces.application.Application in project myfaces by apache.

the class MockApplication20 method _handleAttachedResourceDependency.

private void _handleAttachedResourceDependency(FacesContext context, ResourceDependency annotation) {
    // If this annotation is not present on the class in question, no action must be taken.
    if (annotation != null) {
        Application application = context.getApplication();
        // Create a UIOutput instance by passing jakarta.faces.Output. to
        // Application.createComponent(java.lang.String).
        UIOutput output = (UIOutput) application.createComponent(UIOutput.COMPONENT_TYPE);
        // Get the annotation instance from the class and obtain the values of the name, library, and
        // target attributes.
        String name = annotation.name();
        if (name != null && name.length() > 0) {
            name = _ELText.parse(getExpressionFactory(), context.getELContext(), name).toString(context.getELContext());
        }
        // Obtain the renderer-type for the resource name by passing name to
        // ResourceHandler.getRendererTypeForResourceName(java.lang.String).
        String rendererType = application.getResourceHandler().getRendererTypeForResourceName(name);
        // Call setRendererType on the UIOutput instance, passing the renderer-type.
        output.setRendererType(rendererType);
        // Obtain the Map of attributes from the UIOutput component by calling UIComponent.getAttributes().
        Map<String, Object> attributes = output.getAttributes();
        // Store the name into the attributes Map under the key "name".
        attributes.put("name", name);
        // If library is the empty string, let library be null.
        String library = annotation.library();
        if (library != null && library.length() > 0) {
            library = _ELText.parse(getExpressionFactory(), context.getELContext(), library).toString(context.getELContext());
            // If library is non-null, store it under the key "library".
            attributes.put("library", library);
        }
        // If target is the empty string, let target be null.
        String target = annotation.target();
        if (target != null && target.length() > 0) {
            target = _ELText.parse(getExpressionFactory(), context.getELContext(), target).toString(context.getELContext());
            // If target is non-null, store it under the key "target".
            attributes.put("target", target);
            context.getViewRoot().addComponentResource(context, output, target);
        } else {
            // Otherwise, if target is null, call
            // UIViewRoot.addComponentResource(jakarta.faces.context.FacesContext,
            // jakarta.faces.component.UIComponent),
            // passing the UIOutput instance as the second argument.
            context.getViewRoot().addComponentResource(context, output);
        }
    }
}
Also used : UIOutput(jakarta.faces.component.UIOutput) Application(jakarta.faces.application.Application)

Aggregations

Application (jakarta.faces.application.Application)256 PrintWriter (java.io.PrintWriter)158 FacesContext (jakarta.faces.context.FacesContext)87 IOException (java.io.IOException)48 ServletException (jakarta.servlet.ServletException)35 ValueExpression (jakarta.el.ValueExpression)33 UIComponent (jakarta.faces.component.UIComponent)30 UIViewRoot (jakarta.faces.component.UIViewRoot)25 ViewHandler (jakarta.faces.application.ViewHandler)24 ELException (jakarta.el.ELException)23 FacesException (jakarta.faces.FacesException)18 ArrayList (java.util.ArrayList)18 NavigationCase (jakarta.faces.application.NavigationCase)15 ELContext (jakarta.el.ELContext)13 SystemEvent (jakarta.faces.event.SystemEvent)13 ResourceBundle (java.util.ResourceBundle)13 TCKNavigationCase (com.sun.ts.tests.jsf.common.navigation.TCKNavigationCase)12 HashSet (java.util.HashSet)12 TCKSystemEvent (com.sun.ts.tests.jsf.common.event.TCKSystemEvent)11 Map (java.util.Map)11