com.mizar.faces.javax
Class ServletUtils

java.lang.Object
  extended by com.mizar.faces.javax.ServletUtils

public class ServletUtils
extends java.lang.Object

A collection of static classes that provide convenient interfaces to HttpSession, HttpServletRequest, and ServletContext methods.


Constructor Summary
ServletUtils()
           
 
Method Summary
static java.lang.String getAbsoluteFileName(java.lang.String fileUrl)
           
static java.lang.String getAbsoluteStem()
          Retreive the complete path to the application root for the current HttpServletRequest.
static java.lang.String getContextPath()
           
static java.lang.String getContextRoot()
          Gets the context root from the current HttpSessions in a null safe manner.
static java.util.ArrayList<java.lang.String> getFileURLs(java.io.File folder)
           
static java.util.ArrayList<java.lang.String> getFileURLs(java.lang.String relativeFolder)
           
static javax.servlet.http.HttpServletRequest getHttpRequest()
           
static javax.servlet.http.HttpServletResponse getHttpResponse()
           
static javax.servlet.http.HttpSession getHttpSession()
          Gets the current HTTP Session from the FacesContext/ExternalContext
static java.lang.String getIpAddress()
           
static java.lang.String getMimeType(java.lang.String fileName)
          Just a wrapper for ServletContext.getMimeType()
static int getPort()
           
static java.io.InputStream getResourceAsStream(java.lang.String fileName)
           
static java.lang.Object getServletAttribute(java.lang.String name)
           
static javax.servlet.ServletContext getServletContext()
          Gets the current ServletContext from the FacesContext in a null safe manner.
static java.lang.String getServletContextName()
          The method traverses FacesContext.getCurrentInstance().getExternalContext().getRequest().getContextPath() in a null safe manner.
static java.lang.Object getSessionAttribute(java.lang.String name)
           
static int getSessionTimeOut()
           
static java.lang.String getTimestampedFileName(java.lang.String prefix, java.lang.String type)
          Create a unique file name derived from a timestamp.
static java.lang.String getTimestampedFileName(java.lang.String relativePath, java.lang.String prefix, java.lang.String type)
          Create a unique file name derived from a timestamp.
static java.lang.String getUniqueFileNameForUser(java.lang.String relativePath, java.lang.String userName, java.lang.String type)
          Generate a unique filename for recipient.
static java.lang.String getUserAgent()
           
static java.lang.String readFromRelativeFile(java.lang.String fileName)
           
static void sendFile(java.lang.String filePath)
          This method may or may not differ from sendFile2(java.lang.String).
static void sendFile2(java.lang.String filePath)
          Sends the contents of filePath to the client using filePath to determine the mime type.
static boolean sendHtml(java.lang.String content)
           
static boolean sendOutputStream(java.io.OutputStream baos, java.lang.String fileName)
          Sends the OutputStream contents to the client using fileName to determine the mime type.
static boolean sendOutputStream(java.lang.String content, java.lang.String fileName)
           
static java.lang.Object setServletAttribute(java.lang.String name, java.lang.Object value)
           
static java.lang.Object setSessionAttribute(java.lang.String name, java.lang.Object value)
           
static void setSessionTimeOut(int timeout)
           
static java.lang.String stripContextRoot(java.lang.String absoluteFilePath)
          The method converts an absolute file name to a relative file name be removing the portion that is equal to the ContextRoot.
static javax.servlet.http.HttpSession validateHttpSession(javax.servlet.http.HttpSession httpSession)
          The method is used to test if a httpSession is still valid.
static int writeContentToRelativeFile(byte[] byteStream, java.lang.String fileUrl)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServletUtils

public ServletUtils()
Method Detail

getAbsoluteStem

public static java.lang.String getAbsoluteStem()
Retreive the complete path to the application root for the current HttpServletRequest.

Generates a string of described by
[protocol]://[hostname]:[port]/[contextPath]/faces

