From: <jbo...@li...> - 2005-11-03 09:30:19
|
Author: aron.gombas Date: 2005-11-03 04:30:13 -0500 (Thu, 03 Nov 2005) New Revision: 1489 Modified: trunk/labs/kosmos/src/java/hu/midori/kosmos/server/util/ScrapingUtils.java Log: UserInfo support for URLs Modified: trunk/labs/kosmos/src/java/hu/midori/kosmos/server/util/ScrapingUtils.java =================================================================== --- trunk/labs/kosmos/src/java/hu/midori/kosmos/server/util/ScrapingUtils.java 2005-11-03 09:28:56 UTC (rev 1488) +++ trunk/labs/kosmos/src/java/hu/midori/kosmos/server/util/ScrapingUtils.java 2005-11-03 09:30:13 UTC (rev 1489) @@ -21,6 +21,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.tmatesoft.svn.core.wc.SVNWCUtil; import org.w3c.dom.Document; import org.w3c.tidy.Tidy; @@ -96,6 +97,23 @@ return StringUtils.remove(StringUtils.remove(originalUrl.toString(), originalUrl.getUserInfo()), '@'); } + /** Returns a two-element array with the username and password from the given URL, or <code>null</code> if those are not specified in the URL. */ + public static String[] parseUserInfo(String url) throws MalformedURLException { + String userInfo = new URL(url).getUserInfo(); + if(StringUtils.isBlank(userInfo)) + return null; + + String strings[] = StringUtils.split(userInfo, ':'); + if(strings.length != 2) + throw new IllegalArgumentException(String.format("Invalid user info \"%s\"", userInfo)); + String userName = strings[0]; + String password = strings[1]; + if(StringUtils.isBlank(userName) || StringUtils.isBlank(password)) + throw new IllegalArgumentException(String.format("Invalid username (\"%s\") or password (\"%s\")", userName, password)); + + return strings; + } + /** * Eliminates the empty items from a scraped value string to make * the tokenizer happy. E.g. <code>||xxx|</code> will be transformed to <code> | |xxx| </code>. |