View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import org.lastaflute.web.util.LaRequestUtil;
19  
20  public class UserAgentHelper {
21      protected static final String USER_AGENT = "user-agent";
22  
23      protected static final String USER_AGENT_TYPE = "ViewHelper.UserAgent";
24  
25      public UserAgentType getUserAgentType() {
26          return LaRequestUtil.getOptionalRequest().map(request -> {
27              UserAgentType uaType = (UserAgentType) request.getAttribute(USER_AGENT_TYPE);
28              if (uaType == null) {
29                  final String userAgent = request.getHeader(USER_AGENT);
30                  if (userAgent != null) {
31                      if (userAgent.indexOf("MSIE") >= 0 || userAgent.indexOf("Trident") >= 0) {
32                          uaType = UserAgentType.IE;
33                      } else if (userAgent.indexOf("Firefox") >= 0) {
34                          uaType = UserAgentType.FIREFOX;
35                      } else if (userAgent.indexOf("Chrome") >= 0) {
36                          uaType = UserAgentType.CHROME;
37                      } else if (userAgent.indexOf("Safari") >= 0) {
38                          uaType = UserAgentType.SAFARI;
39                      } else if (userAgent.indexOf("Opera") >= 0) {
40                          uaType = UserAgentType.OPERA;
41                      }
42                  }
43                  if (uaType == null) {
44                      uaType = UserAgentType.OTHER;
45                  }
46                  request.setAttribute(USER_AGENT_TYPE, uaType);
47              }
48              return uaType;
49          }).orElse(UserAgentType.OTHER);
50      }
51  
52      public enum UserAgentType {
53          IE, FIREFOX, CHROME, SAFARI, OPERA, OTHER;
54      }
55  
56  }