Returns:
the complete http[s]:// path to the application root.

getContextPath

public static java.lang.String getContextPath()

getHttpSession

public static javax.servlet.http.HttpSession getHttpSession()
Gets the current HTTP Session from the FacesContext/ExternalContext

Returns:
the current HttpSession or null if the travse fails
See Also:
ExternalContext.getRequest(), HttpServletRequest.getSession(boolean)

getHttpRequest

public static javax.servlet.http.HttpServletRequest getHttpRequest()
Returns:
the HttpServletRequest from current FacesContext
See Also:
ExternalContext.getRequest()

getUserAgent

public static java.lang.String getUserAgent()

getHttpResponse

public static javax.servlet.http.HttpServletResponse getHttpResponse()

getIpAddress

public static java.lang.String getIpAddress()
Returns:
the IP address associated with the HttpServletRequest from the current FacesContext
See Also:
getHttpRequest(), ServletRequest.getRemoteHost()

getPort

public static int getPort()
Returns:
the port number associated with the HttpServletRequest from the current FacesContext

validateHttpSession

public static javax.servlet.http.HttpSession validateHttpSession(javax.servlet.http.HttpSession httpSession)

The method is used to test if a httpSession is still valid. It does this by executing a getter within a try block and catching the Exception that occurs if the session is no longer valid. This would typically be used within a servlet monitor that keeps track of HTTP sessions. If a session times out, it becomes invalid.


Example:
 public HttpSession getHttpSession(){
   return ServletUtils.validateHttpSession(httpSession);
 }
 

Parameters:
httpSession -
Returns:
the input httpSession if valid, else null

setServletAttribute

public static java.lang.Object setServletAttribute(java.lang.String name,
                                                   java.lang.Object value)

getServletAttribute

public static java.lang.Object getServletAttribute(java.lang.String name)

setSessionAttribute

public static java.lang.Object setSessionAttribute(java.lang.String name,
                                                   java.lang.Object value)

getSessionAttribute

public static java.lang.Object getSessionAttribute(java.lang.String name)

getServletContextName

public static java.lang.String getServletContextName()
The method traverses FacesContext.getCurrentInstance().getExternalContext().getRequest().getContextPath() in a null safe manner.

Returns:
The servlet context name or null if it cannot be traversed to.

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String fileName)

getContextRoot

public static java.lang.String getContextRoot()
Gets the context root from the current HttpSessions in a null safe manner.

Returns:
the current context root or null if the ServletContext cannot be traversed.
See Also:
getServletContext()

getServletContext

public static javax.servlet.ServletContext getServletContext()
Gets the current ServletContext from the FacesContext in a null safe manner.

Returns:
the current ServletContext

getMimeType

public static java.lang.String getMimeType(java.lang.String fileName)
Just a wrapper for ServletContext.getMimeType()

Parameters:
fileName -
Returns:
the mime type
See Also:
ServletContext.getMimeType(String)

sendHtml

public static boolean sendHtml(java.lang.String content)

sendOutputStream

public static boolean sendOutputStream(java.lang.String content,
                                       java.lang.String fileName)
                                throws java.io.IOException
Throws:
java.io.IOException

sendOutputStream

public static boolean sendOutputStream(java.io.OutputStream baos,
                                       java.lang.String fileName)
                                throws java.io.IOException
Sends the OutputStream contents to the client using fileName to determine the mime type.

Parameters:
baos - an OutputStream that is castable to a ByteArrayOutputStream
fileName - a file name to include in the response header
Throws:
java.io.IOException
See Also:
getMimeType(String), sendFile(String)

sendFile

public static void sendFile(java.lang.String filePath)
                     throws java.io.IOException
This method may or may not differ from sendFile2(java.lang.String). It looks like they just take different paths to the same write Sends the contents of filePath to the client using filePath to determine the mime type.

Parameters:
filePath - an absolute path to a file
Throws:
java.io.IOException
See Also:
sendFile2(java.lang.String)

