001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.webadapter;
039:
040: import java.util.Collection;
041: import java.util.Enumeration;
042: import java.util.Map;
043:
044: import javax.servlet.ServletException;
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpSession;
047:
048: /**
049: * The WebBrowserProbe uses JavaScript to determine the capabilities
050: * of the client browser.
051: * @author IT Mill Ltd.
052: * @version 3.1.1
053: * @since 3.0
054: */
055: public class WebBrowserProbe {
056:
057: private static final String WA_NOSCRIPT = "WA_NOSCRIPT";
058:
059: private static final String CLIENT_TYPE = "wa_browser";
060:
061: /** Return the terminal type from the given session.
062: * @return WebBrowser instance for the given session.
063: */
064: public static WebBrowser getTerminalType(HttpSession session) {
065: if (session != null)
066: return (WebBrowser) session.getAttribute(CLIENT_TYPE);
067: return null;
068: }
069:
070: /** Set the terminal type for the given session.
071: * @return WebBrowser instance for the given session.
072: */
073: public static void setTerminalType(HttpSession session,
074: WebBrowser terminal) {
075: if (session != null)
076: session.setAttribute(CLIENT_TYPE, terminal);
077: }
078:
079: /** Handle client checking.
080: * @param request The HTTP request to process.
081: * @param response HTTP response to write to.
082: * @return true if response should include a probe script
083: **/
084: public static boolean handleProbeRequest(
085: HttpServletRequest request, Map parameters)
086: throws ServletException {
087:
088: HttpSession s = request.getSession();
089: WebBrowser browser = getTerminalType(s);
090: if (browser != null) {
091:
092: // Check if no-script was requested
093: if (parameters.containsKey(WA_NOSCRIPT)) {
094: String val = ((String[]) parameters.get(WA_NOSCRIPT))[0];
095: if (val != null && "1".equals(val)) {
096: browser
097: .setJavaScriptVersion(WebBrowser.JAVASCRIPT_NONE);
098: browser.setClientSideChecked(true);
099: } else {
100: // Recheck
101: browser.setClientSideChecked(false);
102: }
103: }
104:
105: // If client is alredy checked disable further checking
106: if (browser.isClientSideChecked())
107: return false;
108:
109: }
110:
111: // Create new type based on client parameters
112: browser = probe(browser, request, parameters);
113: setTerminalType(s, browser);
114:
115: // Set client as checked if parameters were found
116: if (parameters.containsKey("wa_clientprobe")) {
117: String val = ((String[]) parameters.get("wa_clientprobe"))[0];
118: browser
119: .setClientSideChecked(val != null
120: && "1".equals(val));
121: }
122:
123: // Include probe script if requested and not alredy probed
124: return browser.performClientCheck()
125: && !browser.isClientSideChecked();
126:
127: }
128:
129: /** Determine versions based on user agent string.
130: * @param agent HTTP User-Agent request header.
131: * @return new WebBrowser instance initialized based on agent features.
132: */
133: public static WebBrowser probe(String agent) {
134: WebBrowser res = new WebBrowser();
135: if (agent == null)
136: return res;
137:
138: // Set the agent string
139: res.setBrowserApplication(agent);
140:
141: // Konqueror
142: if (agent.indexOf("Konqueror") >= 0) {
143: res.setJavaScriptVersion(WebBrowser.JSCRIPT_5_6);
144: res.setJavaEnabled(true);
145: res.setFrameSupport(true);
146: }
147:
148: // Opera
149: else if ((agent.indexOf("Opera 6.") >= 0)
150: || (agent.indexOf("Opera 5.") >= 0)
151: || (agent.indexOf("Opera 4.") >= 0)) {
152: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_3);
153: res.setJavaEnabled(true);
154: res.setFrameSupport(true);
155: res.setMarkupVersion(WebBrowser.MARKUP_HTML_4_0);
156: } else if (agent.indexOf("Opera 3.") >= 0) {
157: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_3);
158: res.setJavaEnabled(false);
159: res.setFrameSupport(true);
160: }
161:
162: // OmniWeb
163: else if (agent.indexOf("OmniWeb") >= 0) {
164: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_3);
165: res.setJavaEnabled(true);
166: res.setFrameSupport(true);
167: }
168:
169: // Mosaic
170: else if (agent.indexOf("Mosaic") >= 0) {
171: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_3);
172: res.setJavaEnabled(true);
173: res.setFrameSupport(true);
174: res.setMarkupVersion(WebBrowser.MARKUP_HTML_2_0);
175: }
176:
177: // Lynx
178: else if (agent.indexOf("Lynx") >= 0) {
179: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_NONE);
180: res.setJavaEnabled(false);
181: res.setFrameSupport(true);
182: }
183:
184: // Microsoft Browsers
185: // See Microsoft documentation for details:
186: // http://msdn.microsoft.com/library/default.asp?url=/library/
187: // en-us/script56/html/js56jsoriversioninformation.asp
188: else if (agent.indexOf("MSIE 6.") >= 0) {
189: res.setJavaScriptVersion(WebBrowser.JSCRIPT_5_6);
190: res.setJavaEnabled(true);
191: res.setFrameSupport(true);
192: res.setMarkupVersion(WebBrowser.MARKUP_HTML_4_0);
193: } else if (agent.indexOf("MSIE 5.5") >= 0) {
194: res.setJavaScriptVersion(WebBrowser.JSCRIPT_5_5);
195: res.setJavaEnabled(true);
196: res.setFrameSupport(true);
197: res.setMarkupVersion(WebBrowser.MARKUP_HTML_4_0);
198: } else if (agent.indexOf("MSIE 5.") >= 0) {
199: res.setJavaScriptVersion(WebBrowser.JSCRIPT_5_0);
200: res.setJavaEnabled(true);
201: res.setFrameSupport(true);
202: res.setMarkupVersion(WebBrowser.MARKUP_HTML_4_0);
203: } else if (agent.indexOf("MSIE 4.") >= 0) {
204: res.setJavaScriptVersion(WebBrowser.JSCRIPT_3_0);
205: res.setJavaEnabled(true);
206: res.setFrameSupport(true);
207: res.setMarkupVersion(WebBrowser.MARKUP_HTML_4_0);
208: } else if (agent.indexOf("MSIE 3.") >= 0) {
209: res.setJavaScriptVersion(WebBrowser.JSCRIPT_3_0);
210: res.setJavaEnabled(true);
211: res.setFrameSupport(true);
212: } else if (agent.indexOf("MSIE 2.") >= 0) {
213: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_NONE);
214: res.setJavaEnabled(false);
215: if (agent.indexOf("Mac") >= 0) {
216: res.setFrameSupport(true);
217: } else {
218: res.setFrameSupport(false);
219: }
220: }
221:
222: // Netscape browsers
223: else if (agent.indexOf("Netscape6") >= 0) {
224: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_5);
225: res.setJavaEnabled(true);
226: res.setFrameSupport(true);
227: res.setMarkupVersion(WebBrowser.MARKUP_HTML_4_0);
228: } else if ((agent.indexOf("Mozilla/4.06") >= 0)
229: || (agent.indexOf("Mozilla/4.7") >= 0)) {
230: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_3);
231: res.setJavaEnabled(true);
232: res.setFrameSupport(true);
233: res.setMarkupVersion(WebBrowser.MARKUP_HTML_4_0);
234: } else if (agent.indexOf("Mozilla/4.") >= 0) {
235: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_2);
236: res.setJavaEnabled(true);
237: res.setFrameSupport(true);
238: } else if (agent.indexOf("Mozilla/3.") >= 0) {
239: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_1);
240: res.setJavaEnabled(true);
241: res.setFrameSupport(true);
242: } else if (agent.indexOf("Mozilla/2.") >= 0) {
243: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_0);
244: res.setJavaEnabled(true);
245: res.setFrameSupport(true);
246: }
247:
248: // Mozilla Open-Source Browsers
249: else if (agent.indexOf("Mozilla/5.") >= 0) {
250: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_1_5);
251: res.setJavaEnabled(true);
252: res.setFrameSupport(true);
253: res.setMarkupVersion(WebBrowser.MARKUP_HTML_4_0);
254: }
255:
256: // Unknown browser
257: else {
258: res.setJavaScriptVersion(WebBrowser.JAVASCRIPT_UNCHECKED);
259: res.setJavaEnabled(false);
260: res.setMarkupVersion(WebBrowser.MARKUP_UNKNOWN);
261: res.setFrameSupport(false);
262: }
263:
264: return res;
265: }
266:
267: /** Create new instance of WebBrowser by initializing the values
268: * based on user request.
269: * @param browser The browser to be updated. If null a new instance is created.
270: * @param request Request to be used as defaults.
271: * @param params Parameters to be used as defaults.
272: * @return new WebBrowser instance initialized based on request parameters.
273: */
274: public static WebBrowser probe(WebBrowser browser,
275: HttpServletRequest request, Map params) {
276:
277: // Initialize defaults based on client features
278: WebBrowser res = browser;
279: if (res == null) {
280: res = probe(request.getHeader("User-Agent"));
281: }
282:
283: // Client locales
284: Collection locales = res.getLocales();
285: locales.clear();
286: for (Enumeration e = request.getLocales(); e.hasMoreElements();) {
287: locales.add(e.nextElement());
288: }
289:
290: //Javascript version
291: if (params.containsKey("wa_jsversion")) {
292: String val = ((String[]) params.get("wa_jsversion"))[0];
293: if (val != null) {
294: res.setJavaScriptVersion(WebBrowser
295: .parseJavaScriptVersion(val));
296: }
297: }
298: //Java support
299: if (params.containsKey("wa_javaenabled")) {
300: String val = ((String[]) params.get("wa_javaenabled"))[0];
301: if (val != null) {
302: res.setJavaEnabled(Boolean.valueOf(val).booleanValue());
303: }
304: }
305: //Screen width
306: if (params.containsKey("wa_screenwidth")) {
307: String val = ((String[]) params.get("wa_screenwidth"))[0];
308: if (val != null) {
309: try {
310: res.setScreenWidth(Integer.parseInt(val));
311: } catch (NumberFormatException e) {
312: res.setScreenWidth(-1);
313: }
314: }
315: }
316: //Screen height
317: if (params.containsKey("wa_screenheight")) {
318: String val = ((String[]) params.get("wa_screenheight"))[0];
319: if (val != null) {
320: try {
321: res.setScreenHeight(Integer.parseInt(val));
322: } catch (NumberFormatException e) {
323: res.setScreenHeight(-1);
324: }
325: }
326: }
327:
328: return res;
329: }
330: }
|