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