001: /* ServletFns.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Apr 11 15:13:44 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.web.fn;
020:
021: import java.io.Writer;
022: import java.io.IOException;
023: import java.io.UnsupportedEncodingException;
024:
025: import javax.servlet.ServletContext;
026: import javax.servlet.ServletRequest;
027: import javax.servlet.ServletResponse;
028: import javax.servlet.ServletException;
029:
030: import org.zkoss.web.servlet.Servlets;
031: import org.zkoss.web.servlet.http.Encodes;
032: import org.zkoss.web.servlet.dsp.action.ActionContext;
033: import org.zkoss.web.servlet.xel.RequestContext;
034: import org.zkoss.web.servlet.xel.RequestContexts;
035:
036: /**
037: * Providing servlet relevant functions for EL.
038: *
039: * @author tomyeh
040: */
041: public class ServletFns {
042: protected ServletFns() {
043: }
044:
045: /** Encodes a URL.
046: *
047: * <p>If an URI contains "*", it will be replaced with a proper Locale.
048: * For example, if the current Locale is zh_TW and the resource is
049: * named "ab*.cd", then it searches "ab_zh_TW.cd", "ab_zh.cd" and
050: * then "ab.cd", until any of them is found.
051: *
052: * <blockquote>Note: "*" must be right before ".", or the last character.
053: * For example, "ab*.cd" and "ab*" are both correct, while
054: * "ab*cd" and "ab*\/cd" are ignored.</blockquote>
055: *
056: * <p>If an URI contains two "*", the first "*" will be replaced with
057: * a browser code and the second with a proper locale.
058: * The browser code depends on what browser
059: * the user are used to visit the web site.
060: * Currently, the code for Internet Explorer is "ie", Safari is "saf",
061: * Opera is "opr" and all others are "moz".
062: * Thus, in the above example, if the resource is named "ab**.cd"
063: * and Firefox is used, then it searches "abmoz_zh_TW.cd", "abmoz_zh.cd"
064: * and then "abmoz.cd", until any of them is found.
065: */
066: public static String encodeURL(String uri) throws ServletException {
067: return Encodes.encodeURL(getCurrentServletContext(),
068: getCurrentRequest(), getCurrentResponse(), uri);
069: }
070:
071: /** Returns whether the browser of the current request is Explorer.
072: */
073: public static boolean isExplorer() {
074: return Servlets.isExplorer(getCurrentRequest());
075: }
076:
077: /** Returns whether the browser of the current request is Explorer 7 or later.
078: */
079: public static boolean isExplorer7() {
080: return Servlets.isExplorer7(getCurrentRequest());
081: }
082:
083: /** Returns whether the browser of the current request is Gecko based,
084: * such as Mozilla, Firefox and Camino.
085: */
086: public static boolean isGecko() {
087: return Servlets.isGecko(getCurrentRequest());
088: }
089:
090: /** Returns whether the browser of the current request is Safari.
091: */
092: public static boolean isSafari() {
093: return Servlets.isSafari(getCurrentRequest());
094: }
095:
096: /** Returns whether the browser of the current request is Opera.
097: */
098: public static boolean isOpera() {
099: return Servlets.isOpera(getCurrentRequest());
100: }
101:
102: /** Returns the current EL context. */
103: public static RequestContext getCurrentContext() {
104: return RequestContexts.getCurrent();
105: }
106:
107: /** Returns the current output. */
108: public static Writer getCurrentOut() throws IOException {
109: return getCurrentContext().getOut();
110: }
111:
112: /** Returns the current servlet context, or null if not available. */
113: public static ServletContext getCurrentServletContext() {
114: return getCurrentContext().getServletContext();
115: }
116:
117: /** Returns the current servlet request, or null if not available. */
118: public static ServletRequest getCurrentRequest() {
119: return getCurrentContext().getRequest();
120: }
121:
122: /** Returns the current servlet response, or null if not available. */
123: public static ServletResponse getCurrentResponse() {
124: return getCurrentContext().getResponse();
125: }
126:
127: /** Renders the DSP fragment from EL.
128: *
129: * @param ac the action context; never null.
130: */
131: public static void render(ActionContext ac)
132: throws ServletException, IOException {
133: ac.renderFragment(null);
134: }
135: }
|