Author: julien(a)jboss.com
Date: 2007-03-07 12:54:30 -0500 (Wed, 07 Mar 2007)
New Revision: 6581
Modified:
trunk/common/build.xml
trunk/common/src/main/org/jboss/portal/common/util/URLTools.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetFactory.java
trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetProvider.java
Log:
added timeout on get retrieval
Modified: trunk/common/build.xml
===================================================================
--- trunk/common/build.xml 2007-03-07 16:38:02 UTC (rev 6580)
+++ trunk/common/build.xml 2007-03-07 17:54:30 UTC (rev 6581)
@@ -72,6 +72,7 @@
<path refid="apache.log4j.classpath"/>
<path refid="jboss.test.classpath"/>
<path refid="junit.junit.classpath"/>
+ <path refid="apache.httpclient.classpath"/>
<pathelement
location="../tools/lib/cargo-core-uberjar-0.8.jar"/>
<pathelement location="../tools/lib/cargo-ant-0.8.jar"/>
<pathelement location="../tools/lib/ant-junit.jar"/>
Modified: trunk/common/src/main/org/jboss/portal/common/util/URLTools.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-03-07 16:38:02
UTC (rev 6580)
+++ trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-03-07 17:54:30
UTC (rev 6581)
@@ -22,7 +22,12 @@
******************************************************************************/
package org.jboss.portal.common.util;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+
import java.io.UnsupportedEncodingException;
+import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
@@ -54,6 +59,51 @@
}
/**
+ * Fetches content from an HTTP server performing a GET operation. If the status code
is 200
+ * then it will return a byte array of the body otherwise returns null.
+ *
+ * @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
+ */
+ public static byte[] performGET(URL url, int soTimeoutMillis, int connTimeoutMillis)
throws IllegalArgumentException, IOException
+ {
+ if (url == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if ("http".equals(url.getProtocol()) ||
"https".equals(url.getProtocol()))
+ {
+ HttpClient client = new HttpClient();
+
+ // Yes deprecated but advocated way to do will not for with 2.0.2 which is what
today JBoss AS ships
+ client.setConnectionTimeout(connTimeoutMillis);
+ client.setTimeout(soTimeoutMillis);
+
+ //
+ HttpMethod method = null;
+ try
+ {
+ method = new GetMethod(url.toString());
+ int statusCode = client.executeMethod(method);
+ if (statusCode == 200)
+ {
+ return method.getResponseBody();
+ }
+ }
+ finally
+ {
+ if (method != null)
+ {
+ method.releaseConnection();
+ }
+ }
+ }
+ throw new IllegalArgumentException("Protocol " + url.getProtocol() +
" not accepted");
+ }
+
+ /**
* @param url
* @return
* @since 2.4.2
Modified: trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetFactory.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetFactory.java 2007-03-07
16:38:02 UTC (rev 6580)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetFactory.java 2007-03-07
17:54:30 UTC (rev 6581)
@@ -24,6 +24,7 @@
import org.jboss.portal.common.util.XML;
import org.jboss.portal.common.util.LocalizedString;
+import org.jboss.portal.common.util.URLTools;
import org.jboss.portal.widget.google.type.DataType;
import org.jboss.portal.widget.google.type.StringType;
import org.jboss.portal.widget.google.type.LocationType;
@@ -41,6 +42,7 @@
import java.util.Locale;
import java.util.Collection;
import java.util.ArrayList;
+import java.io.ByteArrayInputStream;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -70,7 +72,12 @@
{
DocumentBuilderFactory factory = XML.getDocumentBuilderFactory();
DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(url.toString());
+ byte[] bytes = URLTools.performGET(url, 5000, 5000);
+ if (bytes == null)
+ {
+ throw new Exception("Cannot retrieve document " + url);
+ }
+ Document doc = builder.parse(new ByteArrayInputStream(bytes));
Element moduleElt = doc.getDocumentElement();
//
Modified: trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetProvider.java
===================================================================
--- trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetProvider.java 2007-03-07
16:38:02 UTC (rev 6580)
+++ trunk/widget/src/main/org/jboss/portal/widget/google/GGWidgetProvider.java 2007-03-07
17:54:30 UTC (rev 6581)
@@ -25,7 +25,6 @@
import java.net.URL;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
-import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.util.StringTokenizer;
import java.util.HashMap;
@@ -39,7 +38,7 @@
import org.jboss.portal.widget.WidgetProvider;
import org.jboss.portal.widget.Widget;
-import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.common.util.URLTools;
import org.apache.log4j.Logger;
import javax.swing.text.html.HTML;
@@ -57,7 +56,6 @@
/**
* @todo first :
- * - add time out on URL retrieval
* - add content type filtering
* - finish implementation of preferences
* - use categorization
@@ -171,19 +169,13 @@
URL url = new
URL("http://www.google.com/ig/directory?synd=open");
// Read fully the URL content first
- byte[] bytes = null;
- InputStream in = null;
- try
+ log.info("Retrieving " + url);
+ byte[] bytes = URLTools.performGET(url, 5000, 5000);
+ if (bytes == null)
{
- log.info("Retrieving " + url);
- in = url.openStream();
- bytes = Tools.getBytes(in);
- log.info("Document " + url + " has been retrieved");
+ throw new Exception("Cannot retrieve " + url);
}
- finally
- {
- Tools.safeClose(in);
- }
+ log.info("Document " + url + " has been retrieved");
//
final LinkedHashSet urls = new LinkedHashSet();