- java.lang.Object
-
- com.aoapps.servlet.http.HttpServletUtil
-
public class HttpServletUtil extends Object
Static utilities that may be useful by servlet/JSP/taglib environments.- Author:
- AO Industries, Inc.
- See Also:
ServletUtil
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceHttpServletUtil.UrlModifier
-
Field Summary
Fields Modifier and Type Field Description static StringMETHOD_DELETEstatic StringMETHOD_GETstatic StringMETHOD_HEADstatic StringMETHOD_OPTIONSstatic StringMETHOD_POSTstatic StringMETHOD_PUTstatic StringMETHOD_TRACE
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static StringbuildRedirectURL(HttpServletRequest request, HttpServletResponse response, String href, URIParameters params, boolean absolute, boolean canonical)static StringbuildRedirectURL(HttpServletRequest request, HttpServletResponse response, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)Builds a URL that should be used for a redirect location, with path resolved relative to the given request.static StringbuildRedirectURL(HttpServletRequest request, HttpServletResponse response, String servletPath, String href, URIParameters params, boolean absolute, boolean canonical)static StringbuildRedirectURL(HttpServletRequest request, HttpServletResponse response, String servletPath, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)Builds a URL that should be used for a redirect location, including all the proper URL conversions.static StringbuildRedirectURL(JspContext jspContext, String href, URIParameters params, boolean absolute, boolean canonical)static StringbuildRedirectURL(JspContext jspContext, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)static StringbuildRedirectURL(PageContext pageContext, String href, URIParameters params, boolean absolute, boolean canonical)static StringbuildRedirectURL(PageContext pageContext, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)static StringbuildURL(HttpServletRequest request, HttpServletResponse response, String url, URIParameters params, boolean absolute, boolean canonical)static StringbuildURL(HttpServletRequest request, HttpServletResponse response, String url, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)Builds a URL with path resolved relative to the given request.static StringbuildURL(HttpServletRequest request, HttpServletResponse response, String servletPath, String url, URIParameters params, boolean absolute, boolean canonical)static StringbuildURL(HttpServletRequest request, HttpServletResponse response, String servletPath, String url, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)Builds a URL with all the proper URL conversions.static StringbuildURL(JspContext jspContext, String url, URIParameters params, boolean absolute, boolean canonical)static StringbuildURL(JspContext jspContext, String url, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)static StringbuildURL(PageContext pageContext, String url, URIParameters params, boolean absolute, boolean canonical)static StringbuildURL(PageContext pageContext, String url, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)static <S extends HttpServlet>
voiddoOptions(HttpServletResponse response, Class<S> stopClass, Class<? extends S> thisClass, String doGet, String doPost, String doPut, String doDelete, Class<?>[] paramTypes)A reusable doOptions implementation for servlets.static StringgetAbsolutePath(HttpServletRequest request, String path)Resolves a possibly page-relative path to a context-absolute path.static StringgetAbsoluteURL(HttpServletRequest request, boolean contextRelative, String path)Gets an absolute URL for the given path.static voidgetAbsoluteURL(HttpServletRequest request, boolean contextRelative, String path, Encoder encoder, Appendable out)Gets an absolute URL for the given path.static voidgetAbsoluteURL(HttpServletRequest request, boolean contextRelative, String path, Appendable out)Gets an absolute URL for the given path.static StringgetAbsoluteURL(HttpServletRequest request, String path)Gets an absolute URL for the given path.static StringgetAbsoluteURL(HttpServletRequest request, String path, boolean contextRelative)Deprecated.Please usegetAbsoluteURL(javax.servlet.http.HttpServletRequest, boolean, java.lang.String)instead.static voidgetAbsoluteURL(HttpServletRequest request, String path, boolean contextRelative, Encoder encoder, Appendable out)static voidgetAbsoluteURL(HttpServletRequest request, String path, boolean contextRelative, Appendable out)Deprecated.static voidgetAbsoluteURL(HttpServletRequest request, String path, Encoder encoder, Appendable out)Gets an absolute URL for the given path.static voidgetAbsoluteURL(HttpServletRequest request, String path, Appendable out)Gets an absolute URL for the given path.static Method[]getAllDeclaredMethods(Class<?> stopClass, Class<?> c)static StringgetContextRequestUri(HttpServletRequest request)Gets the current request URI in context-absolute form.static StringgetSubmittedFileName(Part part)Gets the submitted filename for a file upload ornullwhen unknown.static booleanisGooglebot(HttpServletRequest request)Determines if the requestor is Googlebot as described at: http://www.google.com/support/webmasters/bin/answer.py?answer=80553static voidsendRedirect(int status, HttpServletRequest request, HttpServletResponse response, String href, URIParameters params, boolean absolute, boolean canonical)static voidsendRedirect(int status, HttpServletRequest request, HttpServletResponse response, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)static voidsendRedirect(int status, HttpServletRequest request, HttpServletResponse response, String servletPath, String href, URIParameters params, boolean absolute, boolean canonical)static voidsendRedirect(int status, HttpServletRequest request, HttpServletResponse response, String servletPath, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)static voidsendRedirect(int status, HttpServletResponse response, String location)Sends a redirect to the provided location.static voidsendRedirect(int status, JspContext jspContext, String href, URIParameters params, boolean absolute, boolean canonical)static voidsendRedirect(int status, JspContext jspContext, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)static voidsendRedirect(int status, PageContext pageContext, String href, URIParameters params, boolean absolute, boolean canonical)static voidsendRedirect(int status, PageContext pageContext, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical)
-
-
-
Field Detail
-
METHOD_DELETE
public static final String METHOD_DELETE
- See Also:
- Constant Field Values
-
METHOD_HEAD
public static final String METHOD_HEAD
- See Also:
- Constant Field Values
-
METHOD_GET
public static final String METHOD_GET
- See Also:
- Constant Field Values
-
METHOD_OPTIONS
public static final String METHOD_OPTIONS
- See Also:
- Constant Field Values
-
METHOD_POST
public static final String METHOD_POST
- See Also:
- Constant Field Values
-
METHOD_PUT
public static final String METHOD_PUT
- See Also:
- Constant Field Values
-
METHOD_TRACE
public static final String METHOD_TRACE
- See Also:
- Constant Field Values
-
-
Method Detail
-
isGooglebot
public static boolean isGooglebot(HttpServletRequest request)
Determines if the requestor is Googlebot as described at: http://www.google.com/support/webmasters/bin/answer.py?answer=80553
-
getAbsolutePath
public static String getAbsolutePath(HttpServletRequest request, String path) throws MalformedURLException
Resolves a possibly page-relative path to a context-absolute path.- Parameters:
path- The absolute URL, context-absolute path, or page-relative path- Throws:
MalformedURLException- See Also:
Dispatcher.getCurrentPagePath(javax.servlet.http.HttpServletRequest),URIResolver.getAbsolutePath(java.lang.String, java.lang.String)
-
getAbsoluteURL
public static String getAbsoluteURL(HttpServletRequest request, boolean contextRelative, String path)
Gets an absolute URL for the given path. This includes protocol, port, context path (optional), and path. No URL rewriting is performed.- Parameters:
contextRelative- Whentrue, includesHttpServletRequest.getContextPath()in the URL.path- The path appended to the URL verbatim. To support page-relative paths, first usegetAbsolutePath(javax.servlet.http.HttpServletRequest, java.lang.String)
-
getAbsoluteURL
@Deprecated public static String getAbsoluteURL(HttpServletRequest request, String path, boolean contextRelative)
Deprecated.Please usegetAbsoluteURL(javax.servlet.http.HttpServletRequest, boolean, java.lang.String)instead.
-
getAbsoluteURL
public static String getAbsoluteURL(HttpServletRequest request, String path)
Gets an absolute URL for the given path. This includes protocol, port, context path, and path. No URL rewriting is performed.- Parameters:
path- The path appended to the URL verbatim. To support page-relative paths, first usegetAbsolutePath(javax.servlet.http.HttpServletRequest, java.lang.String)
-
getAbsoluteURL
public static void getAbsoluteURL(HttpServletRequest request, boolean contextRelative, String path, Appendable out) throws IOException
Gets an absolute URL for the given path. This includes protocol, port, context path (optional), and path. No URL rewriting is performed.- Parameters:
contextRelative- Whentrue, includesHttpServletRequest.getContextPath()in the URL.path- The path appended to the URL verbatim. To support page-relative paths, first usegetAbsolutePath(javax.servlet.http.HttpServletRequest, java.lang.String)- Throws:
IOException
-
getAbsoluteURL
@Deprecated public static void getAbsoluteURL(HttpServletRequest request, String path, boolean contextRelative, Appendable out) throws IOException
Deprecated.- Throws:
IOException
-
getAbsoluteURL
public static void getAbsoluteURL(HttpServletRequest request, String path, Appendable out) throws IOException
Gets an absolute URL for the given path. This includes protocol, port, context path, and path. No URL rewriting is performed.- Parameters:
path- The path appended to the URL verbatim. To support page-relative paths, first usegetAbsolutePath(javax.servlet.http.HttpServletRequest, java.lang.String)- Throws:
IOException
-
getAbsoluteURL
public static void getAbsoluteURL(HttpServletRequest request, boolean contextRelative, String path, Encoder encoder, Appendable out) throws IOException
Gets an absolute URL for the given path. This includes protocol, port, context path (optional), and path. No URL rewriting is performed.- Parameters:
contextRelative- Whentrue, includesHttpServletRequest.getContextPath()in the URL.path- The path appended to the URL verbatim. To support page-relative paths, first usegetAbsolutePath(javax.servlet.http.HttpServletRequest, java.lang.String)- Throws:
IOException
-
getAbsoluteURL
@Deprecated public static void getAbsoluteURL(HttpServletRequest request, String path, boolean contextRelative, Encoder encoder, Appendable out) throws IOException
Deprecated.- Throws:
IOException
-
getAbsoluteURL
public static void getAbsoluteURL(HttpServletRequest request, String path, Encoder encoder, Appendable out) throws IOException
Gets an absolute URL for the given path. This includes protocol, port, context path, and path. No URL rewriting is performed.- Parameters:
path- The path appended to the URL verbatim. To support page-relative paths, first usegetAbsolutePath(javax.servlet.http.HttpServletRequest, java.lang.String)- Throws:
IOException
-
buildRedirectURL
public static String buildRedirectURL(HttpServletRequest request, HttpServletResponse response, String servletPath, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException
Builds a URL that should be used for a redirect location, including all the proper URL conversions. This includes:- Converting a page-relative path to a context-absolute path starting with a slash (/), resolving ./ and ../
- Adding any additional parameters
- Optionally modifying the URL
- Encoding any URL path characters not defined in RFC 3986: Reserved Characters
- Converting any context-absolute path to a site-absolute path by prefixing contextPath
- Optionally convert to an absolute URL:
http[s]://… - Rewrite with
HttpServletResponse.encodeRedirectURL(java.lang.String) - Final US-ASCII encoding since Location must always be RFC 3986
- Parameters:
href- The absolute URL, context-absolute path, or page-relative pathcanonical- The value to use forCanonicalduringHttpServletResponse.encodeRedirectURL(java.lang.String)- Throws:
MalformedURLException- See Also:
sendRedirect(int, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean)
-
buildRedirectURL
public static String buildRedirectURL(HttpServletRequest request, HttpServletResponse response, String servletPath, String href, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException
-
buildRedirectURL
public static String buildRedirectURL(HttpServletRequest request, HttpServletResponse response, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException
Builds a URL that should be used for a redirect location, with path resolved relative to the given request.- Throws:
MalformedURLException- See Also:
Dispatcher.getCurrentPagePath(javax.servlet.http.HttpServletRequest),buildRedirectURL(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean),sendRedirect(int, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean)
-
buildRedirectURL
public static String buildRedirectURL(HttpServletRequest request, HttpServletResponse response, String href, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException
-
buildRedirectURL
public static String buildRedirectURL(PageContext pageContext, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException
- Throws:
MalformedURLException- See Also:
buildRedirectURL(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean),sendRedirect(int, javax.servlet.jsp.PageContext, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean)
-
buildRedirectURL
public static String buildRedirectURL(PageContext pageContext, String href, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException
-
buildRedirectURL
public static String buildRedirectURL(JspContext jspContext, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException
- Throws:
MalformedURLException- See Also:
buildRedirectURL(javax.servlet.jsp.PageContext, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean),sendRedirect(int, javax.servlet.jsp.JspContext, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean)
-
buildRedirectURL
public static String buildRedirectURL(JspContext jspContext, String href, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException
-
buildURL
public static String buildURL(HttpServletRequest request, HttpServletResponse response, String servletPath, String url, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException
Builds a URL with all the proper URL conversions. This includes:- Converting a page-relative path to a context-absolute path starting with a slash (/), resolving ./ and ../
- Adding any additional parameters
- Optionally modifying the URL
- Encoding any URL path characters not defined in RFC 3986: Reserved Characters
- Converting any context-absolute path to a site-absolute path by prefixing contextPath
- Optionally convert to an absolute URL:
http[s]://… - Rewrite with
HttpServletResponse.encodeURL(java.lang.String)
- Parameters:
url- The absolute URL, context-absolute path, or page-relative pathcanonical- The value to use forCanonicalduringHttpServletResponse.encodeURL(java.lang.String)- Throws:
MalformedURLException
-
buildURL
public static String buildURL(HttpServletRequest request, HttpServletResponse response, String servletPath, String url, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException
-
buildURL
public static String buildURL(HttpServletRequest request, HttpServletResponse response, String url, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException
Builds a URL with path resolved relative to the given request.- Throws:
MalformedURLException- See Also:
Dispatcher.getCurrentPagePath(javax.servlet.http.HttpServletRequest),buildURL(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean)
-
buildURL
public static String buildURL(HttpServletRequest request, HttpServletResponse response, String url, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException
-
buildURL
public static String buildURL(PageContext pageContext, String url, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException
-
buildURL
public static String buildURL(PageContext pageContext, String url, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException
-
buildURL
public static String buildURL(JspContext jspContext, String url, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException
-
buildURL
public static String buildURL(JspContext jspContext, String url, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException
-
sendRedirect
public static void sendRedirect(int status, HttpServletResponse response, String location) throws IllegalStateException, IOExceptionSends a redirect to the provided location. Encodes the location to US-ASCII format. Response must not be committed.- Throws:
IllegalStateException- when the response is already committedIOException- See Also:
buildRedirectURL(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean)
-
sendRedirect
public static void sendRedirect(int status, HttpServletRequest request, HttpServletResponse response, String servletPath, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException, IllegalStateException, IOException- Throws:
IllegalStateException- when the response is already committedMalformedURLExceptionIOException- See Also:
buildRedirectURL(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean),sendRedirect(int, javax.servlet.http.HttpServletResponse, java.lang.String)
-
sendRedirect
public static void sendRedirect(int status, HttpServletRequest request, HttpServletResponse response, String servletPath, String href, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException, IllegalStateException, IOException- Throws:
MalformedURLExceptionIllegalStateExceptionIOException- See Also:
sendRedirect(int, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean)
-
sendRedirect
public static void sendRedirect(int status, HttpServletRequest request, HttpServletResponse response, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException, IllegalStateException, IOException- Throws:
IllegalStateException- when the response is already committedMalformedURLExceptionIOException- See Also:
buildRedirectURL(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean),sendRedirect(int, javax.servlet.http.HttpServletResponse, java.lang.String)
-
sendRedirect
public static void sendRedirect(int status, HttpServletRequest request, HttpServletResponse response, String href, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException, IllegalStateException, IOException
-
sendRedirect
public static void sendRedirect(int status, PageContext pageContext, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException, IllegalStateException, IOException- Throws:
IllegalStateException- when the response is already committedMalformedURLExceptionIOException- See Also:
buildRedirectURL(javax.servlet.jsp.PageContext, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean),sendRedirect(int, javax.servlet.http.HttpServletResponse, java.lang.String)
-
sendRedirect
public static void sendRedirect(int status, PageContext pageContext, String href, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException, IllegalStateException, IOException
-
sendRedirect
public static void sendRedirect(int status, JspContext jspContext, String href, URIParameters params, HttpServletUtil.UrlModifier modifier, boolean absolute, boolean canonical) throws MalformedURLException, IllegalStateException, IOException- Throws:
IllegalStateException- when the response is already committedMalformedURLExceptionIOException- See Also:
buildRedirectURL(javax.servlet.jsp.JspContext, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean),sendRedirect(int, javax.servlet.jsp.PageContext, java.lang.String, com.aoapps.net.URIParameters, com.aoapps.servlet.http.HttpServletUtil.UrlModifier, boolean, boolean)
-
sendRedirect
public static void sendRedirect(int status, JspContext jspContext, String href, URIParameters params, boolean absolute, boolean canonical) throws MalformedURLException, IllegalStateException, IOException
-
getContextRequestUri
public static String getContextRequestUri(HttpServletRequest request)
Gets the current request URI in context-absolute form. The contextPath stripped.
-
doOptions
public static <S extends HttpServlet> void doOptions(HttpServletResponse response, Class<S> stopClass, Class<? extends S> thisClass, String doGet, String doPost, String doPut, String doDelete, Class<?>[] paramTypes)
A reusable doOptions implementation for servlets.
-
getSubmittedFileName
public static String getSubmittedFileName(Part part)
Gets the submitted filename for a file upload ornullwhen unknown. In addition to the default implementation ofPart.getSubmittedFileName(), also:- Applies a MSIE fix
- Protects against '/' or NULL characters, since servers have POSIX filesystem
- Returns
nulland never empty string (servlet spec is not clear here)
- See Also:
Part.getSubmittedFileName()
-
-