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.jetspeed.portlet;
18  
19  import java.io.IOException;
20  import javax.portlet.GenericPortlet;
21  import javax.portlet.PortletConfig;
22  import javax.portlet.PortletException;
23  import javax.portlet.PortletPreferences;
24  import javax.portlet.RenderRequest;
25  import javax.portlet.RenderResponse;
26  import javax.portlet.WindowState;
27  
28  /***
29   * IFramePortlet
30   *
31   * TODO:
32   * - add capabilities test for IFRAME
33   * - add locale specific "no iframes" message
34   *
35   * @author <a href="mailto:rwatler@finali.com">Randy Watler</a>
36   * @version $Id: IFramePortlet.java 578925 2007-09-24 19:22:58Z smilek $
37   */
38  public class IFramePortlet extends GenericPortlet
39  {
40      /***
41       * Configuration constants.
42       */
43      public static final String ENABLE_SOURCE_PREFERENCES_PARAM = "enableSourcePreferences";
44      public static final String ENABLE_PREFERENCES_PARAM = "enablePreferences";
45      public static final String CUSTOM_SOURCE_PARAM = "customSource";
46      public static final String MAXIMIZED_CUSTOM_SOURCE_PARAM = "maximizedCustomSource";
47      public static final String EDIT_SOURCE_PARAM = "editSource";
48      public static final String MAXIMIZED_EDIT_SOURCE_PARAM = "maximizedEditSource";
49      public static final String HELP_SOURCE_PARAM = "helpSource";
50      public static final String MAXIMIZED_HELP_SOURCE_PARAM = "maximizedHelpSource";
51      public static final String VIEW_SOURCE_PARAM = "viewSource";
52      public static final String MAXIMIZED_VIEW_SOURCE_PARAM = "maximizedViewSource";
53      public static final String ALIGN_ATTR_PARAM = "align";
54      public static final String CLASS_ATTR_PARAM = "class";
55      public static final String FRAME_BORDER_ATTR_PARAM = "frameBorder";
56      public static final String HEIGHT_ATTR_PARAM = "height";
57      public static final String ID_ATTR_PARAM = "id";
58      public static final String MARGIN_HEIGHT_ATTR_PARAM = "marginHeight";
59      public static final String MARGIN_WIDTH_ATTR_PARAM = "marginWidth";
60      public static final String MAXIMIZED_HEIGHT_ATTR_PARAM = "maximizedHeight";
61      public static final String MAXIMIZED_SCROLLING_ATTR_PARAM = "maximizedScrolling";
62      public static final String MAXIMIZED_STYLE_ATTR_PARAM = "maximizedStyle";
63      public static final String MAXIMIZED_WIDTH_ATTR_PARAM = "maximizedWidth";
64      public static final String NAME_ATTR_PARAM = "name";
65      public static final String SCROLLING_ATTR_PARAM = "scrolling";
66      public static final String STYLE_ATTR_PARAM = "style";
67      public static final String WIDTH_ATTR_PARAM = "width";
68  
69      /***
70       * Configuration default constants.
71       */
72      public static final String ALIGN_ATTR_DEFAULT = "BOTTOM";
73      public static final String FRAME_BORDER_ATTR_DEFAULT = "0";
74      public static final String HEIGHT_ATTR_DEFAULT = "";
75      public static final String MARGIN_HEIGHT_ATTR_DEFAULT = "0";
76      public static final String MARGIN_WIDTH_ATTR_DEFAULT = "0";
77      public static final String MAXIMIZED_HEIGHT_ATTR_DEFAULT = "100%";
78      public static final String MAXIMIZED_SCROLLING_ATTR_DEFAULT = "AUTO";
79      public static final String MAXIMIZED_WIDTH_ATTR_DEFAULT = "100%";
80      public static final String SCROLLING_ATTR_DEFAULT = "NO";
81      public static final String WIDTH_ATTR_DEFAULT = "100%";
82  
83      /***
84       * Enable parameter preferences overrides member.
85       */
86      private boolean enablePreferences;
87  
88      /***
89       * Default IFRAME source attribute members.
90       */
91      private String defaultCustomSource;
92      private String defaultMaximizedCustomSource;
93      private String defaultEditSource;
94      private String defaultMaximizedEditSource;
95      private String defaultHelpSource;
96      private String defaultMaximizedHelpSource;
97      private String defaultViewSource;
98      private String defaultMaximizedViewSource;
99  
100     /***
101      * Default IFRAME attribute members.
102      */
103     private String defaultAlignAttr;
104     private String defaultClassAttr;
105     private String defaultFrameBorderAttr;
106     private String defaultHeightAttr;
107     private String defaultIdAttr;
108     private String defaultMarginHeightAttr;
109     private String defaultMarginWidthAttr;
110     private String defaultMaximizedHeightAttr;
111     private String defaultMaximizedScrollingAttr;
112     private String defaultMaximizedStyleAttr;
113     private String defaultMaximizedWidthAttr;
114     private String defaultNameAttr;
115     private String defaultScrollingAttr;
116     private String defaultStyleAttr;
117     private String defaultWidthAttr;
118 
119     /***
120      * Portlet constructor.
121      */
122     public IFramePortlet()
123     {
124     }
125 
126     /***
127      * Initialize portlet configuration.
128      */
129     public void init(PortletConfig config)
130         throws PortletException
131     {
132         super.init(config);
133 
134         String initParam = config.getInitParameter(ENABLE_PREFERENCES_PARAM);
135         if (initParam == null)
136         {
137             initParam = config.getInitParameter(ENABLE_SOURCE_PREFERENCES_PARAM);
138         }
139         if (initParam != null)
140         {
141             enablePreferences = (new Boolean(initParam)).booleanValue();
142         }
143 
144         defaultCustomSource = config.getInitParameter(CUSTOM_SOURCE_PARAM);
145         defaultMaximizedCustomSource = config.getInitParameter(MAXIMIZED_CUSTOM_SOURCE_PARAM);
146         defaultEditSource = config.getInitParameter(EDIT_SOURCE_PARAM);
147         defaultMaximizedEditSource = config.getInitParameter(MAXIMIZED_EDIT_SOURCE_PARAM);
148         defaultHelpSource = config.getInitParameter(HELP_SOURCE_PARAM);
149         defaultMaximizedHelpSource = config.getInitParameter(MAXIMIZED_HELP_SOURCE_PARAM);
150         defaultViewSource = config.getInitParameter(VIEW_SOURCE_PARAM);
151         defaultMaximizedViewSource = config.getInitParameter(MAXIMIZED_VIEW_SOURCE_PARAM);
152 
153         defaultAlignAttr = getAttributeParam(config, ALIGN_ATTR_PARAM, ALIGN_ATTR_DEFAULT);
154         defaultClassAttr = getAttributeParam(config, CLASS_ATTR_PARAM, null);
155         defaultFrameBorderAttr = getAttributeParam(config, FRAME_BORDER_ATTR_PARAM, FRAME_BORDER_ATTR_DEFAULT);
156         defaultHeightAttr = getAttributeParam(config, HEIGHT_ATTR_PARAM, HEIGHT_ATTR_DEFAULT);
157         defaultIdAttr = getAttributeParam(config, ID_ATTR_PARAM, null);
158         defaultMarginHeightAttr = getAttributeParam(config, MARGIN_HEIGHT_ATTR_PARAM, MARGIN_HEIGHT_ATTR_DEFAULT);
159         defaultMarginWidthAttr = getAttributeParam(config, MARGIN_WIDTH_ATTR_PARAM, MARGIN_WIDTH_ATTR_DEFAULT);
160         defaultMaximizedHeightAttr = getAttributeParam(config, MAXIMIZED_HEIGHT_ATTR_PARAM, MAXIMIZED_HEIGHT_ATTR_DEFAULT);
161         defaultMaximizedScrollingAttr = getAttributeParam(config, MAXIMIZED_SCROLLING_ATTR_PARAM, MAXIMIZED_SCROLLING_ATTR_DEFAULT);
162         defaultMaximizedStyleAttr = getAttributeParam(config, MAXIMIZED_STYLE_ATTR_PARAM, null);
163         defaultMaximizedWidthAttr = getAttributeParam(config, MAXIMIZED_WIDTH_ATTR_PARAM, MAXIMIZED_WIDTH_ATTR_DEFAULT);
164         defaultNameAttr = getAttributeParam(config, NAME_ATTR_PARAM, null);
165         defaultScrollingAttr = getAttributeParam(config, SCROLLING_ATTR_PARAM, SCROLLING_ATTR_DEFAULT);
166         defaultStyleAttr = getAttributeParam(config, STYLE_ATTR_PARAM, null);
167         defaultWidthAttr = getAttributeParam(config, WIDTH_ATTR_PARAM, WIDTH_ATTR_DEFAULT);
168     }
169     
170     /***
171      * Generate IFRAME with custom source.
172      */
173     public void doCustom(RenderRequest request, RenderResponse response)
174         throws PortletException, IOException
175     {
176         // get IFRAME source
177         String source = null;
178         if (request.getWindowState().equals(WindowState.MAXIMIZED))
179         {
180             source = getPreferenceOrDefault(request, MAXIMIZED_CUSTOM_SOURCE_PARAM, defaultMaximizedCustomSource);
181         }
182         if (source == null)
183         {
184             source = getPreferenceOrDefault(request, CUSTOM_SOURCE_PARAM, defaultCustomSource);
185         }
186         if ((source == null) && request.getWindowState().equals(WindowState.MAXIMIZED))
187         {
188             source = getPreferenceOrDefault(request, MAXIMIZED_VIEW_SOURCE_PARAM, defaultMaximizedViewSource);
189         }
190         if (source == null)
191         {
192             source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM, defaultViewSource);
193         }
194         if (source == null)
195         {
196             throw new PortletException("IFRAME source not specified for custom portlet mode.");
197         }
198 
199         // render IFRAME content
200         doIFrame(request, source, response);
201     }
202 
203     /***
204      * Generate IFRAME with edit source.
205      */
206     public void doEdit(RenderRequest request, RenderResponse response)
207         throws PortletException, IOException
208     {
209         // get IFRAME source
210         String source = null;
211         if (request.getWindowState().equals(WindowState.MAXIMIZED))
212         {
213             source = getPreferenceOrDefault(request, MAXIMIZED_EDIT_SOURCE_PARAM, defaultMaximizedEditSource);
214         }
215         if (source == null)
216         {
217             source = getPreferenceOrDefault(request, EDIT_SOURCE_PARAM, defaultEditSource);
218         }
219         if ((source == null) && request.getWindowState().equals(WindowState.MAXIMIZED))
220         {
221             source = getPreferenceOrDefault(request, MAXIMIZED_VIEW_SOURCE_PARAM, defaultMaximizedViewSource);
222         }
223         if (source == null)
224         {
225             source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM, defaultViewSource);
226         }
227         if (source == null)
228         {
229             throw new PortletException("IFRAME source not specified for edit portlet mode.");
230         }
231 
232         // render IFRAME content
233         doIFrame(request, source, response);
234     }
235 
236     /***
237      * Generate IFRAME with help source.
238      */
239     public void doHelp(RenderRequest request, RenderResponse response)
240         throws PortletException, IOException
241     {
242         // get IFRAME source
243         String source = null;
244         if (request.getWindowState().equals(WindowState.MAXIMIZED))
245         {
246             source = getPreferenceOrDefault(request, MAXIMIZED_HELP_SOURCE_PARAM, defaultMaximizedHelpSource);
247         }
248         if (source == null)
249         {
250             source = getPreferenceOrDefault(request, HELP_SOURCE_PARAM, defaultHelpSource);
251         }
252         if ((source == null) && request.getWindowState().equals(WindowState.MAXIMIZED))
253         {
254             source = getPreferenceOrDefault(request, MAXIMIZED_VIEW_SOURCE_PARAM, defaultMaximizedViewSource);
255         }
256         if (source == null)
257         {
258             source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM, defaultViewSource);
259         }
260         if (source == null)
261         {
262             throw new PortletException("IFRAME source not specified for help portlet mode.");
263         }
264 
265         // render IFRAME content
266         doIFrame(request, source, response);
267     }
268 
269     /***
270      * Generate IFRAME with view source.
271      */
272     public void doView(RenderRequest request, RenderResponse response)
273         throws PortletException, IOException
274     {
275         // get IFRAME source
276         String source = null;
277         if (request.getWindowState().equals(WindowState.MAXIMIZED))
278         {
279             source = getPreferenceOrDefault(request, MAXIMIZED_VIEW_SOURCE_PARAM, defaultMaximizedViewSource);
280         }
281         if (source == null)
282         {
283             source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM, defaultViewSource);
284         }
285         if (source == null)
286         {
287             throw new PortletException("IFRAME source not specified for view portlet mode.");
288         }
289 
290         // render IFRAME content
291         doIFrame(request, source, response);
292     }
293 
294     /***
295      * Render IFRAME content
296      */
297     protected void doIFrame(RenderRequest request, String sourceAttr, RenderResponse response)
298         throws IOException
299     {
300         // generate HTML IFRAME content
301         StringBuffer content = new StringBuffer(4096);
302 
303         // fix JS2-349
304         content.append("<TABLE CLASS='iframePortletTableContainer' WIDTH='100%'><TBODY CLASS='iframePortletTbodyContainer'><TR><TD>");
305         
306         content.append("<IFRAME");
307         content.append(" SRC=\"").append(sourceAttr).append("\"");
308         String alignAttr = getPreferenceOrDefault(request, ALIGN_ATTR_PARAM, defaultAlignAttr);
309         if (alignAttr != null)
310         {
311             content.append(" ALIGN=\"").append(alignAttr).append("\"");
312         }
313         String classAttr = getPreferenceOrDefault(request, CLASS_ATTR_PARAM, defaultClassAttr);
314         if (classAttr != null)
315         {
316             content.append(" CLASS=\"").append(classAttr).append("\"");
317         }
318         String frameBorderAttr = getPreferenceOrDefault(request, FRAME_BORDER_ATTR_PARAM, defaultFrameBorderAttr);
319         if (frameBorderAttr != null)
320         {
321             content.append(" FRAMEBORDER=\"").append(frameBorderAttr).append("\"");
322         }
323         String idAttr = getPreferenceOrDefault(request, ID_ATTR_PARAM, defaultIdAttr);
324         if (idAttr != null)
325         {
326             content.append(" ID=\"").append(idAttr).append("\"");
327         }
328         String marginHeightAttr = getPreferenceOrDefault(request, MARGIN_HEIGHT_ATTR_PARAM, defaultMarginHeightAttr);
329         if (marginHeightAttr != null)
330         {
331             content.append(" MARGINHEIGHT=\"").append(marginHeightAttr).append("\"");
332         }
333         String marginWidthAttr = getPreferenceOrDefault(request, MARGIN_WIDTH_ATTR_PARAM, defaultMarginWidthAttr);
334         if (marginWidthAttr != null)
335         {
336             content.append(" MARGINWIDTH=\"").append(marginWidthAttr).append("\"");
337         }
338         String nameAttr = getPreferenceOrDefault(request, NAME_ATTR_PARAM, defaultNameAttr);
339         if (nameAttr != null)
340         {
341             content.append(" NAME=\"").append(nameAttr).append("\"");
342         }
343         if (request.getWindowState().equals(WindowState.MAXIMIZED))
344         {
345             String maximizedHeightAttr = getPreferenceOrDefault(request, MAXIMIZED_HEIGHT_ATTR_PARAM, defaultMaximizedHeightAttr);
346             if (maximizedHeightAttr == null)
347             {
348                 maximizedHeightAttr = getPreferenceOrDefault(request, HEIGHT_ATTR_PARAM, defaultHeightAttr);
349             }
350             if (maximizedHeightAttr != null)
351             {
352                 content.append(" HEIGHT=\"").append(maximizedHeightAttr).append("\"");
353             }
354             String maximizedScrollingAttr = getPreferenceOrDefault(request, MAXIMIZED_SCROLLING_ATTR_PARAM, defaultMaximizedScrollingAttr);
355             if (maximizedScrollingAttr == null)
356             {
357                 maximizedScrollingAttr = getPreferenceOrDefault(request, SCROLLING_ATTR_PARAM, defaultScrollingAttr);
358             }
359             if (maximizedScrollingAttr != null)
360             {
361                 content.append(" SCROLLING=\"").append(maximizedScrollingAttr).append("\"");
362             }
363             String maximizedStyleAttr = getPreferenceOrDefault(request, MAXIMIZED_STYLE_ATTR_PARAM,  defaultMaximizedStyleAttr);
364             if (maximizedStyleAttr == null)
365             {
366                 maximizedStyleAttr = getPreferenceOrDefault(request, STYLE_ATTR_PARAM, defaultStyleAttr);
367             }
368             if (maximizedStyleAttr != null)
369             {
370                 content.append(" STYLE=\"").append(maximizedStyleAttr).append("\"");
371             }
372             String maximizedWidthAttr = getPreferenceOrDefault(request, MAXIMIZED_WIDTH_ATTR_PARAM, defaultMaximizedWidthAttr);
373             if (maximizedWidthAttr == null)
374             {
375                 maximizedWidthAttr = getPreferenceOrDefault(request, WIDTH_ATTR_PARAM, defaultWidthAttr);
376             }
377             if (maximizedWidthAttr != null)
378             {
379                 content.append(" WIDTH=\"").append(maximizedWidthAttr).append("\"");
380             }
381         }
382         else
383         {
384             String heightAttr = getPreferenceOrDefault(request, HEIGHT_ATTR_PARAM, defaultHeightAttr);
385             if (heightAttr != null)
386             {
387                 content.append(" HEIGHT=\"").append(heightAttr).append("\"");
388             }
389             String scrollingAttr = getPreferenceOrDefault(request, SCROLLING_ATTR_PARAM, defaultScrollingAttr);
390             if (scrollingAttr != null)
391             {
392                 content.append(" SCROLLING=\"").append(scrollingAttr).append("\"");
393             }
394             String styleAttr = getPreferenceOrDefault(request, STYLE_ATTR_PARAM, defaultStyleAttr);
395             if (styleAttr != null)
396             {
397                 content.append(" STYLE=\"").append(styleAttr).append("\"");
398             }
399             String widthAttr = getPreferenceOrDefault(request, WIDTH_ATTR_PARAM, defaultWidthAttr);
400             if (widthAttr != null)
401             {
402                 content.append(" WIDTH=\"").append(widthAttr).append("\"");
403             }
404         }
405         content.append(">");
406         content.append("<P STYLE=\"textAlign:center\"><A HREF=\"").append(sourceAttr).append("\">").append(sourceAttr).append("</A></P>");
407         content.append("</IFRAME>");
408 
409         // end fix JS2-349
410         content.append("</TD></TR></TBODY></TABLE>");
411 
412         // set required content type and write HTML IFRAME content
413         response.setContentType("text/html");
414         response.getWriter().print(content.toString());
415     }
416 
417     /***
418      * Get IFRAME attribute parameter.
419      */
420     private String getAttributeParam(PortletConfig config, String name, String defaultValue)
421     {
422         String value = config.getInitParameter(name);
423         if (value == null)
424         {
425             value = defaultValue;
426         }
427         return (((value != null) && (value.length() > 0) && ! value.equalsIgnoreCase("none")) ? value : null);
428     }
429 
430     /***
431      * Get IFRAME preference value if enabled.
432      */
433     private String getPreferenceOrDefault(RenderRequest request, String name, String defaultValue)
434     {
435         if (! enablePreferences)
436         {
437             return defaultValue;
438         }
439         PortletPreferences prefs = request.getPreferences();
440         return ((prefs != null) ? prefs.getValue(name, defaultValue) : defaultValue);
441     }
442 }