View Javadoc

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