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.util;
21  
22  import javax.faces.application.Application;
23  import javax.faces.application.NavigationHandler;
24  import javax.faces.context.FacesContext;
25  import javax.servlet.ServletRequest;
26  import javax.servlet.http.HttpServletRequest;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashSet;
30  import java.util.Set;
31  
32  public class AjaxUtils {
33  
34    public static boolean isAjaxRequest(final FacesContext facesContext) {
35      return facesContext.getPartialViewContext().isAjaxRequest();
36    }
37  
38    public static boolean isAjaxRequest(final ServletRequest request) {
39      String requestType = null;
40      if (request instanceof HttpServletRequest) {
41        final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
42        requestType = httpServletRequest.getHeader("Faces-Request");
43      }
44      return "partial/ajax".equalsIgnoreCase(requestType)
45          || "true".equalsIgnoreCase(request.getParameter("javax.faces.partial.ajax"));
46    }
47  
48    public static void addRenderIds(final String... renderIds) {
49      addRenderIds(FacesContext.getCurrentInstance(), renderIds);
50    }
51  
52    public static void addRenderIds(final FacesContext facesContext, final String... renderIds) {
53      Collections.addAll(facesContext.getPartialViewContext().getRenderIds(), renderIds);
54    }
55  
56    public static void removeRenderIds(final String... renderIds) {
57      removeRenderIds(FacesContext.getCurrentInstance(), renderIds);
58    }
59  
60    public static void removeRenderIds(final FacesContext facesContext, final String... renderIds) {
61      final Collection<String> collection = facesContext.getPartialViewContext().getRenderIds();
62      for (final String renderId : renderIds) {
63        collection.remove(renderId);
64      }
65    }
66  
67    public static Set<String> getRenderIds(final FacesContext facesContext) {
68      return new HashSet<>(facesContext.getPartialViewContext().getRenderIds());
69    }
70  
71    public static void navigate(final FacesContext facesContext, final Object outcome) {
72      final Application application = facesContext.getApplication();
73      final NavigationHandler navigationHandler = application.getNavigationHandler();
74      navigationHandler.handleNavigation(facesContext, null, outcome.toString());
75      facesContext.renderResponse();
76    }
77  }