[hmath-commits] org.hmath.server/WEB-INF/src/org/hartmath/server/taglib BrowserSniffer.java,NONE,1.1
Status: Pre-Alpha
Brought to you by:
jsurfer
|
From: <js...@us...> - 2004-03-12 21:18:31
|
Update of /cvsroot/hmath/org.hmath.server/WEB-INF/src/org/hartmath/server/taglib In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16283/WEB-INF/src/org/hartmath/server/taglib Modified Files: BrowserTag.java Added Files: BrowserSniffer.java HeaderTag.java Log Message: misc changes --- NEW FILE: BrowserSniffer.java --- package org.hartmath.server.taglib; /* * * Copyright (C) Johannes Lietz, joh...@da... Modified by Klaus Hartlage * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ import java.io.Serializable; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; /** * Pretty complete detection for browser user agents. * <p> * Provides functionality for detecting the users http client. It has been tested against most browsers that are used today. * <p> * Intended usage in a JSP is: * </p> * * <pre> <%BrowserDetection browser = BrowserDetection.getBrowserDetection(request); %> * * * <% if (browser.isMozilla()) ... else if (browser.isSafari() || browser.isKonqueror()) ... else ... %> * </pre> * * * <p> * Requires J2SE 1.4 because it uses the java.util.regex package * </p> * * @author Johannes Lietz joh...@da... * @version 1.1 (16. Dec. 2003) * * <p> * New in version 1.1: * <ul> * <li>Test for accepted Mimetypes, Charsets and Languages.</li> * <li>Detection if the user has AOL</li> * <li>Detection of the most common search engines and bots</li> * <li>Better naming scheme</li> * </ul> * </p> */ public class BrowserSniffer implements Serializable { static final long serialVersionUID = 7731753920913450421L; public static final String OPERA = "Opera"; public static final String LYNX = "Lynx"; public static final String MSIE = "MSIE"; public static final String MSPIE = "MSPIE"; public static final String NETSCAPE = "Mozilla"; public static final String MOZILLA = "Gecko"; public static final String SAFARI = "Safari"; public static final String KONQUEROR = "Konqueror"; public static final int OPERAID = 1; public static final int LYNXID = 2; public static final int MSIEID = 3; public static final int MSPIEID = 4; public static final int NETSCAPEID = 5; public static final int MOZILLAID = 6; public static final int SAFARIID = 7; public static final int KONQUERORID = 8; public static final int MATHPLAYERID = 9; // MSIE+MathPlayer public static final String WIN = "Win"; public static final String MAC = "Mac"; public static final String UNIX = "Unix"; public static final String WIN_CE = "Windows CE"; public static final String UNKNOWN = "Unknown"; public static final String DEFAULT_SESSION_ATTRIBUTENAME = "org.hartmath.server.taglib.BrowserSniffer"; private String userAgent = null; private int browserId = 0; private String platform = null; private String accept = null; private String acceptLanguage = null; private String acceptCharset = null; private float version = 0; private boolean bot = false; private boolean aol = false; /** * Creates new BrowserDetection from Request(User-Agent). * * @param request */ public BrowserSniffer(HttpServletRequest request) { this.userAgent = request.getHeader("User-Agent"); this.accept = request.getHeader("Accept"); if (this.accept != null) this.accept = this.accept.toLowerCase(); this.acceptLanguage = request.getHeader("Accept-Language"); if (this.acceptLanguage != null) this.acceptLanguage = this.acceptLanguage.toLowerCase(); this.acceptCharset = request.getHeader("Accept-Charset"); if (this.acceptCharset != null) this.acceptCharset = this.acceptCharset.toLowerCase(); detect(); } /** * Creates new BrowserDetection from Request(User-Agent) and adds it to the Session as "attributeName". * * @param request * the HttpServletRequest * @param attributeName * the Name used for the session attribute */ public static void setBrowserDetection(HttpServletRequest request, String attributeName) { if (request.getSession(true).getAttribute(attributeName) == null) getBrowserDetection(request, attributeName); } /** * Creates new BrowserDetection a given User-Agent String * * @param userAgent * the userAgent * @deprecated please use BrowserDetection(HttpServletRequest request) instead */ public BrowserSniffer(String userAgent) { this.userAgent = userAgent; detect(); } /** * Creates new BrowserDetection from Request(User-Agent) and adds it to the Session as DEFAULT_SESSION_ATTRIBUTENAME. * * @param request * the HttpServletRequest */ public static BrowserSniffer getBrowserDetection(HttpServletRequest request) { return getBrowserDetection(request, DEFAULT_SESSION_ATTRIBUTENAME); } /** * Creates new BrowserDetection from Request(User-Agent) and adds it to the Session as "sessionAttributeName". * * @param request * the HttpServletRequest * @param sessionAttributeName * the Name of the Sessionattribute for the BrowserDetection object */ public static BrowserSniffer getBrowserDetection(HttpServletRequest request, String sessionAttributeName) { BrowserSniffer bd = (BrowserSniffer) request.getSession(true).getAttribute(sessionAttributeName); if (bd == null) { bd = new BrowserSniffer(request); request.setAttribute(sessionAttributeName, bd); } return bd; } /** * Standalone Testcode * * @param args */ public static void main(String[] args) { BrowserSniffer bd = new BrowserSniffer( //"Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) // Opera 7.01 [en] "); //"Lynx/2.8.3rel.1 libwww-FM/2.14"); //"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) // Gecko/20030312"); //"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4.1) // Gecko/20020508 Netscape6/6.2.3"); //"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) "); //"Mozilla/2.0 (compatible; MSIE 3.01; Windows 3.1)"); //"Mozilla/4.75 [en] (Windows NT 5.0; U) "); //"Mozilla/2.0 (Macintosh; U; 68K)"); //"Mozilla/5.0 (Windows; U; Win98; en-US; m18) Gecko/20010131 // Netscape6/6.0"); //"Mozilla/1.1 (compatible; MSPIE 1.1; Windows CE)"); //"Mozilla/4.0 (compatible; MSIE 5.5; Windows 98*)"); //"Mozilla/4.0 (compatible; MSIE 4.0b2; Windows 95)"); //"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/60 (like Gecko) Safari/60"); "Mozilla/3.04 (WinNT; U)"); // System.out.println(bd.getBrowser()); System.out.println(bd.getPlatform()); System.out.println(bd.getVersion()); } private void detect() { if (this.userAgent == null) { return; } /////////////////////////////////////////////////// // Search-Engine /////////////////////////////////////////////////// String ua = this.userAgent.toLowerCase(); if (ua.indexOf("bot") != -1 || ua.indexOf("seek") != -1 || ua.indexOf("slurp") != -1 || ua.indexOf("spider") != -1 || ua.indexOf("crawl") != -1 || ua.indexOf("search") != -1 || ua.indexOf("teoma") != -1 || ua.indexOf("sleuth") != -1 || ua.indexOf("search") != -1 || ua.indexOf("find") != -1 || ua.indexOf("fireball") != -1 || ua.indexOf("scooter") != -1 || ua.indexOf("altavista") != -1 || ua.indexOf("netcraft") != -1 || ua.indexOf("gulliver") != -1 || ua.indexOf("ferret") != -1 || ua.indexOf("informant") != -1) { this.bot = true; return; } this.aol = this.userAgent.indexOf("AOL") != -1; /////////////////////////////////////////////////// // Browser und Version feststellen /////////////////////////////////////////////////// if (userAgent.indexOf(OPERA) != -1) { // this.browser = OPERA; this.browserId = OPERAID; this.version = parseVersion(OPERA); } else if (userAgent.indexOf(MSIE) != -1) { // this.browser = MSIE; this.browserId = MSIEID; this.version = parseVersion(MSIE); if (userAgent.indexOf("MathPlayer") != -1) { this.browserId = MATHPLAYERID; } } else if (userAgent.indexOf(SAFARI) != -1) { // this.browser = SAFARI; this.browserId = SAFARIID; this.version = parseVersion(SAFARI); } else if (userAgent.indexOf(MSPIE) != -1) { // this.browser = MSPIE; this.browserId = MSPIEID; this.version = parseVersion(MSPIE); } else if (userAgent.indexOf(KONQUEROR) != -1) { // this.browser = KONQUEROR; this.browserId = KONQUERORID; this.version = parseVersion(KONQUEROR); } else if (userAgent.indexOf(MOZILLA) != -1) { // "Revision builds"/Netscape ab 6.0(???) // this.browser = MOZILLA; this.browserId = MOZILLAID; this.version = parseVersion("rv:"); // "Milestone Builds"/Netscape 6.0, z.B. "m18" if (this.version == 5) this.version = (float) 0.5; } else if (userAgent.indexOf(NETSCAPE) != -1) { // this.browser = NETSCAPE; this.browserId = NETSCAPEID; this.version = parseVersion(NETSCAPE); } else if (userAgent.indexOf(LYNX) != -1) { // this.browser = LYNX; this.browserId = LYNXID; this.version = parseVersion(LYNX); } else { System.out.println("###\n### No Browsername found for:\n### " + this.userAgent + "\n###"); } /////////////////////////////////////////////////// // Platform feststellen /////////////////////////////////////////////////// if (userAgent.indexOf(WIN_CE) != -1) { this.platform = WIN_CE; } else if (userAgent.indexOf(WIN) != -1) { this.platform = WIN; } else if (userAgent.indexOf(MAC) != -1 || userAgent.indexOf("PPC") != -1) { this.platform = MAC; } else if ( userAgent.indexOf(UNIX) != -1 || userAgent.indexOf("Linux") != -1 || userAgent.indexOf("X11") != -1 || userAgent.indexOf("libwww") != -1 || userAgent.indexOf("SunOS") != -1) { this.platform = UNIX; } else { System.out.println("###\n### No Browser-Platform found for:\n### " + this.userAgent + "\n###"); } } private float parseVersion(String searchPattern) { try { String str = this.userAgent.substring(this.userAgent.indexOf(searchPattern) + searchPattern.length()); Pattern pattern = Pattern.compile("[0-9\\.]{2,}"); Matcher matcher = pattern.matcher(str); if (matcher.find()) { String versionString = matcher.group().trim(); if (versionString.indexOf(".") != -1) { String major = versionString.substring(0, versionString.indexOf(".")); Pattern dot = Pattern.compile("\\."); Matcher m = dot.matcher(versionString.substring(versionString.indexOf("."))); StringBuffer sb = new StringBuffer(); boolean result = m.find(); while (result) { m.appendReplacement(sb, ""); result = m.find(); } m.appendTail(sb); return Float.parseFloat(major + "." + sb.toString()); } else { return Float.parseFloat(versionString); } } } catch (Exception e) { e.printStackTrace(); } if (!this.bot) System.out.println("###\n### No Browser-Version found for:\n### " + this.userAgent + "\n###"); return 0; } /** * Tests if a browser explicitely accepts a given mime type. "*.*" is ignored. * * @param mimeType * a RFC mimetype such as "text/html", "application/xhtml+xml" or "image/gif" * @return true if the browser explicitely accepts a given mime type. * @since 1.1 */ public boolean acceptsMimeType(String mimeType) { if (mimeType == null) return false; mimeType = mimeType.toLowerCase(); return this.accept != null && this.accept.indexOf(mimeType) > -1; } /** * Tests if a browser explicitely accepts a given mime type and preferes to receive it over the second one. "*.*" is ignored. <b> * Note</b> that I assume that we do not pay attention to quantity statements such as "application/xhtml+xml;q=0.99" yet but * only the given order. Safari e.g. does not give a special order to its mimetypes, only quantity statement, so it does not work * with Safari! * * @param mimeType1 * a RFC mimetype such as "text/html", "application/xhtml+xml" or "image/gif" * @param mimeType2 * a RFC mimetype such as "text/html", "application/xhtml+xml" or "image/gif" * @return true if the browser explicitely accepts a given mime type. * @since 1.1 */ public int prefersMimeType(String mimeType1, String mimeType2) { if (this.accept == null || (!this.acceptsMimeType(mimeType1) && !this.acceptsMimeType(mimeType2))) return 0; else if ( this.acceptsMimeType(mimeType1) && !this.acceptsMimeType(mimeType2) || this.accept.indexOf(mimeType1) < this.accept.indexOf(mimeType2)) return 1; else return 2; } /** * Tests if a browser explicitely accepts a given language. * * @param language * a ISO language such as "en", "en-us" or "de" * @return true if the browser explicitely accepts a given language. * @since 1.1 */ public boolean acceptsLanguage(String language) { if (language == null) return false; language = language.toLowerCase(); return this.acceptLanguage != null && this.acceptLanguage.indexOf(language) > -1; } /** * Tests if a browser explicitely accepts a given character set. * * @param charset * a ISO language such as "utf-8", "ISO-8859-1" or "windows-1252" * @return true if the browser explicitely accepts a given language. * @since 1.1 */ public boolean acceptsCharset(String charset) { if (charset == null) return false; charset = charset.toLowerCase(); return this.acceptCharset != null && this.acceptCharset.indexOf(charset) > -1; } /** * @return String */ // public String getBrowser() // { // return (browser != null) ? browser : UNKNOWN; // } /** * @return String */ public String getPlatform() { return (platform != null) ? platform : UNKNOWN; } /** * @return String */ public String getUserAgent() { return (userAgent != null) ? userAgent : UNKNOWN; } /** * @return float the version number, e.g. 5.01 or 6.23 */ public float getVersion() { return version; } /** * Tests if the user agent is likely to be a bot. Note that some bots identify themselves as an old Netscape version. Therefore * we also return true for Netscape < 4. * * @return true if the user agent identifies itself as search engine or bot * @since 1.1 */ public boolean isBot() { return bot || (this.isAncientNetscape() && this.getVersion() < 4); } /** * @return boolean isKonqueror */ public boolean isKonqueror() { return (browserId == KONQUERORID); } /** * @return boolean isLynx */ public boolean isLynx() { return (browserId == LYNXID); } /** * true for Nescape >= 6 and all Mozilla versions * * @return boolean isMozilla * @since 1.1 */ public boolean isModernMozilla() { return (browserId == MOZILLAID); } /** * true for Nescape >= 6 and all Mozilla versions * * @return boolean isMozilla * @deprecated use isModernMozilla() instead. */ public boolean isMozilla() { return (browserId == MOZILLAID); } /** * @return boolean isMsie */ public boolean isMsie() { return (browserId == MSIEID); } /** * true for the MSIE mobile ("Pocket") version. * * @return boolean isMspie */ public boolean isMspie() { return (browserId == MSPIEID); } /** * true for Netscape browsers prior Netscape 6 * * @return boolean isNetscape4 * @deprecated use isAncientNetscape() instead */ public boolean isNetscape4() { return isAncientNetscape(); } /** * true for Netscape browsers prior Netscape 6 * * @return boolean isNetscape4 * @since 1.1 */ public boolean isAncientNetscape() { return (browserId == NETSCAPEID); } /** * true for Opera browsers. Note that Opera browsers like to camouflage as MSIE. * * @return boolean isOpera */ public boolean isOpera() { return (browserId == OPERAID); } /** * @return boolean isSafari */ public boolean isSafari() { return (browserId == SAFARIID); } /** * @return boolean isMac */ public boolean isMac() { return (platform != null && platform.equals(MAC)); } /** * @return boolean isUnix */ public boolean isUnix() { return (platform != null && platform.equals(UNIX)); } /** * @return boolean isWin */ public boolean isWin() { return (platform != null && platform.equals(WIN)); } /** * @return boolean isWindowsCE */ public boolean isWindowsCE() { return (platform != null && platform.equals(WIN_CE)); } /** * @return isAol. * @since 1.1 */ public boolean isAol() { return aol; } /** * @return Returns the browserId. */ public int getBrowserId() { return browserId; } } --- NEW FILE: HeaderTag.java --- package org.hartmath.server.taglib; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; import org.snipsnap.app.Application; /** * Java JSP taglib interface to send browser specific header * */ public class HeaderTag extends BodyTagSupport { public int doStartTag() throws JspException { // log("doPost"); BodyContent body = getBodyContent(); // JspWriter out = body.getEnclosingWriter(); // String bodyData = body.getString(); HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); BrowserSniffer bd = BrowserSniffer.getBrowserDetection(request); try { switch (bd.getBrowserId()) { case BrowserSniffer.MOZILLAID : case BrowserSniffer.NETSCAPEID : pageContext.getResponse().setContentType( "application/xhtml+xml" + "; charset=" + Application.get().getConfiguration().getEncoding()); request.getRequestDispatcher("gecko_header.jsp").include(request, pageContext.getResponse()); break; case BrowserSniffer.OPERAID : pageContext.getResponse().setContentType( "application/xhtml+xml" + "; charset=" + Application.get().getConfiguration().getEncoding()); request.getRequestDispatcher("opera_header.jsp").include(request, pageContext.getResponse()); break; case BrowserSniffer.MATHPLAYERID : pageContext.getResponse().setContentType("text/html" + "; charset=" + Application.get().getConfiguration().getEncoding()); request.getRequestDispatcher("msie_header.jsp").include(request, pageContext.getResponse()); break; default : pageContext.getResponse().setContentType("text/html" + "; charset=" + Application.get().getConfiguration().getEncoding()); request.getRequestDispatcher("gecko_header.jsp").include(request, pageContext.getResponse()); break; } } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } } Index: BrowserTag.java =================================================================== RCS file: /cvsroot/hmath/org.hmath.server/WEB-INF/src/org/hartmath/server/taglib/BrowserTag.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** BrowserTag.java 9 Mar 2004 20:13:09 -0000 1.1 --- BrowserTag.java 12 Mar 2004 20:50:50 -0000 1.2 *************** *** 54,58 **** } } - return version; } --- 54,57 ---- |