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.util.Iterator;
32  
33  /**
34   * {@link org.apache.myfaces.tobago.internal.taglib.component.PageTagDeclaration}
35   */
36  public abstract class AbstractUIPage extends AbstractUIFormBase implements ClientBehaviorHolder {
37  
38    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIPage.class);
39  
40    public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Page";
41  
42    public static final String FORM_ACCEPT_CHARSET = "utf-8";
43  
44    private String formId;
45  
46    @Override
47    public boolean getRendersChildren() {
48      return true;
49    }
50  
51    public String getFormId(final FacesContext facesContext) {
52      if (formId == null) {
53        formId = getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form";
54      }
55      return formId;
56    }
57  
58    @Override
59    public void processDecodes(final FacesContext context) {
60  
61      decode(context);
62  
63      markSubmittedForm(context);
64  
65      // invoke processDecodes() on children
66      for (final Iterator kids = getFacetsAndChildren(); kids.hasNext();) {
67        final UIComponent kid = (UIComponent) kids.next();
68        kid.processDecodes(context);
69      }
70    }
71  
72    public void markSubmittedForm(final FacesContext facesContext) {
73      // find the form of the action command and set submitted to it and all
74      // children
75  
76      final UIViewRoot viewRoot = facesContext.getViewRoot();
77  
78      // reset old submitted state
79      setSubmitted(false);
80  
81      String sourceId = facesContext.getExternalContext().getRequestParameterMap().get("javax.faces.source");
82      UIComponent command = null;
83      if (sourceId != null) {
84        if (LOG.isDebugEnabled()) {
85          LOG.debug("sourceId = '" + sourceId + "'");
86        }
87        command = viewRoot.findComponent(sourceId);
88      } else {
89        LOG.warn("No sourceId found!");
90      }
91  
92      // TODO: remove this if block if proven this never happens anymore
93      if (command == null
94          && sourceId != null && sourceId.matches(".*:\\d+:.*")) {
95        // If currentActionId component was inside a sheet the id contains the
96        // rowIndex and is therefore not found here.
97        // We do not need the row here because we want just to find the
98        // related form, so removing the rowIndex will help here.
99        sourceId = sourceId.replaceAll(":\\d+:", ":");
100       try {
101         command = viewRoot.findComponent(sourceId);
102         //LOG.info("command = \"" + command + "\"", new Exception());
103       } catch (final Exception e) {
104         // ignore
105       }
106     }
107 
108     if (LOG.isTraceEnabled()) {
109       LOG.trace(sourceId);
110       LOG.trace("command:{}", command);
111       LOG.trace(DebugUtils.toString(viewRoot, 0));
112     }
113 
114     if (command != null) {
115       final AbstractUIFormBase form = ComponentUtils.findForm(command);
116       form.setSubmitted(true);
117 
118       if (LOG.isTraceEnabled()) {
119         LOG.trace("form:{}", form);
120         LOG.trace(form.getClientId(facesContext));
121       }
122     } else {
123       if (LOG.isDebugEnabled()) {
124         LOG.debug("Illegal actionId! Render response...");
125       }
126       facesContext.renderResponse();
127     }
128   }
129 }