Author: julien(a)jboss.com
Date: 2007-07-06 10:04:24 -0400 (Fri, 06 Jul 2007)
New Revision: 7685
Modified:
trunk/common/src/main/org/jboss/portal/common/net/URLTools.java
trunk/common/src/main/org/jboss/portal/common/xml/XMLTools.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGWidget.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetInfo.java
trunk/widget/src/main/org/jboss/portal/widget/google/provider/GGProvider.java
trunk/widget/src/main/org/jboss/portal/widget/google/provider/GGWidgetInfoBuilder.java
trunk/widget/src/resources/widget-war/WEB-INF/jsp/edit.jsp
Log:
- honour URLTools.performGet that must returns null when no answer is available
- Use the RSS mode of google gadgets for more reliable catalog queries
Modified: trunk/common/src/main/org/jboss/portal/common/net/URLTools.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/net/URLTools.java 2007-07-06 13:15:55
UTC (rev 7684)
+++ trunk/common/src/main/org/jboss/portal/common/net/URLTools.java 2007-07-06 14:04:24
UTC (rev 7685)
@@ -81,7 +81,7 @@
* @param url the URL the URL of the resource
* @param soTimeoutMillis the socket connection timeout in millis
* @param connTimeoutMillis the connection timeout in millis
- * @return
+ * @return the retrieved byte array
*/
public static byte[] performGET(URL url, int soTimeoutMillis, int connTimeoutMillis)
throws IllegalArgumentException, IOException
{
@@ -135,8 +135,11 @@
method.releaseConnection();
}
}
+
+ //
+ return null;
}
- throw new IllegalArgumentException("Protocol " + url.getProtocol() +
" not accepted");
+ throw new IllegalArgumentException("Protocol " + url.getProtocol() +
" not accepted for url " + url);
}
/**
Modified: trunk/common/src/main/org/jboss/portal/common/xml/XMLTools.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/xml/XMLTools.java 2007-07-06 13:15:55
UTC (rev 7684)
+++ trunk/common/src/main/org/jboss/portal/common/xml/XMLTools.java 2007-07-06 14:04:24
UTC (rev 7685)
@@ -421,10 +421,20 @@
Element childElt = (Element)node;
//
- if ((uri == null || uri.equals(childElt.getNamespaceURI())) &&
childElt.getLocalName().equals(name))
+ if (uri == null)
{
- result.add(childElt);
+ if (childElt.getTagName().equals(name))
+ {
+ result.add(childElt);
+ }
}
+ else if (uri.equals(childElt.getNamespaceURI()))
+ {
+ if (childElt.getLocalName().equals(name))
+ {
+ result.add(childElt);
+ }
+ }
}
}
return result;
Modified: trunk/widget/src/main/org/jboss/portal/widget/google/GGWidget.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGWidget.java 2007-07-06 13:15:55
UTC (rev 7684)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGWidget.java 2007-07-06 14:04:24
UTC (rev 7685)
@@ -26,15 +26,11 @@
import org.jboss.portal.common.util.UUIDGenerator;
import org.jboss.portal.common.text.FastURLEncoder;
import org.jboss.portal.widget.Widget;
-import org.jboss.portal.widget.google.provider.GGWidgetInfoBuilder;
import java.util.Iterator;
import java.util.Map;
import java.net.URL;
-import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
-import edu.emory.mathcs.backport.java.util.concurrent.Callable;
-
/**
* A Google Gadget widget.
* For more details please see the <a
href="http://www.google.com/apis/gadgets/reference.html">API Developer
Guide</a>.
@@ -58,51 +54,13 @@
private final String id;
/** . */
- private final LocalizedString title;
+ private final GGWidgetInfo info;
- /** . */
- private final LocalizedString description;
-
- /** . */
- private final int width;
-
- /** . */
- private final int height;
-
- /** . */
- private final FutureTask future;
-
- public GGWidget(URL url,
- LocalizedString title,
- LocalizedString description,
- int width,
- int height)
+ public GGWidget(URL url, GGWidgetInfo info)
{
- Callable callable = new Callable()
- {
- public Object call() throws Exception
- {
- try
- {
- GGWidgetInfoBuilder builder = new GGWidgetInfoBuilder(GGWidget.this.url);
- return builder.create();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return null;
- }
- }
- };
-
- //
this.id = url.toString();
this.url = url;
- this.title = title;
- this.description = description;
- this.width = width;
- this.height = height;
- this.future = new FutureTask(callable);
+ this.info = info;
}
public String getId()
@@ -112,27 +70,17 @@
public GGWidgetInfo getInfo()
{
- try
- {
- return (GGWidgetInfo)future.get();
- }
- catch (Exception e)
- {
- e.printStackTrace();
-
- //
- return null;
- }
+ return info;
}
public LocalizedString getTitle()
{
- return title;
+ return getInfo().getTitle();
}
public LocalizedString getDescription()
{
- return description;
+ return getInfo().getDescription();
}
public String render(Map parameters)
@@ -140,20 +88,21 @@
String id = generator.generateKey();
StringBuffer tmp = new
StringBuffer("http://gmodules.com/ig/ifr?url=").append(url);
- // Generate base params
- tmp.append("&synd=open");
- tmp.append("&w=").append(width);
- tmp.append("&h=").append(height);
-
tmp.append("&title=").append(FastURLEncoder.DEFAULT_ENCODER.encode(title.getDefaultString()));
- tmp.append("&border=%23ffffff%7C3px%2C1px+none+%23999999"); //
"&border=%23ffffff%7C3px%2C1px+solid+%23999999"
- tmp.append("&output=js");
-
//
GGWidgetInfo info = getInfo();
//
if (info != null)
{
+ // Generate base params
+ tmp.append("&synd=open");
+ tmp.append("&w=").append(info.getWidth());
+ tmp.append("&h=").append(info.getHeight());
+
tmp.append("&title=").append(FastURLEncoder.DEFAULT_ENCODER.encode(info.getTitle().getDefaultString()));
+ tmp.append("&border=%23ffffff%7C3px%2C1px+none+%23999999"); //
"&border=%23ffffff%7C3px%2C1px+solid+%23999999"
+ tmp.append("&output=js");
+
+ //
GGPreferencesInfo prefsInfo = info.getPreferences();
// Append default values
Modified: trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetInfo.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetInfo.java 2007-07-06
13:15:55 UTC (rev 7684)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetInfo.java 2007-07-06
14:04:24 UTC (rev 7685)
@@ -22,6 +22,8 @@
******************************************************************************/
package org.jboss.portal.widget.google;
+import org.jboss.portal.common.i18n.LocalizedString;
+
/**
* Meta data retrieved from the XML file.
*
@@ -32,10 +34,31 @@
{
/** . */
+ private final LocalizedString title;
+
+ /** . */
+ private final LocalizedString description;
+
+ /** . */
+ private final int width;
+
+ /** . */
+ private final int height;
+
+ /** . */
private final GGPreferencesInfo preferences;
- public GGWidgetInfo(GGPreferencesInfo preferences)
+ public GGWidgetInfo(
+ LocalizedString title,
+ LocalizedString description,
+ int width,
+ int height,
+ GGPreferencesInfo preferences)
{
+ this.title = title;
+ this.description = description;
+ this.width = width;
+ this.height = height;
this.preferences = preferences;
}
@@ -43,4 +66,24 @@
{
return preferences;
}
+
+ public LocalizedString getTitle()
+ {
+ return title;
+ }
+
+ public LocalizedString getDescription()
+ {
+ return description;
+ }
+
+ public int getWidth()
+ {
+ return width;
+ }
+
+ public int getHeight()
+ {
+ return height;
+ }
}
Modified: trunk/widget/src/main/org/jboss/portal/widget/google/provider/GGProvider.java
===================================================================
---
trunk/widget/src/main/org/jboss/portal/widget/google/provider/GGProvider.java 2007-07-06
13:15:55 UTC (rev 7684)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/provider/GGProvider.java 2007-07-06
14:04:24 UTC (rev 7685)
@@ -35,9 +35,12 @@
import java.net.MalformedURLException;
import java.util.Collections;
import java.util.Collection;
+import java.util.Iterator;
+import java.util.ArrayList;
import org.jboss.portal.widget.google.GGWidget;
import org.jboss.portal.widget.google.GGWidgetCategoryInfo;
+import org.jboss.portal.widget.google.GGWidgetInfo;
import org.jboss.portal.common.util.CollectionBuilder;
/**
@@ -57,9 +60,6 @@
private final ConcurrentMap searches = new ConcurrentHashMap();
/** . */
- private final Context context = new Context();
-
- /** . */
private ExecutorService executor;
/** . */
@@ -91,6 +91,9 @@
return CATEGORIES;
}
+ /**
+ * Returns a specified widget.
+ */
public GGWidget getWidget(String id)
{
if (id == null)
@@ -102,49 +105,69 @@
try
{
URL url = new URL(id);
- return context.getWidgetEntry(url).getWidget();
+ return getWidget(url);
}
catch (MalformedURLException e)
{
e.printStackTrace();
+
+ //
return null;
}
}
- public GGQueryResult search(GGQuery query)
+ /**
+ * Returns a specified widget.
+ */
+ private GGWidget getWidget(URL url)
{
+ GGProvider.GGWidgetEntry tmp = new GGProvider.GGWidgetEntry(url);
+ GGProvider.GGWidgetEntry entry = (GGProvider.GGWidgetEntry)entries.putIfAbsent(url,
tmp);
+ if (entry == null)
+ {
+ entry = tmp;
+ entry.retrieve();
+ }
+ return entry.getWidget();
+ }
+
+ /**
+ * Returns a collection of widgets from a query.
+ */
+ public Collection search(GGQuery query)
+ {
if (query == null)
{
throw new IllegalArgumentException();
}
//
- QueryEntry tmp = new QueryEntry(query);
- QueryEntry entry = (QueryEntry)searches.putIfAbsent(query, tmp);
+ GGQueryEntry tmp = new GGQueryEntry(query);
+ GGQueryEntry entry = (GGQueryEntry)searches.putIfAbsent(query, tmp);
if (entry == null)
{
entry = tmp;
entry.retrieve();
}
- return entry.getResult();
- }
+ GGQueryResult result = entry.getResult();
- public class Context
- {
- GGProvider.GGWidgetEntry getWidgetEntry(URL url)
+ //
+ Collection widgets = new ArrayList();
+ for (Iterator i = result.entries();i.hasNext();)
{
- GGProvider.GGWidgetEntry tmp = new GGProvider.GGWidgetEntry(url);
- GGProvider.GGWidgetEntry entry =
(GGProvider.GGWidgetEntry)entries.putIfAbsent(url, tmp);
- if (entry == null)
+ GGQueryResultEntry resultEntry = (GGQueryResultEntry)i.next();
+ GGWidget widget = getWidget(resultEntry.getURL());
+ if (widget != null)
{
- entry = tmp;
- entry.retrieve();
+ widgets.add(widget);
}
- return entry;
}
+
+ //
+ return widgets;
}
- public class GGWidgetEntry implements Callable
+ private class GGWidgetEntry implements Callable
{
/** The widget id. */
@@ -173,26 +196,28 @@
catch (Exception e)
{
e.printStackTrace();
+
+ //
return null;
}
}
- public Object call()
+ public Object call() throws Exception
{
- try
+ GGWidgetInfoBuilder builder = new GGWidgetInfoBuilder(url);
+ GGWidgetInfo info = builder.create();
+ if (info != null)
{
- GGWidgetInfoBuilder builder = new GGWidgetInfoBuilder(url);
- return builder.create();
+ return new GGWidget(url, info);
}
- catch (Exception e)
+ else
{
- e.printStackTrace();
return null;
}
}
}
- private class QueryEntry implements Callable
+ private class GGQueryEntry implements Callable
{
/** . */
@@ -201,7 +226,7 @@
/** . */
private FutureTask future;
- public QueryEntry(GGQuery query)
+ public GGQueryEntry(GGQuery query)
{
this.query = query;
this.future = new FutureTask(this);
@@ -214,30 +239,31 @@
public GGQueryResult getResult()
{
+ GGQueryResult result = null;
+
+ //
try
{
- return (GGQueryResult)future.get();
+ result = (GGQueryResult)future.get();
}
catch (Exception e)
{
- e.printStackTrace(); //To change body of catch statement use File | Settings
| File Templates.
- return new GGQueryResult(Collections.EMPTY_LIST);
+ e.printStackTrace();
}
- }
- public Object call()
- {
- try
+ //
+ if (result == null)
{
- return new GGQueryResultBuilder(query).build();
+ result = new GGQueryResult(Collections.EMPTY_LIST);
}
- catch (MalformedURLException e)
- {
- e.printStackTrace();
- // todo
- return null;
- }
+ //
+ return result;
}
+
+ public Object call() throws MalformedURLException
+ {
+ return new GGQueryResultBuilder(query).build();
+ }
}
}
Modified:
trunk/widget/src/main/org/jboss/portal/widget/google/provider/GGWidgetInfoBuilder.java
===================================================================
---
trunk/widget/src/main/org/jboss/portal/widget/google/provider/GGWidgetInfoBuilder.java 2007-07-06
13:15:55 UTC (rev 7684)
+++
trunk/widget/src/main/org/jboss/portal/widget/google/provider/GGWidgetInfoBuilder.java 2007-07-06
14:04:24 UTC (rev 7685)
@@ -25,7 +25,6 @@
import org.jboss.portal.common.xml.XMLTools;
import org.jboss.portal.common.i18n.LocalizedString;
import org.jboss.portal.common.net.URLTools;
-import org.jboss.portal.common.concurrent.loader.ObjectLoader;
import org.jboss.portal.widget.google.type.DataType;
import org.jboss.portal.widget.google.type.StringType;
import org.jboss.portal.widget.google.type.LocationType;
@@ -33,7 +32,6 @@
import org.jboss.portal.widget.google.type.BoolType;
import org.jboss.portal.widget.google.type.ListType;
import org.jboss.portal.widget.google.type.EnumType;
-import org.jboss.portal.widget.google.GGWidget;
import org.jboss.portal.widget.google.GGPreferenceInfo;
import org.jboss.portal.widget.google.GGPreferencesInfo;
import org.jboss.portal.widget.google.GGWidgetInfo;
@@ -67,6 +65,8 @@
{
throw new IllegalArgumentException("No null URL");
}
+
+ //
this.url = url;
}
@@ -75,15 +75,24 @@
return url;
}
+ /**
+ * Build the widget info from the url or return null if it is not possible.
+ * @return the widget info
+ * @throws Exception
+ */
public GGWidgetInfo create() throws Exception
{
- DocumentBuilderFactory factory = XMLTools.getDocumentBuilderFactory();
- DocumentBuilder builder = factory.newDocumentBuilder();
byte[] bytes = obtainWidget(url);
+
+ // No response was obtained
if (bytes == null)
{
- throw new Exception("Cannot retrieve document " + url);
+ return null;
}
+
+ //
+ DocumentBuilderFactory factory = XMLTools.getDocumentBuilderFactory();
+ DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(bytes));
Element moduleElt = doc.getDocumentElement();
@@ -165,28 +174,26 @@
prefsInfo = new GGPreferencesInfo(tmp);
}
- //
- return new GGWidgetInfo(prefsInfo);
-// Iterator modulePrefsEltIterator = XMLTools.getChildrenIterator(moduleElt,
"ModulePrefs");
-// if (modulePrefsEltIterator.hasNext())
-// {
-// Element modulePrefsElt = (Element)modulePrefsEltIterator.next();
-// String titleAttr = modulePrefsElt.getAttribute("title");
-// String descriptionAttr =
modulePrefsElt.getAttribute("description");
-// String widthAttr = modulePrefsElt.getAttribute("width");
-// String heightAttr = modulePrefsElt.getAttribute("height");
-//
-// //
-// LocalizedString title = titleAttr != null ? new LocalizedString(titleAttr,
Locale.ENGLISH) : null;
-// LocalizedString description = descriptionAttr != null ? new
LocalizedString(descriptionAttr, Locale.ENGLISH) : null;
-// int width = (widthAttr != null && widthAttr.length() > 0) ?
Integer.parseInt(widthAttr) : 320;
-// int height = (heightAttr != null && heightAttr.length() > 0) ?
Integer.parseInt(heightAttr) : 200;
-// return new GGWidget(url.toString(), prefsInfo, title, description, width,
height);
-// }
-// else
-// {
-// throw new Exception(); // Basic for now
-// }
+ Iterator modulePrefsEltIterator = XMLTools.getChildrenIterator(moduleElt,
"ModulePrefs");
+ if (modulePrefsEltIterator.hasNext())
+ {
+ Element modulePrefsElt = (Element)modulePrefsEltIterator.next();
+ String titleAttr = modulePrefsElt.getAttribute("title");
+ String descriptionAttr = modulePrefsElt.getAttribute("description");
+ String widthAttr = modulePrefsElt.getAttribute("width");
+ String heightAttr = modulePrefsElt.getAttribute("height");
+
+ //
+ LocalizedString title = titleAttr != null ? new LocalizedString(titleAttr,
Locale.ENGLISH) : null;
+ LocalizedString description = descriptionAttr != null ? new
LocalizedString(descriptionAttr, Locale.ENGLISH) : null;
+ int width = (widthAttr != null && widthAttr.length() > 0) ?
Integer.parseInt(widthAttr) : 320;
+ int height = (heightAttr != null && heightAttr.length() > 0) ?
Integer.parseInt(heightAttr) : 200;
+ return new GGWidgetInfo(title, description, width, height, prefsInfo);
+ }
+ else
+ {
+ throw new Exception(); // Basic for now
+ }
}
protected byte[] obtainWidget(URL url) throws Exception
Modified: trunk/widget/src/resources/widget-war/WEB-INF/jsp/edit.jsp
===================================================================
--- trunk/widget/src/resources/widget-war/WEB-INF/jsp/edit.jsp 2007-07-06 13:15:55 UTC
(rev 7684)
+++ trunk/widget/src/resources/widget-war/WEB-INF/jsp/edit.jsp 2007-07-06 14:04:24 UTC
(rev 7685)
@@ -9,6 +9,7 @@
<%@ page import="org.jboss.portal.widget.google.type.EnumType" %>
<%@ page import="javax.portlet.PortletURL" %>
<%@ page import="org.jboss.portal.common.util.IteratorStatus" %>
+<%@ page import="java.util.Collection" %>
<%@ page language="java"%>
<%@ taglib
uri="http://java.sun.com/portlet" prefix="p" %>
<%@ page isELIgnored="false" %>
@@ -44,7 +45,7 @@
// Compute query
GGQuery query = new GGQuery(0, catTerm, queryTerm);
- GGQueryResult result = provider.search(query);
+ Collection widgets = provider.search(query);
%>
@@ -69,7 +70,7 @@
<p>
<table style="width:100%;border:1px solid" cellspacing="0"
cellpadding="0">
<%
- for (IteratorStatus i = new IteratorStatus(result.entries()); i.hasNext();)
+ for (IteratorStatus i = new IteratorStatus(widgets.iterator()); i.hasNext();)
{
GGWidget widget = (GGWidget)i.next();
boolean selected = selWidget != null &&
selWidget.getId().equals(widget.getId());
@@ -80,7 +81,7 @@
selectURL.setParameter("content.uri", widget.getId());
// Set default parametrization state
- for (Iterator j = widget.getPreferencesInfo().getPreferences().iterator();
j.hasNext();)
+ for (Iterator j = widget.getInfo().getPreferences().getPreferences().iterator();
j.hasNext();)
{
GGPreferenceInfo prefInfo = (GGPreferenceInfo)j.next();
String paramName = "content.param." + prefInfo.getName();
@@ -105,7 +106,7 @@
</p>
<%
- if (selWidget != null &&
!selWidget.getPreferencesInfo().getPreferences().isEmpty())
+ if (selWidget != null &&
!selWidget.getInfo().getPreferences().getPreferences().isEmpty())
{
PortletURL selectURL = renderResponse.createActionURL();
@@ -120,7 +121,7 @@
<p>
<form action="<%= selectURL %>" method="post">
<%
- for (Iterator i = selWidget.getPreferencesInfo().getPreferences().iterator();
i.hasNext();)
+ for (Iterator i =
selWidget.getInfo().getPreferences().getPreferences().iterator(); i.hasNext();)
{
GGPreferenceInfo prefInfo = (GGPreferenceInfo)i.next();
if (prefInfo.getType().getOrdinal() == DataType.HIDDEN)
@@ -135,7 +136,7 @@
<table>
<tbody>
<%
- for (Iterator i = selWidget.getPreferencesInfo().getPreferences().iterator();
i.hasNext();)
+ for (Iterator i =
selWidget.getInfo().getPreferences().getPreferences().iterator(); i.hasNext();)
{
GGPreferenceInfo prefInfo = (GGPreferenceInfo)i.next();