001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
003     * agreements. See the NOTICE file distributed with this work for additional information regarding
004     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
005     * "License"); you may not use this file except in compliance with the License. You may obtain a
006     * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
007     * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
008     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
009     * for the specific language governing permissions and limitations under the License.
010     */
011    
012    package javax.portlet.faces;
013    
014    import java.util.Map;
015    
016    import javax.faces.context.FacesContext;
017    
018    import javax.portlet.faces.Bridge;
019    
020      /** Utility class designed to make it easy for Faces subsystems including the
021       * bridge itself to determine whether this request is running in a portlet
022       * container and/or which portlet request phase it is executing in.
023       */
024    public class BridgeUtil
025    {
026      /** Indicates whether the current request is executing in the portlet container.  
027       * If it returns <code>true</code> the request is a portlet request, otherwise it
028       * is not.
029       */
030      public static boolean isPortletRequest() 
031      {
032        FacesContext ctx = FacesContext.getCurrentInstance();
033        
034        // This method might be called during App startup (via a context listener) and hence no FacesContext
035        // For example a renderkit might createComponents during such time -- as the bridge overrides faces Application
036        // which implements createComponent and calls this method (to see if we need to wrap/replace with the NamingContainer
037        if (ctx == null)  return false;
038        
039        Map<String, Object> m = ctx.getExternalContext().getRequestMap();
040        Bridge.PortletPhase phase = (Bridge.PortletPhase) m.get(Bridge.PORTLET_LIFECYCLE_PHASE);
041        return phase != null;
042      }
043      
044      /** Return describes the portlet request phase currently being executed.  If 
045       * <code>null</code> then this request is not being executed in a portlet 
046       * container -- note this should never be called unless running within a Faces lifecyle in a portlet request.
047       */
048      public static Bridge.PortletPhase getPortletRequestPhase() 
049      {
050        Map<String, Object> m = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
051        return (Bridge.PortletPhase) m.get(Bridge.PORTLET_LIFECYCLE_PHASE);
052      }
053      
054    }