View Javadoc
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.mylasta.direction.sponsor;
17  
18  import java.util.Locale;
19  
20  import org.apache.commons.lang3.LocaleUtils;
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.codelibs.core.lang.StringUtil;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.util.ComponentUtil;
26  import org.dbflute.optional.OptionalObject;
27  import org.dbflute.optional.OptionalThing;
28  import org.dbflute.util.DfTypeUtil;
29  import org.lastaflute.web.ruts.process.ActionRuntime;
30  import org.lastaflute.web.servlet.request.RequestManager;
31  import org.lastaflute.web.servlet.request.UserLocaleProcessProvider;
32  
33  /**
34   * The provider of user locale process.
35   *
36   * @author jflute
37   */
38  public class FessUserLocaleProcessProvider implements UserLocaleProcessProvider {
39      private static final Logger logger = LogManager.getLogger(FessUserLocaleProcessProvider.class);
40  
41      @Override
42      public boolean isAcceptCookieLocale() {
43          return false;
44      }
45  
46      @Override
47      public OptionalThing<Locale> findBusinessLocale(final ActionRuntime runtimeMeta, final RequestManager requestManager) {
48          final FessConfig fessConfig = ComponentUtil.getFessConfig();
49          final String name = fessConfig.getQueryBrowserLangParameterName();
50          if (StringUtil.isNotBlank(name)) {
51              try {
52                  return requestManager.getParameter(name).filter(StringUtil::isNotBlank).map(LocaleUtils::toLocale);
53              } catch (final Exception e) {
54                  logger.debug("Failed to parse locale: name={}", name, e);
55              }
56          }
57          return OptionalObject.empty();
58      }
59  
60      @Override
61      public OptionalThing<Locale> getRequestedLocale(final RequestManager requestManager) {
62          return OptionalObject.empty(); // means browser default
63      }
64  
65      // ===================================================================================
66      //                                                                      Basic Override
67      //                                                                      ==============
68      @Override
69      public String toString() {
70          final String hash = Integer.toHexString(hashCode());
71          return DfTypeUtil.toClassTitle(this) + ":{acceptCookieLocale=" + isAcceptCookieLocale() + "}@" + hash;
72      }
73  }