View Javadoc

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