001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlet;
018:
019: import java.io.IOException;
020: import javax.portlet.GenericPortlet;
021: import javax.portlet.PortletConfig;
022: import javax.portlet.PortletException;
023: import javax.portlet.PortletPreferences;
024: import javax.portlet.RenderRequest;
025: import javax.portlet.RenderResponse;
026: import javax.portlet.WindowState;
027:
028: /**
029: * IFramePortlet
030: *
031: * TODO:
032: * - add capabilities test for IFRAME
033: * - add locale specific "no iframes" message
034: *
035: * @author <a href="mailto:rwatler@finali.com">Randy Watler</a>
036: * @version $Id: IFramePortlet.java 578925 2007-09-24 19:22:58Z smilek $
037: */
038: public class IFramePortlet extends GenericPortlet {
039: /**
040: * Configuration constants.
041: */
042: public static final String ENABLE_SOURCE_PREFERENCES_PARAM = "enableSourcePreferences";
043: public static final String ENABLE_PREFERENCES_PARAM = "enablePreferences";
044: public static final String CUSTOM_SOURCE_PARAM = "customSource";
045: public static final String MAXIMIZED_CUSTOM_SOURCE_PARAM = "maximizedCustomSource";
046: public static final String EDIT_SOURCE_PARAM = "editSource";
047: public static final String MAXIMIZED_EDIT_SOURCE_PARAM = "maximizedEditSource";
048: public static final String HELP_SOURCE_PARAM = "helpSource";
049: public static final String MAXIMIZED_HELP_SOURCE_PARAM = "maximizedHelpSource";
050: public static final String VIEW_SOURCE_PARAM = "viewSource";
051: public static final String MAXIMIZED_VIEW_SOURCE_PARAM = "maximizedViewSource";
052: public static final String ALIGN_ATTR_PARAM = "align";
053: public static final String CLASS_ATTR_PARAM = "class";
054: public static final String FRAME_BORDER_ATTR_PARAM = "frameBorder";
055: public static final String HEIGHT_ATTR_PARAM = "height";
056: public static final String ID_ATTR_PARAM = "id";
057: public static final String MARGIN_HEIGHT_ATTR_PARAM = "marginHeight";
058: public static final String MARGIN_WIDTH_ATTR_PARAM = "marginWidth";
059: public static final String MAXIMIZED_HEIGHT_ATTR_PARAM = "maximizedHeight";
060: public static final String MAXIMIZED_SCROLLING_ATTR_PARAM = "maximizedScrolling";
061: public static final String MAXIMIZED_STYLE_ATTR_PARAM = "maximizedStyle";
062: public static final String MAXIMIZED_WIDTH_ATTR_PARAM = "maximizedWidth";
063: public static final String NAME_ATTR_PARAM = "name";
064: public static final String SCROLLING_ATTR_PARAM = "scrolling";
065: public static final String STYLE_ATTR_PARAM = "style";
066: public static final String WIDTH_ATTR_PARAM = "width";
067:
068: /**
069: * Configuration default constants.
070: */
071: public static final String ALIGN_ATTR_DEFAULT = "BOTTOM";
072: public static final String FRAME_BORDER_ATTR_DEFAULT = "0";
073: public static final String HEIGHT_ATTR_DEFAULT = "";
074: public static final String MARGIN_HEIGHT_ATTR_DEFAULT = "0";
075: public static final String MARGIN_WIDTH_ATTR_DEFAULT = "0";
076: public static final String MAXIMIZED_HEIGHT_ATTR_DEFAULT = "100%";
077: public static final String MAXIMIZED_SCROLLING_ATTR_DEFAULT = "AUTO";
078: public static final String MAXIMIZED_WIDTH_ATTR_DEFAULT = "100%";
079: public static final String SCROLLING_ATTR_DEFAULT = "NO";
080: public static final String WIDTH_ATTR_DEFAULT = "100%";
081:
082: /**
083: * Enable parameter preferences overrides member.
084: */
085: private boolean enablePreferences;
086:
087: /**
088: * Default IFRAME source attribute members.
089: */
090: private String defaultCustomSource;
091: private String defaultMaximizedCustomSource;
092: private String defaultEditSource;
093: private String defaultMaximizedEditSource;
094: private String defaultHelpSource;
095: private String defaultMaximizedHelpSource;
096: private String defaultViewSource;
097: private String defaultMaximizedViewSource;
098:
099: /**
100: * Default IFRAME attribute members.
101: */
102: private String defaultAlignAttr;
103: private String defaultClassAttr;
104: private String defaultFrameBorderAttr;
105: private String defaultHeightAttr;
106: private String defaultIdAttr;
107: private String defaultMarginHeightAttr;
108: private String defaultMarginWidthAttr;
109: private String defaultMaximizedHeightAttr;
110: private String defaultMaximizedScrollingAttr;
111: private String defaultMaximizedStyleAttr;
112: private String defaultMaximizedWidthAttr;
113: private String defaultNameAttr;
114: private String defaultScrollingAttr;
115: private String defaultStyleAttr;
116: private String defaultWidthAttr;
117:
118: /**
119: * Portlet constructor.
120: */
121: public IFramePortlet() {
122: }
123:
124: /**
125: * Initialize portlet configuration.
126: */
127: public void init(PortletConfig config) throws PortletException {
128: super .init(config);
129:
130: String initParam = config
131: .getInitParameter(ENABLE_PREFERENCES_PARAM);
132: if (initParam == null) {
133: initParam = config
134: .getInitParameter(ENABLE_SOURCE_PREFERENCES_PARAM);
135: }
136: if (initParam != null) {
137: enablePreferences = (new Boolean(initParam)).booleanValue();
138: }
139:
140: defaultCustomSource = config
141: .getInitParameter(CUSTOM_SOURCE_PARAM);
142: defaultMaximizedCustomSource = config
143: .getInitParameter(MAXIMIZED_CUSTOM_SOURCE_PARAM);
144: defaultEditSource = config.getInitParameter(EDIT_SOURCE_PARAM);
145: defaultMaximizedEditSource = config
146: .getInitParameter(MAXIMIZED_EDIT_SOURCE_PARAM);
147: defaultHelpSource = config.getInitParameter(HELP_SOURCE_PARAM);
148: defaultMaximizedHelpSource = config
149: .getInitParameter(MAXIMIZED_HELP_SOURCE_PARAM);
150: defaultViewSource = config.getInitParameter(VIEW_SOURCE_PARAM);
151: defaultMaximizedViewSource = config
152: .getInitParameter(MAXIMIZED_VIEW_SOURCE_PARAM);
153:
154: defaultAlignAttr = getAttributeParam(config, ALIGN_ATTR_PARAM,
155: ALIGN_ATTR_DEFAULT);
156: defaultClassAttr = getAttributeParam(config, CLASS_ATTR_PARAM,
157: null);
158: defaultFrameBorderAttr = getAttributeParam(config,
159: FRAME_BORDER_ATTR_PARAM, FRAME_BORDER_ATTR_DEFAULT);
160: defaultHeightAttr = getAttributeParam(config,
161: HEIGHT_ATTR_PARAM, HEIGHT_ATTR_DEFAULT);
162: defaultIdAttr = getAttributeParam(config, ID_ATTR_PARAM, null);
163: defaultMarginHeightAttr = getAttributeParam(config,
164: MARGIN_HEIGHT_ATTR_PARAM, MARGIN_HEIGHT_ATTR_DEFAULT);
165: defaultMarginWidthAttr = getAttributeParam(config,
166: MARGIN_WIDTH_ATTR_PARAM, MARGIN_WIDTH_ATTR_DEFAULT);
167: defaultMaximizedHeightAttr = getAttributeParam(config,
168: MAXIMIZED_HEIGHT_ATTR_PARAM,
169: MAXIMIZED_HEIGHT_ATTR_DEFAULT);
170: defaultMaximizedScrollingAttr = getAttributeParam(config,
171: MAXIMIZED_SCROLLING_ATTR_PARAM,
172: MAXIMIZED_SCROLLING_ATTR_DEFAULT);
173: defaultMaximizedStyleAttr = getAttributeParam(config,
174: MAXIMIZED_STYLE_ATTR_PARAM, null);
175: defaultMaximizedWidthAttr = getAttributeParam(config,
176: MAXIMIZED_WIDTH_ATTR_PARAM,
177: MAXIMIZED_WIDTH_ATTR_DEFAULT);
178: defaultNameAttr = getAttributeParam(config, NAME_ATTR_PARAM,
179: null);
180: defaultScrollingAttr = getAttributeParam(config,
181: SCROLLING_ATTR_PARAM, SCROLLING_ATTR_DEFAULT);
182: defaultStyleAttr = getAttributeParam(config, STYLE_ATTR_PARAM,
183: null);
184: defaultWidthAttr = getAttributeParam(config, WIDTH_ATTR_PARAM,
185: WIDTH_ATTR_DEFAULT);
186: }
187:
188: /**
189: * Generate IFRAME with custom source.
190: */
191: public void doCustom(RenderRequest request, RenderResponse response)
192: throws PortletException, IOException {
193: // get IFRAME source
194: String source = null;
195: if (request.getWindowState().equals(WindowState.MAXIMIZED)) {
196: source = getPreferenceOrDefault(request,
197: MAXIMIZED_CUSTOM_SOURCE_PARAM,
198: defaultMaximizedCustomSource);
199: }
200: if (source == null) {
201: source = getPreferenceOrDefault(request,
202: CUSTOM_SOURCE_PARAM, defaultCustomSource);
203: }
204: if ((source == null)
205: && request.getWindowState().equals(
206: WindowState.MAXIMIZED)) {
207: source = getPreferenceOrDefault(request,
208: MAXIMIZED_VIEW_SOURCE_PARAM,
209: defaultMaximizedViewSource);
210: }
211: if (source == null) {
212: source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM,
213: defaultViewSource);
214: }
215: if (source == null) {
216: throw new PortletException(
217: "IFRAME source not specified for custom portlet mode.");
218: }
219:
220: // render IFRAME content
221: doIFrame(request, source, response);
222: }
223:
224: /**
225: * Generate IFRAME with edit source.
226: */
227: public void doEdit(RenderRequest request, RenderResponse response)
228: throws PortletException, IOException {
229: // get IFRAME source
230: String source = null;
231: if (request.getWindowState().equals(WindowState.MAXIMIZED)) {
232: source = getPreferenceOrDefault(request,
233: MAXIMIZED_EDIT_SOURCE_PARAM,
234: defaultMaximizedEditSource);
235: }
236: if (source == null) {
237: source = getPreferenceOrDefault(request, EDIT_SOURCE_PARAM,
238: defaultEditSource);
239: }
240: if ((source == null)
241: && request.getWindowState().equals(
242: WindowState.MAXIMIZED)) {
243: source = getPreferenceOrDefault(request,
244: MAXIMIZED_VIEW_SOURCE_PARAM,
245: defaultMaximizedViewSource);
246: }
247: if (source == null) {
248: source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM,
249: defaultViewSource);
250: }
251: if (source == null) {
252: throw new PortletException(
253: "IFRAME source not specified for edit portlet mode.");
254: }
255:
256: // render IFRAME content
257: doIFrame(request, source, response);
258: }
259:
260: /**
261: * Generate IFRAME with help source.
262: */
263: public void doHelp(RenderRequest request, RenderResponse response)
264: throws PortletException, IOException {
265: // get IFRAME source
266: String source = null;
267: if (request.getWindowState().equals(WindowState.MAXIMIZED)) {
268: source = getPreferenceOrDefault(request,
269: MAXIMIZED_HELP_SOURCE_PARAM,
270: defaultMaximizedHelpSource);
271: }
272: if (source == null) {
273: source = getPreferenceOrDefault(request, HELP_SOURCE_PARAM,
274: defaultHelpSource);
275: }
276: if ((source == null)
277: && request.getWindowState().equals(
278: WindowState.MAXIMIZED)) {
279: source = getPreferenceOrDefault(request,
280: MAXIMIZED_VIEW_SOURCE_PARAM,
281: defaultMaximizedViewSource);
282: }
283: if (source == null) {
284: source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM,
285: defaultViewSource);
286: }
287: if (source == null) {
288: throw new PortletException(
289: "IFRAME source not specified for help portlet mode.");
290: }
291:
292: // render IFRAME content
293: doIFrame(request, source, response);
294: }
295:
296: /**
297: * Generate IFRAME with view source.
298: */
299: public void doView(RenderRequest request, RenderResponse response)
300: throws PortletException, IOException {
301: // get IFRAME source
302: String source = null;
303: if (request.getWindowState().equals(WindowState.MAXIMIZED)) {
304: source = getPreferenceOrDefault(request,
305: MAXIMIZED_VIEW_SOURCE_PARAM,
306: defaultMaximizedViewSource);
307: }
308: if (source == null) {
309: source = getPreferenceOrDefault(request, VIEW_SOURCE_PARAM,
310: defaultViewSource);
311: }
312: if (source == null) {
313: throw new PortletException(
314: "IFRAME source not specified for view portlet mode.");
315: }
316:
317: // render IFRAME content
318: doIFrame(request, source, response);
319: }
320:
321: /**
322: * Render IFRAME content
323: */
324: protected void doIFrame(RenderRequest request, String sourceAttr,
325: RenderResponse response) throws IOException {
326: // generate HTML IFRAME content
327: StringBuffer content = new StringBuffer(4096);
328:
329: // fix JS2-349
330: content
331: .append("<TABLE CLASS='iframePortletTableContainer' WIDTH='100%'><TBODY CLASS='iframePortletTbodyContainer'><TR><TD>");
332:
333: content.append("<IFRAME");
334: content.append(" SRC=\"").append(sourceAttr).append("\"");
335: String alignAttr = getPreferenceOrDefault(request,
336: ALIGN_ATTR_PARAM, defaultAlignAttr);
337: if (alignAttr != null) {
338: content.append(" ALIGN=\"").append(alignAttr).append("\"");
339: }
340: String classAttr = getPreferenceOrDefault(request,
341: CLASS_ATTR_PARAM, defaultClassAttr);
342: if (classAttr != null) {
343: content.append(" CLASS=\"").append(classAttr).append("\"");
344: }
345: String frameBorderAttr = getPreferenceOrDefault(request,
346: FRAME_BORDER_ATTR_PARAM, defaultFrameBorderAttr);
347: if (frameBorderAttr != null) {
348: content.append(" FRAMEBORDER=\"").append(frameBorderAttr)
349: .append("\"");
350: }
351: String idAttr = getPreferenceOrDefault(request, ID_ATTR_PARAM,
352: defaultIdAttr);
353: if (idAttr != null) {
354: content.append(" ID=\"").append(idAttr).append("\"");
355: }
356: String marginHeightAttr = getPreferenceOrDefault(request,
357: MARGIN_HEIGHT_ATTR_PARAM, defaultMarginHeightAttr);
358: if (marginHeightAttr != null) {
359: content.append(" MARGINHEIGHT=\"").append(marginHeightAttr)
360: .append("\"");
361: }
362: String marginWidthAttr = getPreferenceOrDefault(request,
363: MARGIN_WIDTH_ATTR_PARAM, defaultMarginWidthAttr);
364: if (marginWidthAttr != null) {
365: content.append(" MARGINWIDTH=\"").append(marginWidthAttr)
366: .append("\"");
367: }
368: String nameAttr = getPreferenceOrDefault(request,
369: NAME_ATTR_PARAM, defaultNameAttr);
370: if (nameAttr != null) {
371: content.append(" NAME=\"").append(nameAttr).append("\"");
372: }
373: if (request.getWindowState().equals(WindowState.MAXIMIZED)) {
374: String maximizedHeightAttr = getPreferenceOrDefault(
375: request, MAXIMIZED_HEIGHT_ATTR_PARAM,
376: defaultMaximizedHeightAttr);
377: if (maximizedHeightAttr == null) {
378: maximizedHeightAttr = getPreferenceOrDefault(request,
379: HEIGHT_ATTR_PARAM, defaultHeightAttr);
380: }
381: if (maximizedHeightAttr != null) {
382: content.append(" HEIGHT=\"")
383: .append(maximizedHeightAttr).append("\"");
384: }
385: String maximizedScrollingAttr = getPreferenceOrDefault(
386: request, MAXIMIZED_SCROLLING_ATTR_PARAM,
387: defaultMaximizedScrollingAttr);
388: if (maximizedScrollingAttr == null) {
389: maximizedScrollingAttr = getPreferenceOrDefault(
390: request, SCROLLING_ATTR_PARAM,
391: defaultScrollingAttr);
392: }
393: if (maximizedScrollingAttr != null) {
394: content.append(" SCROLLING=\"").append(
395: maximizedScrollingAttr).append("\"");
396: }
397: String maximizedStyleAttr = getPreferenceOrDefault(request,
398: MAXIMIZED_STYLE_ATTR_PARAM,
399: defaultMaximizedStyleAttr);
400: if (maximizedStyleAttr == null) {
401: maximizedStyleAttr = getPreferenceOrDefault(request,
402: STYLE_ATTR_PARAM, defaultStyleAttr);
403: }
404: if (maximizedStyleAttr != null) {
405: content.append(" STYLE=\"").append(maximizedStyleAttr)
406: .append("\"");
407: }
408: String maximizedWidthAttr = getPreferenceOrDefault(request,
409: MAXIMIZED_WIDTH_ATTR_PARAM,
410: defaultMaximizedWidthAttr);
411: if (maximizedWidthAttr == null) {
412: maximizedWidthAttr = getPreferenceOrDefault(request,
413: WIDTH_ATTR_PARAM, defaultWidthAttr);
414: }
415: if (maximizedWidthAttr != null) {
416: content.append(" WIDTH=\"").append(maximizedWidthAttr)
417: .append("\"");
418: }
419: } else {
420: String heightAttr = getPreferenceOrDefault(request,
421: HEIGHT_ATTR_PARAM, defaultHeightAttr);
422: if (heightAttr != null) {
423: content.append(" HEIGHT=\"").append(heightAttr).append(
424: "\"");
425: }
426: String scrollingAttr = getPreferenceOrDefault(request,
427: SCROLLING_ATTR_PARAM, defaultScrollingAttr);
428: if (scrollingAttr != null) {
429: content.append(" SCROLLING=\"").append(scrollingAttr)
430: .append("\"");
431: }
432: String styleAttr = getPreferenceOrDefault(request,
433: STYLE_ATTR_PARAM, defaultStyleAttr);
434: if (styleAttr != null) {
435: content.append(" STYLE=\"").append(styleAttr).append(
436: "\"");
437: }
438: String widthAttr = getPreferenceOrDefault(request,
439: WIDTH_ATTR_PARAM, defaultWidthAttr);
440: if (widthAttr != null) {
441: content.append(" WIDTH=\"").append(widthAttr).append(
442: "\"");
443: }
444: }
445: content.append(">");
446: content.append("<P STYLE=\"textAlign:center\"><A HREF=\"")
447: .append(sourceAttr).append("\">").append(sourceAttr)
448: .append("</A></P>");
449: content.append("</IFRAME>");
450:
451: // end fix JS2-349
452: content.append("</TD></TR></TBODY></TABLE>");
453:
454: // set required content type and write HTML IFRAME content
455: response.setContentType("text/html");
456: response.getWriter().print(content.toString());
457: }
458:
459: /**
460: * Get IFRAME attribute parameter.
461: */
462: private String getAttributeParam(PortletConfig config, String name,
463: String defaultValue) {
464: String value = config.getInitParameter(name);
465: if (value == null) {
466: value = defaultValue;
467: }
468: return (((value != null) && (value.length() > 0) && !value
469: .equalsIgnoreCase("none")) ? value : null);
470: }
471:
472: /**
473: * Get IFRAME preference value if enabled.
474: */
475: private String getPreferenceOrDefault(RenderRequest request,
476: String name, String defaultValue) {
477: if (!enablePreferences) {
478: return defaultValue;
479: }
480: PortletPreferences prefs = request.getPreferences();
481: return ((prefs != null) ? prefs.getValue(name, defaultValue)
482: : defaultValue);
483: }
484: }
|