View Javadoc
1   /*
2    * Copyright 2014 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.application.util;
20  
21  import javax.servlet.http.HttpServletRequest;
22  
23  import org.springframework.web.context.request.RequestContextHolder;
24  import org.springframework.web.context.request.ServletRequestAttributes;
25  
26  import com.vaadin.server.WebBrowser;
27  
28  /**
29   * The Class WebBrowserUtil.
30   */
31  public final class WebBrowserUtil {
32  
33  	/** The Constant UNKNOWN. */
34  	private static final String UNKNOWN = "unknown";
35  
36  	/** The Constant I_PHONE. */
37  	private static final String I_PHONE = "IPhone";
38  
39  	/** The Constant IPAD. */
40  	private static final String IPAD = "IPad";
41  
42  	/** The Constant IOS. */
43  	private static final String IOS = "IOS";
44  
45  	/** The Constant ANDROID. */
46  	private static final String ANDROID = "Android";
47  
48  	/** The Constant MAC_OSX. */
49  	private static final String MAC_OSX = "MacOSX";
50  
51  	/** The Constant WINDOWS_PHONE. */
52  	private static final String WINDOWS_PHONE = "WindowsPhone";
53  
54  	/** The Constant WINDOWS2. */
55  	private static final String WINDOWS2 = "Windows";
56  
57  	/** The Constant LINUX. */
58  	private static final String LINUX = "Linux";
59  
60  	/** The Constant X_FORWARDED_FOR. */
61  	private static final String X_FORWARDED_FOR = "X-Forwarded-For";
62  
63  	/**
64  	 * Instantiates a new web browser util.
65  	 */
66  	private WebBrowserUtil() {
67  		super();
68  	}
69  
70  
71  	/**
72  	 * Gets the ip information.
73  	 *
74  	 * @param webBrowser
75  	 *            the web browser
76  	 * @return the ip information
77  	 */
78  	public static String getIpInformation(final WebBrowser webBrowser) {
79  		String ipInformation=webBrowser.getAddress();
80  
81  		final HttpServletRequest httpRequest=((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
82  		final String xForwardedForHeader = httpRequest.getHeader(X_FORWARDED_FOR);
83  		if (xForwardedForHeader != null) {
84  			final String[] split = xForwardedForHeader.split(",");
85  			if (split.length != 0) {
86  				ipInformation = split[0];
87  			}
88  		}
89  		return ipInformation;
90  	}
91  
92  
93  	/**
94  	 * Gets the operating system.
95  	 *
96  	 * @param webBrowser
97  	 *            the web browser
98  	 * @return the operating system
99  	 */
100 	public static String getOperatingSystem(final WebBrowser webBrowser) {
101 		String osName = UNKNOWN;
102 	       if (webBrowser.isLinux()) {
103 			osName = LINUX;
104 		} else if (webBrowser.isWindows()) {
105 			osName = WINDOWS2;
106 		} else if (webBrowser.isWindowsPhone()) {
107 			osName = WINDOWS_PHONE;
108 		} else if (webBrowser.isMacOSX()) {
109 			osName = MAC_OSX;
110 		} else if (webBrowser.isAndroid()) {
111 			osName = ANDROID;
112 		} else if (webBrowser.isIOS()) {
113 			osName = IOS;
114 		} else if (webBrowser.isIPad()) {
115 			osName = IPAD;
116 		} else if (webBrowser.isIPhone()) {
117 			osName = I_PHONE;
118 		}
119 		return osName;
120 	}
121 
122 
123 }