View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.bridges.portletfilter;
18  
19  import java.io.IOException;
20  import java.util.Enumeration;
21  import java.util.Locale;
22  import java.util.ResourceBundle;
23  
24  import javax.portlet.ActionRequest;
25  import javax.portlet.ActionResponse;
26  import javax.portlet.GenericPortlet;
27  import javax.portlet.Portlet;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletContext;
30  import javax.portlet.PortletException;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /***
38   * FilterPortlet provides a portlet implementation to filter the 
39   * specified portlet. The filtered portlet and filters are defined in 
40   * a portlet descriptor(portlet.xml).  The filetered portlet is specified
41   * by portlet-class, and the filters are specified by portlet-filters.
42   * 
43   * Example:
44   * <pre>
45   * &lt;portlet-app id="example-portlets" version="1.0"&gt;
46   *    &lt;portlet id="ExamplePortlet"&gt;
47   * ...
48   *        &lt;init-param&gt;
49   *            &lt;name&gt;portlet-class&lt;/name&gt;
50   *            &lt;value&gt;org.apache.myfaces.portlet.MyFacesGenericPortlet&lt;/value&gt;
51   *        &lt;/init-param&gt;
52   *        &lt;init-param&gt;
53   *            &lt;name&gt;portlet-filters&lt;/name&gt;
54   *            &lt;value&gt;org.apache.myfaces.portlet.TomahawkPortletFilter&lt;/value&gt;
55   *        &lt;/init-param&gt;
56   *        &lt;init-param&gt;
57   *            &lt;name&gt;org.apache.myfaces.portlet.TomahawkPortletFilter:upload-threshold-size&lt;/name&gt;
58   *            &lt;value&gt;1m&lt;/value&gt;
59   *        &lt;/init-param&gt;
60   *        &lt;init-param&gt;
61   *            &lt;name&gt;org.apache.myfaces.portlet.TomahawkPortletFilter:upload-max-file-size&lt;/name&gt;
62   *            &lt;value&gt;10m&lt;/value&gt;
63   *        &lt;/init-param&gt;
64   * ...
65   * </pre>
66   * 
67   * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
68   */
69  public class FilterPortlet extends GenericPortlet
70  {
71      private static final Log log = LogFactory.getLog(FilterPortlet.class);
72  
73      public static final String PORTLET_CLASS = "portlet-class";
74  
75      private PortletFilterChain portletFilterChain;
76  
77      private Portlet portlet;
78  
79      public void init(PortletConfig config) throws PortletException
80      {
81          super.init(config);
82          if (log.isTraceEnabled())
83          {
84              log.trace("Initializing FilterPortlet.");
85          }
86  
87          // create Portlet
88          String portletClassName = config.getInitParameter(PORTLET_CLASS);
89          if (portletClassName == null)
90          {
91              throw new PortletException("Portlet Class Name is null.");
92          }
93  
94          // create PortletFilterChain
95          portletFilterChain = new PortletFilterChain(config);
96  
97          portlet = null;
98          try
99          {
100             Class portletClass = Class.forName(portletClassName);
101             Object portletObj = portletClass.newInstance();
102             if (portletObj instanceof Portlet)
103             {
104                 portlet = (Portlet) portletObj;
105                 portlet.init(config);
106             }
107             else
108             {
109                 throw new PortletException(portletClassName + " is not Portlet instance.");
110             }
111         }
112         catch (ClassNotFoundException e)
113         {
114             throw new PortletException("Class " + portletClassName + " is not found.", e);
115         }
116         catch (InstantiationException e)
117         {
118             throw new PortletException("Could not instantiate " + portletClassName + ".", e);
119         }
120         catch (IllegalAccessException e)
121         {
122             throw new PortletException("Illegal Access: " + portletClassName, e);
123         }
124 
125         portletFilterChain.setPortlet(portlet);
126     }
127 
128     public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
129     {
130         if (log.isTraceEnabled())
131         {
132             log.trace("called processAction method.");
133         }
134         portletFilterChain.reset();
135         portletFilterChain.processActionFilter(request, response);
136     }
137 
138     public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException
139     {
140         if (log.isTraceEnabled())
141         {
142             log.trace("called render method.");
143         }
144         portletFilterChain.reset();
145         portletFilterChain.renderFilter(request, response);
146     }
147 
148     public void destroy()
149     {
150         if (log.isTraceEnabled())
151         {
152             log.trace("called destory method.");
153         }
154         portletFilterChain.release();
155         portlet.destroy();
156         portlet = null;
157         portletFilterChain = null;
158     }
159 
160     /* (non-Javadoc)
161      * @see javax.portlet.PortletConfig#getInitParameter(java.lang.String)
162      */
163     public String getInitParameter(String arg0)
164     {
165         if (portlet instanceof PortletConfig)
166         {
167             return ((PortletConfig) portlet).getInitParameter(arg0);
168         }
169         return super.getInitParameter(arg0);
170     }
171 
172     /* (non-Javadoc)
173      * @see javax.portlet.PortletConfig#getInitParameterNames()
174      */
175     public Enumeration getInitParameterNames()
176     {
177         if (portlet instanceof PortletConfig)
178         {
179             return ((PortletConfig) portlet).getInitParameterNames();
180         }
181         return super.getInitParameterNames();
182     }
183 
184     /* (non-Javadoc)
185      * @see javax.portlet.PortletConfig#getPortletContext()
186      */
187     public PortletContext getPortletContext()
188     {
189         if (portlet instanceof PortletConfig)
190         {
191             return ((PortletConfig) portlet).getPortletContext();
192         }
193         return super.getPortletContext();
194     }
195 
196     /* (non-Javadoc)
197      * @see javax.portlet.PortletConfig#getPortletName()
198      */
199     public String getPortletName()
200     {
201         if (portlet instanceof PortletConfig)
202         {
203             return ((PortletConfig) portlet).getPortletName();
204         }
205         return super.getPortletName();
206     }
207 
208     /* (non-Javadoc)
209      * @see javax.portlet.PortletConfig#getResourceBundle(java.util.Locale)
210      */
211     public ResourceBundle getResourceBundle(Locale arg0)
212     {
213         if (portlet instanceof PortletConfig)
214         {
215             return ((PortletConfig) portlet).getResourceBundle(arg0);
216         }
217         return super.getResourceBundle(arg0);
218     }
219 }