Search in sources :

Example 1 with UIViewParameter

use of jakarta.faces.component.UIViewParameter in project myfaces by apache.

the class PartialViewContextImpl method processPartialRendering.

private void processPartialRendering(UIViewRoot viewRoot, PhaseId phaseId) {
    // note that we cannot use this.getPartialResponseWriter(), because
    // this could cause problems if PartialResponseWriter is wrapped
    PartialResponseWriter writer = context.getPartialViewContext().getPartialResponseWriter();
    PartialViewContext pvc = context.getPartialViewContext();
    ResponseWriter oldWriter = context.getResponseWriter();
    boolean inDocument = false;
    // response type = text/xml
    // no caching and no timeout if possible!
    ExternalContext externalContext = context.getExternalContext();
    externalContext.setResponseContentType("text/xml");
    externalContext.addResponseHeader("Pragma", "no-cache");
    externalContext.addResponseHeader("Cache-control", "no-cache");
    // under normal circumstances pragma should be enough, IE needs
    // a special treatment!
    // http://support.microsoft.com/kb/234067
    externalContext.addResponseHeader("Expires", "-1");
    try {
        String currentEncoding = writer.getCharacterEncoding();
        writer.writePreamble("<?xml version=\"1.0\" encoding=\"" + (currentEncoding == null ? "UTF-8" : currentEncoding) + "\"?>");
        writer.startDocument();
        writer.writeAttribute("id", viewRoot.getContainerClientId(context), "id");
        inDocument = true;
        context.setResponseWriter(writer);
        if (isResetValues()) {
            viewRoot.resetValues(context, getRenderIds());
        }
        if (pvc.isRenderAll()) {
            processRenderAll(viewRoot, writer);
        } else {
            Collection<String> renderIds = pvc.getRenderIds();
            // Only apply partial visit if we have ids to traverse
            if (renderIds != null && !renderIds.isEmpty()) {
                // render=@all, so output the body.
                if (renderIds.contains(PartialResponseWriter.RENDER_ALL_MARKER)) {
                    processRenderAll(viewRoot, writer);
                } else {
                    // In JSF 2.3 it was added jakarta.faces.Resource as an update target to add scripts or
                    // stylesheets inside <head> tag
                    List<UIComponent> updatedComponents = new ArrayList<>();
                    RequestViewContext rvc = RequestViewContext.getCurrentInstance(context);
                    processRenderResource(context, writer, rvc, updatedComponents, "head");
                    processRenderResource(context, writer, rvc, updatedComponents, "body");
                    processRenderResource(context, writer, rvc, updatedComponents, "form");
                    VisitContext visitCtx = getVisitContextFactory().getVisitContext(context, renderIds, MyFacesVisitHints.SET_SKIP_UNRENDERED);
                    viewRoot.visitTree(visitCtx, new PhaseAwareVisitCallback(context, phaseId, updatedComponents));
                }
            } else {
                List<UIComponent> updatedComponents = new ArrayList<>();
                RequestViewContext rvc = RequestViewContext.getCurrentInstance(context);
                processRenderResource(context, writer, rvc, updatedComponents, "head");
                processRenderResource(context, writer, rvc, updatedComponents, "body");
                processRenderResource(context, writer, rvc, updatedComponents, "form");
            }
            List<String> evalScripts = pvc.getEvalScripts();
            if (evalScripts != null && evalScripts.size() > 0) {
                for (String script : evalScripts) {
                    writer.startEval();
                    writer.write(script);
                    writer.endEval();
                }
            }
        }
        // invoke encodeAll() on every UIViewParameter in the view to
        // enable every UIViewParameter to save its value in the state
        // just like UIViewRoot.encodeEnd() does on a normal request
        // (see MYFACES-2645 for details)
        Collection<UIViewParameter> viewParams = ViewMetadata.getViewParameters(viewRoot);
        if (!viewParams.isEmpty()) {
            for (UIViewParameter param : viewParams) {
                param.encodeAll(context);
            }
        }
        // Retrieve the state and apply it if it is not null.
        String viewState = context.getApplication().getStateManager().getViewState(context);
        if (viewState != null) {
            writer.startUpdate(HtmlResponseStateManager.generateUpdateViewStateId(context));
            writer.write(viewState);
            writer.endUpdate();
        } else if (viewRoot.isTransient()) {
            writer.startUpdate(HtmlResponseStateManager.generateUpdateViewStateId(context));
            writer.write(StateTokenProcessor.STATELESS_TOKEN);
            writer.endUpdate();
        }
        ClientWindow cw = context.getExternalContext().getClientWindow();
        if (cw != null) {
            writer.startUpdate(HtmlResponseStateManager.generateUpdateClientWindowId(context));
            writer.writeText(cw.getId(), null);
            writer.endUpdate();
        }
    } catch (IOException ex) {
        if (log.isLoggable(Level.SEVERE)) {
            log.log(Level.SEVERE, "", ex);
        }
    } finally {
        try {
            if (inDocument) {
                writer.endDocument();
            }
            writer.flush();
        } catch (IOException ex) {
            if (log.isLoggable(Level.SEVERE)) {
                log.log(Level.SEVERE, "", ex);
            }
        }
        context.setResponseWriter(oldWriter);
    }
}
Also used : VisitContext(jakarta.faces.component.visit.VisitContext) ClientWindow(jakarta.faces.lifecycle.ClientWindow) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList) RequestViewContext(org.apache.myfaces.context.RequestViewContext) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) IOException(java.io.IOException) ResponseWriter(jakarta.faces.context.ResponseWriter) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) ExternalContext(jakarta.faces.context.ExternalContext) UIViewParameter(jakarta.faces.component.UIViewParameter) PartialViewContext(jakarta.faces.context.PartialViewContext)

