Author: chris.laprun(a)jboss.com
Date: 2007-03-03 14:04:10 -0500 (Sat, 03 Mar 2007)
New Revision: 6502
Modified:
trunk/common/src/main/org/jboss/portal/common/util/URLTools.java
trunk/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java
Log:
- Added exists(urlAsString, allowNull) method.
- Added documentation and remark on possibility of hanging in case the URL refers to a
host on which the resource does not exist.
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-03 17:47:59
UTC (rev 6501)
+++ trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-03-03 19:04:10
UTC (rev 6502)
@@ -25,6 +25,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
@@ -107,12 +109,18 @@
return address != null && Pattern.matches(RE_EMAIL_VALIDATION, address);
}
- public static boolean exists(java.net.URL url)
+ /**
+ * Determines that the specified URL corresponds to an existing resource by trying to
open a stream from it. NOTE:
+ * This is a potentially blocking method since by default there is no timeout on the
connection... See
+ *
http://jira.jboss.com/jira/browse/JBPORTAL-1279
+ *
+ * @param url
+ * @return
+ */
+ public static boolean exists(URL url)
{
- if (url == null)
- {
- throw new IllegalArgumentException("No null urls allowed");
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(url, "URL");
+
InputStream in = null;
try
{
@@ -129,6 +137,32 @@
}
}
+ /**
+ * @param urlAsString
+ * @param allowNull <code>true</code> if passing
<code>null</code> will be ignored and just return
+ * <code>false</code>, <code>false</code>
to throw an {@link IllegalArgumentException} is the
+ * given URL is <code>null</code>.
+ * @return
+ * @since 2.6
+ */
+ public static boolean exists(String urlAsString, boolean allowNull)
+ {
+ if (!allowNull)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(urlAsString,
"URL", null);
+ }
+
+ try
+ {
+ URL url = new URL(urlAsString);
+ return exists(url);
+ }
+ catch (MalformedURLException e)
+ {
+ return false;
+ }
+ }
+
public static URLMatch[] extractURLsFrom(String markup)
{
// todo: will need to re-write without regex after 2.4
Modified: trunk/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java 2007-03-03
17:47:59 UTC (rev 6501)
+++ trunk/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java 2007-03-03
19:04:10 UTC (rev 6502)
@@ -160,4 +160,10 @@
assertEquals("http://hostname:8088/some/path",
URLTools.replaceServerPortInURL("http://hostname/some/path", 8088));
assertEquals("https://hostname:8088/some/path",
URLTools.replaceServerPortInURL("https://hostname/some/path", 8088));
}
+
+ public void testExistsURL()
+ {
+ // todo: add more tests
+ assertFalse(URLTools.exists(null, true));
+ }
}