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