| java.lang.Object org.zkoss.web.servlet.http.Encodes
Encodes | public class Encodes (Code) | | Encoding utilities for servlets.
author: tomyeh See Also: Https |
Inner Class :public static interface URLEncoder | |
Constructor Summary | |
protected | Encodes() |
Method Summary | |
final public static StringBuffer | addToQueryString(StringBuffer sb, Map params) /** Appends a map of parameters (name=value) to a query string.
It returns the query string preceding with '?' if any parameter exists,
or an empty string if no parameter at all. | final public static StringBuffer | addToQueryString(StringBuffer sb, String name, Object value) Appends a parameter (name=value) to a query string.
This method automatically detects whether other query is already
appended. | final public static boolean | containsQuery(String str, String name) Tests whether a parameter exists in the query string. | final public static String | encodeURI(String s) Does the HTTP encoding for the URI location. | final public static String | encodeURIComponent(String s) Does the HTTP encoding for an URI query parameter.
For example, '/' is translated to '%2F'.
Both name and value must be encoded seperately. | final public static String | encodeURL(ServletContext ctx, ServletRequest request, ServletResponse response, String uri) Encodes an URL.
It resolves "*" contained in URI, if any, to the proper Locale,
and the browser code.
Refer to
Servlets.locate(ServletContextServletRequestStringLocator) for details. | public static URLEncoder | getURLEncoder() Returns the URI encoder, or null if no uri encoder. | final public static String | removeFromQueryString(String str, String name) Remove all name/value pairs of the specified name from a string. | final public static StringBuffer | removeFromQueryString(StringBuffer sb, String name) Remove all name/value pairs of the specified name from a string. | final public static String | setToQueryString(String str, String name, Object value) Sets the parameter (name=value) to a query string.
If the name already exists in the query string, it will be removed first.
If your name has appeared in the string, it will replace
your new value to the query string.
Otherwise, it will append the name/value to the new string. | final public static StringBuffer | setToQueryString(StringBuffer sb, String name, Object value) Sets the parameter (name=value) to a query string. | final public static String | setToQueryString(String str, Map params) Sets a map of parameters (name=value) to a query string. | final public static StringBuffer | setToQueryString(StringBuffer sb, Map params) Sets a map of parameters (name=value) to a query string. | public static void | setURLEncoder(URLEncoder encoder) Sets the URI encoder. |
Encodes | protected Encodes()(Code) | | |
addToQueryString | final public static StringBuffer addToQueryString(StringBuffer sb, Map params) throws UnsupportedEncodingException(Code) | | /** Appends a map of parameters (name=value) to a query string.
It returns the query string preceding with '?' if any parameter exists,
or an empty string if no parameter at all. Thus, you could do
request.getRequestDispatcher(
addToQueryStirng(new StringBuffer(uri), params).toString());
Since RequestDispatcher.include and forward do not allow wrapping
the request and response -- see spec and the implementation of both
Jetty and Catalina, we have to use this method to pass the parameters.
Parameters: params - a map of parameters; format: (String, Object) or(String, Object[]); null is OK |
addToQueryString | final public static StringBuffer addToQueryString(StringBuffer sb, String name, Object value) throws UnsupportedEncodingException(Code) | | Appends a parameter (name=value) to a query string.
This method automatically detects whether other query is already
appended. If so, & is used instead of ?.
The query string might contain servlet path and other parts.
This method starts the searching from the first '?'.
If the query string doesn't contain '?', it is assumed to be a string
without query's name/value pairs.
Parameters: value - the value. If it is null, only name is appened.If it is an array of objects, multipe pairs of name=value[j] willbe appended. |
containsQuery | final public static boolean containsQuery(String str, String name)(Code) | | Tests whether a parameter exists in the query string.
|
encodeURL | final public static String encodeURL(ServletContext ctx, ServletRequest request, ServletResponse response, String uri) throws ServletException(Code) | | Encodes an URL.
It resolves "*" contained in URI, if any, to the proper Locale,
and the browser code.
Refer to
Servlets.locate(ServletContextServletRequestStringLocator) for details.
In additions, if uri starts with "/", the context path, e.g.,
/zkdemo, is prefixed.
In other words, "/ab/cd" means it is relevant to the servlet
context path (say, "/zkdemo").
If uri starts with "~abc/", it means it is relevant to a foreign
Web context called /abc. And, it will be converted to "/abc/" first
(without prefix request.getContextPath()).
Finally, the uri is encoded by HttpServletResponse.encodeURL.
This method invokes
Encodes.encodeURI for any characters
before '?'. However, it does NOT encode any character after '?'. Thus,
you might hvae to invoke
Encodes.encodeURIComponent or
Encodes.addToQueryString(StringBuffer,Map) to encode the query parameters.
The URL Prefix and Encoder
In a sophisticated environment, e.g.,
Reverse Proxy,
the encoded URL might have to be prefixed with some special prefix.
To do that, you can implement
URLEncoder , and then
specify it with
Encodes.setURLEncoder .
When
Encodes.encodeURL encodes an URL, it will check
any URL encoder is defined (by
Encodes.setURLEncoder .
If any, it will invoke
URLEncoder.encodeURL with
the encoded URL to give it the last chance to post-process it, such
as inserting a special prefix.
Parameters: request - the request; never null Parameters: response - the response; never null Parameters: uri - it must be null, empty or starts with "/". It might contain"*" for current browser code and Locale. Parameters: ctx - the servlet context; used only if "*" is contained in uri exception: IndexOutOfBoundException - if uri is empty See Also: org.zkoss.web.servlet.Servlets.locate See Also: org.zkoss.web.servlet.Servlets.generateURI |
removeFromQueryString | final public static String removeFromQueryString(String str, String name) throws UnsupportedEncodingException(Code) | | Remove all name/value pairs of the specified name from a string.
The query string might contain servlet path and other parts.
This method starts the searching from the last '?'.
If the query string doesn't contain '?', it is assumed to be a string
without query's name/value pairs.
See Also: Encodes.addToQueryString |
setToQueryString | final public static String setToQueryString(String str, String name, Object value) throws UnsupportedEncodingException(Code) | | Sets the parameter (name=value) to a query string.
If the name already exists in the query string, it will be removed first.
If your name has appeared in the string, it will replace
your new value to the query string.
Otherwise, it will append the name/value to the new string.
The query string might contain servlet path and other parts.
This method starts the searching from the first '?'.
If the query string doesn't contain '?', it is assumed to be a string
without query's name/value pairs.
Parameters: str - The query string like xxx?xxx=xxx&xxx=xxx or null. Parameters: name - The get parameter name. Parameters: value - The value associated with the get parameter name. The new or result query string with your name/value. See Also: Encodes.addToQueryString |
setURLEncoder | public static void setURLEncoder(URLEncoder encoder)(Code) | | Sets the URI encoder.
Default: null
The URI encoder is used to post process the encoded URL
returned by
Encodes.encodeURL .
When
Encodes.encodeURL encodes an URL, it will check
any URL encoder is defined (by
Encodes.setURLEncoder .
If any, it will invoke
URLEncoder.encodeURL with
the encoded URL to give it the last chance to 'manipulate' it.
since: 3.0.1 See Also: Encodes.encodeURL |
|
|