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.PortletURL; 027 import javax.portlet.RenderResponse; 028 import java.io.UnsupportedEncodingException; 029 030 031 /** 032 * Static utility class for portlet-related operations. 033 */ 034 public final class PortletUtils { 035 036 private static final boolean PORTLET_API_AVAILABLE = portletApiAvailable(); 037 038 /** 039 * This flag is imbedded in the request. 040 * It signifies that the request is coming from a portlet. 041 */ 042 // public static final String PORTLET_REQUEST = PortletUtils.class.getName() + ".PORTLET_REQUEST"; 043 private static final String VIEW_ID = PortletUtils.class.getName() + ".VIEW_ID"; 044 045 private static boolean portletApiAvailable() { 046 try { 047 return PortletRequest.class != null; // never false 048 } catch (NoClassDefFoundError e) { 049 return false; 050 } 051 } 052 053 private PortletUtils() { 054 // avoid instantiation 055 } 056 057 /** 058 * Determine if we are processing a portlet RenderResponse. 059 * 060 * @param facesContext The current FacesContext. 061 * @return <code>true</code> if we are processing a RenderResponse, 062 * <code>false</code> otherwise. 063 */ 064 public static boolean isRenderResponse(FacesContext facesContext) { 065 return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getResponse() instanceof RenderResponse; 066 } 067 068 /** 069 * Determine if we are running as a portlet. 070 * 071 * @param facesContext The current FacesContext. 072 * @return <code>true</code> if we are running as a portlet, 073 * <code>false</code> otherwise. 074 */ 075 // public static boolean isPortletRequest(FacesContext facesContext) { 076 // return facesContext.getExternalContext().getSessionMap().get(PORTLET_REQUEST) != null; 077 // } 078 public static boolean isPortletRequest(FacesContext facesContext) { 079 return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getContext() instanceof PortletContext; 080 } 081 082 public static String getViewId(FacesContext facesContext) { 083 PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest(); 084 return request.getParameter(PortletUtils.VIEW_ID); 085 } 086 087 /** 088 * @return The action url. 089 */ 090 public static String setViewIdForUrl(FacesContext facesContext, String viewId) { 091 RenderResponse response = (RenderResponse) facesContext.getExternalContext().getResponse(); 092 PortletURL url = response.createActionURL(); 093 url.setParameter(VIEW_ID, viewId); 094 return url.toString(); 095 } 096 097 public static void ensureEncoding(FacesContext facesContext) throws UnsupportedEncodingException { 098 ActionRequest request = (ActionRequest) facesContext.getExternalContext().getRequest(); 099 if (request.getCharacterEncoding() == null) { 100 request.setCharacterEncoding("UTF-8"); 101 } 102 } 103 }