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.jetspeed.portal.portlets;
17  
18  //Element Construction Set
19  import org.apache.ecs.ConcreteElement;
20  import org.apache.ecs.StringElement;
21  
22  //Jetspeed stuff
23  import org.apache.jetspeed.portal.PortletException;
24  import org.apache.jetspeed.portal.PortletConfig;
25  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
26  import org.apache.jetspeed.services.logging.JetspeedLogger;
27  
28  //turbine
29  import org.apache.turbine.util.RunData;
30  import org.apache.turbine.util.ServerData;
31  import org.apache.turbine.services.servlet.TurbineServlet;
32  import org.apache.turbine.TurbineConstants;
33  import org.apache.turbine.util.DynamicURI;
34  
35  //JDK stuff
36  import java.util.Hashtable;
37  
38  /***
39   *   A Portlet that displays the contents of a source URL in an IFRAME tag.
40   *   portlets.xreg Usage example:
41   *     <PRE>
42   *           <portlet-entry name="IFrame" hidden="false" type="abstract" application="false">
43   *               <classname>org.apache.jetspeed.portal.portlets.IFramePortlet</classname>
44   *           </portlet-entry>
45   *     </PRE>
46   * 
47   *  local-portlets.xreg Usage example:
48   *     <PRE>
49   *           <portlet-entry name="SomeSite" hidden="false" type="ref" parent="IFramePortlet" application="false">
50   *             &lt;meta-info&gt;
51   *                 &lt;title&gt;SomeSite Info&lt;/title&gt;
52   *                 <description>Navigate SomeSite within an IFRAME</description>
53   *             &lt;/meta-info&gt;
54   *             <parameter name="source" value="http://somesite" hidden="false"/>
55   *             <media-type ref="html"/>
56   *           </portlet-entry>
57   *     </PRE>
58   * 
59   *     <P>The following parameters are accepted: </P>
60   *     <UL>
61   *         <LI> source - The target of the IFRAME, where it grabs it's content from. Can use ${webappRoot}.
62   *             Default is "http://127.0.0.1" </LI>
63   *         <LI> width - The width of the IFRAME, or null to let the browser decide.
64   *             Default is null.</LI>
65   *         <LI> height - The height of the IFRAME, or null to let the browser decide.
66   *             Default is null.</LI>
67   *         <LI> scrolling - How to display a scrollbar.
68   *             Default is "auto", to let the browser decide.</LI>
69   *         <LI> frameborder - Whether or not to display a border around the IFRAME.
70   *             Default is 1 (yes).</LI>
71   *         <LI> <code>refresh</code> - value in seconds to auto refresh contents of the IFRAME. </LI>
72   *         <LI> <code>align</code> - top | bottom | middle | left | <i>right</i> - How to align the IFRAME in relation to surrounding content.</LI>
73   *         <LI> <code>marginwidth</code> - size of the top and bottom margin inside the iframe. </LI>
74   *         <LI> <code>marginheight</code> - size of the left and right margin inside the iframe.</LI>
75   *     </UL>
76   * 
77   * @created February 23, 2002
78   * @author <a href="mailto:wbarnhil@twcny.rr.com">Bill Barnhill</a>
79   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
80   * @version $Id: IFramePortlet.java,v 1.7 2004/02/23 04:03:34 jford Exp $
81   * @see AbstractPortlet
82   */
83  
84  public class IFramePortlet extends AbstractInstancePortlet
85  {
86      /***
87       * Static initialization of the logger for this class
88       */    
89      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(IFramePortlet.class.getName());
90      
91      static final String DEFAULT_NOTSUPP_MSG =
92              "[Your user agent does not support inline frames or is currently" 
93              + " configured not to display frames]";
94  
95      static final String NO_SOURCE_MSG = "Please customize source for this IFrame";
96  
97      static final String DEFAULT_SOURCE = "http://127.0.0.1";
98      static final String DEFAULT_WIDTH = null;
99      static final String DEFAULT_HEIGHT = null;
100     static final String DEFAULT_SCROLLING = "auto";
101     static final String DEFAULT_FRAMEBORDER = "1";
102 
103     static final String PARAM_SOURCE = "source";
104     static final String PARAM_WIDTH = "width";
105     static final String PARAM_HEIGHT = "height";
106     static final String PARAM_SCROLLING = "scrolling";
107     static final String PARAM_FRAMEBORDER = "frameborder";
108     static final String PARAM_NAME = "name";
109     static final String PARAM_STYLE = "style";    
110     static final String PARAM_MARGINWIDTH = "marginwidth";
111     static final String PARAM_MARGINHEIGHT = "marginheight";
112     static final String PARAM_REFRESH = "refresh";
113     static final String PARAM_ALIGN = "align";
114     static final String WEBAPPROOT = "${" + TurbineConstants.WEBAPP_ROOT + "}";
115 
116     private String iSource = DEFAULT_SOURCE;
117     private String iWidth = DEFAULT_WIDTH;
118     private String iHeight = DEFAULT_HEIGHT;
119     private String iScrolling = DEFAULT_SCROLLING;
120     private String iFrameBorder = DEFAULT_FRAMEBORDER;
121     private String iMarginWidth = null;
122     private String iMarginHeight = null;
123     private String iStyle = null;
124     private String iName = null;
125     private String iRefresh = null;
126     private String iAlign = null;
127 
128     /***
129      *  Sets the source attribute of the IFramePortlet object
130      *
131      * @param  source  The new source value
132      * @since
133      */
134     public void setSource(String source)
135     {
136         if (source != null)
137         {
138             // Handle replacement variables
139             Hashtable parms = new Hashtable();
140             if (source.indexOf("${") >= 0) 
141             {
142                 // Add all portlet parms
143                 parms.putAll(this.getPortletConfig().getInitParameters());
144 
145                 // Add web app root variable replacement
146                 try 
147                 {
148                     ServerData sd = new ServerData(TurbineServlet.getServerName(),
149                                                 Integer.parseInt(TurbineServlet.getServerPort()),
150                                                 TurbineServlet.getServerScheme(),
151                                                 TurbineServlet.getContextPath(),
152                                                 TurbineServlet.getContextPath());
153                     DynamicURI uri = new DynamicURI(sd);                
154                     parms.put(TurbineConstants.WEBAPP_ROOT, uri.toString() + "/");
155                 } 
156                 catch (Exception e) 
157                 {
158                     logger.error("Exception",  e);
159                 }
160                 // Add portlet name variable replacement
161                 parms.put("portlet", this.getName());
162             }
163 
164             this.iSource = org.apache.jetspeed.util.StringUtils.replaceVars(source, parms);
165         }
166 
167     }
168 
169     /***
170      *  Sets the scrolling attribute of the IFramePortlet object
171      *
172      * @param  scrolling  The new scrolling value
173      * @since
174      */
175     public void setScrolling(String scrolling)
176     {
177         iScrolling = scrolling;
178     }
179 
180 
181     /***
182      *  Sets the width attribute of the IFramePortlet object
183      *
184      * @param  width  The new width value
185      * @since
186      */
187     public void setWidth(String width)
188     {
189         iWidth = width;
190     }
191 
192 
193     /***
194      *  Sets the height attribute of the IFramePortlet object
195      *
196      * @param  height  The new height value
197      * @since
198      */
199     public void setHeight(String height)
200     {
201         iHeight = height;
202     }
203 
204 
205     /***
206      *  Sets the frameBorder attribute of the IFramePortlet object
207      *
208      * @param  frameBorder  The new frameBorder value
209      * @since
210      */
211     public void setFrameBorder(String frameBorder)
212     {
213         iFrameBorder = frameBorder;
214     }
215 
216     /***
217      *  Sets the width attribute of the IFramePortlet object
218      * 
219      * @param width  The new width value
220      */
221     public void setMarginWidth(String width) 
222     {
223 
224         iMarginWidth = width;
225     }
226 
227     /***
228      *  Sets the marginheight attribute of the IFramePortlet object
229      * 
230      * @param height The new height value
231      */
232     public void setMarginHeight(String height) 
233     {
234 
235         iMarginHeight = height;
236     }
237 
238     /***
239      *  Sets the marginheight attribute of the IFramePortlet object
240      * 
241      * @param height The new height value
242      */
243     public void setAlign(String value) 
244     {
245 
246         iAlign = value;
247     }
248 
249     /***
250      *  Sets the refresh meta tag
251      * 
252      * @param value in seconds
253      */
254     public void setRefresh(String value) 
255     {
256 
257         iRefresh = value;
258     }
259 
260 
261     /***
262      * Sets the style of iframe. Some useful style effects:
263      * <UL>
264      * <LI>border:5px dashed purple
265      * <LI>border:5px dotted red
266      * <LI>border:5px double red
267      * <LI>border:5px inset red
268      * </UL>
269      * 
270      * @param value
271      */
272     public void setStyle(String value) 
273     {
274 
275         iStyle = value;
276     }
277 
278     /***
279      * Sets the name of iframe. This is useful when referencing
280      * the iframe as a target from another link.
281      * 
282      * @param value
283      */
284     public void setFrameName(String value) 
285     {
286 
287         iName = value;
288     }
289 
290     /***
291      *  This methods outputs the content of the portlet for a given request.
292      *
293      * @param  runData  the RunData object for the request
294      * @return          the content to be displayed to the user-agent
295      */
296     public ConcreteElement getContent(RunData runData)
297     {
298 
299         // Reinitialize if user customized the portlet (this will be useful
300         // when portlet preferences include user name and password for authentication or
301         // when other exposed iframe attributes are changed)
302         if (org.apache.jetspeed.util.PortletSessionState.getPortletConfigChanged(this, runData))
303         {
304             try {
305                 this.init();
306             }
307             catch (PortletException pe)
308             {
309                 logger.error("Exception",  pe);
310             }
311         }
312 
313         StringBuffer text = new StringBuffer();
314 
315         if (getSource() == null || getSource().trim().length() == 0)
316         {
317             text.append(NO_SOURCE_MSG);
318             return (new StringElement(text.toString()));
319         }
320 
321         text.append("<IFRAME ");
322 
323         text.append("src = \"" + getSource() + "\" ");
324         if (getWidth() != null)
325         {
326             text.append("width = \"" + getWidth() + "\" ");
327         }
328 
329         if (getHeight() != null)
330         {
331             text.append("height = \"" + getHeight() + "\" ");
332         }
333 
334         if (getFrameName() != null) 
335         {
336             text.append("name = \"" + getFrameName() + "\" ");
337         }
338 
339         if (getStyle() != null) 
340         {
341             text.append("style = \"" + getStyle() + "\" ");
342         }
343 
344         if (getMarginWidth() != null) 
345         {
346             text.append("marginwidth = \"" + getMarginWidth() + "\" ");
347         }
348 
349         if (getMarginHeight() != null) 
350         {
351             text.append("marginheight = \"" + getMarginHeight() + "\" ");
352         }
353 
354         if (getAlign() != null) 
355         {
356             text.append("align = \"" + getAlign() + "\" ");
357         }
358 
359         text.append("scrolling = \"" + getScrolling() + "\" ");
360         text.append("frameborder = \"" + getFrameBorder() + "\" ");
361         text.append(">");
362 
363         text.append("</IFRAME>");
364         return (new StringElement(text.toString()));
365     }
366 
367 
368     /***
369      *  Gets the source attribute of the IFramePortlet object
370      *
371      * @return    The source value
372      */
373     public String getSource()
374     {
375         return iSource;
376     }
377 
378 
379     /***
380      *  Gets the scrolling attribute of the IFramePortlet object
381      *
382      * @return    The scrolling value
383      */
384     public String getScrolling()
385     {
386         return iScrolling;
387     }
388 
389 
390     /***
391      *  Gets the width attribute of the IFramePortlet object
392      *
393      * @return    The width value
394      */
395     public String getWidth()
396     {
397         return iWidth;
398     }
399 
400 
401     /***
402      *  Gets the height attribute of the IFramePortlet object
403      *
404      * @return    The height value
405      */
406     public String getHeight()
407     {
408         return iHeight;
409     }
410 
411 
412     /***
413      *  Gets whether to display a border around the IFRAME. "1" == yes.
414      *
415      * @return    The frameBorder value
416      */
417     public String getFrameBorder()
418     {
419         String trueValues = "1,yes,true";
420         if (iFrameBorder != null && trueValues.indexOf(iFrameBorder) >= 0)
421         {
422             return "1";
423         }
424         return "0";
425     }
426 
427 
428     /***
429      *  Gets the message displayed when IFRAME is not supported
430      *  This includes when Frames are turned off.
431      *
432      * @todo        This should be localized
433      * @return    The notSupportedMsg value
434      */
435     public String getNotSupportedMsg()
436     {
437         return DEFAULT_NOTSUPP_MSG;
438     }
439 
440     /***
441      *  Gets the aling attribute of the IFramePortlet object
442      * 
443      * @return The marginheight value
444      */
445     public String getAlign() 
446     {
447 
448         return iAlign;
449     }
450 
451     /***
452      *  Gets iframe style
453      * 
454      * @return The style value
455      */
456     public String getStyle() 
457     {
458 
459         return iStyle;
460     }
461 
462     /***
463      *  Gets iframe name
464      * 
465      * @return The name value
466      */
467     public String getFrameName() 
468     {
469 
470         return iName;
471     }
472 
473     /***
474      *  Gets iframe refresh
475      * 
476      * @return The refresh value
477      */
478     public String getRefresh() 
479     {
480 
481         return iRefresh;
482     }
483 
484     /***
485      *  Gets the marginheight attribute of the IFramePortlet object
486      * 
487      * @return The marginheight value
488      */
489     public String getMarginHeight() 
490     {
491 
492         return iMarginHeight;
493     }
494 
495     /***
496      *  Gets the marginwidth attribute of the IFramePortlet object
497      * 
498      * @return The marginwidth value
499      */
500     public String getMarginWidth() 
501     {
502 
503         return iMarginWidth;
504     }
505 
506     /***
507      *  Initialize this portlet by setting inst. vars from InitParamaters.
508      *
509      * @throws  PortletException  Initialization failed
510      */
511     public void init() throws PortletException
512     {
513         // first make sure we propagate init
514         super.init();
515 
516         try
517         {
518             PortletConfig config = this.getPortletConfig();
519             String param = null;
520 
521             param = config.getInitParameter(PARAM_SOURCE);
522             if (param != null)
523             {
524                 setSource(param);
525             }
526 
527             param = config.getInitParameter(PARAM_WIDTH);
528             if (param != null)
529             {
530                 setWidth(param);
531             }
532 
533             param = config.getInitParameter(PARAM_HEIGHT);
534             if (param != null)
535             {
536                 setHeight(param);
537             }
538 
539             param = config.getInitParameter(PARAM_SCROLLING);
540             if (param != null)
541             {
542                 setScrolling(param);
543             }
544 
545             param = config.getInitParameter(PARAM_FRAMEBORDER);
546             if (param != null)
547             {
548                 setFrameBorder(param);
549             }
550 
551             param = config.getInitParameter(PARAM_STYLE);
552             if (param != null) 
553             {
554                 setStyle(param);
555             }
556 
557             param = config.getInitParameter(PARAM_NAME);
558             if (param != null) 
559             {
560                 setFrameName(param);
561             }
562 
563             param = config.getInitParameter(PARAM_REFRESH);
564             if (param != null) 
565             {
566                 setRefresh(param);
567             }
568 
569             param = config.getInitParameter(PARAM_MARGINWIDTH);
570             if (param != null) 
571             {
572                 setMarginWidth(param);
573             }
574 
575             param = config.getInitParameter(PARAM_MARGINHEIGHT);
576             if (param != null) 
577             {
578                 setMarginHeight(param);
579             }
580 
581             param = config.getInitParameter(PARAM_ALIGN);
582             if (param != null) 
583             {
584                 setAlign(param);
585             }
586 
587         }
588         catch (Exception e)
589         {
590             logger.error("Exception in init()", e);
591             throw new PortletException(e.getMessage());
592         }
593     }
594 }
595 
596