|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.bristle.javalib.net.http.HttpUtil
public class HttpUtil
This class contains utility routines for interacting with the HTTP protocol.
Usage: - The typical scenario for using this class is: - To get the user's credentials, prompting if necessary: HttpUtil.Credentials credentials = HttpUtil.getCredentials (request, response, "", !HttpUtil.blnREJECT_CURRENT_CREDENTIALS, HttpUtil.blnASK_CLIENT_IF_NECESSARY); - To reject any credentials the user may previously have specified, forcing a new prompt: HttpUtil.Credentials credentials = HttpUtil.getCredentials (request, response, "", HttpUtil.blnREJECT_CURRENT_CREDENTIALS, HttpUtil.blnASK_CLIENT_IF_NECESSARY); - To get any credentials the user may previously have specified, without permitting a new prompt: HttpUtil.Credentials credentials = HttpUtil.getCredentials (request, null, null, !HttpUtil.blnREJECT_CURRENT_CREDENTIALS, !HttpUtil.blnASK_CLIENT_IF_NECESSARY); - To get only the username, without permitting a new prompt: String strUsername = HttpUtil.getUsername(request); Assumptions: Effects: - Interacts with the HTTP client via the HTTP protocol. If the HTTP client is a Web Browser, getCredentials may cause it to prompt its interactive user for credentials. Anticipated Changes: Notes: Implementation Notes: Portability Issues: Revision History: $Log$
Nested Class Summary | |
---|---|
static class |
HttpUtil.Credentials
Nested class used as argument of getCredentials, so that it can allocate and return multiple Strings. |
static class |
HttpUtil.Tester
Each class contains a Tester inner class with a main() for easier unit testing. |
Field Summary | |
---|---|
static boolean |
blnASK_CLIENT_IF_NECESSARY
Constant for use as parameter to getCredentials(). |
static boolean |
blnREJECT_CURRENT_CREDENTIALS
Constant for use as parameter to getCredentials(). |
static boolean |
blnUSE_PATH_TO_SERVLET_FOLDER
Constant for use as parameter to getConfigFileName(). |
static boolean |
blnUSE_SERVER_NAME
Constant for use as parameter to getConfigFileName(). |
private static Map |
st_mapAbbrevs
|
static String |
strCONTENT_TYPE_HTML
HTML content-type. |
static String |
strCONTENT_TYPE_XML
XML content-type. |
static String |
strUSER_AGENT_UNKNOWN
Constant included in the value returned by getHttpUserAgentAbbrev() when the specified user agent string and/or OS are unknown. |
Constructor Summary | |
---|---|
HttpUtil()
|
Method Summary | |
---|---|
static String |
getConfigFileName(HttpServletRequest request,
String strPrefix,
String strSuffix)
Get the fully qualified filename of a config file. |
static String |
getConfigFileName(HttpServletRequest request,
String strPrefix,
String strSuffix,
boolean blnUsePathToServletFolder,
boolean blnUseServerName)
Get the fully qualified filename of a config file. |
static GetOpt |
getCookies(HttpServletRequest request)
Returns a GetOpt object that can be used to get the values of the HTTP cookies of the specified HttpServletRequest object. |
static HttpUtil.Credentials |
getCredentials(HttpServletRequest request,
HttpServletResponse response,
String strRealm,
boolean blnRejectCurrentCredentials,
boolean blnAskClientIfNecessary)
Get the credentials of the current user from the HTTP client via the HTTP protocol. |
static String |
getFormattedStringOfParameters(HttpServletRequest request)
Return a formatted multi-line string containing all parameter names and values of the specified HttpServletRequest. |
static String |
getFullRequestURI(HttpServletRequest request)
Return the full URI string, including URI parameters. |
static String |
getFullRequestURL(HttpServletRequest request)
Return the full URL string, including URL parameters. |
static GetOpt |
getHeaders(HttpServletRequest request)
Returns a GetOpt object that can be used to get the values of the HTTP headers of the specified HttpServletRequest object. |
static String |
getHttpUserAgentAbbrev(String strHttpUserAgent)
Return a short human-readable abbreviation for the Web browser that is identified by the specified user agent string, which should be the full string value of the HTTP "user-agent" header from a common Web browser. |
static Document |
getInfoAvailableToServlet(HttpServlet servlet,
HttpServletRequest request)
Get an XML DOM document containing all information available to a servlet. |
static int |
getParam_int(HttpServletRequest request,
String strName)
Returns the int value of the parameter with the specified name, getting it from the specified HttpServletRequest object. |
static int |
getParam_int(HttpServletRequest request,
String strName,
int intDefault)
Returns the int value of the parameter with the specified name, getting it from the specified HttpServletRequest object. |
static String |
getParam(HttpServletRequest request,
String strName)
Returns the value of the parameter with the specified name, getting it from the specified HttpServletRequest object, and mapping empty values to null. |
static Integer |
getParamInteger(HttpServletRequest request,
String strName)
Returns the Integer value of the parameter with the specified name, getting it from the specified HttpServletRequest object, and mapping non-integer values to null. |
static boolean |
getParamPresent(HttpServletRequest request,
String strName)
Returns true if the parameter with the specified name is present in the HttpServletRequest object; false otherwise. |
static GetOpt |
getParams(HttpServletRequest request)
Returns a GetOpt object that can be used to get the values of the HTTP parameters of the specified HttpServletRequest object. |
static String |
getParamString(HttpServletRequest request,
String strName,
String strDefault)
Returns the value of the parameter with the specified name, getting it from the specified HttpServletRequest object, and mapping empty values ("") and missing parameters (null) to the specified default. |
static Object |
getServletAttribute(HttpServletRequest request,
String strName)
Get the value of the specified attribute from the ServletContext (application context) of the servlet associated with the specified HttpServletRequest. |
static ServletContext |
getServletContext(HttpServletRequest request)
Get the ServletContext of the specified request. |
static String |
getServletDirectory(GenericServlet servlet)
Get the full path of the directory where the specified servlet resides. |
static String |
getServletDirectory(HttpServletRequest request)
Get the full path of the directory where the servlet associated with the specified request resides. |
static String |
getServletDirectory(ServletContext context)
Get the full path of the directory associated with the specified ServletContext. |
static int |
getSessionAttribute_int(HttpServletRequest request,
String strName,
int intDefault)
Get the int value of the specified attribute from the Session associated with the specified HttpServletRequest. |
static Object |
getSessionAttribute(HttpServletRequest request,
String strName)
Get the value of the specified attribute from the Session associated with the specified HttpServletRequest. |
static String |
getUsername(HttpServletRequest request)
Get the uppercased username of the current user from the HTTP headers of the specified HTTP request. |
static Reader |
openHttpReader(String strURL,
HttpUtil.Credentials credentials)
Open a Reader to the specified URL, using the specified HTTP credentials. |
static Reader |
openURLReader(String strURL)
Open a Reader to the specified URL. |
static void |
setResponseDownloadFilename(HttpServletResponse response,
String strFilename)
Set the response header to indentify the response content as a file download, specifying a default name for the HTTP client to use when saving the file. |
static void |
setResponseNoCache(HttpServletResponse response)
Set the response header to prevent caching of the page by the browser. |
static void |
setServletAttribute(HttpServletRequest request,
String strName,
Object obj)
Set the value of the specified attribute in the ServletContext (application context) of the servlet associated with the specified HttpServletRequest. |
static void |
setSessionAttribute(HttpServletRequest request,
String strName,
Object obj)
Set the value of the specified attribute in the Session associated with the specified HttpServletRequest. |
static void |
writeFriendlyErrorMessage(HttpServletResponse response,
String strMessage,
Throwable throwable)
Write a friendly error message to the HTTP client. |
static void |
writeInfoAvailableToServlet(HttpServlet servlet,
HttpServletRequest request,
HttpServletResponse response)
Write as an XML stream to the HTTP client all information available to a servlet. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static Map st_mapAbbrevs
public static final String strCONTENT_TYPE_HTML
public static final String strCONTENT_TYPE_XML
public static final String strUSER_AGENT_UNKNOWN
public static final boolean blnREJECT_CURRENT_CREDENTIALS
public static final boolean blnASK_CLIENT_IF_NECESSARY
public static final boolean blnUSE_PATH_TO_SERVLET_FOLDER
public static final boolean blnUSE_SERVER_NAME
Constructor Detail |
---|
public HttpUtil()
Method Detail |
---|
public static HttpUtil.Credentials getCredentials(HttpServletRequest request, HttpServletResponse response, String strRealm, boolean blnRejectCurrentCredentials, boolean blnAskClientIfNecessary) throws IOException
request
- The HttpServletRequest object used to get the
HTTP headers from the HTTP client.response
- The HttpServletResponse object used to tell the
HTTP client to re-issue the HTTP request.
Can be null if blnAskClientIfNecessary is false.strRealm
- String to pass to the HTTP client as the "realm".
If the client is a Web Browser, it may show this
string to its user when prompting for credentials.
Can be null if blnAskClientIfNecessary is false.blnRejectCurrentCredentials
- Boolean specifying whether to reject any
credentials already in the HTTP headers.blnAskClientIfNecessary
- Boolean specifying whether to allow any
interaction with the HTTP client that may cause
it to prompt its user for new credentials.
IOException
- When an I/O error occurs trying to tell the
HTTP client to re-issue the HTTP request with
credentials, or when an error occurs trying to
decode the credentials sent by the HTTP client.public static String getUsername(HttpServletRequest request)
request
- The HttpServletRequest object used to get the
HTTP headers from the HTTP client.
getCredentials(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, boolean, boolean)
public static GetOpt getParams(HttpServletRequest request)
request
- The HttpServletRequest to get the parameters from.
public static GetOpt getHeaders(HttpServletRequest request)
request
- The HttpServletRequest to get the headers from.
public static GetOpt getCookies(HttpServletRequest request)
request
- The HttpServletRequest to get the cookies from.
public static boolean getParamPresent(HttpServletRequest request, String strName)
request
- The HttpServletRequest object to get the value from.strName
- Name of the parameter.
public static String getParam(HttpServletRequest request, String strName)
request
- The HttpServletRequest object to get the value from.strName
- Name of the parameter.
public static String getParamString(HttpServletRequest request, String strName, String strDefault)
request
- The HttpServletRequest object to get the value from.strName
- Name of the parameter.strDefault
- Default value to use if no parameter value exists.
public static Integer getParamInteger(HttpServletRequest request, String strName)
request
- The HttpServletRequest object to get the value from.strName
- Name of the parameter.
public static int getParam_int(HttpServletRequest request, String strName)
request
- The HttpServletRequest object to get the value from.strName
- Name of the parameter.
NullPointerException
- When the specified value cannot be interpreted as an
int.public static int getParam_int(HttpServletRequest request, String strName, int intDefault)
request
- The HttpServletRequest object to get the value from.strName
- Name of the parameter.intDefault
- Default value to use if no valid parameter value exists.
public static Reader openURLReader(String strURL) throws IOException
strURL
- URL to connect to.
IOException
- When an I/O error occurs reading from the URL.public static Reader openHttpReader(String strURL, HttpUtil.Credentials credentials) throws IOException
strURL
- URL to connect to.credentials
- Credentials to use in connecting.
IOException
- When an I/O error occurs reading from the URL.public static String getFullRequestURL(HttpServletRequest request)
request
- The HttpServletRequest object of the servlet.public static String getFullRequestURI(HttpServletRequest request)
request
- The HttpServletRequest object of the servlet.public static ServletContext getServletContext(HttpServletRequest request)
request
- The HttpServletRequest object.
public static String getServletDirectory(ServletContext context)
context
- The ServletContext
public static String getServletDirectory(GenericServlet servlet)
servlet
- The HttpServlet object.
public static String getServletDirectory(HttpServletRequest request)
request
- The HttpServletRequest object.
public static Object getSessionAttribute(HttpServletRequest request, String strName)
request
- The HttpServletRequest object.strName
- The attribute name.
public static int getSessionAttribute_int(HttpServletRequest request, String strName, int intDefault)
request
- The HttpServletRequest object.strName
- The attribute name.intDefault
- Default value to use if an error occurs.
public static void setSessionAttribute(HttpServletRequest request, String strName, Object obj)
request
- The HttpServletRequest object.strName
- The attribute name.public static Object getServletAttribute(HttpServletRequest request, String strName)
request
- The HttpServletRequest object.strName
- The attribute name.
public static void setServletAttribute(HttpServletRequest request, String strName, Object obj)
request
- The HttpServletRequest object.strName
- The attribute name.public static Document getInfoAvailableToServlet(HttpServlet servlet, HttpServletRequest request)
servlet
- The HttpServlet object defining the servlet.request
- The HttpServletRequest object of the servlet.
public static void writeInfoAvailableToServlet(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) throws IOException
servlet
- The HttpServlet object defining the servlet.request
- The HttpServletRequest object of the servlet.response
- The HttpServletResponse object of the servlet.
IOException
- When an I/O error occurs during interaction
with the servlet, request, or response.public static void writeFriendlyErrorMessage(HttpServletResponse response, String strMessage, Throwable throwable)
response
- The HttpServletResponse object of the servlet.strMessage
- A String to put in front of the error message.throwable
- The Throwable containing the error message.public static void setResponseNoCache(HttpServletResponse response)
response
- The HttpServletResponse object.public static void setResponseDownloadFilename(HttpServletResponse response, String strFilename)
response
- The HttpServletResponse object.strFilename
- The name of the file.public static String getHttpUserAgentAbbrev(String strHttpUserAgent)
strHttpUserAgent
- The full string value of the "user-agent"
header from a common Web browser.
public static String getFormattedStringOfParameters(HttpServletRequest request)
request
- The HttpServletRequest object.
public static String getConfigFileName(HttpServletRequest request, String strPrefix, String strSuffix, boolean blnUsePathToServletFolder, boolean blnUseServerName)
request
- HttpServletRequest object.strPrefix
- The prefix to use, or null or empty string.strSuffix
- The suffix to use, or null or empty string.blnUsePathToServletFolder
- If false, the path to the servlet folder and its
trailing path separator are omitted.blnUseServerName
- If false, servername is omitted, so the prefix
is immediately followed by the suffix.
public static String getConfigFileName(HttpServletRequest request, String strPrefix, String strSuffix)
getConfigFileName(HttpServletRequest, String, String,
boolean, boolean)
specifying true for the 2 boolean parameters.
request
- HttpServletRequest object.strPrefix
- The prefix to use, or null or empty string.strSuffix
- The suffix to use, or null or empty string.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |