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.common;
17  
18  import java.io.IOException;
19  
20  import javax.portlet.ActionRequest;
21  import javax.portlet.ActionResponse;
22  import javax.portlet.GenericPortlet;
23  import javax.portlet.PortletConfig;
24  import javax.portlet.PortletContext;
25  import javax.portlet.PortletException;
26  import javax.portlet.PortletMode;
27  import javax.portlet.PortletPreferences;
28  import javax.portlet.PortletRequestDispatcher;
29  import javax.portlet.RenderRequest;
30  import javax.portlet.RenderResponse;
31  
32  import org.apache.portals.bridges.util.PreferencesHelper;
33  
34  /***
35   * ServletPortlet will call a server, as defined by a xxxPage preference, for
36   * the Action, Custom, Edit, Help, and View operations.  This allow the use of
37   * existing servlets or JSPs in the portlet.  Since this is a very thin class,
38   * it is up to the servlet, or JSP, to return properly formated content.  See
39   * the JSR 168 for more information
40   *
41   * <pre>
42   *  <!-- Portlet Preferences -->
43   *  <portlet-preferences>
44   *    <preference>
45   *      <description>Action Servlet</description>
46   *      <name>ActionPage</name>
47   *      <value>/WEB-INF/action.do</value>
48   *      <non-modifiable/>
49   *    </preference>
50   *    <preference>
51   *      <description>Custom Servlet</description>
52   *      <name>CustomPage</name>
53   *      <value>/WEB-INF/custom.jsp</value>
54   *      <non-modifiable/>
55   *    </preference>
56   *    <preference>
57   *      <description>Edit Servlet</description>
58   *      <name>EditPage</name>
59   *      <value>/WEB-INF/edit.jsp</value>
60   *      <non-modifiable/>
61   *    </preference>
62   *    <preference>
63   *      <description>Help Servlet</description>
64   *      <name>HelpPage</name>
65   *      <value>/WEB-INF/help.jsp</value>
66   *      <non-modifiable/>
67   *    </preference>
68   *    <preference>
69   *      <description>View Servlet</description>
70   *      <name>ViewPage</name>
71   *      <value>/WEB-INF/view.jsp</value>
72   *      <non-modifiable/>
73   *    </preference>
74   *  </portlet-preferences>
75   *
76   * @author  paul
77   * @version $Id: GenericServletPortlet.java 188405 2005-03-19 13:47:14 +0100 (Sat, 19 Mar 2005) sgala $
78   */
79  public class GenericServletPortlet extends GenericPortlet
80  {
81  
82      /***
83       * Name of portlet preference for Action page
84       */
85      public static final String PARAM_ACTION_PAGE = "ActionPage";
86  
87      /***
88       * Name of portlet preference to allow the use of preferenecs to set pages
89       */
90      public static final String PARAM_ALLOW_PREFERENCES   = "AllowPreferences";
91  
92      /***
93       * Name of portlet preference for Custom page
94       */
95      public static final String PARAM_CUSTOM_PAGE = "CustomPage";
96  
97      /***
98       * Name of portlet preference for Edit page
99       */
100     public static final String PARAM_EDIT_PAGE   = "EditPage";
101 
102     /***
103      * Name of portlet preference for Edit page
104      */
105     public static final String PARAM_HELP_PAGE   = "HelpPage";
106 
107     /***
108      * Name of portlet preference for View page
109      */
110     public static final String PARAM_VIEW_PAGE   = "ViewPage";
111 
112     /***
113      * Name of portlet preference for View page
114      */
115     public static final String PARAM_MAX_PAGE   = "MaxPage";
116     
117     /***
118      * Allow preferences to be set by preferences.
119      */
120     private boolean allowPreferences = false;
121 
122     /***
123      * Default URL for the action page.
124      */
125     private String defaultActionPage = null;
126 
127     /***
128      * Default URL for the custom page.
129      */
130     private String defaultCustomPage = null;
131 
132     /***
133      * Default URL for the edit page.
134      */
135     private String defaultEditPage = null;
136 
137     /***
138      * Default URL for the help page.
139      */
140     private String defaultHelpPage = null;
141 
142     /***
143      * Default URL for the view page.
144      */
145     private String defaultViewPage = null;
146 
147     /***
148      * Creates a new instance of portlet
149      */
150     public GenericServletPortlet()
151     {
152     }
153 
154     public void init(PortletConfig config)
155     throws PortletException
156     {
157         super.init(config);
158         this.defaultActionPage = config.getInitParameter(PARAM_ACTION_PAGE);
159         this.defaultCustomPage = config.getInitParameter(PARAM_CUSTOM_PAGE);
160         this.defaultEditPage = config.getInitParameter(PARAM_EDIT_PAGE);
161         this.defaultViewPage = config.getInitParameter(PARAM_VIEW_PAGE);
162         this.defaultHelpPage = config.getInitParameter(PARAM_HELP_PAGE);
163         String allowPreferencesString = config.getInitParameter(PARAM_ALLOW_PREFERENCES);
164         if (allowPreferencesString != null)
165         {
166             this.allowPreferences = new Boolean(allowPreferencesString).booleanValue();
167         }
168 
169 //        if ((this.defaultActionPage == null) &&
170 //        (this.defaultCustomPage == null) &&
171 //        (this.defaultEditPage == null) &&
172 //        (this.defaultViewPage == null) &&
173 //        (this.defaultHelpPage == null) &&
174 //        (this.allowPreferences == false) )
175 //        {
176 //            // This portlet is configured to do nothing!
177 //            throw new PortletException("Portlet " + config.getPortletName() + " is incorrectly configured. No pages are defined.");
178 //        }
179     }
180 
181     /***
182      * Execute the servlet as define by the init parameter or preference PARAM_ACTION_PAGE.  The value
183      * if the parameter is a relative URL, i.e. /actionPage.jsp will execute the
184      * JSP editPage.jsp in the portlet application's web app.  The action should
185      * not generate any content.  The content will be generate by doCustom(),
186      * doHelp() , doEdit(), or doView().
187      *
188      * See section PLT.16.2 of the JSR 168 Portlet Spec for more information
189      * around executing a servlet or JSP in processAction()
190      *
191      * @see javax.portlet.GenericPortlet#processAction
192      *
193      * @task Need to be able to execute a servlet for the action
194      */
195     public void processAction(ActionRequest request, ActionResponse actionResponse)
196     throws PortletException, IOException
197     {
198         String actionPage = this.defaultActionPage;
199         if (this.allowPreferences == true)
200         {
201             PortletPreferences prefs = request.getPreferences();
202             if (prefs != null)
203             {
204                 actionPage = prefs.getValue(PARAM_ACTION_PAGE, this.defaultActionPage);
205             }
206         }
207 
208 
209         if (actionPage != null)
210         {
211           /*
212            * At this point the desire action should be execute.  See the @TASK.
213            */
214         }
215 
216 
217         return;
218     }
219 
220     /***
221      * Execute the servlet as define by the init parameter or preference PARAM_EDIT_PAGE.  The value
222      * if the parameter is a relative URL, i.e. /editPage.jsp will execute the
223      * JSP editPage.jsp in the portlet application's web app.
224      *
225      * @see javax.portlet.GenericPortlet#doCustom
226      */
227     public void doCustom(RenderRequest request, RenderResponse response)
228     throws PortletException, IOException
229     {
230         String customPage = this.defaultCustomPage;
231         if (this.allowPreferences == true)
232         {
233             PortletPreferences prefs = request.getPreferences();
234             // allow ViewPage override by the request
235             customPage = (String) request.getAttribute(PARAM_CUSTOM_PAGE);            
236             
237             if (prefs != null && customPage == null)
238             {
239                 customPage = prefs.getValue(PARAM_CUSTOM_PAGE, this.defaultCustomPage);
240             }
241         }
242 
243         if (customPage != null)
244         {
245             PortletContext context = getPortletContext();
246             PortletRequestDispatcher rd = context.getRequestDispatcher(customPage);
247             rd.include(request, response);
248         }
249         return;
250     }
251 
252     /***
253      * Execute the servlet as define by the init parameter or preference PARAM_EDIT_PAGE.
254      * The value if the parameter is a relative URL, i.e. /editPage.jsp will execute the
255      * JSP editPage.jsp in the portlet application's web app.
256      *
257      * @see javax.portlet.GenericPortlet#doEdit
258      */
259     public void doEdit(RenderRequest request, RenderResponse response)
260     throws PortletException, IOException
261     {
262         String editPage = this.defaultEditPage;
263         
264         //  allow EditPage override by the request
265         String reqEditPage = (String) request.getAttribute(PARAM_EDIT_PAGE);
266         if(reqEditPage != null)
267         {
268             editPage = reqEditPage;
269         }
270         
271         if (this.allowPreferences == true)
272         {                       
273             PortletPreferences prefs = request.getPreferences();
274 
275             if (prefs != null && reqEditPage == null)
276             {
277                 editPage = prefs.getValue(PARAM_EDIT_PAGE, this.defaultEditPage);
278             }
279         }
280 
281         if (editPage != null)
282         {
283             PortletContext context = getPortletContext();
284             PortletRequestDispatcher rd = context.getRequestDispatcher(editPage);
285             rd.include(request, response);
286         }
287         return;
288     }
289 
290     /***
291      * Execute the servlet as define by the init parameter or preference PARAM_HELP_PAGE.
292      * The value if the parameter is a relative URL, i.e. /helpPage.jsp will exeute the
293      * JSP helpPage.jsp in the portlet application's web app.
294      *
295      * @see javax.portlet.GenericPortlet#doView
296      */
297     public void doHelp(RenderRequest request, RenderResponse response)
298     throws PortletException, IOException
299     {
300         String helpPage = this.defaultHelpPage;
301         
302         //  allow HelpPage override by the request
303         String reqHelpPage = (String) request.getAttribute(PARAM_HELP_PAGE);
304         if(reqHelpPage != null)
305         {
306             helpPage = reqHelpPage;
307         }
308         
309         if (this.allowPreferences == true)
310         {
311 
312             PortletPreferences prefs = request.getPreferences();
313 
314             if (prefs != null && reqHelpPage == null)
315             {
316                 helpPage = prefs.getValue(PARAM_HELP_PAGE, this.defaultHelpPage);
317             }
318         }
319 
320         if (helpPage != null)
321         {
322             PortletContext context = getPortletContext();
323             PortletRequestDispatcher rd = context.getRequestDispatcher(helpPage);
324             rd.include(request, response);
325         }
326         return;
327     }
328 
329     /***
330      * Execute the servlet as define by the init parameter or preference PARAM_VIEW_PAGE.
331      * The value if the parameter is a relative URL, i.e. /viewPage.jsp will execute the
332      * JSP viewPage.jsp in the portlet application's web app.
333      *
334      * @see javax.portlet.GenericPortlet#doView
335      */
336     public void doView(RenderRequest request, RenderResponse response)
337     throws PortletException, IOException
338     {
339         String viewPage = this.defaultViewPage;
340         
341          //	allow ViewPage override by the request
342 		String reqViewPage = (String) request.getAttribute(PARAM_VIEW_PAGE);
343 		if(reqViewPage != null)
344 		{
345 			viewPage = reqViewPage;
346 		}
347 		
348         if (this.allowPreferences == true)
349         {
350             PortletPreferences prefs = request.getPreferences();
351 
352             
353             if (prefs != null && reqViewPage == null)
354             {
355                 viewPage = prefs.getValue(PARAM_VIEW_PAGE, this.defaultViewPage);
356             }          
357         }
358 
359         if (viewPage != null)
360         {
361             PortletContext context = getPortletContext();
362             PortletRequestDispatcher rd = context.getRequestDispatcher(viewPage);
363             rd.include(request, response);
364         }
365         return;
366     }
367 
368     /***
369      * Getter for property defaultViewPage.
370      *
371      * @return Value of property defaultViewPage.
372      */
373     public java.lang.String getDefaultViewPage()
374     {
375         return defaultViewPage;
376     }
377 
378     /***
379      * Setter for property defaultViewPage.
380      *
381      * @param defaultViewPage New value of property defaultViewPage.
382      */
383     public void setDefaultViewPage(java.lang.String defaultViewPage)
384     {
385         this.defaultViewPage = defaultViewPage;
386     }
387 
388     /***
389      * Getter for property defaultHelpPage.
390      *
391      * @return Value of property defaultHelpPage.
392      */
393     public java.lang.String getDefaultHelpPage()
394     {
395         return defaultHelpPage;
396     }
397 
398     /***
399      * Setter for property defaultHelpPage.
400      *
401      * @param defaultHelpPage New value of property defaultHelpPage.
402      */
403     public void setDefaultHelpPage(java.lang.String defaultHelpPage)
404     {
405         this.defaultHelpPage = defaultHelpPage;
406     }
407 
408     /***
409      * Getter for property defaultEditPage.
410      *
411      * @return Value of property defaultEditPage.
412      */
413     public java.lang.String getDefaultEditPage()
414     {
415         return defaultEditPage;
416     }
417 
418     /***
419      * Setter for property defaultEditPage.
420      *
421      * @param defaultEditPage New value of property defaultEditPage.
422      */
423     public void setDefaultEditPage(java.lang.String defaultEditPage)
424     {
425         this.defaultEditPage = defaultEditPage;
426     }
427 
428     /***
429      * Getter for property defaultCustomPage.
430      *
431      * @return Value of property defaultCustomPage.
432      */
433     public java.lang.String getDefaultCustomPage()
434     {
435         return defaultCustomPage;
436     }
437 
438     /***
439      * Setter for property defaultCustomPage.
440      *
441      * @param defaultCustomPage New value of property defaultCustomPage.
442      */
443     public void setDefaultCustomPage(java.lang.String defaultCustomPage)
444     {
445         this.defaultCustomPage = defaultCustomPage;
446     }
447 
448     /***
449      * Getter for property defaultActionPage.
450      *
451      * @return Value of property defaultActionPage.
452      */
453     public java.lang.String getDefaultActionPage()
454     {
455         return defaultActionPage;
456     }
457 
458     /***
459      * Setter for property defaultActionPage.
460      *
461      * @param defaultActionPage New value of property defaultActionPage.
462      */
463     public void setDefaultActionPage(java.lang.String defaultActionPage)
464     {
465         this.defaultActionPage = defaultActionPage;
466     }
467 
468     /***
469      * Save the prefs
470      */
471     public void processPreferencesAction(ActionRequest request, ActionResponse actionResponse)
472     throws PortletException, IOException
473     {
474         PortletPreferences prefs = request.getPreferences();
475         PreferencesHelper.requestParamsToPreferences(request);
476         prefs.store();
477         actionResponse.setPortletMode(PortletMode.VIEW);
478     }
479     
480 }