org.jaffa.util
Class URLHelper

java.lang.Object
  extended byorg.jaffa.util.URLHelper

public class URLHelper
extends Object

Utility class to manipulate URL's and provide some additional capability for using them.

Version:
1.0
Author:
PaulE

Field Summary
static String DEFAULT_PAGE
          This is the default value for the initial page of the application
static String PROTOCOL_CLASSPATH
          This contains the name of the URL protocol that implies the resouce can be located in the classpath
static String PROTOCOL_WEBROOT
          This contains the name of the URL protocol that implies the resource can be found relative to the location of the web-root on the local file system, This assumes that you are running this inside a servlet, on a web server
 
Constructor Summary
URLHelper()
           
 
Method Summary
static String buildUrl(String url, HttpServletRequest req)
          Build up a valid URL string based on the supplied source URL string.
static String getBase(HttpServletRequest request)
          Based on a HttpRequest, this routine will ingure out the URL that represents the root of the web application.
static String getFullUrl(HttpServletRequest request)
          Get a complete string based representation of a request's source URL include query parameters
static InputStream getInputStream(String resourceName)
          This method will try to load the input resource off the classpath.
static URL getUrl(String resourceName)
          Deprecated. Use the newExtendedURL() method.
static void main(String[] args)
          Test rig
static URL newExtendedURL(String url)
          Create a URL object from a string, this can handle the two new Custom URL protocols, 'classpath:///' and 'webroot:///'.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROTOCOL_CLASSPATH

public static final String PROTOCOL_CLASSPATH
This contains the name of the URL protocol that implies the resouce can be located in the classpath

See Also:
Constant Field Values

PROTOCOL_WEBROOT

public static final String PROTOCOL_WEBROOT
This contains the name of the URL protocol that implies the resource can be found relative to the location of the web-root on the local file system, This assumes that you are running this inside a servlet, on a web server

See Also:
Constant Field Values

DEFAULT_PAGE

public static final String DEFAULT_PAGE
This is the default value for the initial page of the application

See Also:
Constant Field Values
Constructor Detail

URLHelper

public URLHelper()
Method Detail

getBase

public static String getBase(HttpServletRequest request)
Based on a HttpRequest, this routine will ingure out the URL that represents the root of the web application. It derives the URL protocol, host, port and application. An example of a returned string may be http://www.example.org/myApp/ or https://www.example.com:888/SecureApp/

Parameters:
request - A HttpRequest used to derive information about the root of the web applicatoin
Returns:
a string that represents the base URL for this web application

buildUrl

public static String buildUrl(String url,
                              HttpServletRequest req)
Build up a valid URL string based on the supplied source URL string. If the supplied string in null, use the string defined in DEFAULT_PAGE and append this to web root ( using URLHelper.getBase() ) If the supplied string already is a valid url, leave it as is. If it is currently invalid, see if it can be made valid by making it relative to the web application base ( using URLHelper.getBase() ), if so return this value If this still doesn't yield a valid url, assume it was null, and build it based on DEFAULT_PAGE and getBase()

Parameters:
url - The URL to build and validate
req - The httpRequest needed to derive the web app base
Returns:
A fully expressed valid URL

getFullUrl

public static String getFullUrl(HttpServletRequest request)
Get a complete string based representation of a request's source URL include query parameters

Parameters:
request - HttpRequest containing the url to extracts
Returns:
string representation of URL

getInputStream

public static InputStream getInputStream(String resourceName)
                                  throws IOException
This method will try to load the input resource off the classpath. If its not found, then it will look for it in the filesystem. A null will be returned, if the resource could not be located.

Parameters:
resourceName - the resource to be located.
Returns:
an input stream for reading from the resource.
Throws:
IOException - if any error occurs while opening the stream.

getUrl

public static URL getUrl(String resourceName)
Deprecated. Use the newExtendedURL() method.

This method will try to load the input resource off the classpath. If its not found, then it will look for it in the filesystem. A null will be returned, if the resource could not be located. This method merely invokes the newExtendedURL() method, returning a null if any exception is raised.

Parameters:
resourceName - the resource to be located.
Returns:
a URL for reading from the resource.

newExtendedURL

public static URL newExtendedURL(String url)
                          throws MalformedURLException
Create a URL object from a string, this can handle the two new Custom URL protocols, 'classpath:///' and 'webroot:///'. If either of these new ones are used they will be converted into the appropriate 'file://' format. If no protocol is specified, then it'll try to load the input resource off the classpath. If its not found, then it will look for the input resource in the filesystem.

Parameters:
url - source URL that may use one of the new protocols
Returns:
valid URL object, as these two new protocols are not really supported by the java.net.URL object
Throws:
MalformedURLException - if the supplied URL is not valid, or can't be translated into something that is valid

main

public static void main(String[] args)
Test rig

Parameters:
args - none required


Copyright © 2002-2004 JAFFA Project.