KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > ClientAnalyzerProcessor


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Locale JavaDoc;
38 import java.util.Set JavaDoc;
39
40 import nextapp.echo2.webrender.service.SynchronizeService;
41 import nextapp.echo2.webrender.util.DomUtil;
42
43 import org.w3c.dom.Element JavaDoc;
44
45 /**
46  * <code>SynchronizeService.ClientMessagePartProcessor</code> which creates
47  * a <code>ClientProperties</code> object based on the client script's
48  * analysis of its environment.
49  */

50 public class ClientAnalyzerProcessor
51 implements SynchronizeService.ClientMessagePartProcessor {
52
53     /**
54      * <code>Set</code> containing valid properties which may be received from
55      * the client. Property settings received from the client that are not in
56      * this set are discarded.
57      */

58     private static final Set JavaDoc VALID_PROPERTIES;
59     static {
60         Set JavaDoc set = new HashSet JavaDoc();
61         set.add(ClientProperties.NAVIGATOR_APP_CODE_NAME);
62         set.add(ClientProperties.NAVIGATOR_APP_NAME);
63         set.add(ClientProperties.NAVIGATOR_APP_VERSION);
64         set.add(ClientProperties.NAVIGATOR_COOKIE_ENABLED);
65         set.add(ClientProperties.NAVIGATOR_JAVA_ENABLED);
66         set.add(ClientProperties.NAVIGATOR_LANGUAGE);
67         set.add(ClientProperties.NAVIGATOR_PLATFORM);
68         set.add(ClientProperties.NAVIGATOR_USER_AGENT);
69         set.add(ClientProperties.SCREEN_WIDTH);
70         set.add(ClientProperties.SCREEN_HEIGHT);
71         set.add(ClientProperties.SCREEN_COLOR_DEPTH);
72         set.add(ClientProperties.UTC_OFFSET);
73         VALID_PROPERTIES = Collections.unmodifiableSet(set);
74     }
75     
76     /**
77      * Analyzes the state of <code>ClientProperties</code> and adds additional
78      * inferred data, such as quirk attributes based on browser type.
79      *
80      * @param clientProperties the <code>ClientProperties</code> to analyze
81      * and update
82      */

83     private void analyze(ClientProperties clientProperties) {
84         Connection conn = WebRenderServlet.getActiveConnection();
85         
86         Enumeration JavaDoc localeEnum = conn.getRequest().getLocales();
87         List JavaDoc localeList = new ArrayList JavaDoc();
88         while (localeEnum.hasMoreElements()) {
89             localeList.add(localeEnum.nextElement());
90         }
91         clientProperties.setProperty(ClientProperties.LOCALES, localeList.toArray(new Locale JavaDoc[localeList.size()]));
92         
93         clientProperties.setProperty(ClientProperties.REMOTE_HOST, conn.getRequest().getRemoteHost());
94     
95         String JavaDoc userAgent = clientProperties.getString(ClientProperties.NAVIGATOR_USER_AGENT).toLowerCase();
96         
97         boolean browserOpera = userAgent.indexOf("opera") != -1;
98         boolean browserSafari = userAgent.indexOf("safari") != -1;
99         boolean browserKonqueror = userAgent.indexOf("konqueror") != -1;
100         
101         // Note deceptive user agent fields:
102
// - Konqueror and Safari UA fields contain "like Gecko"
103
// - Opera UA field typically contains "MSIE"
104
boolean deceptiveUserAgent = browserOpera || browserSafari || browserKonqueror;
105         
106         boolean browserMozilla = !deceptiveUserAgent && userAgent.indexOf("gecko") != -1;
107         boolean browserFireFox = userAgent.indexOf("firefox") != -1;
108         boolean browserInternetExplorer = !deceptiveUserAgent && userAgent.indexOf("msie") != -1;
109         
110         int majorVersion = -1, minorVersion = -1;
111         
112         // Store browser information.
113
if (browserOpera) {
114             clientProperties.setProperty(ClientProperties.BROWSER_OPERA, Boolean.TRUE);
115             if (userAgent.indexOf("opera/9") != -1 || userAgent.indexOf("opera 9") != -1) {
116                 majorVersion = 9;
117             } else if (userAgent.indexOf("opera/8") != -1 || userAgent.indexOf("opera 8") != -1) {
118                 majorVersion = 8;
119             } else {
120                 // Assume future version with Opera 9 compatibility.
121
// (Versions prior to 8 do not provide minimum requirements, i.e., XMLHTTPRequest.)
122
majorVersion = 9;
123             }
124         } else if (browserKonqueror) {
125             clientProperties.setProperty(ClientProperties.BROWSER_KONQUEROR, Boolean.TRUE);
126         } else if (browserSafari) {
127             clientProperties.setProperty(ClientProperties.BROWSER_SAFARI, Boolean.TRUE);
128         } else if (browserMozilla) {
129             clientProperties.setProperty(ClientProperties.BROWSER_MOZILLA, Boolean.TRUE);
130             if (browserFireFox) {
131                 clientProperties.setProperty(ClientProperties.BROWSER_MOZILLA_FIREFOX, Boolean.TRUE);
132             }
133         } else if (browserInternetExplorer) {
134             clientProperties.setProperty(ClientProperties.BROWSER_INTERNET_EXPLORER, Boolean.TRUE);
135             if (userAgent.indexOf("msie 6.") != -1) {
136                 majorVersion = 6;
137             } else if (userAgent.indexOf("msie 7.") != -1) {
138                 majorVersion = 7;
139             }
140         }
141         
142         if (majorVersion != -1) {
143             clientProperties.setProperty(ClientProperties.BROWSER_VERSION_MAJOR, Integer.toString(majorVersion));
144         }
145         
146         if (minorVersion != -1) {
147             clientProperties.setProperty(ClientProperties.BROWSER_VERSION_MINOR, Integer.toString(minorVersion));
148         }
149         
150         // Set quirk flags.
151
if (browserInternetExplorer) {
152             clientProperties.setProperty(ClientProperties.QUIRK_IE_REPAINT, Boolean.TRUE);
153             clientProperties.setProperty(ClientProperties.QUIRK_TEXTAREA_CONTENT, Boolean.TRUE);
154             clientProperties.setProperty(ClientProperties.QUIRK_IE_TEXTAREA_NEWLINE_OBLITERATION, Boolean.TRUE);
155             clientProperties.setProperty(ClientProperties.QUIRK_IE_SELECT_LIST_DOM_UPDATE, Boolean.TRUE);
156             clientProperties.setProperty(ClientProperties.QUIRK_CSS_BORDER_COLLAPSE_INSIDE, Boolean.TRUE);
157             clientProperties.setProperty(ClientProperties.QUIRK_CSS_BORDER_COLLAPSE_FOR_0_PADDING, Boolean.TRUE);
158             
159             clientProperties.setProperty(ClientProperties.NOT_SUPPORTED_CSS_OPACITY, Boolean.TRUE);
160
161             clientProperties.setProperty(ClientProperties.PROPRIETARY_IE_CSS_EXPRESSIONS_SUPPORTED, Boolean.TRUE);
162             clientProperties.setProperty(ClientProperties.PROPRIETARY_EVENT_MOUSE_ENTER_LEAVE_SUPPORTED, Boolean.TRUE);
163             clientProperties.setProperty(ClientProperties.PROPRIETARY_IE_OPACITY_FILTER_REQUIRED, Boolean.TRUE);
164
165             // The following flaws are verified as present in IE7.0Beta2:
166
clientProperties.setProperty(ClientProperties.QUIRK_IE_TABLE_PERCENT_WIDTH_SCROLLBAR_ERROR, Boolean.TRUE);
167             clientProperties.setProperty(ClientProperties.QUIRK_IE_SELECT_PERCENT_WIDTH, Boolean.TRUE);
168             
169             if (majorVersion < 7) {
170                 // The following flaws are verified as fixed in IE7.0Beta2:
171
clientProperties.setProperty(ClientProperties.PROPRIETARY_IE_PNG_ALPHA_FILTER_REQUIRED, Boolean.TRUE);
172                 clientProperties.setProperty(ClientProperties.QUIRK_CSS_POSITIONING_ONE_SIDE_ONLY, Boolean.TRUE);
173                 clientProperties.setProperty(ClientProperties.QUIRK_CSS_BACKGROUND_ATTACHMENT_USE_FIXED, Boolean.TRUE);
174                 clientProperties.setProperty(ClientProperties.QUIRK_IE_SELECT_Z_INDEX, Boolean.TRUE);
175             }
176         }
177         if (browserOpera) {
178             clientProperties.setProperty(ClientProperties.QUIRK_TEXTAREA_CONTENT, Boolean.TRUE);
179             clientProperties.setProperty(ClientProperties.QUIRK_OPERA_NO_CSS_TEXT, Boolean.TRUE);
180             clientProperties.setProperty(ClientProperties.QUIRK_SELECT_REQUIRES_NULL_OPTION, Boolean.TRUE);
181             clientProperties.setProperty(ClientProperties.QUIRK_IE_SELECT_PERCENT_WIDTH, Boolean.TRUE);
182             if (majorVersion < 9) {
183                 clientProperties.setProperty(ClientProperties.NOT_SUPPORTED_CSS_OPACITY, Boolean.TRUE);
184             }
185         }
186         if (browserMozilla) {
187             clientProperties.setProperty(ClientProperties.QUIRK_SELECT_REQUIRES_NULL_OPTION, Boolean.TRUE);
188             clientProperties.setProperty(ClientProperties.QUIRK_MOZILLA_TEXT_INPUT_REPAINT, Boolean.TRUE);
189             clientProperties.setProperty(ClientProperties.QUIRK_MOZILLA_PERFORMANCE_LARGE_DOM_REMOVE, Boolean.TRUE);
190         }
191         
192         if (browserKonqueror) {
193             clientProperties.setProperty(ClientProperties.QUIRK_SELECT_REQUIRES_NULL_OPTION, Boolean.TRUE);
194             clientProperties.setProperty(ClientProperties.QUIRK_IE_SELECT_PERCENT_WIDTH, Boolean.TRUE);
195             clientProperties.setProperty(ClientProperties.NOT_SUPPORTED_CSS_MANIPULATION, Boolean.TRUE);
196         }
197         
198         if (browserSafari) {
199             clientProperties.setProperty(ClientProperties.NOT_SUPPORTED_CSS_MANIPULATION, Boolean.TRUE);
200         }
201     }
202     
203     /**
204      * @see nextapp.echo2.webrender.service.SynchronizeService.ClientMessagePartProcessor#getName()
205      */

206     public String JavaDoc getName() {
207         return "EchoClientAnalyzer";
208     }
209     
210     /**
211      * @see nextapp.echo2.webrender.service.SynchronizeService.ClientMessagePartProcessor#process(
212      * nextapp.echo2.webrender.UserInstance, org.w3c.dom.Element)
213      */

214     public void process(UserInstance userInstance, Element JavaDoc messagePartElement) {
215         ClientProperties clientProperties = new ClientProperties();
216         Element JavaDoc[] propertyElements = DomUtil.getChildElementsByTagName(messagePartElement, "property");
217         for (int i = 0; i < propertyElements.length; ++i) {
218             String JavaDoc propertyName = propertyElements[i].getAttribute("name");
219             if (VALID_PROPERTIES.contains(propertyName)) {
220                 String JavaDoc type = propertyElements[i].getAttribute("type");
221                 if ("text".equals(type)) {
222                     clientProperties.setProperty(propertyName, propertyElements[i].getAttribute("value"));
223                 } else if ("integer".equals(type)) {
224                     try {
225                         clientProperties.setProperty(propertyName,
226                                 new Integer JavaDoc(propertyElements[i].getAttribute("value")));
227                     } catch (NumberFormatException JavaDoc ex) { }
228                 } else if ("boolean".equals(type)) {
229                     clientProperties.setProperty(propertyName,
230                             new Boolean JavaDoc("true".equals(propertyElements[i].getAttribute("value"))));
231                 }
232             }
233         }
234         userInstance.setClientProperties(clientProperties);
235         analyze(clientProperties);
236     }
237 }
238
Popular Tags