Example 2 with UIViewParameter

use of jakarta.faces.component.UIViewParameter in project myfaces by apache.

the class ViewHandlerImpl method getViewParameterList.

private Map<String, List<String>> getViewParameterList(FacesContext context, String viewId, Map<String, List<String>> parametersFromArg) {
    UIViewRoot viewRoot = context.getViewRoot();
    String currentViewId = viewRoot.getViewId();
    Collection<UIViewParameter> toViewParams = null;
    Collection<UIViewParameter> currentViewParams = ViewMetadata.getViewParameters(viewRoot);
    if (currentViewId.equals(viewId)) {
        toViewParams = currentViewParams;
    } else {
        String calculatedViewId = getViewIdSupport(context).deriveLogicalViewId(context, viewId);
        // we cannot use this.getVDL() directly (see getViewHandler())
        // ViewDeclarationLanguage vdl = getViewHandler(context).
        // getViewDeclarationLanguage(context, calculatedViewId);
        // -= Leonardo Uribe =- Temporally reverted by TCK issues.
        ViewDeclarationLanguage vdl = getViewDeclarationLanguage(context, calculatedViewId);
        ViewMetadata viewMetadata = vdl.getViewMetadata(context, viewId);
        if (viewMetadata != null) {
            UIViewRoot viewFromMetaData = viewMetadata.createMetadataView(context);
            toViewParams = ViewMetadata.getViewParameters(viewFromMetaData);
        }
    }
    if (toViewParams == null || toViewParams.isEmpty()) {
        return parametersFromArg;
    }
    // we need to use a custom Map to add the view parameters,
    // otherwise the current value of the view parameter will be added to
    // the navigation case as a static (!!!) parameter, thus the value
    // won't be updated on any following request
    // (Note that parametersFromArg is the Map from the NavigationCase)
    // Also note that we don't have to copy the Lists, because they won't be changed
    Map<String, List<String>> parameters = new HashMap<>(parametersFromArg);
    for (UIViewParameter viewParameter : toViewParams) {
        if (!parameters.containsKey(viewParameter.getName())) {
            String parameterValue = viewParameter.getStringValueFromModel(context);
            if (parameterValue == null) {
                if (currentViewId.equals(viewId)) {
                    parameterValue = viewParameter.getStringValue(context);
                } else {
                    if (viewParameter.getName() != null) {
                        for (UIViewParameter curParam : currentViewParams) {
                            if (viewParameter.getName().equals(curParam.getName())) {
                                parameterValue = curParam.getStringValue(context);
                                break;
                            }
                        }
                    }
                }
            }
            if (parameterValue != null) {
                // since we have checked !parameters.containsKey(viewParameter.getName())
                // here already, the parameters Map will never contain a List under the
                // key viewParameter.getName(), thus we do not have to check it here (again).
                List<String> parameterValueList = new ArrayList<>(1);
                parameterValueList.add(parameterValue);
                parameters.put(viewParameter.getName(), parameterValueList);
            }
        }
    }
    return parameters;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UIViewParameter(jakarta.faces.component.UIViewParameter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ViewDeclarationLanguage(jakarta.faces.view.ViewDeclarationLanguage) UIViewRoot(jakarta.faces.component.UIViewRoot) ViewMetadata(jakarta.faces.view.ViewMetadata)

Example 3 with UIViewParameter

use of jakarta.faces.component.UIViewParameter in project myfaces by apache.

the class ViewMetadata method getViewParameters.

public static Collection<UIViewParameter> getViewParameters(UIViewRoot root) {
    LinkedList<UIViewParameter> result = null;
    UIComponent metadataFacet = root.getFacet(UIViewRoot.METADATA_FACET_NAME);
    if (metadataFacet == null) {
        // No metadata, so return an empty collection.
        return Collections.emptyList();
    }
    // Iterate over all the children, keep only the view parameters.
    if (metadataFacet.getChildCount() > 0) {
        Iterator<UIComponent> children = metadataFacet.getChildren().iterator();
        while (children.hasNext()) {
            UIComponent component = children.next();
            if (result == null) {
                result = new LinkedList<>();
            }
            if (component instanceof UIViewParameter) {
                result.add((UIViewParameter) component);
            }
        }
    }
    // TODO: does this need to be immutable?  Spec does not indicate either way.
    if (result == null) {
        return Collections.emptyList();
    } else {
        return Collections.unmodifiableCollection(result);
    }
}
Also used : UIViewParameter(jakarta.faces.component.UIViewParameter) UIComponent(jakarta.faces.component.UIComponent)

Example 4 with UIViewParameter

use of jakarta.faces.component.UIViewParameter in project myfaces by apache.

the class ViewMetadataTestCase method testSimpleViewMetadata.

@Test
public void testSimpleViewMetadata() throws Exception {
    HelloWorld helloWorld = new HelloWorld();
    facesContext.getExternalContext().getRequestMap().put("helloWorldBean", helloWorld);
    Set<NavigationCase> cases = new HashSet<NavigationCase>();
    NavigationCase navCase = new NavigationCase("viewMetadata.xhtml", null, "somePage.xhtml", null, "somePage.xhtml", null, false, false);
    cases.add(navCase);
    navigationHandler.getNavigationCases().put("somePage.xhtml", cases);
    navigationHandler.getNavigationCase(facesContext, null, "somePage.xhtml");
    ViewMetadata metadata = vdl.getViewMetadata(facesContext, "viewMetadata.xhtml");
    UIViewRoot root = metadata.createMetadataView(facesContext);
    Collection<UIViewParameter> viewParameters = metadata.getViewParameters(root);
    Assert.assertEquals(1, viewParameters.size());
    // root.setViewId("viewMetadata.xhtml");
    vdl.buildView(facesContext, root, "viewMetadata.xhtml");
    facesContext.setViewRoot(root);
    StringWriter sw = new StringWriter();
    ResponseWriter mrw = new HtmlResponseWriterImpl(sw, "text/html", "UTF-8");
    facesContext.setResponseWriter(mrw);
    root.encodeAll(facesContext);
    sw.flush();
    System.out.print(sw.toString());
}
Also used : StringWriter(java.io.StringWriter) NavigationCase(jakarta.faces.application.NavigationCase) ResponseWriter(jakarta.faces.context.ResponseWriter) HtmlResponseWriterImpl(org.apache.myfaces.renderkit.html.HtmlResponseWriterImpl) UIViewParameter(jakarta.faces.component.UIViewParameter) UIViewRoot(jakarta.faces.component.UIViewRoot) HelloWorld(org.apache.myfaces.view.facelets.bean.HelloWorld) HashSet(java.util.HashSet) ViewMetadata(jakarta.faces.view.ViewMetadata) Test(org.junit.Test)

Example 5 with UIViewParameter

use of jakarta.faces.component.UIViewParameter in project mojarra by eclipse-ee4j.

the class MultiViewHandler method addViewParameters.

protected void addViewParameters(FacesContext ctx, String viewId, Map<String, List<String>> existingParameters) {
    UIViewRoot currentRoot = ctx.getViewRoot();
    String currentViewId = currentRoot.getViewId();
    Collection<UIViewParameter> toViewParams = Collections.emptyList();
    Collection<UIViewParameter> currentViewParams;
    boolean currentIsSameAsNew = false;
    currentViewParams = ViewMetadata.getViewParameters(currentRoot);
    if (currentViewId.equals(viewId)) {
        currentIsSameAsNew = true;
        toViewParams = currentViewParams;
    } else {
        ViewMetadata viewMetadata = getViewDeclarationLanguage(ctx, viewId).getViewMetadata(ctx, viewId);
        if (viewMetadata != null) {
            UIViewRoot root = viewMetadata.createMetadataView(ctx);
            toViewParams = ViewMetadata.getViewParameters(root);
        }
    }
    if (toViewParams.isEmpty()) {
        return;
    }
    for (UIViewParameter viewParam : toViewParams) {
        String value = null;
        // don't bother looking at view parameter if it's been overridden
        if (existingParameters.containsKey(viewParam.getName())) {
            continue;
        }
        if (paramHasValueExpression(viewParam)) {
            value = viewParam.getStringValueFromModel(ctx);
        }
        if (value == null) {
            if (currentIsSameAsNew) {
                /*
                     * Anonymous view parameter: get string value from UIViewParameter instance stored in current view.
                     */
                value = viewParam.getStringValue(ctx);
            } else {
                /*
                     * Or transfer string value from matching UIViewParameter instance stored in current view.
                     */
                value = getStringValueToTransfer(ctx, viewParam, currentViewParams);
            }
        }
        if (value != null) {
            List<String> existing = existingParameters.get(viewParam.getName());
            if (existing == null) {
                existing = new ArrayList<>(4);
                existingParameters.put(viewParam.getName(), existing);
            }
            existing.add(value);
        }
    }
}
Also used : UIViewParameter(jakarta.faces.component.UIViewParameter) MessageUtils.getExceptionMessageString(com.sun.faces.util.MessageUtils.getExceptionMessageString) UIViewRoot(jakarta.faces.component.UIViewRoot) ViewMetadata(jakarta.faces.view.ViewMetadata)

Aggregations

UIViewParameter (jakarta.faces.component.UIViewParameter)14 UIComponent (jakarta.faces.component.UIComponent)4 PrintWriter (java.io.PrintWriter)4 UIViewRoot (jakarta.faces.component.UIViewRoot)3 ViewMetadata (jakarta.faces.view.ViewMetadata)3 Test (org.junit.Test)3 UICommand (jakarta.faces.component.UICommand)2 UIPanel (jakarta.faces.component.UIPanel)2 ResponseWriter (jakarta.faces.context.ResponseWriter)2 ArrayList (java.util.ArrayList)2 MessageUtils.getExceptionMessageString (com.sun.faces.util.MessageUtils.getExceptionMessageString)1 ELContext (jakarta.el.ELContext)1 ExpressionFactory (jakarta.el.ExpressionFactory)1 NavigationCase (jakarta.faces.application.NavigationCase)1 VisitContext (jakarta.faces.component.visit.VisitContext)1 ExternalContext (jakarta.faces.context.ExternalContext)1 FacesContext (jakarta.faces.context.FacesContext)1 PartialResponseWriter (jakarta.faces.context.PartialResponseWriter)1 PartialViewContext (jakarta.faces.context.PartialViewContext)1 ClientWindow (jakarta.faces.lifecycle.ClientWindow)1