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.config.TobagoConfig;
23  import org.apache.myfaces.tobago.event.CollapsibleActionListener;
24  import org.apache.myfaces.tobago.internal.util.AuthorizationHelper;
25  import org.apache.myfaces.tobago.util.ComponentUtils;
26  
27  import javax.el.MethodExpression;
28  import javax.faces.component.UICommand;
29  import javax.faces.component.UIComponent;
30  import javax.faces.context.FacesContext;
31  import javax.faces.event.ComponentSystemEvent;
32  import javax.faces.event.ComponentSystemEventListener;
33  import javax.faces.event.FacesEvent;
34  import javax.faces.event.ListenerFor;
35  import javax.faces.event.PhaseId;
36  import javax.faces.event.PostAddToViewEvent;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  @ListenerFor(systemEventClass = PostAddToViewEvent.class)
41  public abstract class AbstractUICommandBase extends UICommand
42      implements ComponentSystemEventListener {
43  
44    @Override
45    public void processEvent(ComponentSystemEvent event) {
46      super.processEvent(event);
47  
48      if (event instanceof PostAddToViewEvent) {
49        final List<AbstractUIOperation> list = ComponentUtils.findDescendantList(this, AbstractUIOperation.class);
50        for (AbstractUIOperation operation : list) {
51          addActionListener(new CollapsibleActionListener(operation.getFor()));
52        }
53      }
54    }
55  
56    @Override
57    public void processDecodes(final FacesContext context) {
58      // Skip processing if our rendered flag is false
59      if (!isRendered()) {
60        return;
61      }
62  
63      // Process this component itself
64      try {
65        decode(context);
66      } catch (final RuntimeException e) {
67        context.renderResponse();
68        throw e;
69      }
70  
71      final Iterator kids = getFacetsAndChildren();
72      while (kids.hasNext()) {
73        final UIComponent kid = (UIComponent) kids.next();
74        kid.processDecodes(context);
75      }
76    }
77  
78    @Override
79    public void queueEvent(final FacesEvent facesEvent) {
80      // fix for TOBAGO-262
81      super.queueEvent(facesEvent);
82      if (this == facesEvent.getSource()) {
83        if (isImmediate()) {
84          facesEvent.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
85        } else {
86          facesEvent.setPhaseId(PhaseId.INVOKE_APPLICATION);
87        }
88      }
89    }
90  
91    @Override
92    public boolean isRendered() {
93      return super.isRendered() && isAllowed();
94    }
95  
96    /**
97     Flag indicating that this element is disabled.
98     <br>Default: <code>false</code>
99     */
100   public boolean isDisabled() {
101 
102     if (!isAllowed()) {
103       return true;
104     }
105 
106     Boolean bool = (Boolean) getStateHelper().eval(AbstractUICommand.PropertyKeys.disabled);
107     if (bool != null) {
108       return bool;
109     }
110     return false;
111   }
112 
113   private boolean isAllowed() {
114     final FacesContext facesContext = getFacesContext();
115     final TobagoConfig tobagoConfig = TobagoConfig.getInstance(facesContext);
116     if (tobagoConfig.isCheckSecurityAnnotations()) {
117       final AuthorizationHelper authorizationHelper = AuthorizationHelper.getInstance(facesContext);
118       final MethodExpression actionExpression = getActionExpression();
119       if (actionExpression != null) {
120         final boolean authorized =
121             authorizationHelper.isAuthorized(facesContext, actionExpression.getExpressionString());
122         if (!authorized) {
123           return false;
124         }
125       }
126     }
127     return true;
128   }
129 
130   public void setDisabled(boolean disabled) {
131     getStateHelper().put(AbstractUICommand.PropertyKeys.disabled, disabled);
132   }
133 
134   public abstract String getTarget();
135 
136   public abstract boolean isTransition();
137 
138   public abstract boolean isOmit();
139 
140   public abstract void setOmit(boolean omit);
141 
142   public abstract String getConfirmation();
143 
144   public abstract String getLink();
145 }