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.Portlet;
26  import javax.portlet.PortletConfig;
27  import javax.portlet.PortletContext;
28  import javax.portlet.PortletException;
29  import javax.portlet.RenderRequest;
30  import javax.portlet.RenderResponse;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /***
36   * FilterPortlet provides a portlet implementation to filter the 
37   * specified portlet. The filtered portlet and filters are defined in 
38   * a portlet descriptor(portlet.xml).  The filetered portlet is specified
39   * by portlet-class, and the filters are specified by portlet-filters.
40   * 
41   * Example:
42   * <pre>
43   * &lt;portlet-app id="example-portlets" version="1.0"&gt;
44   *    &lt;portlet id="ExamplePortlet"&gt;
45   * ...
46   *        &lt;init-param&gt;
47   *            &lt;name&gt;portlet-class&lt;/name&gt;
48   *            &lt;value&gt;org.apache.myfaces.portlet.MyFacesGenericPortlet&lt;/value&gt;
49   *        &lt;/init-param&gt;
50   *        &lt;init-param&gt;
51   *            &lt;name&gt;portlet-filters&lt;/name&gt;
52   *            &lt;value&gt;org.apache.myfaces.portlet.TomahawkPortletFilter&lt;/value&gt;
53   *        &lt;/init-param&gt;
54   *        &lt;init-param&gt;
55   *            &lt;name&gt;org.apache.myfaces.portlet.TomahawkPortletFilter:upload-threshold-size&lt;/name&gt;
56   *            &lt;value&gt;1m&lt;/value&gt;
57   *        &lt;/init-param&gt;
58   *        &lt;init-param&gt;
59   *            &lt;name&gt;org.apache.myfaces.portlet.TomahawkPortletFilter:upload-max-file-size&lt;/name&gt;
60   *            &lt;value&gt;10m&lt;/value&gt;
61   *        &lt;/init-param&gt;
62   * ...
63   * </pre>
64   * 
65   * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
66   */
67  public class FilterPortlet implements Portlet, PortletConfig
68  {
69      private static final Log log = LogFactory.getLog(FilterPortlet.class);
70  
71      public static final String PORTLET_CLASS = "portlet-class";
72  
73      private PortletFilterChain portletFilterChain;
74  
75      private PortletConfig portletConfig;
76  
77      private Portlet portlet;
78  
79      public void init(PortletConfig config) throws PortletException
80      {
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          portlet = null;
93  
94          try
95          {
96              Class portletClass = Class.forName(portletClassName);
97              Object portletObj = portletClass.newInstance();
98              if (portletObj instanceof Portlet)
99              {
100                 portlet = (Portlet) portletObj;
101                 portlet.init(config);
102             }
103             else
104             {
105                 throw new PortletException(portletClassName + " is not Portlet instance.");
106             }
107         }
108         catch (ClassNotFoundException e)
109         {
110             throw new PortletException("Class " + portletClassName + " is not found.", e);
111         }
112         catch (InstantiationException e)
113         {
114             throw new PortletException("Could not instantiate " + portletClassName + ".", e);
115         }
116         catch (IllegalAccessException e)
117         {
118             throw new PortletException("Illegal Access: " + portletClassName, e);
119         }
120 
121         // create PortletFilterChain
122         portletFilterChain = new PortletFilterChain(config);
123         portletFilterChain.setPortlet(portlet);
124     }
125 
126     public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
127     {
128         if (log.isTraceEnabled())
129         {
130             log.trace("called processAction method.");
131         }
132         portletFilterChain.processActionFilter(request, response);
133     }
134 
135     public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException
136     {
137         if (log.isTraceEnabled())
138         {
139             log.trace("called render method.");
140         }
141         portletFilterChain.renderFilter(request, response);
142     }
143 
144     public void destroy()
145     {
146         if (log.isTraceEnabled())
147         {
148             log.trace("called destory method.");
149         }
150         portletFilterChain.release();
151     }
152 
153     /* (non-Javadoc)
154      * @see javax.portlet.PortletConfig#getInitParameter(java.lang.String)
155      */
156     public String getInitParameter(String arg0)
157     {
158         if (portlet instanceof PortletConfig)
159         {
160             return ((PortletConfig) portlet).getInitParameter(arg0);
161         }
162         return portletConfig.getInitParameter(arg0);
163     }
164 
165     /* (non-Javadoc)
166      * @see javax.portlet.PortletConfig#getInitParameterNames()
167      */
168     public Enumeration getInitParameterNames()
169     {
170         if (portlet instanceof PortletConfig)
171         {
172             return ((PortletConfig) portlet).getInitParameterNames();
173         }
174         return portletConfig.getInitParameterNames();
175     }
176 
177     /* (non-Javadoc)
178      * @see javax.portlet.PortletConfig#getPortletContext()
179      */
180     public PortletContext getPortletContext()
181     {
182         if (portlet instanceof PortletConfig)
183         {
184             return ((PortletConfig) portlet).getPortletContext();
185         }
186         return portletConfig.getPortletContext();
187     }
188 
189     /* (non-Javadoc)
190      * @see javax.portlet.PortletConfig#getPortletName()
191      */
192     public String getPortletName()
193     {
194         if (portlet instanceof PortletConfig)
195         {
196             return ((PortletConfig) portlet).getPortletName();
197         }
198         return portletConfig.getPortletName();
199     }
200 
201     /* (non-Javadoc)
202      * @see javax.portlet.PortletConfig#getResourceBundle(java.util.Locale)
203      */
204     public ResourceBundle getResourceBundle(Locale arg0)
205     {
206         if (portlet instanceof PortletConfig)
207         {
208             return ((PortletConfig) portlet).getResourceBundle(arg0);
209         }
210         return portletConfig.getResourceBundle(arg0);
211     }
212 
213 }