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