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.util;
21  
22  import org.apache.commons.collections.list.SetUniqueList;
23  import org.apache.commons.collections.set.ListOrderedSet;
24  import org.apache.myfaces.tobago.internal.component.AbstractUIPopup;
25  
26  import javax.faces.context.FacesContext;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.List;
30  import java.util.Set;
31  
32  @SuppressWarnings("unchecked")
33  public final class FacesContextUtils {
34    private static final String TOBAGO_AJAX = "org.apache.myfaces.tobago.ajax";
35    private static final String TOBAGO_AJAX_COMPONENT_ID = "org.apache.myfaces.tobago.ajaxComponentId";
36    private static final String TOBAGO_ENCTYPE = "org.apache.myfaces.tobago.enctype";
37    private static final String TOBAGO_SCRIPT_FILES = "org.apache.myfaces.tobago.scriptFiles";
38    /**
39     * @deprecated since Tobago 2.0, because of CSP
40     */
41    @Deprecated
42    private static final String TOBAGO_SCRIPT_BLOCKS = "org.apache.myfaces.tobago.scriptBlocks";
43    private static final String TOBAGO_STYLE_FILES = "org.apache.myfaces.tobago.styleFiles";
44    /**
45     * @deprecated since Tobago 2.0, because of CSP
46     */
47    @Deprecated
48    private static final String TOBAGO_STYLE_BLOCKS = "org.apache.myfaces.tobago.styleBlocks";
49    /**
50     * @deprecated since Tobago 2.0, because of CSP
51     */
52    @Deprecated
53    private static final String TOBAGO_ONLOAD_SCRIPTS = "org.apache.myfaces.tobago.onloadScripts";
54    /**
55     * @deprecated since Tobago 2.0, because of CSP
56     */
57    @Deprecated
58    private static final String TOBAGO_ONUNLOAD_SCRIPTS = "org.apache.myfaces.tobago.onunloadScripts";
59    /**
60     * @deprecated since Tobago 2.0, because of CSP
61     */
62    @Deprecated
63    private static final String TOBAGO_ONEXIT_SCRIPTS = "org.apache.myfaces.tobago.onexitScripts";
64    /**
65     * @deprecated since Tobago 2.0, because of CSP
66     */
67    @Deprecated
68    private static final String TOBAGO_ONSUBMIT_SCRIPTS = "org.apache.myfaces.tobago.onsubmitScripts";
69    private static final String TOBAGO_POPUPS = "org.apache.myfaces.tobago.popups";
70    /**
71     * @deprecated since Tobago 2.0, because of CSP
72     */
73    @Deprecated
74    private static final String TOBAGO_MENU_ACCELERATORS = "org.apache.myfaces.tobago.menuAccelerators";
75    private static final String TOBAGO_FOCUS_ID = "org.apache.myfaces.tobago.focusId";
76    private static final String TOBAGO_ACTION_ID = "org.apache.myfaces.tobago.actionId";
77  
78    private FacesContextUtils() {
79    }
80  
81    public static boolean isAjax(final FacesContext context) {
82      return context.getAttributes().containsKey(TOBAGO_AJAX);
83    }
84  
85    public static void setAjax(final FacesContext context, final boolean ajax) {
86      context.getAttributes().put(TOBAGO_AJAX, ajax);
87    }
88  
89    public static void setFocusId(final FacesContext context, final String focusId) {
90      context.getAttributes().put(TOBAGO_FOCUS_ID, focusId);
91    }
92  
93    public static String getFocusId(final FacesContext context) {
94      return (String) context.getAttributes().get(TOBAGO_FOCUS_ID);
95    }
96  
97    public static void setActionId(final FacesContext context, final String actionId) {
98      context.getAttributes().put(TOBAGO_ACTION_ID, actionId);
99    }
100 
101   public static String getActionId(final FacesContext context) {
102     return (String) context.getAttributes().get(TOBAGO_ACTION_ID);
103   }
104 
105 
106   public static String getAjaxComponentId(final FacesContext context) {
107     return (String) context.getAttributes().get(TOBAGO_AJAX_COMPONENT_ID);
108   }
109 
110   public static void setAjaxComponentId(final FacesContext context, final String ajaxComponentId) {
111     context.getAttributes().put(TOBAGO_AJAX_COMPONENT_ID, ajaxComponentId);
112   }
113 
114   public static String getEnctype(final FacesContext context) {
115     return (String) context.getAttributes().get(TOBAGO_ENCTYPE);
116   }
117 
118   public static void setEnctype(final FacesContext context, final String enctype) {
119     context.getAttributes().put(TOBAGO_ENCTYPE, enctype);
120   }
121 
122   public static List<String> getScriptFiles(final FacesContext context) {
123     final List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
124     if (list == null) {
125       return Collections.emptyList();
126     }
127     return list;
128   }
129   
130   public static void addScriptFile(final FacesContext context, final String file) {
131     List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
132     if (list == null) {
133       list = SetUniqueList.decorate(new ArrayList());
134       context.getAttributes().put(TOBAGO_SCRIPT_FILES, list);
135     }
136     list.add(file);
137 
138   }
139 
140   /**
141    * @deprecated since Tobago 2.0, because of CSP
142    */
143   @Deprecated
144   public static Set<String> getScriptBlocks(final FacesContext context) {
145     final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_SCRIPT_BLOCKS);
146      if (set == null) {
147       return Collections.emptySet();
148     }
149     return set;
150   }
151 
152   /**
153    * @deprecated since Tobago 2.0, because of CSP
154    */
155   @Deprecated
156   public static void addScriptBlock(final FacesContext context, final String script) {
157     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_SCRIPT_BLOCKS);
158     if (set == null) {
159       set = new ListOrderedSet();
160       context.getAttributes().put(TOBAGO_SCRIPT_BLOCKS, set);
161     }
162     set.add(script);
163   }
164 
165 
166   public static Set<String> getStyleFiles(final FacesContext context) {
167     final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
168     if (set == null) {
169       return Collections.emptySet();
170     }
171     return set;
172   }
173 
174   public static void addStyleFile(final FacesContext context, final String script) {
175     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
176     if (set == null) {
177       set = new ListOrderedSet();
178       context.getAttributes().put(TOBAGO_STYLE_FILES, set);
179     }
180     set.add(script);
181   }
182 
183   /**
184    * @deprecated since Tobago 2.0, because of CSP
185    */
186   @Deprecated
187   public static Set<String> getStyleBlocks(final FacesContext context) {
188     final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_BLOCKS);
189     if (set == null) {
190       return Collections.emptySet();
191     }
192     return set;
193   }
194 
195   /**
196    * @deprecated since Tobago 2.0, because of CSP
197    */
198   @Deprecated
199   public static void addStyleBlock(final FacesContext context, final String script) {
200     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_BLOCKS);
201     if (set == null) {
202       set = new ListOrderedSet();
203       context.getAttributes().put(TOBAGO_STYLE_BLOCKS, set);
204     }
205     set.add(script);
206   }
207 
208   /**
209    * @deprecated since Tobago 2.0, because of CSP
210    */
211   @Deprecated
212   public static List<String> getOnloadScripts(final FacesContext context) {
213     final List<String> list = (List<String>) context.getAttributes().get(TOBAGO_ONLOAD_SCRIPTS);
214     if (list == null) {
215       return Collections.emptyList();
216     }
217     return list;
218   }
219 
220   /**
221    * @deprecated since Tobago 2.0, because of CSP
222    */
223   @Deprecated
224   public static void addOnloadScript(final FacesContext context, final String file) {
225     List<String> list = (List<String>) context.getAttributes().get(TOBAGO_ONLOAD_SCRIPTS);
226     if (list == null) {
227       list = SetUniqueList.decorate(new ArrayList());
228       context.getAttributes().put(TOBAGO_ONLOAD_SCRIPTS, list);
229     }
230     list.add(file);
231   }
232 
233   /**
234    * @deprecated since Tobago 2.0, because of CSP
235    */
236   @Deprecated
237   public static void addOnloadScript(final FacesContext context, final int position, final String file) {
238     List<String> list = (List<String>) context.getAttributes().get(TOBAGO_ONLOAD_SCRIPTS);
239     if (list == null) {
240       list = SetUniqueList.decorate(new ArrayList());
241       context.getAttributes().put(TOBAGO_ONLOAD_SCRIPTS, list);
242     }
243     list.add(position, file);
244   }
245 
246   /**
247    * @deprecated since Tobago 2.0, because of CSP
248    */
249   @Deprecated
250   public static Set<String> getOnunloadScripts(final FacesContext context) {
251     final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONUNLOAD_SCRIPTS);
252     if (set == null) {
253       return Collections.emptySet();
254     }
255     return set;
256   }
257 
258   /**
259    * @deprecated since Tobago 2.0, because of CSP
260    */
261   @Deprecated
262   public static void addOnunloadScript(final FacesContext context, final String script) {
263     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONUNLOAD_SCRIPTS);
264     if (set == null) {
265       set = new ListOrderedSet();
266       context.getAttributes().put(TOBAGO_ONUNLOAD_SCRIPTS, set);
267     }
268     set.add(script);
269   }
270 
271   /**
272    * @deprecated since Tobago 2.0, because of CSP
273    */
274   @Deprecated
275   public static Set<String> getOnexitScripts(final FacesContext context) {
276     final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONEXIT_SCRIPTS);
277     if (set == null) {
278       return Collections.emptySet();
279     }
280     return set;
281   }
282 
283   /**
284    * @deprecated since Tobago 2.0, because of CSP
285    */
286   @Deprecated
287   public static void addOnexitScript(final FacesContext context, final String script) {
288     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONEXIT_SCRIPTS);
289     if (set == null) {
290       set = new ListOrderedSet();
291       context.getAttributes().put(TOBAGO_ONEXIT_SCRIPTS, set);
292     }
293     set.add(script);
294   }
295 
296   /**
297    * @deprecated since Tobago 2.0, because of CSP
298    */
299   @Deprecated
300   public static Set<String> getOnsubmitScripts(final FacesContext context) {
301     final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONSUBMIT_SCRIPTS);
302     if (set == null) {
303       return Collections.emptySet();
304     }
305     return set;
306   }
307 
308   /**
309    * @deprecated since Tobago 2.0, because of CSP
310    */
311   @Deprecated
312   public static void addOnsubmitScript(final FacesContext context, final String script) {
313     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONSUBMIT_SCRIPTS);
314     if (set == null) {
315       set = new ListOrderedSet();
316       context.getAttributes().put(TOBAGO_ONSUBMIT_SCRIPTS, set);
317     }
318     set.add(script);
319   }
320 
321   /**
322    * @deprecated since Tobago 2.0, because of CSP
323    */
324   @Deprecated
325   public static void clearMenuAcceleratorScripts(final FacesContext context) {
326     final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_MENU_ACCELERATORS);
327     set.clear();
328   }
329 
330   /**
331    * @deprecated since Tobago 2.0, because of CSP
332    */
333   @Deprecated
334   public static Set<String> getMenuAcceleratorScripts(final FacesContext context) {
335     final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_MENU_ACCELERATORS);
336     if (set == null) {
337       return Collections.emptySet();
338     }
339     return set;
340   }
341 
342   /**
343    * @deprecated since Tobago 2.0, because of CSP
344    */
345   @Deprecated
346   public static void addMenuAcceleratorScript(final FacesContext context, final String script) {
347     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_MENU_ACCELERATORS);
348     if (set == null) {
349       set = new ListOrderedSet();
350       context.getAttributes().put(TOBAGO_MENU_ACCELERATORS, set);
351     }
352     set.add(script);
353   }
354 
355   public static Set<AbstractUIPopup> getPopups(final FacesContext context) {
356     final Set<AbstractUIPopup> set = (Set<AbstractUIPopup>) context.getAttributes().get(TOBAGO_POPUPS);
357     if (set == null) {
358       return Collections.emptySet();
359     }
360     return set;
361   }
362 
363   public static void addPopup(final FacesContext context, final AbstractUIPopup popup) {
364     Set<AbstractUIPopup> set = (Set<AbstractUIPopup>) context.getAttributes().get(TOBAGO_POPUPS);
365     if (set == null) {
366       set = new ListOrderedSet();
367       context.getAttributes().put(TOBAGO_POPUPS, set);
368     }
369     set.add(popup);
370   }
371 }