Author: woonsan
Date: Wed Nov 4 14:25:27 2009
New Revision: 832756
URL: http://svn.apache.org/viewvc?rev=832756&view=rev
Log:
APA-21: Adding a generic web widget iframe portlet.
TODO: Add a demo in apa-demo.
Added:
portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/
portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/WebWidgetIFramePortlet.java
(with props)
Modified:
portals/applications/gems/trunk/pom.xml
Modified: portals/applications/gems/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/portals/applications/gems/trunk/pom.xml?rev=832756&r1=832755&r2=832756&view=diff
==============================================================================
--- portals/applications/gems/trunk/pom.xml (original)
+++ portals/applications/gems/trunk/pom.xml Wed Nov 4 14:25:27 2009
@@ -40,7 +40,7 @@
<org.apache.portals.bridges.common.version>1.0.4</org.apache.portals.bridges.common.version>
<org.apache.portals.bridges.velocity.version>1.0.4</org.apache.portals.bridges.velocity.version>
<commons-httpclient.version>3.0.1</commons-httpclient.version>
- <commons-lang.version>2.1</commons-lang.version>
+ <commons-lang.version>2.4</commons-lang.version>
<slf4j.version>1.5.6</slf4j.version>
<spring.version>2.5.6</spring.version>
</properties>
Added:
portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/WebWidgetIFramePortlet.java
URL:
http://svn.apache.org/viewvc/portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/WebWidgetIFramePortlet.java?rev=832756&view=auto
==============================================================================
---
portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/WebWidgetIFramePortlet.java
(added)
+++
portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/WebWidgetIFramePortlet.java
Wed Nov 4 14:25:27 2009
@@ -0,0 +1,276 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.gems.webwidget;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletConfig;
+import javax.portlet.PortletException;
+import javax.portlet.PortletPreferences;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import javax.portlet.ResourceRequest;
+import javax.portlet.ResourceResponse;
+import javax.portlet.WindowState;
+
+import org.apache.commons.collections.keyvalue.DefaultMapEntry;
+import org.apache.commons.lang.StringUtils;
+import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
+
+/**
+ * WebWidgetIFramePortlet
+ *
+ * @version $Id$
+ */
+public class WebWidgetIFramePortlet extends GenericVelocityPortlet
+{
+
+ private static final String DEFAULT_WEB_WIDGET_CODE =
+ "<script
src=\"http://www.gmodules.com/ig/ifr?url=http://www.google.com/ig/modules/wikipedia.xml&up_mylang=en&synd=open&w=320&h=46&title=Wikipedia&lang=en&country=ALL&border=%23ffffff%7C3px%2C1px+solid+%23999999&output=js\"></script>";
+
+ private Map<String, String> attributes = new HashMap<String, String>();
+
+ private Map<String, String> maxAttributes = new HashMap<String, String>();
+
+ @Override
+ public void init(PortletConfig config) throws PortletException
+ {
+ super.init(config);
+
+ attributes.put("TITLE", "");
+ attributes.put("ALIGN", "BOTTOM");
+ attributes.put("CLASS", "");
+ attributes.put("FRAMEBORDER", "0");
+ attributes.put("ID", "");
+ attributes.put("MARGINHEIGHT", "0");
+ attributes.put("MARGINWIDTH", "0");
+ attributes.put("NAME", "");
+
+ attributes.put("HEIGHT", "");
+ attributes.put("WIDTH", "100%");
+ attributes.put("SCROLLING", "NO");
+ attributes.put("STYLE", "");
+
+ attributes.put("WIDGETCODE", DEFAULT_WEB_WIDGET_CODE);
+
+ maxAttributes.put("HEIGHT", "800");
+ maxAttributes.put("WIDTH", "100%");
+ maxAttributes.put("SCROLLING", "AUTO");
+ maxAttributes.put("STYLE", "");
+ }
+
+ private String getAttributePreference(PortletPreferences prefs, String
attribute)
+ {
+ return this.getMappedAttributePreference(prefs, attribute, attributes);
+ }
+
+ private String getMaxAttributePreference(PortletPreferences prefs, String
attribute)
+ {
+ return this.getMappedAttributePreference(prefs, "MAX-" + attribute,
maxAttributes);
+ }
+
+ private String getMappedAttributePreference(PortletPreferences prefs,
String attribute, Map<String, String> map)
+ {
+ return prefs.getValue(attribute, map.get(attribute));
+ }
+
+ private void appendAttribute(PortletPreferences prefs, StringBuilder
content, String attribute, Map<String, String> map)
+ {
+ String value;
+
+ if (map == maxAttributes)
+ value = getMaxAttributePreference(prefs, attribute);
+ else
+ value = getAttributePreference(prefs, attribute);
+
+ if (value == null || value.length() == 0) { return; }
+ content.append("
").append(attribute).append("=\"").append(value).append("\"");
+ }
+
+ private void appendAttribute(PortletPreferences prefs, StringBuilder
content, String attribute)
+ {
+ appendAttribute(prefs, content, attribute, attributes);
+ }
+
+ private void appendMaxAttribute(PortletPreferences prefs, StringBuilder
content, String attribute)
+ {
+ appendAttribute(prefs, content, attribute, maxAttributes);
+ }
+
+ @Override
+ public void doView(RenderRequest request, RenderResponse response) throws
PortletException, IOException
+ {
+ String viewPage = (String) request.getAttribute(PARAM_VIEW_PAGE);
+
+ if (viewPage != null)
+ {
+ super.doView(request, response);
+ }
+ else
+ {
+ doIFrame(request, response);
+ }
+ }
+
+ @Override
+ public void setupPreferencesEdit(RenderRequest request, RenderResponse
response)
+ {
+ PortletPreferences prefs = request.getPreferences();
+
+ String editablePrefNames = getAttributePreference(prefs,
"EDITABLEPREFS");
+
+ if (StringUtils.isBlank(editablePrefNames))
+ {
+ super.setupPreferencesEdit(request, response);
+ }
+ else
+ {
+ List<DefaultMapEntry> prefEntryList = new
ArrayList<DefaultMapEntry>();
+ Map<String, String> prefsMap = new HashMap<String, String>();
+ String [] prefNames = StringUtils.split(editablePrefNames, ",");
+ String [] emptyValues = {};
+
+ for (String prefName : prefNames)
+ {
+ prefName = prefName.trim();
+ String [] prefValues = prefs.getValues(prefName, emptyValues);
+ prefsMap.put(prefName, prefValues.length == 0 ? "" :
prefValues[0]);
+ prefEntryList.add(new DefaultMapEntry(prefName, prefValues));
+ }
+
+ getContext(request, response).put("prefs",
prefEntryList.iterator());
+ getContext(request, response).put("prefsMap", prefsMap);
+ }
+ }
+
+ @Override
+ public void doEdit(RenderRequest request, RenderResponse response) throws
PortletException, IOException
+ {
+ response.setContentType("text/html");
+ doPreferencesEdit(request, response);
+ }
+
+ @Override
+ public void doHelp(RenderRequest request, RenderResponse response) throws
PortletException, IOException
+ {
+ super.doHelp(request, response);
+ }
+
+ @Override
+ public void doCustom(RenderRequest request, RenderResponse response)
throws PortletException, IOException
+ {
+ super.doCustom(request, response);
+ }
+
+ /**
+ * Render IFRAME content
+ */
+ protected void doIFrame(RenderRequest request, RenderResponse response)
throws IOException
+ {
+ PortletPreferences prefs = request.getPreferences();
+ String source = response.createResourceURL().toString();
+ // generate HTML IFRAME content
+ StringBuilder content = new StringBuilder(256);
+
+ // fix JS2-349
+ content.append("<TABLE CLASS='iframePortletTableContainer'
WIDTH='100%'><TBODY CLASS='iframePortletTbodyContainer'><TR><TD>");
+
+ content.append("<IFRAME");
+
+ // special case source
+ content.append("
").append("SRC").append("=\"").append(source).append("\"");
+
+ appendAttribute(prefs, content, "ALIGN");
+ appendAttribute(prefs, content, "CLASS");
+ appendAttribute(prefs, content, "FRAMEBORDER");
+ appendAttribute(prefs, content, "ID");
+ appendAttribute(prefs, content, "MARGINHEIGHT");
+ appendAttribute(prefs, content, "MARGINWIDTH");
+ appendAttribute(prefs, content, "NAME");
+
+ if (request.getWindowState().equals(WindowState.MAXIMIZED))
+ {
+ appendMaxAttribute(prefs, content, "HEIGHT");
+ appendMaxAttribute(prefs, content, "WIDTH");
+ appendMaxAttribute(prefs, content, "SCROLLING");
+ appendMaxAttribute(prefs, content, "STYLE");
+ }
+ else
+ {
+ appendAttribute(prefs, content, "HEIGHT");
+ appendAttribute(prefs, content, "WIDTH");
+ appendAttribute(prefs, content, "SCROLLING");
+ appendAttribute(prefs, content, "STYLE");
+ }
+
+ content.append(">");
+ content.append("<P STYLE=\"textAlign:center\"><A
HREF=\"").append(source).append("\">").append(source).append(
+ "</A></P>");
+ content.append("</IFRAME>");
+
+ // end fix JS2-349
+ content.append("</TD></TR></TBODY></TABLE>");
+
+ // set required content type and write HTML IFRAME content
+ response.setContentType("text/html");
+ response.getWriter().print(content.toString());
+ }
+
+ /**
+ * Save the prefs
+ */
+ @Override
+ public void processAction(ActionRequest request, ActionResponse
actionResponse) throws PortletException,
+ IOException
+ {
+ processPreferencesAction(request, actionResponse);
+ }
+
+ @Override
+ protected String getTitle(RenderRequest request)
+ {
+ String title = getAttributePreference(request.getPreferences(),
"TITLE");
+
+ if (!StringUtils.isEmpty(title))
+ {
+ return title;
+ }
+ else
+ {
+ return super.getTitle(request);
+ }
+ }
+
+ @Override
+ public void serveResource(ResourceRequest request, ResourceResponse
response) throws PortletException, IOException
+ {
+ String widgetCode = getAttributePreference(request.getPreferences(),
"WIDGETCODE");
+
+ if (!StringUtils.isBlank(widgetCode))
+ {
+ response.setContentType("text/html");
+ response.getWriter().print(widgetCode);
+ }
+ }
+
+}
Propchange:
portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/WebWidgetIFramePortlet.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/WebWidgetIFramePortlet.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/applications/gems/trunk/src/main/java/org/apache/portals/gems/webwidget/WebWidgetIFramePortlet.java
------------------------------------------------------------------------------
svn:mime-type = text/plain