1 /*
2 * Copyright 2012-2025 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 /**
21 * Helper class for detecting and categorizing user agent types from HTTP requests.
22 * This class analyzes the User-Agent header to determine which browser type is being used
23 * and caches the result in the request attribute for performance optimization.
24 *
25 */
26 public class UserAgentHelper {
27
28 /**
29 * Default constructor for UserAgentHelper.
30 */
31 public UserAgentHelper() {
32 // Default constructor
33 }
34
35 /** The HTTP header name for User-Agent */
36 protected static final String USER_AGENT = "user-agent";
37
38 /** The request attribute key for storing cached user agent type */
39 protected static final String USER_AGENT_TYPE = "ViewHelper.UserAgent";
40
41 /**
42 * Determines the user agent type from the current HTTP request.
43 * The method analyzes the User-Agent header to categorize the browser type
44 * and caches the result in the request attribute for subsequent calls.
45 *
46 * @return the detected user agent type, or OTHER if no specific type is detected
47 */
48 public UserAgentType getUserAgentType() {
49 return LaRequestUtil.getOptionalRequest().map(request -> {
50 UserAgentType uaType = (UserAgentType) request.getAttribute(USER_AGENT_TYPE);
51 if (uaType == null) {
52 final String userAgent = request.getHeader(USER_AGENT);
53 if (userAgent != null) {
54 if (userAgent.indexOf("MSIE") >= 0 || userAgent.indexOf("Trident") >= 0) {
55 uaType = UserAgentType.IE;
56 } else if (userAgent.indexOf("Firefox") >= 0) {
57 uaType = UserAgentType.FIREFOX;
58 } else if (userAgent.indexOf("Chrome") >= 0) {
59 uaType = UserAgentType.CHROME;
60 } else if (userAgent.indexOf("Safari") >= 0) {
61 uaType = UserAgentType.SAFARI;
62 } else if (userAgent.indexOf("Opera") >= 0) {
63 uaType = UserAgentType.OPERA;
64 }
65 }
66 if (uaType == null) {
67 uaType = UserAgentType.OTHER;
68 }
69 request.setAttribute(USER_AGENT_TYPE, uaType);
70 }
71 return uaType;
72 }).orElse(UserAgentType.OTHER);
73 }
74
75 /**
76 * Enumeration of supported browser types for user agent detection.
77 * Each type represents a major browser family that can be detected
78 * from the User-Agent header string.
79 */
80 public enum UserAgentType {
81 /** Internet Explorer and Trident-based browsers */
82 IE,
83 /** Mozilla Firefox browser */
84 FIREFOX,
85 /** Google Chrome browser */
86 CHROME,
87 /** Apple Safari browser */
88 SAFARI,
89 /** Opera browser */
90 OPERA,
91 /** Other or unrecognized browser types */
92 OTHER;
93 }
94
95 }