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  package org.apache.myfaces.webapp.filter;
20  
21  import javax.portlet.ActionRequest;
22  import javax.portlet.PortletConfig;
23  import javax.portlet.PortletContext;
24  
25  import org.apache.commons.fileupload.portlet.PortletFileUpload;
26  
27  /**
28   * This class is used to hold some utilities used by tomahawk in portlet
29   * environments. The idea is that all calls to portlet api methods
30   * should be done here, to avoid ClassNotFoundException error in
31   * servlet environments.
32   * 
33   * The public methods should not use classes on portlet api, so the
34   * other classes calling this class does not have dependencies to this api.
35   * 
36   * @since 1.1.8
37   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
38   * @version $Revision: 954965 $ $Date: 2010-06-15 11:58:31 -0500 (Tue, 15 Jun 2010) $
39   */
40  public class PortletUtils
41  {
42  
43      public static boolean isDisabledTomahawkFacesContextWrapper(Object contextOrConfig)
44      {
45          PortletContext portletContext = null;
46          if (contextOrConfig instanceof PortletConfig)
47          {
48              portletContext = ((PortletConfig)contextOrConfig).getPortletContext();
49          }
50          else
51          {
52              portletContext = (PortletContext)contextOrConfig;
53          }
54          
55          return getBooleanValue(portletContext.getInitParameter(
56                  TomahawkFacesContextFactory.DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER),
57                  TomahawkFacesContextFactory.DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER_DEFAULT);
58      }
59      
60      private static boolean getBooleanValue(String initParameter, boolean defaultVal)
61      {
62          if(initParameter == null || initParameter.trim().length()==0)
63              return defaultVal;
64  
65          return (initParameter.equalsIgnoreCase("on") || initParameter.equals("1") || initParameter.equalsIgnoreCase("true"));
66      }
67      
68      public static boolean isMultipartContent(Object request)
69      {
70          if (request instanceof ActionRequest)
71          {
72              return PortletFileUpload.isMultipartContent((ActionRequest)request);
73          }
74          else
75          {
76              return false;
77          }
78      }
79      
80      public static String getContextInitParameter(Object context, String paramName)
81      {
82          PortletContext portletContext = (PortletContext) context;
83          return portletContext.getInitParameter(paramName);
84      }
85      
86      public static Object getAttribute(Object context, String key)
87      {
88          PortletContext portletContext = (PortletContext) context;
89          return portletContext.getAttribute(key);
90      }
91      
92      public static void setAttribute(Object context, String key, Object value)
93      {
94          PortletContext portletContext = (PortletContext) context;
95          portletContext.setAttribute(key, value);
96      }
97  }