Package org.zkoss.web.servlet.http
Class Https
- java.lang.Object
-
- org.zkoss.web.servlet.Servlets
-
- org.zkoss.web.servlet.http.Https
-
public class Https extends Servlets
The Servlet-related utilities.- Author:
- tomyeh
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.zkoss.web.servlet.Servlets
Servlets.ClientIdentifier
-
-
Field Summary
-
Fields inherited from class org.zkoss.web.servlet.Servlets
APPEND_PARAM, IGNORE_PARAM, OVERWRITE_URI, PASS_THRU_ATTR
-
-
Constructor Summary
Constructors Constructor Description Https()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
encodeRedirectURL(javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String uri, java.util.Map params, int mode)
Encodes an URL such that it can be used with HttpServletResponse.sendRedirect.static java.lang.String
getCompleteContext(javax.servlet.http.HttpServletRequest hreq)
Gets the complete context path, including protocol, server, ports, and context.static java.lang.String
getCompleteServerName(javax.servlet.http.HttpServletRequest hreq)
Gets the complete server name, including protocol, server, and ports.static java.lang.String
getCookieValue(javax.servlet.http.HttpServletRequest request, java.lang.String name)
Gets the value of the specified cookie, or null if not found.static java.lang.String
getOriginContextPath(javax.servlet.ServletRequest request)
Gets the original context path regardless of being forwarded or not.static java.lang.String
getOriginFullRequest(javax.servlet.ServletRequest request)
Returns the request uri + query string.static java.lang.String
getOriginFullServlet(javax.servlet.ServletRequest request)
Returns the servlet path + path info + query string.static java.lang.String
getOriginPathInfo(javax.servlet.ServletRequest request)
Gets the path info regardless of being forwarded or not.static java.lang.String
getOriginQueryString(javax.servlet.ServletRequest request)
Gets the query string regardless of being forwarded or not.static java.lang.String
getOriginRequestURI(javax.servlet.ServletRequest request)
Gets the request URI regardless of being forwarded or not.static java.lang.String
getOriginServletPath(javax.servlet.ServletRequest request)
Gets the original servlet path regardless of being forwarded or not.static java.lang.String
getServletURI(javax.servlet.http.HttpServletRequest request)
Returns the servlet uri of the request.static java.lang.String
getThisContextPath(javax.servlet.ServletRequest request)
Gets the context path of this page.static java.lang.String
getThisPathInfo(javax.servlet.ServletRequest request)
Gets the path info of this page.static java.lang.String
getThisQueryString(javax.servlet.ServletRequest request)
Gets the query string of this page.static java.lang.String
getThisRequestURI(javax.servlet.ServletRequest request)
Gets the request URI of this page.static java.lang.String
getThisServletPath(javax.servlet.ServletRequest request)
Gets the servlet path of this page.static byte[]
gzip(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.io.InputStream content1, byte[] content2)
Compresses the content into an byte array, or null if the browser doesn't support the compression (accept-encoding).static boolean
isValidPath(java.lang.String path)
Returns whether the specified path is valid.static java.lang.String
normalizePath(java.lang.String path)
Returns the normalized path, or null if it is invalid.static java.lang.String
sanitizePath(java.lang.String path)
Returns the path of the specified URI, or null if not found.static void
sendRedirect(javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String uri, java.util.Map params, int mode)
Redirects to another URL by prefixing the context path and encoding with encodeRedirectURL.static java.util.Date
toDate(java.lang.String sdate)
Converts a date string to a Date instance.static java.lang.String
toString(java.util.Date date)
Converts a data to a string complaint to HTTP protocol.static void
write(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Media media, boolean download, boolean repeatable)
Write the specified media to HTTP response.-
Methods inherited from class org.zkoss.web.servlet.Servlets
addExtendletContext, forward, forward, generateURI, getBrowser, getBrowser, getBrowser, getBrowser, getClientIdentifier, getContextPaths, getDetail, getExtendletContext, getExtension, getExtension, getLimitTimeOffer, getNormalPath, getRequestDispatcher, getResource, getResourceAsStream, getUserAgent, include, include, isBrowser, isBrowser, isForwarded, isIncluded, isOfferExpired, isServlet23, isServlet24, isServlet3, isUniversalURL, locate, removeExtendletContext, setClientIdentifier
-
-
-
-
Method Detail
-
gzip
public static final byte[] gzip(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.io.InputStream content1, byte[] content2) throws java.io.IOException
Compresses the content into an byte array, or null if the browser doesn't support the compression (accept-encoding).- Parameters:
content1
- the first part of the content to compress; null to ignore. If you have multiple input streams, use java.io.SequenceInputStream to concatenate themcontent2
- the second part of the content to compress; null to ignore.- Returns:
- the compressed result in an byte array, null if the browser doesn't support the compression.
- Throws:
java.io.IOException
- Since:
- 2.4.1
-
getCompleteServerName
public static final java.lang.String getCompleteServerName(javax.servlet.http.HttpServletRequest hreq)
Gets the complete server name, including protocol, server, and ports. Example, http://mysite.com:8080
-
getCompleteContext
public static final java.lang.String getCompleteContext(javax.servlet.http.HttpServletRequest hreq)
Gets the complete context path, including protocol, server, ports, and context. Example, http://mysite.com:8080/we
-
getCookieValue
public static final java.lang.String getCookieValue(javax.servlet.http.HttpServletRequest request, java.lang.String name)
Gets the value of the specified cookie, or null if not found.- Parameters:
name
- the cookie's name
-
getServletURI
public static final java.lang.String getServletURI(javax.servlet.http.HttpServletRequest request)
Returns the servlet uri of the request. A servlet uri is getServletPath() + getPathInfo(). In other words, a servlet uri is a request uri without the context path.However, HttpServletRequest.getRequestURI returns in encoded format, while this method returns in decode format (i.e., %nn is converted).
-
getThisContextPath
public static final java.lang.String getThisContextPath(javax.servlet.ServletRequest request)
Gets the context path of this page. Unlike getContextPath, it detects whether the current page is included.- Returns:
- "" if request is not a http request
-
getThisServletPath
public static final java.lang.String getThisServletPath(javax.servlet.ServletRequest request)
Gets the servlet path of this page. Unlike getServletPath, it detects whether the current page is included.- Returns:
- "/" if request is not a http request
-
getThisRequestURI
public static final java.lang.String getThisRequestURI(javax.servlet.ServletRequest request)
Gets the request URI of this page. Unlike getRequestURI, it detects whether the current page is included.- Returns:
- "/" if request is not a http request
-
getThisQueryString
public static final java.lang.String getThisQueryString(javax.servlet.ServletRequest request)
Gets the query string of this page. Unlike getQueryString, it detects whether the current page is included.- Returns:
- null if request is not a http request
-
getThisPathInfo
public static final java.lang.String getThisPathInfo(javax.servlet.ServletRequest request)
Gets the path info of this page. Unlike getPathInfo, it detects whether the current page is included.- Returns:
- null if request is not a http request
-
getOriginContextPath
public static final java.lang.String getOriginContextPath(javax.servlet.ServletRequest request)
Gets the original context path regardless of being forwarded or not. Unlike getContextPath, it won't be affected by forwarding.
-
getOriginServletPath
public static final java.lang.String getOriginServletPath(javax.servlet.ServletRequest request)
Gets the original servlet path regardless of being forwarded or not. Unlike getServletPath, it won't be affected by forwarding.
-
getOriginRequestURI
public static final java.lang.String getOriginRequestURI(javax.servlet.ServletRequest request)
Gets the request URI regardless of being forwarded or not. Unlike HttpServletRequest.getRequestURI, it won't be affected by forwarding.
-
getOriginPathInfo
public static final java.lang.String getOriginPathInfo(javax.servlet.ServletRequest request)
Gets the path info regardless of being forwarded or not. Unlike getPathInfo, it won't be affected by forwarding.
-
getOriginQueryString
public static final java.lang.String getOriginQueryString(javax.servlet.ServletRequest request)
Gets the query string regardless of being forwarded or not. Unlike getQueryString, it won't be affected by forwarding.
-
getOriginFullServlet
public static final java.lang.String getOriginFullServlet(javax.servlet.ServletRequest request)
Returns the servlet path + path info + query string. Because the path info is decoded, the return string can be considered as decoded. On the other handgetOriginFullRequest(javax.servlet.ServletRequest)
is in the encoded form.
-
getOriginFullRequest
public static final java.lang.String getOriginFullRequest(javax.servlet.ServletRequest request)
Returns the request uri + query string. UnlikegetOriginFullServlet(javax.servlet.ServletRequest)
, this is in the encoded form (e.g., %nn still exists, if any). Note: request uri = context path + servlet path + path info.
-
sendRedirect
public static final void sendRedirect(javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String uri, java.util.Map params, int mode) throws java.io.IOException, javax.servlet.ServletException
Redirects to another URL by prefixing the context path and encoding with encodeRedirectURL.It encodes the URI automatically (encodeRedirectURL). Parameters are encoded by
Encodes.setToQueryString(StringBuffer,Map)
.Like
Encodes.encodeURL(javax.servlet.ServletContext, javax.servlet.ServletRequest, javax.servlet.ServletResponse, java.lang.String)
, the servlet context is prefixed if uri starts with "/". In other words, to redirect other application, the complete URL must be used, e.g., http://host/other.Also, HttpServletResponse.encodeRedirectURL is called automatically.
- Parameters:
request
- the request; used only if params is not nullresponse
- the responseuri
- the redirect uri (not encoded; not including context-path), or null to denotegetOriginFullServlet(javax.servlet.ServletRequest)
It is OK to relevant (without leading '/'). If starts with "/", the context path of request is assumed. To reference to foreign context, use "~ctx/" where ctx is the context path of the foreign context (without leading '/').
Notice that, since 3.6.3,uri
could contain '*' (to denote locale and browser). Refer toServlets.locate(javax.servlet.ServletContext, javax.servlet.ServletRequest, java.lang.String, org.zkoss.util.resource.Locator)
.params
- the attributes that will be set when the redirection is back; null to ignore; format: (String, Object)mode
- one ofServlets.OVERWRITE_URI
,Servlets.IGNORE_PARAM
, andServlets.APPEND_PARAM
. It defines how to handle if both uri and params contains the same parameter.- Throws:
java.io.IOException
javax.servlet.ServletException
-
encodeRedirectURL
public static final java.lang.String encodeRedirectURL(javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String uri, java.util.Map params, int mode)
Encodes an URL such that it can be used with HttpServletResponse.sendRedirect.
-
toDate
public static final java.util.Date toDate(java.lang.String sdate) throws java.text.ParseException
Converts a date string to a Date instance. The format of the giving date string must be complaint to HTTP protocol.- Throws:
java.text.ParseException
- if the string is not valid
-
normalizePath
public static java.lang.String normalizePath(java.lang.String path)
Returns the normalized path, or null if it is invalid. It is invalid if it is null, or starts with "/" and contains "..".- Since:
- 10.0.0
-
isValidPath
public static boolean isValidPath(java.lang.String path)
Returns whether the specified path is valid. It is valid if it is null, or starts with "/" and doesn't contain "..".- Since:
- 10.0.0
-
sanitizePath
public static java.lang.String sanitizePath(java.lang.String path)
Returns the path of the specified URI, or null if not found. It is the same as new URI(uri).getPath(), except that it returns null if the path is invalid.- Since:
- 10.0.0
- See Also:
isValidPath(String)
-
toString
public static final java.lang.String toString(java.util.Date date)
Converts a data to a string complaint to HTTP protocol.
-
write
public static void write(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Media media, boolean download, boolean repeatable) throws java.io.IOException
Write the specified media to HTTP response.- Parameters:
response
- the HTTP response to write tomedia
- the content to be writtendownload
- whether to cause the download to show at the client. If true, it sets the Content-Disposition header.repeatable
- whether to useRepeatableInputStream
orRepeatableReader
to read the media. It is better to specify true if the media might be read repeatedly.- Throws:
java.io.IOException
- Since:
- 3.5.0
-
-