PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/WebApps/ServerII/src/com/dimdim/conference/UtilMethods.java

https://github.com/MarcosBL/DimSim
Java | 225 lines | 179 code | 17 blank | 29 comment | 71 complexity | 1f02617707c76904da736a722920de9b MD5 | raw file
  1. /*
  2. **************************************************************************
  3. * *
  4. * DDDDD iii DDDDD iii *
  5. * DD DD mm mm mmmm DD DD mm mm mmmm *
  6. * DD DD iii mmm mm mm DD DD iii mmm mm mm *
  7. * DD DD iii mmm mm mm DD DD iii mmm mm mm *
  8. * DDDDDD iii mmm mm mm DDDDDD iii mmm mm mm *
  9. * *
  10. **************************************************************************
  11. **************************************************************************
  12. * *
  13. * Part of the DimDim V 1.0 Codebase (http://www.dimdim.com) *
  14. * *
  15. * Copyright (c) 2006 Communiva Inc. All Rights Reserved. *
  16. * *
  17. * *
  18. * This code is licensed under the DimDim License *
  19. * For details please visit http://www.dimdim.com/license *
  20. * *
  21. **************************************************************************
  22. */
  23. package com.dimdim.conference;
  24. import java.util.Map;
  25. import java.util.Enumeration;
  26. import javax.servlet.http.HttpServletRequest;
  27. public class UtilMethods
  28. {
  29. // If the user agent is unknown, set os to Linux, browser to ff and version 2.0
  30. private static final String UNKNOWN_USER_AGENT = "UNKNOWN_USER_AGENT";
  31. public static String findUserAgent(HttpServletRequest servletRequest)
  32. {
  33. String userAgent = null;
  34. Enumeration headers = servletRequest.getHeaderNames();
  35. while (headers.hasMoreElements())
  36. {
  37. String headerName = (String)headers.nextElement();
  38. if (headerName.toLowerCase().equals("user-agent"))
  39. {
  40. userAgent = servletRequest.getHeader(headerName);
  41. break;
  42. }
  43. }
  44. if (userAgent == null)
  45. {
  46. userAgent = UtilMethods.UNKNOWN_USER_AGENT;
  47. }
  48. else
  49. {
  50. userAgent = userAgent.toLowerCase();
  51. }
  52. return userAgent;
  53. }
  54. public static boolean isPublisherSupportable(String osType, String browserType, String browserVersion)
  55. {
  56. boolean pubSupportable = false;
  57. if (osType != null && osType.equals(ConferenceConsoleConstants.OS_WINDOWS))
  58. {
  59. // Publisher is only supportable on windows and in ie and firefox.
  60. if (browserType != null &&
  61. (browserType.equals(ConferenceConsoleConstants.BROWSER_TYPE_IE) ||
  62. browserType.equals(ConferenceConsoleConstants.BROWSER_TYPE_FIREFOX)))
  63. {
  64. pubSupportable = true;
  65. }
  66. }else if (osType != null && osType.equals(ConferenceConsoleConstants.OS_MAC))
  67. {
  68. pubSupportable = true;
  69. }
  70. return pubSupportable;
  71. }
  72. public static void setSessionParameters(Map session, String userAgent)
  73. {
  74. session.put(ConferenceConsoleConstants.OS_TYPE,UtilMethods.getOsType(userAgent));
  75. session.put(ConferenceConsoleConstants.BROWSER_TYPE,UtilMethods.getBrowserType(userAgent));
  76. session.put(ConferenceConsoleConstants.BROWSER_VERSION,UtilMethods.getBrowserVersion(userAgent));
  77. }
  78. public static String getOsType(String userAgent)
  79. {
  80. if (null == userAgent)
  81. return ConferenceConsoleConstants.OS_LINUX;
  82. if (userAgent.equals(UtilMethods.UNKNOWN_USER_AGENT))
  83. return ConferenceConsoleConstants.OS_LINUX;
  84. if (userAgent.contains(ConferenceConsoleConstants.OS_WINDOWS))
  85. return ConferenceConsoleConstants.OS_WINDOWS;
  86. if (userAgent.contains(ConferenceConsoleConstants.OS_MAC))
  87. return ConferenceConsoleConstants.OS_MAC;
  88. if (userAgent.contains(ConferenceConsoleConstants.OS_LINUX))
  89. return ConferenceConsoleConstants.OS_LINUX;
  90. if (userAgent.contains(ConferenceConsoleConstants.OS_UNIX))
  91. return ConferenceConsoleConstants.OS_UNIX;
  92. return ConferenceConsoleConstants.OS_UNKNOWN;
  93. }
  94. public static String getBrowserVersion(String userAgent)
  95. {
  96. if (null == userAgent || userAgent.equals(UtilMethods.UNKNOWN_USER_AGENT))
  97. return ConferenceConsoleConstants.BROWSER_VERSION_FIREFOX_2;
  98. // int indexSafariWindows = userAgent.indexOf("windows");
  99. int indexIEBrowserCheckVersion6 = userAgent.indexOf("msie 6");
  100. int indexIEBrowserCheckVersion7 = userAgent.indexOf("msie 7");
  101. System.out.println(" inside getBrowserType User agent is ---------"+userAgent+"--------");
  102. boolean is_opera = (userAgent.indexOf("opera") != -1);
  103. boolean is_firefox = ((userAgent.indexOf("firefox")!=-1) && (userAgent.indexOf("gecko")!=-1));
  104. boolean is_safari = ((userAgent.indexOf("safari")!=-1)&&(userAgent.indexOf("mac")!=-1))?true:false;
  105. // System.out.println(is_safari);
  106. if(is_safari)
  107. {
  108. if (userAgent.indexOf("safari/522") != -1)
  109. {
  110. return ConferenceConsoleConstants.BROWSER_VERSION_SAFARI_3;
  111. }
  112. else //if(userAgent.indexOf("safari/419") != -1)
  113. {
  114. return ConferenceConsoleConstants.BROWSER_VERSION_SAFARI_2;
  115. }
  116. }
  117. else if (is_firefox)
  118. {
  119. if (userAgent.indexOf("firefox/2") != -1)
  120. {
  121. return ConferenceConsoleConstants.BROWSER_VERSION_FIREFOX_2;
  122. }
  123. else if (userAgent.indexOf("firefox/3") != -1)
  124. {
  125. return ConferenceConsoleConstants.BROWSER_VERSION_FIREFOX_3;
  126. }
  127. else if (userAgent.indexOf("iceweasel") != -1)
  128. {
  129. return ConferenceConsoleConstants.BROWSER_VERSION_FIREFOX_3;
  130. }
  131. else //if (userAgent.indexOf("firefox/1.5") != -1)
  132. {
  133. return ConferenceConsoleConstants.BROWSER_VERSION_FIREFOX_15_OR_LOWER;
  134. }
  135. }
  136. else
  137. {
  138. int iePos = userAgent.indexOf("msie");
  139. int kqPos = userAgent.indexOf("konqueror");
  140. boolean is_konq = false;
  141. if (kqPos != -1)
  142. {
  143. is_konq = true;
  144. }
  145. // boolean is_safari = ((userAgent.indexOf("safari")!=-1)&&(userAgent.indexOf("mac")!=-1))?true:false;
  146. boolean is_khtml = (is_safari || is_konq);
  147. boolean is_ie = ((iePos!=-1) && (!is_opera) && (!is_khtml));
  148. if (is_ie)
  149. {
  150. // System.out.println("msie 7: " + userAgent.indexOf("msie 7"));
  151. // System.out.println("msie 6: " + userAgent.indexOf("msie 6"));
  152. if(indexIEBrowserCheckVersion6 > 0 || indexIEBrowserCheckVersion7 > 0)
  153. {
  154. if (userAgent.indexOf("msie 7") != -1)
  155. {
  156. return ConferenceConsoleConstants.BROWSER_VERSION_IE_7;
  157. }
  158. else //if (userAgent.indexOf("msie 6") != -1)
  159. {
  160. return ConferenceConsoleConstants.BROWSER_VERSION_IE_6_OR_LOWER;
  161. }
  162. }
  163. else
  164. {
  165. return ConferenceConsoleConstants.BROWSER_VERSION_IE_6_OR_LOWER;
  166. }
  167. }
  168. else if(is_opera)
  169. {
  170. return userAgent.substring(userAgent.indexOf('/')+1, userAgent.indexOf('('));
  171. }
  172. }
  173. return ConferenceConsoleConstants.BROWSER_VERSION_UNKOWN;
  174. }
  175. public static String getBrowserType(String userAgent)
  176. {
  177. if (null == userAgent || userAgent.equals(UtilMethods.UNKNOWN_USER_AGENT))
  178. return ConferenceConsoleConstants.BROWSER_TYPE_FIREFOX;
  179. boolean is_opera = (userAgent.indexOf("opera") != -1);
  180. boolean is_firefox = (((userAgent.indexOf("firefox")!=-1) || userAgent.indexOf("iceweasel") != -1) && (userAgent.indexOf("gecko")!=-1));
  181. boolean is_safari = userAgent.indexOf("safari") != -1 ;
  182. boolean is_ie = userAgent.indexOf("msie") != -1;
  183. if(is_safari)
  184. {
  185. return ConferenceConsoleConstants.BROWSER_TYPE_SAFARI;
  186. }
  187. else if(is_opera)
  188. {
  189. return ConferenceConsoleConstants.BROWSER_TYPE_OPERA;
  190. }
  191. else if(is_firefox)
  192. {
  193. return ConferenceConsoleConstants.BROWSER_TYPE_FIREFOX;
  194. }
  195. else if(is_opera)
  196. {
  197. return ConferenceConsoleConstants.BROWSER_TYPE_OPERA;
  198. }
  199. else if(is_ie)
  200. {
  201. return ConferenceConsoleConstants.BROWSER_TYPE_IE;
  202. }
  203. else
  204. {
  205. return ConferenceConsoleConstants.BROWSER_TYPE_UNKNOWN;
  206. }
  207. }
  208. }