001: /*
002: * Copyright 2005 jWic group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.web.UserAgentInfo
017: * Created on 14.02.2006
018: * $Id: UserAgentInfo.java,v 1.3 2006/08/14 09:34:58 lordsam Exp $
019: */
020: package de.jwic.base;
021:
022: import java.io.Serializable;
023:
024: import javax.servlet.http.HttpServletRequest;
025:
026: /**
027: * Contains information about the user agent (browser). The type and version is
028: * identified from the requests header informations.
029: * @author Florian Lippisch
030: * @version $Revision: 1.3 $
031: */
032: public class UserAgentInfo implements Serializable {
033:
034: private static final long serialVersionUID = 1L;
035:
036: public final static String TYPE_IE = "IE";
037: public final static String TYPE_NS = "NS";
038: public final static String TYPE_FIREFOX = "FIREFOX";
039: public final static String TYPE_OPERA = "OPERA";
040: public final static String TYPE_KONQUEROR = "KONQUEROR";
041: public final static String TYPE_UNKNOWN = "UNKNOWN";
042:
043: private final static String IDENTIFIER_IE = "compatible; MSIE ";
044: private final static String IDENTIFIER_FIREFOX = "Firefox/";
045: private final static String IDENTIFIER_OPERA = "Opera/";
046: private final static String IDENTIFIER_NETSCAPE = "Netscape/";
047: private final static String IDENTIFIER_KONQUEROR = "Konqueror/";
048: private final static String IDENTIFIER_GOOGLEBOT = "Googlebot";
049:
050: private String type = TYPE_UNKNOWN;
051: private String userAgentID = null;
052: private String version = "";
053:
054: private boolean bot = false;
055:
056: /**
057: * Construct a new UserAgentInfo based upon the header informations in the request.
058: * @param request
059: */
060: public UserAgentInfo(HttpServletRequest request) {
061:
062: if (request != null) {
063: userAgentID = request.getHeader("user-agent");
064: if (userAgentID != null) {
065:
066: int idx = userAgentID.indexOf(IDENTIFIER_IE);
067: if (idx != -1) {
068: // its an IE
069: setType(TYPE_IE);
070: // identify the version
071: int end = userAgentID.indexOf(";", idx
072: + IDENTIFIER_IE.length());
073: if (end != -1) {
074: setVersion(userAgentID.substring(idx
075: + IDENTIFIER_IE.length(), end));
076: }
077: } else if ((idx = userAgentID
078: .indexOf(IDENTIFIER_FIREFOX)) != -1) {
079: // its a firefox
080: setType(TYPE_FIREFOX);
081: // identify the version
082: setVersion(userAgentID.substring(idx
083: + IDENTIFIER_FIREFOX.length()));
084:
085: } else if ((idx = userAgentID.indexOf(IDENTIFIER_OPERA)) != -1) {
086: // its a opera
087: setType(TYPE_OPERA);
088: // identify the version
089: int end = userAgentID.indexOf(" ", idx
090: + IDENTIFIER_OPERA.length());
091: if (end != -1) {
092: setVersion(userAgentID.substring(idx
093: + IDENTIFIER_OPERA.length(), end));
094: }
095: } else if ((idx = userAgentID
096: .indexOf(IDENTIFIER_KONQUEROR)) != -1) {
097: // its a opera
098: setType(TYPE_KONQUEROR);
099: // identify the version
100: int end = userAgentID.indexOf(")", idx
101: + IDENTIFIER_KONQUEROR.length());
102: if (end != -1) {
103: setVersion(userAgentID.substring(idx
104: + IDENTIFIER_KONQUEROR.length(), end));
105: }
106: } else if ((idx = userAgentID
107: .indexOf(IDENTIFIER_NETSCAPE)) != -1) {
108: // its a opera
109: setType(TYPE_NS);
110: // identify the version
111: setVersion(userAgentID.substring(idx
112: + IDENTIFIER_NETSCAPE.length()));
113:
114: } else if (userAgentID.indexOf(IDENTIFIER_GOOGLEBOT) != -1) {
115: bot = true;
116: setType("GOOGLEBOT");
117:
118: } else if (userAgentID.indexOf("NutchCVS") == 0) {
119: bot = true;
120:
121: } else if (userAgentID.indexOf("msnbot") == 0) {
122: bot = true;
123: setType("MSNBOT");
124:
125: } else if (userAgentID.indexOf("Bot") != -1
126: || userAgentID.indexOf("bot") != -1) {
127: // an unknown bot
128: bot = true;
129:
130: } else if (userAgentID.indexOf("Mozilla/4.76") == 0) {
131: setType(TYPE_NS);
132: setVersion("4.7");
133: }
134: }
135: }
136: }
137:
138: /**
139: * Returns true if the user agent is an Internet Explorer.
140: * @return
141: */
142: public boolean isIE() {
143: return type.equals(TYPE_IE);
144: }
145:
146: /**
147: * Returns true if the user agent is a Netscape.
148: * @return
149: */
150: public boolean isNS() {
151: return type.equals(TYPE_NS);
152: }
153:
154: /**
155: * Returns true if the user agent is a Firefox.
156: * @return
157: */
158: public boolean isFirefox() {
159: return type.equals(TYPE_FIREFOX);
160: }
161:
162: /**
163: * Returns true if the user agent is Opera.
164: * @return
165: */
166: public boolean isOpera() {
167: return type.equals(TYPE_OPERA);
168: }
169:
170: /**
171: * Returns true if the user agent is Konqueror.
172: * @return
173: */
174: public boolean isKonqueror() {
175: return type.equals(TYPE_KONQUEROR);
176: }
177:
178: /**
179: * @return Returns the type.
180: */
181: public String getType() {
182: return type;
183: }
184:
185: /**
186: * @param type The type to set.
187: */
188: private void setType(String type) {
189: this .type = type;
190: }
191:
192: /**
193: * @return Returns the userAgentID.
194: */
195: public String getUserAgentID() {
196: return userAgentID;
197: }
198:
199: /**
200: * @return Returns the version.
201: */
202: public String getVersion() {
203: return version;
204: }
205:
206: /**
207: * @param version The version to set.
208: */
209: public void setVersion(String version) {
210: this .version = version;
211: }
212:
213: /**
214: * Returns a string representation of this user agent.
215: */
216: public String toString() {
217: return type + " " + version;
218: }
219:
220: /**
221: * @return Returns the bot.
222: */
223: public boolean isBot() {
224: return bot;
225: }
226:
227: }
|