WebBrowserUtil.java

  1. /*
  2.  * Copyright 2010-2025 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. import nl.basjes.parse.useragent.UserAgent;
  25. import nl.basjes.parse.useragent.UserAgentAnalyzer;

  26. /**
  27.  * The Class WebBrowserUtil.
  28.  */
  29. public final class WebBrowserUtil {

  30.     /** The Constant USER_AGENT_ANALYZER. */
  31.     private static final UserAgentAnalyzer USER_AGENT_ANALYZER = UserAgentAnalyzer
  32.             .newBuilder()
  33.             .hideMatcherLoadStats()
  34.             .withCache(10000)
  35.             .build();

  36.     /** The Constant X_FORWARDED_FOR. */
  37.     public static final String X_FORWARDED_FOR = "X-Forwarded-For";

  38.     /**
  39.      * Default constructor for WebBrowserUtil.
  40.      */
  41.     public WebBrowserUtil() {
  42.         // Default constructor
  43.     }

  44.     /**
  45.      * Gets the ip information.
  46.      *
  47.      * @param webBrowser
  48.      *            the web browser
  49.      * @return the ip information
  50.      */
  51.     public static String getIpInformation(final WebBrowser webBrowser) {
  52.         String ipInformation=webBrowser.getAddress();

  53.         final HttpServletRequest httpRequest=((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
  54.         final String xForwardedForHeader = httpRequest.getHeader(X_FORWARDED_FOR);
  55.         if (xForwardedForHeader != null) {
  56.             final String[] split = xForwardedForHeader.split(",");
  57.             if (split.length != 0) {
  58.                 ipInformation = split[0];
  59.             }
  60.         }
  61.         return ipInformation;
  62.     }


  63.     /**
  64.      * Gets the operating system.
  65.      *
  66.      * @param webBrowser
  67.      *            the web browser
  68.      * @return the operating system
  69.      */
  70.     public static String getOperatingSystem(final WebBrowser webBrowser) {
  71.         synchronized (USER_AGENT_ANALYZER) {
  72.             final UserAgent userAgent = USER_AGENT_ANALYZER.parse(webBrowser.getBrowserApplication());
  73.             return userAgent.getValue(UserAgent.DEVICE_CLASS) + "." +userAgent.getValue(UserAgent.OPERATING_SYSTEM_NAME) +"." + userAgent.getValue(UserAgent.OPERATING_SYSTEM_VERSION);
  74.         }
  75.     }

  76. }