org.bioquery.bqutils
Class UrlUtils

java.lang.Object
  |
  +--org.bioquery.bqutils.UrlUtils

public class UrlUtils
extends java.lang.Object

This class provides some simple static methods to simplify working with HTTP calls.

This class was originally written to provide an alternate way to extract querystring paramaters from the URL of an HTTP request. This function is available in the javax.servlet.http.HttpUtils class, but at time bioquery was hosted on a server with a bug in the parseQueryString() method of that class. This situation will soon be resolved, and it may be possible, but not necessary to migrate some of the calls to class to javax.servlet.http.HttpUtils methods.

Author:
James Brundege

Constructor Summary
UrlUtils()
           
 
Method Summary
static java.lang.String addParameter(java.lang.String url, java.lang.String paramName, java.lang.String parameter)
          Takes the given url String and adds the given parameter to the querystring of that url.
static java.util.Hashtable getAllParameters(java.lang.String queryString)
          Given a querystring from a URL, this method will extract all of the parameters and put them in a Hashtable in which the first part of the querystring is the key and the second part is the value.
static java.lang.String removeSpaces(java.lang.String originalString)
          Finds all spaces in the given String and replaces them with '+' chars.
static java.lang.String restoreSpaces(java.lang.String urlString)
          Finds all '+' characters in the given String and replaces them with spaces.
static java.lang.String setSession(java.lang.String url, java.lang.String sessionId)
          Takes the given url String and adds the given JSessionId.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UrlUtils

public UrlUtils()
Method Detail

setSession

public static java.lang.String setSession(java.lang.String url,
                                          java.lang.String sessionId)
Takes the given url String and adds the given JSessionId. It is expected that the url String does not already have any parameters added to it's querystring. setSession should therefore be called on a url String prior to using the addParameter method. this method appends a semicolon ';' char and the sessionid to the String in the format:
http://www.bioquery.org/servletname?jsessionid=owieu938739
This method must be applied to the URL while it is in String form, before it is passed to the constructor of a URL object.
Parameters:
url - the String representing the URL
sessionId - the sessionId for this session that was determined by the web container at the start of the session.
Returns:
a String representing the URL in which the sessionId has been appended to the querystring.

addParameter

public static java.lang.String addParameter(java.lang.String url,
                                            java.lang.String paramName,
                                            java.lang.String parameter)
Takes the given url String and adds the given parameter to the querystring of that url. It does not matter whether the url already contains a querystring or not. This method uses the '?' char to represent the beginning of the querystring and the '&' to represent each additional parameter in the format:
http://www.bioquery.org/servletname;jsessionid=owieu938739?filemethod=savefile&pathname=somepath
This method must be applied to the URL while it is in String form, before it is passed to the constructor of a URL object.
Parameters:
url - the String representing the URL
paramName - the first half of the querystring. Such as 'filename' in the example above.
parameter - the second half of the querystring. Such as 'somepath' in the example above.
Returns:
a String representing the URL in which the parameter has been appended to the querystring.

getAllParameters

public static java.util.Hashtable getAllParameters(java.lang.String queryString)
                                            throws java.lang.StringIndexOutOfBoundsException
Given a querystring from a URL, this method will extract all of the parameters and put them in a Hashtable in which the first part of the querystring is the key and the second part is the value. This method uses the '?' char to represent the beginning of the querystring and the '&' to represent each additional parameter. It may be passed just the querystring (such as is extracted by the javax.servlet.http.HttpServletRequest.getQueryString() method, or it may be passed a String version of the entire URL. Either way it removes everything before the first '?' character.

If a querystring has a blank value (example: 'filename= ') the name will be placed in the Hashtable paired with an empty string: "". If the querystring is otherwise malformed (example: ends with a '&') it will throw a StringIndexOutOfBoundsException.

Please note that this method automatically restores spaces in the parameters. It replaces any '+' symbols found with spaces before placing the Strings in the HashTable.

This method does not extract the jsessionid from the url. The jsessionid is ignored

Note that this method is essentially identical to the javax.servlet.http.HttpUtils.parseQueryString() method, which may be preferrable to use. This was originally developed in response to a bug in that method in the web container in which bioquery was originally hosted. This problem may now be resolved.

Parameters:
queryString - a String representing a URL or the querystring from a URL
Returns:
a Hashtable contains each parameter from the querystring a name-value pair of String objects.
Throws:
java.lang.StringIndexOutOfBoundsException - if the querystring is malformed (does not contain a 'paramname=' after the '?' or each '&' char.

removeSpaces

public static java.lang.String removeSpaces(java.lang.String originalString)
Finds all spaces in the given String and replaces them with '+' chars. This includes leading and trailing spaces.
Parameters:
originalString - any String that may contain spaces.
Returns:
the String with each space replaced by a '+'.

restoreSpaces

public static java.lang.String restoreSpaces(java.lang.String urlString)
Finds all '+' characters in the given String and replaces them with spaces. Can reverse the removeSpaces() methos.
Parameters:
urlString - any String that contains '+' where there should be spaces.
Returns:
the String with each '+' replaced by a space character.