View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.component;
21  
22  import org.apache.myfaces.tobago.util.ComponentUtils;
23  import org.apache.myfaces.tobago.util.DebugUtils;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.UIViewRoot;
29  import javax.faces.component.behavior.ClientBehaviorHolder;
30  import javax.faces.context.FacesContext;
31  import java.lang.invoke.MethodHandles;
32  import java.nio.charset.Charset;
33  import java.nio.charset.StandardCharsets;
34  import java.util.Iterator;
35  
36  /**
37   * {@link org.apache.myfaces.tobago.internal.taglib.component.PageTagDeclaration}
38   */
39  public abstract class AbstractUIPage extends AbstractUIFormBase implements ClientBehaviorHolder {
40  
41    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
42  
43    /**
44     * @deprecated since 4.4.0.
45     */
46    @Deprecated
47    public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Page";
48  
49    public static final Charset FORM_ACCEPT_CHARSET = StandardCharsets.UTF_8;
50  
51    private String formId;
52  
53    @Override
54    public boolean getRendersChildren() {
55      return true;
56    }
57  
58    public String getFormId(final FacesContext facesContext) {
59      if (formId == null) {
60        formId = getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form";
61      }
62      return formId;
63    }
64  
65    @Override
66    public void processDecodes(final FacesContext context) {
67  
68      decode(context);
69  
70      markSubmittedForm(context);
71  
72      // invoke processDecodes() on children
73      for (final Iterator kids = getFacetsAndChildren(); kids.hasNext();) {
74        final UIComponent kid = (UIComponent) kids.next();
75        kid.processDecodes(context);
76      }
77    }
78  
79    public void markSubmittedForm(final FacesContext facesContext) {
80      // find the form of the action command and set submitted to it and all
81      // children
82  
83      final UIViewRoot viewRoot = facesContext.getViewRoot();
84  
85      // reset old submitted state
86      setSubmitted(false);
87  
88      String sourceId = facesContext.getExternalContext().getRequestParameterMap().get("javax.faces.source");
89      UIComponent command = null;
90      if (sourceId != null) {
91        if (LOG.isDebugEnabled()) {
92          LOG.debug("sourceId = '" + sourceId + "'");
93        }
94        command = viewRoot.findComponent(sourceId);
95      } else {
96        LOG.warn("No sourceId found!");
97      }
98  
99      // TODO: remove this if block if proven this never happens anymore
100     if (command == null
101         && sourceId != null && sourceId.matches(".*:\\d+:.*")) {
102       // If currentActionId component was inside a sheet the id contains the
103       // rowIndex and is therefore not found here.
104       // We do not need the row here because we want just to find the
105       // related form, so removing the rowIndex will help here.
106       sourceId = sourceId.replaceAll(":\\d+:", ":");
107       try {
108         command = viewRoot.findComponent(sourceId);
109         //LOG.info("command = \"" + command + "\"", new Exception());
110       } catch (final Exception e) {
111         // ignore
112       }
113     }
114 
115     if (LOG.isTraceEnabled()) {
116       LOG.trace(sourceId);
117       LOG.trace("command:{}", command);
118       LOG.trace(DebugUtils.toString(viewRoot, 0));
119     }
120 
121     if (command != null) {
122       final AbstractUIFormBase form = ComponentUtils.findForm(command);
123       form.setSubmitted(true);
124 
125       if (LOG.isTraceEnabled()) {
126         LOG.trace("form:{}", form);
127         LOG.trace(form.getClientId(facesContext));
128       }
129     } else {
130       if (LOG.isDebugEnabled()) {
131         LOG.debug("Illegal actionId! Render response...");
132       }
133       facesContext.renderResponse();
134     }
135   }
136 
137   public abstract String getLabel();
138 
139   public abstract String getFocusId();
140 }