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