org.ibisph.util
Class NetLib

java.lang.Object
  extended by org.ibisph.util.NetLib

public abstract class NetLib
extends java.lang.Object

NetLib is an abstract library class with various utility methods used for odd ball HTML and URL related helper functions.

 URLConnections vs. URLs
  - Can send output as well as read input
  - Can post data to CGIs
  - Can read headers from a connection

Author:
Garth Braithwaite, STG

Constructor Summary
NetLib()
           
 
Method Summary
static java.lang.String base64Encode(byte[] octetString)
          David W.
static java.lang.String base64Encode(java.lang.String string)
          David W.
static java.lang.String encode(java.lang.String url, java.lang.String characterEncoding)
          Helper that simply localizes the URLEncoder with UTF-8.
static java.lang.String getCookieValue(javax.servlet.http.HttpServletRequest request, java.lang.String cookieName)
          Get's a cookie value from the request's header.
static java.lang.String getCookieValue(javax.servlet.http.HttpServletRequest request, java.lang.String cookieName, java.lang.String defaultValue)
          Get's a cookie value from the request's header.
static java.net.URLConnection getHTTPPostURLConnection(java.lang.String urlAddress)
          See getHTTPPostURLConnection(String urlAddress, String contentType); with the default value of "text/plain".
static java.net.URLConnection getHTTPPostURLConnection(java.lang.String urlAddress, java.lang.String contentType)
          Sets up a URLConnection for an HTTP Post Request.
static java.lang.String getURLContent(java.lang.String address)
           
static java.lang.String getURLContent(java.lang.String address, java.lang.String username, java.lang.String password)
           
static java.lang.String getURLContent(java.net.URL baseContext, java.lang.String address)
           
static java.lang.String getURLContent(java.net.URL baseContext, java.lang.String address, java.lang.String username, java.lang.String password)
          Opens the specified URL and returns the request's contents as a string.
static java.io.InputStream getURLResponseInputStream(java.net.URL baseContext, java.lang.String address)
          Gets a URL input stream connection.
static java.io.InputStream getURLResponseInputStream(java.net.URL baseContext, java.lang.String address, java.lang.String username, java.lang.String password)
          Gets a URL input stream connection with optional username/password.
static java.io.InputStream httpPostContent(java.lang.String urlAddress, java.lang.String content)
          Same as httpPostContent(String urlAddress, null, String content);.
static java.io.InputStream httpPostContent(java.lang.String urlAddress, java.lang.String contentType, java.lang.String content)
          Sends data to a URL via an HTTP post (a get type request is simply a URL with a query string suffix).
static void logCookies(javax.servlet.http.HttpServletRequest request)
          Shows all of the available request's header cookies by name and value.
static int saveURLResponse(java.lang.String address, java.lang.String filename)
          Save the specified URL address to a file named with the filename.
static boolean sendHTML(javax.servlet.http.HttpServletResponse response, java.lang.String html)
          Simple helper which sets the response's content type to text/html and writes the contents.
static void sendText(javax.servlet.http.HttpServletResponse response, java.lang.String text)
          Simple helper which sets the response's content type to text/plain and writes the contents.
static void sendTextAsHTML(javax.servlet.http.HttpServletResponse response, java.lang.String text)
          Simple helper which sets the response's content type to text/html and writes the contents wrapped between tags.
static void sendTHMLErrorPage(javax.servlet.http.HttpServletResponse response, java.lang.String errorTitle, java.lang.String errorMessage)
          Creates a simply formatted error HTML page.
static void setNoCacheHTTPHeaders(javax.servlet.http.HttpServletResponse response)
          Sets HTTP Meta tag headers that tells servers/browsers that this response should not be cached.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetLib

public NetLib()
Method Detail

base64Encode

public static java.lang.String base64Encode(byte[] octetString)
David W. Croft's method used to encode username/password strings.

Parameters:
octetString - Byte array to be encoded.
Returns:
Base 64 encoded string.

base64Encode

public static java.lang.String base64Encode(java.lang.String string)
David W. Croft's method used to encode username/password strings.

Parameters:
string - Text to be encoded.
Returns:
Base 64 encoded string.

encode

public static java.lang.String encode(java.lang.String url,
                                      java.lang.String characterEncoding)
                               throws java.io.UnsupportedEncodingException
