001: /*
002: * $Id: WebClientInfo.java 530673 2007-04-20 07:09:05Z ivaynberg $ $Revision:
003: * 1.1 $ $Date: 2007-04-20 09:09:05 +0200 (Fri, 20 Apr 2007) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.protocol.http.request;
019:
020: import javax.servlet.http.HttpServletRequest;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import wicket.WicketRuntimeException;
026: import wicket.protocol.http.ClientProperties;
027: import wicket.protocol.http.WebRequestCycle;
028: import wicket.request.ClientInfo;
029:
030: /**
031: * Default client info object for web applications.
032: *
033: * Based on <code>nextapp.echo2.webrender.ClientAnalyzerProcessor</code> from
034: * collegue java webapplication framework <a
035: * href="http://www.nextapp.com/platform/echo2/echo/">NextApp Echo 2</a>.
036: *
037: * @author Eelco Hillenius
038: */
039: public class WebClientInfo extends ClientInfo {
040: private static final long serialVersionUID = 1L;
041:
042: /** log. */
043: private static final Log log = LogFactory
044: .getLog(WebClientInfo.class);
045:
046: /**
047: * The user agent string from the User-Agent header, app. Theoretically,
048: * this might differ from {@link ClientProperties#NAVIGATOR_JAVA_ENABLED}
049: * property, which is not set until an actual reply from a browser (e.g.
050: * using {@link wicket.markup.html.pages.BrowserInfoPage} is set.
051: */
052: private final String userAgent;
053:
054: /** Client properties object. */
055: private final ClientProperties properties = new ClientProperties();
056:
057: /**
058: * Construct.
059: *
060: * @param requestCycle
061: * the request cycle
062: */
063: public WebClientInfo(WebRequestCycle requestCycle) {
064: super ();
065: HttpServletRequest httpServletRequest = requestCycle
066: .getWebRequest().getHttpServletRequest();
067: userAgent = httpServletRequest.getHeader("User-Agent");
068: init();
069: }
070:
071: /**
072: * Construct.
073: *
074: * @param requestCycle
075: * the request cycle
076: * @param userAgent
077: * the user agent
078: */
079: public WebClientInfo(WebRequestCycle requestCycle, String userAgent) {
080: super ();
081: this .userAgent = userAgent;
082: init();
083: }
084:
085: /**
086: * Initialize the client properties object
087: */
088: private final void init() {
089: String userAgent = (getUserAgent() != null) ? getUserAgent()
090: .toLowerCase() : "";
091:
092: boolean browserOpera = userAgent.indexOf("opera") != -1;
093: boolean browserSafari = userAgent.indexOf("safari") != -1;
094: boolean browserKonqueror = userAgent.indexOf("konqueror") != -1;
095:
096: // Note deceptive user agent fields:
097: // - Konqueror and Safari UA fields contain "like Gecko"
098: // - Opera UA field typically contains "MSIE"
099: boolean deceptiveUserAgent = browserOpera || browserSafari
100: || browserKonqueror;
101:
102: boolean browserMozilla = !deceptiveUserAgent
103: && userAgent.indexOf("gecko") != -1;
104: boolean browserFireFox = userAgent.indexOf("firefox") != -1;
105: boolean browserInternetExplorer = !deceptiveUserAgent
106: && userAgent.indexOf("msie") != -1;
107:
108: int majorVersion = -1, minorVersion = -1;
109:
110: // Store browser information.
111: if (browserOpera) {
112: properties.setProperty(ClientProperties.BROWSER_OPERA,
113: Boolean.TRUE);
114: } else if (browserKonqueror) {
115: properties.setProperty(ClientProperties.BROWSER_KONQUEROR,
116: Boolean.TRUE);
117: } else if (browserSafari) {
118: properties.setProperty(ClientProperties.BROWSER_SAFARI,
119: Boolean.TRUE);
120: } else if (browserMozilla) {
121: properties.setProperty(ClientProperties.BROWSER_MOZILLA,
122: Boolean.TRUE);
123: if (browserFireFox) {
124: properties.setProperty(
125: ClientProperties.BROWSER_MOZILLA_FIREFOX,
126: Boolean.TRUE);
127: }
128: } else if (browserInternetExplorer) {
129: properties.setProperty(
130: ClientProperties.BROWSER_INTERNET_EXPLORER,
131: Boolean.TRUE);
132: if (userAgent.indexOf("msie 6.") != -1) {
133: majorVersion = 6;
134: } else if (userAgent.indexOf("msie 7.") != -1) {
135: majorVersion = 7;
136: }
137: }
138:
139: if (majorVersion != -1) {
140: properties.setProperty(
141: ClientProperties.BROWSER_VERSION_MAJOR, Integer
142: .toString(majorVersion));
143: }
144:
145: if (minorVersion != -1) {
146: properties.setProperty(
147: ClientProperties.BROWSER_VERSION_MINOR, Integer
148: .toString(minorVersion));
149: }
150:
151: // Set quirk flags.
152: if (browserInternetExplorer) {
153: properties
154: .setProperty(
155: ClientProperties.PROPRIETARY_IE_CSS_EXPRESSIONS_SUPPORTED,
156: Boolean.TRUE);
157: properties
158: .setProperty(
159: ClientProperties.QUIRK_CSS_POSITIONING_ONE_SIDE_ONLY,
160: Boolean.TRUE);
161: properties.setProperty(ClientProperties.QUIRK_IE_REPAINT,
162: Boolean.TRUE);
163: properties.setProperty(
164: ClientProperties.QUIRK_IE_SELECT_Z_INDEX,
165: Boolean.TRUE);
166: properties
167: .setProperty(
168: ClientProperties.QUIRK_IE_TEXTAREA_NEWLINE_OBLITERATION,
169: Boolean.TRUE);
170: properties.setProperty(
171: ClientProperties.QUIRK_IE_SELECT_PERCENT_WIDTH,
172: Boolean.TRUE);
173: properties.setProperty(
174: ClientProperties.QUIRK_IE_SELECT_LIST_DOM_UPDATE,
175: Boolean.TRUE);
176: properties
177: .setProperty(
178: ClientProperties.QUIRK_IE_TABLE_PERCENT_WIDTH_SCROLLBAR_ERROR,
179: Boolean.TRUE);
180: properties
181: .setProperty(
182: ClientProperties.QUIRK_CSS_BACKGROUND_ATTACHMENT_USE_FIXED,
183: Boolean.TRUE);
184: properties.setProperty(
185: ClientProperties.QUIRK_CSS_BORDER_COLLAPSE_INSIDE,
186: Boolean.TRUE);
187: properties
188: .setProperty(
189: ClientProperties.QUIRK_CSS_BORDER_COLLAPSE_FOR_0_PADDING,
190: Boolean.TRUE);
191: if (majorVersion < 7) {
192: properties
193: .setProperty(
194: ClientProperties.PROPRIETARY_IE_PNG_ALPHA_FILTER_REQUIRED,
195: Boolean.TRUE);
196: }
197: }
198: if (browserMozilla) {
199: properties.setProperty(
200: ClientProperties.QUIRK_MOZILLA_TEXT_INPUT_REPAINT,
201: Boolean.TRUE);
202: properties
203: .setProperty(
204: ClientProperties.QUIRK_MOZILLA_PERFORMANCE_LARGE_DOM_REMOVE,
205: Boolean.TRUE);
206: }
207:
208: if (log.isDebugEnabled()) {
209: log.debug("determined user agent: " + properties);
210: }
211: }
212:
213: /**
214: * Gets the user agent string.
215: *
216: * @return the user agent string
217: */
218: public final String getUserAgent() {
219: return userAgent;
220: }
221:
222: /**
223: * Gets the client properties object.
224: *
225: * @return the client properties object
226: */
227: public final ClientProperties getProperties() {
228: return properties;
229: }
230: }
|