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.portlet;
21  
22  import javax.faces.context.FacesContext;
23  import javax.portlet.ActionRequest;
24  import javax.portlet.PortletContext;
25  import javax.portlet.PortletRequest;
26  import javax.portlet.PortletURL;
27  import javax.portlet.RenderResponse;
28  import java.io.UnsupportedEncodingException;
29  
30  
31  /**
32   * Static utility class for portlet-related operations.
33   */
34  public final class PortletUtils {
35  
36    private static final boolean PORTLET_API_AVAILABLE = portletApiAvailable();
37  
38    /**
39     * This flag is imbedded in the request.
40     * It signifies that the request is coming from a portlet.
41     */
42  //  public static final String PORTLET_REQUEST = PortletUtils.class.getName() + ".PORTLET_REQUEST";
43    private static final String VIEW_ID = PortletUtils.class.getName() + ".VIEW_ID";
44  
45    private static boolean portletApiAvailable() {
46      try {
47        return PortletRequest.class != null; // never false
48      } catch (NoClassDefFoundError e) {
49        return false;
50      }
51    }
52  
53    private PortletUtils() {
54      // avoid instantiation
55    }
56  
57    /**
58     * Determine if we are processing a portlet RenderResponse.
59     *
60     * @param facesContext The current FacesContext.
61     * @return <code>true</code> if we are processing a RenderResponse,
62     *         <code>false</code> otherwise.
63     */
64    public static boolean isRenderResponse(FacesContext facesContext) {
65      return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getResponse() instanceof RenderResponse;
66    }
67  
68    /**
69     * Determine if we are running as a portlet.
70     *
71     * @param facesContext The current FacesContext.
72     * @return <code>true</code> if we are running as a portlet,
73     *         <code>false</code> otherwise.
74     */
75  //  public static boolean isPortletRequest(FacesContext facesContext) {
76  //    return facesContext.getExternalContext().getSessionMap().get(PORTLET_REQUEST) != null;
77  //  }
78    public static boolean isPortletRequest(FacesContext facesContext) {
79      return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getContext() instanceof PortletContext;
80    }
81  
82    public static String getViewId(FacesContext facesContext) {
83      PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest();
84      return request.getParameter(PortletUtils.VIEW_ID);
85    }
86  
87    /**
88     * @return The action url.
89     */
90    public static String setViewIdForUrl(FacesContext facesContext, String viewId) {
91      RenderResponse response = (RenderResponse) facesContext.getExternalContext().getResponse();
92      PortletURL url = response.createActionURL();
93      url.setParameter(VIEW_ID, viewId);
94      return url.toString();
95    }
96  
97    public static void ensureEncoding(FacesContext facesContext) throws UnsupportedEncodingException {
98      ActionRequest request = (ActionRequest) facesContext.getExternalContext().getRequest();
99      if (request.getCharacterEncoding() == null) {
100       request.setCharacterEncoding("UTF-8");
101     }
102   }
103 }