001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: ClientImpl.java 6385 2007-10-25 14:02:26Z wehrens $
004: */
005: package org.gridsphere.portletcontainer.impl;
006:
007: import org.gridsphere.portletcontainer.Client;
008:
009: import javax.servlet.http.HttpServletRequest;
010: import java.io.Serializable;
011:
012: /**
013: * The <code>ClientImpl</code> class represents the client device that the user
014: * connects to the portal with. It defines methods to obtain information about
015: * clients, e.g. browsers running on PCs, WAP phones, PDAs etc.
016: */
017: public class ClientImpl implements Client, Serializable {
018:
019: public static final String[] MIME_TYPES = { "text/html",
020: "text/vnd.wap.wml" };
021: public static final String[] MARKUP_TYPES = { "html", "wml",
022: "chtml" };
023: public static final String[] MANUFACTURER_NAMES = { "opera",
024: "netscape", "mozilla", "IE", "4thpass", "Alcatel",
025: "BlackBerry", "jBrowser", "M3Gate", "Motorola", "Sony",
026: "Nokia", "Panasonic", "Philips", "Sagem", "Samsung",
027: "Siemens", "SonyEricsson", "WinWAP" };
028: public static final String[] WAP_IDENTIFIER = { "4thpass",
029: "Alcatel-", "BlackBerry/", "jBrowser", "M3GATE/", "MOT-",
030: "Sony CMD", "Nokia", "Panasonic", "PHILIPS", "SAGEM-",
031: "SAMSUNG-", "SIE-", "SonyEricsson", "WinWAP" };
032: private String manufacturer = null;
033: private String model = null;
034: private String version = null;
035: private String userAgent = null;
036: private String mimeType = null;
037: private String markupName = null;
038:
039: /**
040: * Constructs an instance of ClientImpl from a servlet request
041: *
042: * @param req an <code>HttpServletRequest</code>
043: */
044: public ClientImpl(HttpServletRequest req) {
045: // get the user-agent string containg client browser information
046: userAgent = req.getHeader("user-agent");
047: //System.err.println("User-agent: " + userAgent);
048: // parse it!
049: // below are Mac OS X
050: // Netscape 6.2: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:0.9.4.1) Gecko/20020315 Netscape6/6.2.2
051: // Netscape 7.0: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0
052: // IE 5.2: Mozilla/4.0 (compatible; MSIE 5.22; Mac_PowerPC)
053: // Mozilla 1.0: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20020826
054: // Opera 6.0b2: Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC) Opera 6.0 [en]
055:
056: // Compaq IPAQ: Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)
057:
058: String browserInfo = "Unknown browser";
059: //WAP 2.0 identifification begins here:
060: for (int identString = 0; identString < 15; identString++) {
061: if (userAgent == null)
062: continue;
063: if (userAgent.indexOf(WAP_IDENTIFIER[identString]) != -1) {
064: browserInfo = MANUFACTURER_NAMES[identString + 4];
065: manufacturer = MANUFACTURER_NAMES[identString + 4];
066: mimeType = MIME_TYPES[0];
067: markupName = MARKUP_TYPES[1];
068: }
069: }
070: //WAP 2.0 identification ends here
071:
072: if (browserInfo.equals("Unknown browser")) {//WAP 2.0
073: if (userAgent != null) {
074: int i = userAgent.indexOf(" ");
075:
076: //System.err.println("mozilla version: " + mozillaVersion);
077: int j = userAgent.lastIndexOf(")");
078:
079: if (j >= 0) {
080: browserInfo = userAgent.substring(j + 1).trim();
081: }
082: }
083: }
084: //System.err.println("browser info: " + browserInfo);
085: mimeType = req.getHeader("accept");
086:
087: //System.err.println("MIME types: " + mimeTypes);
088: // Netscape 6.2: text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, image/png, image/jpeg, image/gif;q=0.2, text/plain;q=0.8, text/css, */*;q=0.1
089: // IE 5.2: */*
090: // Mozilla 1.0: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1
091: // Opera 6.0b2: text/html, image/png, image/jpeg, image/gif, image/x-xbitmap, */*
092:
093: // get manufacturer
094: if (browserInfo.startsWith("Opera")) {
095: manufacturer = MANUFACTURER_NAMES[0];
096: } else if (browserInfo.startsWith("Gecko")) {
097: int i = browserInfo.indexOf(" ");
098: if (i > 0) {
099: if (browserInfo.substring(i).trim().startsWith(
100: "Netscape")) {
101: manufacturer = MANUFACTURER_NAMES[1];
102: } else {
103: manufacturer = MANUFACTURER_NAMES[2];
104: }
105: } else {
106: manufacturer = MANUFACTURER_NAMES[2];
107: }
108: } else {
109: manufacturer = MANUFACTURER_NAMES[3];
110: }
111:
112: if (markupName == null) {//WAP 2.0
113: if (mimeType != null) {
114: int i = mimeType.indexOf("html");
115: if (i < 0) {
116: // IE 5.2 on Mac OS X
117: if (mimeType.indexOf("*/*") >= 0) {
118: mimeType = MIME_TYPES[0];
119: markupName = MARKUP_TYPES[0];
120: }
121: } else {
122: mimeType = MIME_TYPES[0];
123: markupName = MARKUP_TYPES[0];
124: }
125: }
126: }
127: // make up version for now
128: version = "1.0";
129: // make up model for now
130: model = "gridsphere model";
131:
132: // if all else fails, it is html
133: if (markupName == null)
134: markupName = MARKUP_TYPES[0];
135: if (mimeType == null)
136: mimeType = MIME_TYPES[0];
137:
138: //logRequest(req);
139: }
140:
141: /**
142: * Returns the name of the manufacturer of this client, or
143: * <code>null</code> if the name is not available.
144: *
145: * @return the manufacturer
146: */
147: public String getManufacturer() {
148: return manufacturer;
149: }
150:
151: /**
152: * Returns the name of the model of this client, or <code>null</code>
153: * if the name is not available.
154: *
155: * @return the model
156: */
157: public String getModel() {
158: return model;
159: }
160:
161: /**
162: * Returns the version of the model of this client, or <code>null</code>
163: * if the version is not available.
164: *
165: * @return the version
166: */
167: public String getVersion() {
168: return version;
169: }
170:
171: /**
172: * Returns the exact user agent that this client uses to identify
173: * itself to the portal. If the client does not send a user agent,
174: * this method returns <code>null</code>
175: *
176: * @return the user agent
177: */
178: public String getUserAgent() {
179: return userAgent;
180: }
181:
182: /**
183: * Returns the preferred mime-type that this client device supports.
184: *
185: * @return the mime-type
186: */
187: public String getMimeType() {
188: return mimeType;
189: }
190:
191: /**
192: * Returns the preferred markup name that this client device supports.
193: *
194: * @return the name of the markup
195: */
196: public String getMarkupName() {
197: return markupName;
198: }
199:
200: /**
201: * Return a <code>String</code> representation of the <code>Client</code>
202: *
203: * @return a <code>String</code> representation of the <code>Client</code>
204: */
205: public String toString() {
206: StringBuffer sb = new StringBuffer("\n");
207: sb.append("User-agent: " + userAgent + "\n");
208: sb.append("manufacturer: " + manufacturer + "\n");
209: sb.append("model: " + model + "\n");
210: sb.append("version: " + version + "\n");
211: sb.append("markup: " + markupName + "\n");
212: sb.append("mimeType: " + mimeType);
213: return sb.toString();
214: }
215:
216: }
|