Coverage report

  %line %branch
org.apache.jetspeed.portlet.IFramePortlet
0% 
0% 

 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  0
     {
 124  0
     }
 125  
 
 126  
     /**
 127  
      * Initialize portlet configuration.
 128  
      */
 129  
     public void init(PortletConfig config)
 130  
         throws PortletException
 131  
     {
 132  0
         super.init(config);
 133  
 
 134  0
         String initParam = config.getInitParameter(ENABLE_PREFERENCES_PARAM);
 135  0
         if (initParam == null)
 136  
         {
 137  0
             initParam = config.getInitParameter(ENABLE_SOURCE_PREFERENCES_PARAM);
 138  
         }
 139  0
         if (initParam != null)
 140  
         {
 141  0
             enablePreferences = (new Boolean(initParam)).booleanValue();
 142  
         }
 143  
 
 144  0
         defaultCustomSource = config.getInitParameter(CUSTOM_SOURCE_PARAM);
 145  0
         defaultMaximizedCustomSource = config.getInitParameter(MAXIMIZED_CUSTOM_SOURCE_PARAM);
 146  0
         defaultEditSource = config.getInitParameter(EDIT_SOURCE_PARAM);
 147  0
         defaultMaximizedEditSource = config.getInitParameter(MAXIMIZED_EDIT_SOURCE_PARAM);
 148  0
         defaultHelpSource = config.getInitParameter(HELP_SOURCE_PARAM);
 149  0
         defaultMaximizedHelpSource = config.getInitParameter(MAXIMIZED_HELP_SOURCE_PARAM);
 150  0
         defaultViewSource = config.getInitParameter(VIEW_SOURCE_PARAM);
 151  0
         defaultMaximizedViewSource = config.getInitParameter(MAXIMIZED_VIEW_SOURCE_PARAM);
 152  
 
 153  0
         defaultAlignAttr = getAttributeParam(config, ALIGN_ATTR_PARAM, ALIGN_ATTR_DEFAULT);
 154  0
         defaultClassAttr = getAttributeParam(config, CLASS_ATTR_PARAM, null);
 155  0
         defaultFrameBorderAttr = getAttributeParam(config, FRAME_BORDER_ATTR_PARAM, FRAME_BORDER_ATTR_DEFAULT);
 156  0
         defaultHeightAttr = getAttributeParam(config, HEIGHT_ATTR_PARAM, HEIGHT_ATTR_DEFAULT);
 157  0
         defaultIdAttr = getAttributeParam(config, ID_ATTR_PARAM, null);
 158  0
         defaultMarginHeightAttr = getAttributeParam(config, MARGIN_HEIGHT_ATTR_PARAM, MARGIN_HEIGHT_ATTR_DEFAULT);
 159  0
         defaultMarginWidthAttr = getAttributeParam(config, MARGIN_WIDTH_ATTR_PARAM, MARGIN_WIDTH_ATTR_DEFAULT);
 160  0
         defaultMaximizedHeightAttr = getAttributeParam(config, MAXIMIZED_HEIGHT_ATTR_PARAM, MAXIMIZED_HEIGHT_ATTR_DEFAULT);
 161  0
         defaultMaximizedScrollingAttr = getAttributeParam(config, MAXIMIZED_SCROLLING_ATTR_PARAM, MAXIMIZED_SCROLLING_ATTR_DEFAULT);
 162  0
         defaultMaximizedStyleAttr = getAttributeParam(config, MAXIMIZED_STYLE_ATTR_PARAM, null);
 163  0
         defaultMaximizedWidthAttr = getAttributeParam(config, MAXIMIZED_WIDTH_ATTR_PARAM, MAXIMIZED_WIDTH_ATTR_DEFAULT);
 164  0
         defaultNameAttr = getAttributeParam(config, NAME_ATTR_PARAM, null);
 165  0
         defaultScrollingAttr = getAttributeParam(config, SCROLLING_ATTR_PARAM, SCROLLING_ATTR_DEFAULT);
 166  0
         defaultStyleAttr = getAttributeParam(config, STYLE_ATTR_PARAM, null);
 167  0
         defaultWidthAttr = getAttributeParam(config, WIDTH_ATTR_PARAM, WIDTH_ATTR_DEFAULT);
 168  0
     }
 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  0
         String source = null;
 178  0
         if (request.getWindowState().equals(WindowState.MAXIMIZED))
 179  
         {
 180  0
             source = getPreferenceOrDefault(request, MAXIMIZED_CUSTOM_SOURCE_PARAM, defaultMaximizedCustomSource);
 181  
         }
 182  0
         if (source == null)
 183  
         {
 184  0
             source = getPreferenceOrDefault(request, CUSTOM_SOURCE_PARAM, defaultCustomSource);
 185  
         }
 186  0
         if ((source == null) && request.getWindowState().equals(WindowState.MAXIMIZED))
 187  
         {
 188  0
             source = getPreferenceOrDefault(request, MAXIMIZED_VIEW_SOURCE_PARAM, defaultMaximizedViewSource);
 189  
         }
 190  0
         if (source == null)
 191  
         {
 192  0
             source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM, defaultViewSource);
 193  
         }
 194  0
         if (source == null)
 195  
         {
 196  0
             throw new PortletException("IFRAME source not specified for custom portlet mode.");
 197  
         }
 198  
 
 199  
         // render IFRAME content
 200  0
         doIFrame(request, source, response);
 201  0
     }
 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  0
         String source = null;
 211  0
         if (request.getWindowState().equals(WindowState.MAXIMIZED))
 212  
         {
 213  0
             source = getPreferenceOrDefault(request, MAXIMIZED_EDIT_SOURCE_PARAM, defaultMaximizedEditSource);
 214  
         }
 215  0
         if (source == null)
 216  
         {
 217  0
             source = getPreferenceOrDefault(request, EDIT_SOURCE_PARAM, defaultEditSource);
 218  
         }
 219  0
         if ((source == null) && request.getWindowState().equals(WindowState.MAXIMIZED))
 220  
         {
 221  0
             source = getPreferenceOrDefault(request, MAXIMIZED_VIEW_SOURCE_PARAM, defaultMaximizedViewSource);
 222  
         }
 223  0
         if (source == null)
 224  
         {
 225  0
             source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM, defaultViewSource);
 226  
         }
 227  0
         if (source == null)
 228  
         {
 229  0
             throw new PortletException("IFRAME source not specified for edit portlet mode.");
 230  
         }
 231  
 
 232  
         // render IFRAME content
 233  0
         doIFrame(request, source, response);
 234  0
     }
 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  0
         String source = null;
 244  0
         if (request.getWindowState().equals(WindowState.MAXIMIZED))
 245  
         {
 246  0
             source = getPreferenceOrDefault(request, MAXIMIZED_HELP_SOURCE_PARAM, defaultMaximizedHelpSource);
 247  
         }
 248  0
         if (source == null)
 249  
         {
 250  0
             source = getPreferenceOrDefault(request, HELP_SOURCE_PARAM, defaultHelpSource);
 251  
         }
 252  0
         if ((source == null) && request.getWindowState().equals(WindowState.MAXIMIZED))
 253  
         {
 254  0
             source = getPreferenceOrDefault(request, MAXIMIZED_VIEW_SOURCE_PARAM, defaultMaximizedViewSource);
 255  
         }
 256  0
         if (source == null)
 257  
         {
 258  0
             source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM, defaultViewSource);
 259  
         }
 260  0
         if (source == null)
 261  
         {
 262  0
             throw new PortletException("IFRAME source not specified for help portlet mode.");
 263  
         }
 264  
 
 265  
         // render IFRAME content
 266  0
         doIFrame(request, source, response);
 267  0
     }
 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  0
         String source = null;
 277  0
         if (request.getWindowState().equals(WindowState.MAXIMIZED))
 278  
         {
 279  0
             source = getPreferenceOrDefault(request, MAXIMIZED_VIEW_SOURCE_PARAM, defaultMaximizedViewSource);
 280  
         }
 281  0
         if (source == null)
 282  
         {
 283  0
             source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM, defaultViewSource);
 284  
         }
 285  0
         if (source == null)
 286  
         {
 287  0
             throw new PortletException("IFRAME source not specified for view portlet mode.");
 288  
         }
 289  
 
 290  
         // render IFRAME content
 291  0
         doIFrame(request, source, response);
 292  0
     }
 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  0
         StringBuffer content = new StringBuffer(4096);
 302  
 
 303  
         // fix JS2-349
 304  0
         content.append("<TABLE CLASS='iframePortletTableContainer' WIDTH='100%'><TBODY CLASS='iframePortletTbodyContainer'><TR><TD>");
 305  
         
 306  0
         content.append("<IFRAME");
 307  0
         content.append(" SRC=\"").append(sourceAttr).append("\"");
 308  0
         String alignAttr = getPreferenceOrDefault(request, ALIGN_ATTR_PARAM, defaultAlignAttr);
 309  0
         if (alignAttr != null)
 310  
         {
 311  0
             content.append(" ALIGN=\"").append(alignAttr).append("\"");
 312  
         }
 313  0
         String classAttr = getPreferenceOrDefault(request, CLASS_ATTR_PARAM, defaultClassAttr);
 314  0
         if (classAttr != null)
 315  
         {
 316  0
             content.append(" CLASS=\"").append(classAttr).append("\"");
 317  
         }
 318  0
         String frameBorderAttr = getPreferenceOrDefault(request, FRAME_BORDER_ATTR_PARAM, defaultFrameBorderAttr);
 319  0
         if (frameBorderAttr != null)
 320  
         {
 321  0
             content.append(" FRAMEBORDER=\"").append(frameBorderAttr).append("\"");
 322  
         }
 323  0
         String idAttr = getPreferenceOrDefault(request, ID_ATTR_PARAM, defaultIdAttr);
 324  0
         if (idAttr != null)
 325  
         {
 326  0
             content.append(" ID=\"").append(idAttr).append("\"");
 327  
         }
 328  0
         String marginHeightAttr = getPreferenceOrDefault(request, MARGIN_HEIGHT_ATTR_PARAM, defaultMarginHeightAttr);
 329  0
         if (marginHeightAttr != null)
 330  
         {
 331  0
             content.append(" MARGINHEIGHT=\"").append(marginHeightAttr).append("\"");
 332  
         }
 333  0
         String marginWidthAttr = getPreferenceOrDefault(request, MARGIN_WIDTH_ATTR_PARAM, defaultMarginWidthAttr);
 334  0
         if (marginWidthAttr != null)
 335  
         {
 336  0
             content.append(" MARGINWIDTH=\"").append(marginWidthAttr).append("\"");
 337  
         }
 338  0
         String nameAttr = getPreferenceOrDefault(request, NAME_ATTR_PARAM, defaultNameAttr);
 339  0
         if (nameAttr != null)
 340  
         {
 341  0
             content.append(" NAME=\"").append(nameAttr).append("\"");
 342  
         }
 343  0
         if (request.getWindowState().equals(WindowState.MAXIMIZED))
 344  
         {
 345  0
             String maximizedHeightAttr = getPreferenceOrDefault(request, MAXIMIZED_HEIGHT_ATTR_PARAM, defaultMaximizedHeightAttr);
 346  0
             if (maximizedHeightAttr == null)
 347  
             {
 348  0
                 maximizedHeightAttr = getPreferenceOrDefault(request, HEIGHT_ATTR_PARAM, defaultHeightAttr);
 349  
             }
 350  0
             if (maximizedHeightAttr != null)
 351  
             {
 352  0
                 content.append(" HEIGHT=\"").append(maximizedHeightAttr).append("\"");
 353  
             }
 354  0
             String maximizedScrollingAttr = getPreferenceOrDefault(request, MAXIMIZED_SCROLLING_ATTR_PARAM, defaultMaximizedScrollingAttr);
 355  0
             if (maximizedScrollingAttr == null)
 356  
             {
 357  0
                 maximizedScrollingAttr = getPreferenceOrDefault(request, SCROLLING_ATTR_PARAM, defaultScrollingAttr);
 358  
             }
 359  0
             if (maximizedScrollingAttr != null)
 360  
             {
 361  0
                 content.append(" SCROLLING=\"").append(maximizedScrollingAttr).append("\"");
 362  
             }
 363  0
             String maximizedStyleAttr = getPreferenceOrDefault(request, MAXIMIZED_STYLE_ATTR_PARAM,  defaultMaximizedStyleAttr);
 364  0
             if (maximizedStyleAttr == null)
 365  
             {
 366  0
                 maximizedStyleAttr = getPreferenceOrDefault(request, STYLE_ATTR_PARAM, defaultStyleAttr);
 367  
             }
 368  0
             if (maximizedStyleAttr != null)
 369  
             {
 370  0
                 content.append(" STYLE=\"").append(maximizedStyleAttr).append("\"");
 371  
             }
 372  0
             String maximizedWidthAttr = getPreferenceOrDefault(request, MAXIMIZED_WIDTH_ATTR_PARAM, defaultMaximizedWidthAttr);
 373  0
             if (maximizedWidthAttr == null)
 374  
             {
 375  0
                 maximizedWidthAttr = getPreferenceOrDefault(request, WIDTH_ATTR_PARAM, defaultWidthAttr);
 376  
             }
 377  0
             if (maximizedWidthAttr != null)
 378  
             {
 379  0
                 content.append(" WIDTH=\"").append(maximizedWidthAttr).append("\"");
 380  
             }
 381  0
         }
 382  
         else
 383  
         {
 384  0
             String heightAttr = getPreferenceOrDefault(request, HEIGHT_ATTR_PARAM, defaultHeightAttr);
 385  0
             if (heightAttr != null)
 386  
             {
 387  0
                 content.append(" HEIGHT=\"").append(heightAttr).append("\"");
 388  
             }
 389  0
             String scrollingAttr = getPreferenceOrDefault(request, SCROLLING_ATTR_PARAM, defaultScrollingAttr);
 390  0
             if (scrollingAttr != null)
 391  
             {
 392  0
                 content.append(" SCROLLING=\"").append(scrollingAttr).append("\"");
 393  
             }
 394  0
             String styleAttr = getPreferenceOrDefault(request, STYLE_ATTR_PARAM, defaultStyleAttr);
 395  0
             if (styleAttr != null)
 396  
             {
 397  0
                 content.append(" STYLE=\"").append(styleAttr).append("\"");
 398  
             }
 399  0
             String widthAttr = getPreferenceOrDefault(request, WIDTH_ATTR_PARAM, defaultWidthAttr);
 400  0
             if (widthAttr != null)
 401  
             {
 402  0
                 content.append(" WIDTH=\"").append(widthAttr).append("\"");
 403  
             }
 404  
         }
 405  0
         content.append(">");
 406  0
         content.append("<P STYLE=\"textAlign:center\"><A HREF=\"").append(sourceAttr).append("\">").append(sourceAttr).append("</A></P>");
 407  0
         content.append("</IFRAME>");
 408  
 
 409  
         // end fix JS2-349
 410  0
         content.append("</TD></TR></TBODY></TABLE>");
 411  
 
 412  
         // set required content type and write HTML IFRAME content
 413  0
         response.setContentType("text/html");
 414  0
         response.getWriter().print(content.toString());
 415  0
     }
 416  
 
 417  
     /**
 418  
      * Get IFRAME attribute parameter.
 419  
      */
 420  
     private String getAttributeParam(PortletConfig config, String name, String defaultValue)
 421  
     {
 422  0
         String value = config.getInitParameter(name);
 423  0
         if (value == null)
 424  
         {
 425  0
             value = defaultValue;
 426  
         }
 427  0
         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  0
         if (! enablePreferences)
 436  
         {
 437  0
             return defaultValue;
 438  
         }
 439  0
         PortletPreferences prefs = request.getPreferences();
 440  0
         return ((prefs != null) ? prefs.getValue(name, defaultValue) : defaultValue);
 441  
     }
 442  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.