com.senn.magic.util
Class WebUtils

java.lang.Object
  extended by com.senn.magic.util.WebUtils

public final class WebUtils
extends Object

This class provides useful methods for web related development.
Some of the methods are similar to some servlet methods (working with request parameters).

Since:
3.2
Author:
Bart Thierens
Last modification: 11/02/2010
See Also:
BareBonesBrowserLaunch

Field Summary
static String PARAM_SEPARATOR_CHAR
          The character used to separate the request parameters: &
static String PARAM_START_CHAR
          The character used to indicate the start of the request parameters: ?
static String PARAM_VALUE_CHAR
          The character used to show the value of a request parameter: =
static String PROTOCOL_SEPARATOR
          The character sequence used to indicate the end of the protocol name: ://

For example: http://senn-magic.blogspot.com
 
Method Summary
static String getBitlyURL(String login, String apiKey, String longURL)
          This method shortens a provided url using the Bitly API.
static String getDummyContent(int lineAmount)
          Provides dummy content for filling up a web page with dummy content.
static int getParameterCount(String url)
          Counts how many request parameters a url contains
static Map<String,String> getParameterMap(String url)
          Gets all the request parameters of a url with their values.
static String getParameterValue(String url, String paramKey, boolean ignoreCase)
          Gets the value of the provided request parameter
static String getProtocol(String url)
          Returns the protocol name of the provided url.
static String getTinyURL(String longURL)
          This method shortens a provided url using the TinyURL API.
static void openURL(String url)
          This method opens the provided url in the system's default browser.
static boolean parameterExists(String url, String paramKey)
          Checks if a parameter exists in the provided url.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PARAM_START_CHAR

public static final String PARAM_START_CHAR
The character used to indicate the start of the request parameters: ?

See Also:
Constant Field Values

PARAM_SEPARATOR_CHAR

public static final String PARAM_SEPARATOR_CHAR
The character used to separate the request parameters: &

See Also:
Constant Field Values

PARAM_VALUE_CHAR

public static final String PARAM_VALUE_CHAR
The character used to show the value of a request parameter: =

See Also:
Constant Field Values

PROTOCOL_SEPARATOR

public static final String PROTOCOL_SEPARATOR
The character sequence used to indicate the end of the protocol name: ://

For example: http://senn-magic.blogspot.com

See Also:
Constant Field Values
Method Detail

openURL

public static void openURL(String url)
                    throws Exception
This method opens the provided url in the system's default browser.
This method works on Mac OS (X), Windows and most Unix/Linux operating systems.

NOTE:
This method first checks if Desktop can be found and will try to launch the browser from the Desktop class.
If it can't be found, or it fails for whatever reason, it will try to launch a browser in a way, specific for every operating system.
Depending on the operating system, it may use Runtime.getRuntime().exec() which may affect the performance of your application.

I got my inspiration for the WebUtils.openURL(String) method from BareBonesBrowserLaunch

Parameters:
url - the url of the webpage you want to open
Throws:
Exception
See Also:
BareBonesBrowserLaunch

getDummyContent

public static String getDummyContent(int lineAmount)
Provides dummy content for filling up a web page with dummy content.

Parameters:
lineAmount - the amount of lines (separated by <br />)
Returns:
String - the dummy content
See Also:
StringUtils.getFillerText(int, String)

getProtocol

public static String getProtocol(String url)
Returns the protocol name of the provided url.
For example:
{url = "http://senn-magic.blogspot.com" will return "http"
Will return null when no valid protocol name is found

Parameters:
url -
Returns:
String - the protocol of the url

getParameterMap

public static Map<String,String> getParameterMap(String url)
Gets all the request parameters of a url with their values.
Will return an empty Map if no parameters are present, and null when the url is invalid

Parameters:
url -
Returns:
Map - a Map containing the parameters and their values

getParameterCount

public static int getParameterCount(String url)
Counts how many request parameters a url contains

Parameters:
url -
Returns:
int - the amount of parameters

parameterExists

public static boolean parameterExists(String url,
                                      String paramKey)
Checks if a parameter exists in the provided url.

Parameters:
url -
paramKey - the name of the parameter you want to check - this is case sensitive
Returns:
boolean - true if the parameter exists, false if it doesn't

getParameterValue

public static String getParameterValue(String url,
                                       String paramKey,
                                       boolean ignoreCase)
Gets the value of the provided request parameter

Parameters:
url -
paramKey - the name of the parameter
ignoreCase - set to false if you want @param paramKey to be case sensitive
Returns:
String - the value of the parameter, or null if the parameter doesn't exist

getBitlyURL

public static String getBitlyURL(String login,
                                 String apiKey,
                                 String longURL)
                          throws org.apache.commons.httpclient.HttpException,
                                 IOException,
                                 Exception
This method shortens a provided url using the Bitly API.

Example:
http://senn-magic.blogspot.com => http://bit.ly/bPvmjY
This method uses the Apache Commons HttpClient framework (and indirectly Apache Commons Codec and Apache Commons Logging) so you must have these in your classpath.

NOTE:
You must have a Bitly-account in order to use their API. You must provide your login and your API-key (which can be found on your profile).

Parameters:
login - a valid Bitly login
apiKey - a valid Bitly API-key associated with the login
longURL - the url to shorten
Returns:
String - a shortened link to your url (ie. http://bit.ly/bPvmjY)
Throws:
HttpException
IOException
Exception

getTinyURL

public static String getTinyURL(String longURL)
                         throws org.apache.commons.httpclient.HttpException,
                                IOException
This method shortens a provided url using the TinyURL API.

Example:
http://senn-magic.blogspot.com => http://tinyurl.com/ylj7zwb
This method uses the Apache Commons HttpClient framework (and indirectly Apache Commons Codec and Apache Commons Logging) so you must have these in your classpath.

Parameters:
longURL - the url to shorten
Returns:
String - a shortened link to your url (ie. http://tinyurl.com/ylj7zwb)
Throws:
HttpException
IOException