Helper that simply localizes the URLEncoder with UTF-8.

Parameters:
url - URL to be encoded.
characterEncoding - Optional character Encoding to be used. Defaults to UTF-8.
Returns:
encoded URL string.
Throws:
java.io.UnsupportedEncodingException

getURLResponseInputStream

public static java.io.InputStream getURLResponseInputStream(java.net.URL baseContext,
                                                            java.lang.String address,
                                                            java.lang.String username,
                                                            java.lang.String password)
                                                     throws java.io.IOException,
                                                            java.net.MalformedURLException,
                                                            java.net.URISyntaxException
Gets a URL input stream connection with optional username/password.

Parameters:
baseContext - Optional context to base the address from. If baseContext != null then the address is relative to this base context.
address - URL address. if baseContext != null then treated as relative, else the address must be an explicit URL complete with protocol, server name, port, and context.
username - Optional username to authenticate with.
password - Optional password to authenticate with.
Returns:
Connection InputStream.
Throws:
java.io.IOException - Stream and connection related.
java.net.MalformedURLException - URL related.
java.net.URISyntaxException

getURLResponseInputStream

public static java.io.InputStream getURLResponseInputStream(java.net.URL baseContext,
                                                            java.lang.String address)
                                                     throws java.io.IOException,
                                                            java.net.MalformedURLException,
                                                            java.net.URISyntaxException
Gets a URL input stream connection.

Parameters:
baseContext - Optional context to base the address from. If baseContext != null then the address is relative to this base context.
address - URL address. if baseContext != null then treated as relative, else the address must be an explicit URL complete with protocol, server name, port, and context.
Returns:
Connection InputStream.
Throws:
java.io.IOException - Stream and connection related.
java.net.MalformedURLException - URL related.
java.net.URISyntaxException

getURLContent

public static java.lang.String getURLContent(java.net.URL baseContext,
                                             java.lang.String address,
                                             java.lang.String username,
                                             java.lang.String password)
                                      throws java.io.IOException,
                                             java.net.URISyntaxException
Opens the specified URL and returns the request's contents as a string.

Parameters:
baseContext - Optional context to base the address from. If baseContext != null then the address is relative to this base context.
address - URL address. if baseContext != null then treated as relative, else the address must be an explicit URL complete with protocol, server name, port, and context.
username - Optional username to authenticate with.
password - Optional password to authenticate with.
Returns:
Connection InputStream.
Throws:
java.io.IOException - Stream and connection related.
java.net.URISyntaxException

getURLContent

public static java.lang.String getURLContent(java.net.URL baseContext,
                                             java.lang.String address)
                                      throws java.io.IOException,
                                             java.net.URISyntaxException
Throws:
java.io.IOException
java.net.URISyntaxException

getURLContent

public static java.lang.String getURLContent(java.lang.String address,
                                             java.lang.String username,
                                             java.lang.String password)
                                      throws java.io.IOException,
                                             java.net.URISyntaxException
Throws:
java.io.IOException
java.net.URISyntaxException

getURLContent

public static java.lang.String getURLContent(java.lang.String address)
                                      throws java.io.IOException,
                                             java.net.URISyntaxException
Throws:
java.io.IOException
java.net.URISyntaxException

saveURLResponse

public static int saveURLResponse(java.lang.String address,
                                  java.lang.String filename)
                           throws java.io.IOException,
                                  java.net.MalformedURLException
Save the specified URL address to a file named with the filename.

Parameters:
address - URL address complete with protocol, server name, port, and context.
filename - Complete path and filename of where to save the URL request's response content to.
Returns:
Number of bytes received/written.
Throws:
java.io.IOException - File and stream related.
java.net.MalformedURLException - URL address related.

sendText

public static void sendText(javax.servlet.http.HttpServletResponse response,
                            java.lang.String text)
                     throws java.io.IOException
Simple helper which sets the response's content type to text/plain and writes the contents.

Parameters:
response - Destination response to write the contents to.
text - Contents to write.
Throws:
java.io.IOException - Response writer related.

sendHTML

public static boolean sendHTML(javax.servlet.http.HttpServletResponse response,
                               java.lang.String html)
                        throws java.io.IOException
Simple helper which sets the response's content type to text/html and writes the contents.

Parameters:
response - Destination response to write the contents to.
html - Contents to write.
Throws:
java.io.IOException - Response writer related.

