1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 * <portlet-app id="example-portlets" version="1.0">
45 * <portlet id="ExamplePortlet">
46 * ...
47 * <init-param>
48 * <name>portlet-class</name>
49 * <value>org.apache.myfaces.portlet.MyFacesGenericPortlet</value>
50 * </init-param>
51 * <init-param>
52 * <name>portlet-filters</name>
53 * <value>org.apache.myfaces.portlet.TomahawkPortletFilter</value>
54 * </init-param>
55 * <init-param>
56 * <name>org.apache.myfaces.portlet.TomahawkPortletFilter:upload-threshold-size</name>
57 * <value>1m</value>
58 * </init-param>
59 * <init-param>
60 * <name>org.apache.myfaces.portlet.TomahawkPortletFilter:upload-max-file-size</name>
61 * <value>10m</value>
62 * </init-param>
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
87 String portletClassName = config.getInitParameter(PORTLET_CLASS);
88 if (portletClassName == null)
89 {
90 throw new PortletException("Portlet Class Name is null.");
91 }
92
93
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
160
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
172
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
184
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
196
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
208
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 }