001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.portlet;
021
022 import javax.faces.context.FacesContext;
023 import javax.portlet.ActionRequest;
024 import javax.portlet.PortletContext;
025 import javax.portlet.PortletRequest;
026 import javax.portlet.PortletSession;
027 import javax.portlet.PortletURL;
028 import javax.portlet.RenderResponse;
029 import java.io.UnsupportedEncodingException;
030
031
032 /**
033 * Static utility class for portlet-related operations.
034 */
035 public final class PortletUtils {
036
037 private static final boolean PORTLET_API_AVAILABLE = portletApiAvailable();
038
039 /**
040 * This flag is imbedded in the request.
041 * It signifies that the request is coming from a portlet.
042 */
043 // public static final String PORTLET_REQUEST = PortletUtils.class.getName() + ".PORTLET_REQUEST";
044 private static final String VIEW_ID = PortletUtils.class.getName() + ".VIEW_ID";
045
046 private static boolean portletApiAvailable() {
047 try {
048 return PortletRequest.class != null; // never false
049 } catch (NoClassDefFoundError e) {
050 return false;
051 }
052 }
053
054 private PortletUtils() {
055 // avoid instantiation
056 }
057
058 /**
059 * Determine if we are processing a portlet RenderResponse.
060 *
061 * @param facesContext The current FacesContext.
062 * @return <code>true</code> if we are processing a RenderResponse,
063 * <code>false</code> otherwise.
064 */
065 public static boolean isRenderResponse(FacesContext facesContext) {
066 return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getResponse() instanceof RenderResponse;
067 }
068
069 /**
070 * Determine if we are running as a portlet.
071 *
072 * @param facesContext The current FacesContext.
073 * @return <code>true</code> if we are running as a portlet,
074 * <code>false</code> otherwise.
075 */
076 // public static boolean isPortletRequest(FacesContext facesContext) {
077 // return facesContext.getExternalContext().getSessionMap().get(PORTLET_REQUEST) != null;
078 // }
079 public static boolean isPortletRequest(FacesContext facesContext) {
080 return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getContext() instanceof PortletContext;
081 }
082
083 public static String getViewId(FacesContext facesContext) {
084 PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest();
085 return request.getParameter(PortletUtils.VIEW_ID);
086 }
087
088 /**
089 * @return The action url.
090 */
091 public static String setViewIdForUrl(FacesContext facesContext, String viewId) {
092 RenderResponse response = (RenderResponse) facesContext.getExternalContext().getResponse();
093 PortletURL url = response.createActionURL();
094 url.setParameter(VIEW_ID, viewId);
095 return url.toString();
096 }
097
098 public static void ensureEncoding(FacesContext facesContext) throws UnsupportedEncodingException {
099 ActionRequest request = (ActionRequest) facesContext.getExternalContext().getRequest();
100 if (request.getCharacterEncoding() == null) {
101 request.setCharacterEncoding("UTF-8");
102 }
103 }
104
105 public static Object getAttributeFromSessionForApplication(Object session, String name) {
106
107 if (PORTLET_API_AVAILABLE && session instanceof PortletSession) {
108 return ((PortletSession) session).getAttribute(name, PortletSession.APPLICATION_SCOPE);
109 } else {
110 throw new IllegalArgumentException("Unknown session type: " + session.getClass().getName());
111 }
112 }
113 }