sendTextAsHTML

public static void sendTextAsHTML(javax.servlet.http.HttpServletResponse response,
                                  java.lang.String text)
                           throws java.io.IOException
Simple helper which sets the response's content type to text/html and writes the contents wrapped between tags.

Parameters:
response - Destination response to write the contents to.
text - Contents to wrap within an tag and written to the response.
Throws:
java.io.IOException - Response writer related.

sendTHMLErrorPage

public static void sendTHMLErrorPage(javax.servlet.http.HttpServletResponse response,
                                     java.lang.String errorTitle,
                                     java.lang.String errorMessage)
                              throws java.lang.Exception
Creates a simply formatted error HTML page. The content is set to text/html, a page title is set, and the error message is wrapped within tags.

Parameters:
response - Destination response to write the contents to.
errorTitle - Text used for the page title and displayed as the main problem message.
errorMessage - Detailed error message text displayed.
Throws:
java.lang.Exception - Response print writer related.

setNoCacheHTTPHeaders

public static void setNoCacheHTTPHeaders(javax.servlet.http.HttpServletResponse response)
Sets HTTP Meta tag headers that tells servers/browsers that this response should not be cached. This is redundant since these items are also implemented in the appropriate XSLTs meta data.

Parameters:
response - Response which will be modified/have headers set.

getHTTPPostURLConnection

public static java.net.URLConnection getHTTPPostURLConnection(java.lang.String urlAddress,
                                                              java.lang.String contentType)
                                                       throws java.io.IOException,
                                                              java.net.MalformedURLException
Sets up a URLConnection for an HTTP Post Request. This is commonly used to talk to CGIs or other Java servlets that use the POST method.

Parameters:
urlAddress - Complete explicit URL address to be used for a request.
contentType - HTTP content type. If null, this value defaults to "text/plain".
Returns:
URL connection based on the content type and address.
Throws:
java.io.IOException - URL connection related.
java.net.MalformedURLException - URL address related.
See Also:
Web Caching Tutorial, Cache docs

getHTTPPostURLConnection

public static java.net.URLConnection getHTTPPostURLConnection(java.lang.String urlAddress)
                                                       throws java.io.IOException,
                                                              java.net.MalformedURLException
See getHTTPPostURLConnection(String urlAddress, String contentType); with the default value of "text/plain".

Throws:
java.io.IOException
java.net.MalformedURLException

httpPostContent

public static java.io.InputStream httpPostContent(java.lang.String urlAddress,
                                                  java.lang.String contentType,
                                                  java.lang.String content)
                                           throws java.io.IOException
Sends data to a URL via an HTTP post (a get type request is simply a URL with a query string suffix). The difference is that a post sets the content type and streams the data to the destination connection.

Parameters:
urlAddress - Complete destination URL address to post content to.
contentType - Type of HTTP content to be streamed to destination. Any valid HTTP Content Type such as "text/plain" etc.
content - Text to be posted/streamed.
Returns:
Destination's response Input stream that could be read etc.
Throws:
java.io.IOException - Stream and connection related.

httpPostContent

public static java.io.InputStream httpPostContent(java.lang.String urlAddress,
                                                  java.lang.String content)
                                           throws java.io.IOException
Same as httpPostContent(String urlAddress, null, String content);.

Throws:
java.io.IOException

getCookieValue

public static java.lang.String getCookieValue(javax.servlet.http.HttpServletRequest request,
                                              java.lang.String cookieName,
                                              java.lang.String defaultValue)
Get's a cookie value from the request's header.

Parameters:
request - Source which should contain all available domain and context cookies.
cookieName - Key value used to locate the cookie value.
defaultValue - Value to be returned if the cookie is not found.
Returns:
Value of the looked up cookie -or- if not found, the defaultValue passed in.

getCookieValue

public static java.lang.String getCookieValue(javax.servlet.http.HttpServletRequest request,
                                              java.lang.String cookieName)
Get's a cookie value from the request's header.

Parameters:
request - Source which should contain all available domain and context cookies.
cookieName - Key value used to locate the cookie value.
Returns:
Value of the looked up cookie. Null if not found.

logCookies

public static void logCookies(javax.servlet.http.HttpServletRequest request)
Shows all of the available request's header cookies by name and value.

Parameters:
request - Source which should contain all available domain and context cookies.