WebBrowserUtil.java

  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. import javax.servlet.http.HttpServletRequest;

  21. import org.springframework.web.context.request.RequestContextHolder;
  22. import org.springframework.web.context.request.ServletRequestAttributes;

  23. import com.vaadin.server.WebBrowser;

  24. /**
  25.  * The Class WebBrowserUtil.
  26.  */
  27. public final class WebBrowserUtil {

  28.     /** The Constant UNKNOWN. */
  29.     private static final String UNKNOWN = "unknown";

  30.     /** The Constant I_PHONE. */
  31.     private static final String I_PHONE = "IPhone";

  32.     /** The Constant IPAD. */
  33.     private static final String IPAD = "IPad";

  34.     /** The Constant IOS. */
  35.     private static final String IOS = "IOS";

  36.     /** The Constant ANDROID. */
  37.     private static final String ANDROID = "Android";

  38.     /** The Constant MAC_OSX. */
  39.     private static final String MAC_OSX = "MacOSX";

  40.     /** The Constant WINDOWS_PHONE. */
  41.     private static final String WINDOWS_PHONE = "WindowsPhone";

  42.     /** The Constant WINDOWS2. */
  43.     private static final String WINDOWS2 = "Windows";

  44.     /** The Constant LINUX. */
  45.     private static final String LINUX = "Linux";

  46.     /** The Constant X_FORWARDED_FOR. */
  47.     private static final String X_FORWARDED_FOR = "X-Forwarded-For";

  48.     /**
  49.      * Instantiates a new web browser util.
  50.      */
  51.     private WebBrowserUtil() {
  52.         super();
  53.     }


  54.     /**
  55.      * Gets the ip information.
  56.      *
  57.      * @param webBrowser
  58.      *            the web browser
  59.      * @return the ip information
  60.      */
  61.     public static String getIpInformation(final WebBrowser webBrowser) {
  62.         String ipInformation=webBrowser.getAddress();

  63.         final HttpServletRequest httpRequest=((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
  64.         final String xForwardedForHeader = httpRequest.getHeader(X_FORWARDED_FOR);
  65.         if (xForwardedForHeader != null) {
  66.             final String[] split = xForwardedForHeader.split(",");
  67.             if (split.length != 0) {
  68.                 ipInformation = split[0];
  69.             }
  70.         }
  71.         return ipInformation;
  72.     }


  73.     /**
  74.      * Gets the operating system.
  75.      *
  76.      * @param webBrowser
  77.      *            the web browser
  78.      * @return the operating system
  79.      */
  80.     public static String getOperatingSystem(final WebBrowser webBrowser) {
  81.         String osName = UNKNOWN;
  82.            if (webBrowser.isLinux()) {
  83.             osName = LINUX;
  84.         } else if (webBrowser.isWindows()) {
  85.             osName = WINDOWS2;
  86.         } else if (webBrowser.isWindowsPhone()) {
  87.             osName = WINDOWS_PHONE;
  88.         } else if (webBrowser.isMacOSX()) {
  89.             osName = MAC_OSX;
  90.         } else if (webBrowser.isAndroid()) {
  91.             osName = ANDROID;
  92.         } else if (webBrowser.isIOS()) {
  93.             osName = IOS;
  94.         } else if (webBrowser.isIPad()) {
  95.             osName = IPAD;
  96.         } else if (webBrowser.isIPhone()) {
  97.             osName = I_PHONE;
  98.         }
  99.         return osName;
  100.     }


  101. }