sendFile2

public static void sendFile2(java.lang.String filePath)
                      throws java.io.IOException
Sends the contents of filePath to the client using filePath to determine the mime type.

Parameters:
filePath - an absolute path to a file
Throws:
java.io.IOException
See Also:
sendOutputStream(OutputStream, String), sendFile(java.lang.String)

getUniqueFileNameForUser

public static java.lang.String getUniqueFileNameForUser(java.lang.String relativePath,
                                                        java.lang.String userName,
                                                        java.lang.String type)
Generate a unique filename for recipient.

The generated filename will have the form
[contextRoot]/[relativePath]/[recipient]-yyyy-MM-dd-kk-mm.[type]

Parameters:
relativePath - Path relative to the ContextRoot to use
userName - User to associate with the filename
type - file extension
Returns:
a unique file name

getTimestampedFileName

public static java.lang.String getTimestampedFileName(java.lang.String prefix,
                                                      java.lang.String type)
Create a unique file name derived from a timestamp.

The generated filename will have the form:
[prefix]-yyyy-MM-dd-kk-mm.[type]

Parameters:
prefix - prefix to add to the unique filename
type - file extension type to use
Returns:
generated filename
See Also:
which gets the absolute file name

getAbsoluteFileName

public static java.lang.String getAbsoluteFileName(java.lang.String fileUrl)
Parameters:
fileUrl - a file path and name relative to the context root
Returns:
the absolute path to fileUrl
See Also:
getContextRoot()

getTimestampedFileName

public static java.lang.String getTimestampedFileName(java.lang.String relativePath,
                                                      java.lang.String prefix,
                                                      java.lang.String type)
Create a unique file name derived from a timestamp.

The generated filename will have the form:
[contextRoot]/[relativePath]/[prefix]-yyyy-MM-dd-kk-mm.[type]

Parameters:
relativePath - Path relative to the context root
prefix - prefix to add to the unique filename
type - file extension type to use
Returns:
generated filename
See Also:
which gets just the simple file name

readFromRelativeFile

public static java.lang.String readFromRelativeFile(java.lang.String fileName)
                                             throws java.io.FileNotFoundException,
                                                    java.io.IOException
Parameters:
fileName - name of a file relative to the ContextRoot
Returns:
the contents of fileName
Throws:
java.io.FileNotFoundException
java.io.IOException
See Also:
FileUtils.readFromAbsoluteFile(String), getContextRoot()

stripContextRoot

public static java.lang.String stripContextRoot(java.lang.String absoluteFilePath)
The method converts an absolute file name to a relative file name be removing the portion that is equal to the ContextRoot.

Parameters:
absoluteFilePath -
Returns:
the part of absoluteFilePath that is relative to the Context Root
See Also:
getContextRoot()

writeContentToRelativeFile

public static int writeContentToRelativeFile(byte[] byteStream,
                                             java.lang.String fileUrl)
                                      throws java.io.FileNotFoundException,
                                             java.io.IOException
Parameters:
fileUrl - a relative URL for the file. This is prepended with the context root to form an absolute path to the file.
byteStream - the binary content to write to docUrl
Throws:
java.io.FileNotFoundException
java.io.IOException
See Also:
FileUtils.writeContentToAbsoluteFile(byte[], String)

getSessionTimeOut

public static int getSessionTimeOut()
Returns:
the current session timeout in seconds.
See Also:
HttpSession.getMaxInactiveInterval(), getHttpSession()

setSessionTimeOut

public static void setSessionTimeOut(int timeout)
Parameters:
timeout - a new session timeout in seconds.
See Also:
HttpSession.setMaxInactiveInterval(int), getHttpSession()

getFileURLs

public static java.util.ArrayList<java.lang.String> getFileURLs(java.lang.String relativeFolder)

getFileURLs

public static java.util.ArrayList<java.lang.String> getFileURLs(java.io.File folder)