View Javadoc

1   package org.apache.myfaces.tobago.ajax.api;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.myfaces.tobago.util.ComponentUtil;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import java.io.IOException;
29  import java.util.Map;
30  import java.util.StringTokenizer;
31  import java.util.HashMap;
32  
33  public class AjaxUtils {
34  
35    private static final Log LOG = LogFactory.getLog(AjaxUtils.class);
36  
37    public static final String AJAX_COMPONENTS = AjaxUtils.class.getName() + ".AJAX_COMPONENTS";
38  
39    public static void checkParamValidity(FacesContext facesContext, UIComponent uiComponent, Class compClass) {
40      if (facesContext == null) {
41        throw new NullPointerException("facesContext may not be null");
42      }
43      if (uiComponent == null) {
44        throw new NullPointerException("uiComponent may not be null");
45      }
46      //if (compClass != null && !(compClass.isAssignableFrom(uiComponent.getClass())))
47      // why isAssignableFrom with additional getClass method call if isInstance does the same?
48      if (compClass != null && !(compClass.isInstance(uiComponent))) {
49        throw new IllegalArgumentException("uiComponent : "
50            + uiComponent.getClass().getName() + " is not instance of "
51            + compClass.getName() + " as it should be");
52      }
53    }
54  
55  
56  
57  
58    public static void encodeAjaxComponent(FacesContext facesContext, UIComponent component) throws IOException {
59      if (facesContext == null) {
60        throw new NullPointerException("facesContext");
61      }
62      if (!component.isRendered()) {
63        return;
64      }
65      Object renderer = ComponentUtil.getRenderer(facesContext, component);
66      if (renderer != null && renderer instanceof AjaxRenderer) {
67        ((AjaxRenderer) renderer).encodeAjax(facesContext, component);
68      }
69    }
70  
71    public static Map<String, UIComponent> parseAndStoreComponents(FacesContext facesContext) {
72      Map parameterMap = facesContext.getExternalContext().getRequestParameterMap();
73      String ajaxComponentIds = (String) parameterMap.get("tobago::partialIds");
74      if (ajaxComponentIds != null) {
75        if (LOG.isInfoEnabled()) {
76          LOG.info("ajaxComponentIds = \"" + ajaxComponentIds + "\"");
77        }
78        StringTokenizer tokenizer = new StringTokenizer(ajaxComponentIds, ",");
79        Map<String, UIComponent> ajaxComponents = new HashMap<String, UIComponent>(tokenizer.countTokens());
80        //noinspection unchecked
81        facesContext.getExternalContext().getRequestMap().put(AJAX_COMPONENTS, ajaxComponents);
82        javax.faces.component.UIViewRoot viewRoot = facesContext.getViewRoot();
83        while (tokenizer.hasMoreTokens()) {
84          String ajaxId = tokenizer.nextToken();
85          UIComponent ajaxComponent = viewRoot.findComponent(ajaxId);
86          if (ajaxComponent != null) {
87            if (LOG.isInfoEnabled()) {
88              LOG.info("ajaxComponent for \"" + ajaxId + "\" = \"" + ajaxComponent + "\"");
89            }
90            ajaxComponents.put(ajaxId, ajaxComponent);
91          }
92        }
93        return ajaxComponents;
94      }
95      return null;
96    }
97  
98    public static Map<String, UIComponent> getAjaxComponents(FacesContext facesContext) {
99      //noinspection unchecked
100     return (Map<String, UIComponent>)
101         facesContext.getExternalContext().getRequestMap().get(AJAX_COMPONENTS);
102   }
103 
104   public static boolean isAjaxRequest(FacesContext facesContext) {
105     return facesContext.getExternalContext().getRequestMap().containsKey(AJAX_COMPONENTS);
106   }
107 
108   public static void removeAjaxComponent(FacesContext facesContext, String clientId) {
109     Map<String, UIComponent> ajaxComponents = getAjaxComponents(facesContext);
110     if (ajaxComponents != null) {
111       ajaxComponents.remove(clientId);
112     }
113   }
114 
115   public static void addAjaxComponent(FacesContext facesContext, String clientId) {
116     addAjaxComponent(facesContext, facesContext.getViewRoot().findComponent(clientId));
117   }
118 
119   public static void addAjaxComponent(FacesContext facesContext, UIComponent component) {
120     if (component instanceof AjaxComponent) {
121       Map<String, UIComponent> ajaxComponents = getAjaxComponents(facesContext);
122       if (ajaxComponents != null) {
123         ajaxComponents.put(component.getClientId(facesContext), component);
124       }
125     } else {
126       LOG.warn("Ignore non AjaxComponent : \""
127           + (component != null ? component.getClientId(facesContext) : "null")
128           + "\" = " + (component != null ? component.getClass().getName() : "null"));
129     }
130   }
131 
132 
133   public static void ensureDecoded(FacesContext facesContext, String clientId) {
134     ensureDecoded(facesContext, facesContext.getViewRoot().findComponent(clientId));
135   }
136 
137   public static void ensureDecoded(FacesContext facesContext, UIComponent component) {
138     Map<String, UIComponent> ajaxComponents = getAjaxComponents(facesContext);
139     if (ajaxComponents != null) {
140       for (UIComponent uiComponent : ajaxComponents.values()) {
141         UIComponent currentComponent = component;
142         while (currentComponent != null) {
143           if (component == uiComponent) {
144             return;
145           }
146           currentComponent = currentComponent.getParent();
147         }
148       }
149       component.processDecodes(facesContext);
150     }
151   }
152 
153   public static String encodeJavascriptString(String value) {
154     String result = StringUtils.replace(value, "\\", "\\\\");
155     result = StringUtils.replace(result, "\n", "\\n");
156     result = StringUtils.replace(result, "\r", "\\r");
157     return StringUtils.replace(result, "\"", "\\\"");
158   }
159 }