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.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   * @author jflute
35   */
36  public class FessUserLocaleProcessProvider implements UserLocaleProcessProvider {
37      private static final Logger logger = LogManager.getLogger(FessUserLocaleProcessProvider.class);
38  
39      @Override
40      public boolean isAcceptCookieLocale() {
41          return false;
42      }
43  
44      @Override
45      public OptionalThing<Locale> findBusinessLocale(final ActionRuntime runtimeMeta, final RequestManager requestManager) {
46          final FessConfig fessConfig = ComponentUtil.getFessConfig();
47          final String name = fessConfig.getQueryBrowserLangParameterName();
48          if (StringUtil.isNotBlank(name)) {
49              try {
50                  return requestManager.getParameter(name).filter(StringUtil::isNotBlank).map(LocaleUtils::toLocale);
51              } catch (final Exception e) {
52                  logger.debug("Failed to parse a value of {}.", name, e);
53              }
54          }
55          return OptionalObject.empty();
56      }
57  
58      @Override
59      public OptionalThing<Locale> getRequestedLocale(final RequestManager requestManager) {
60          return OptionalObject.empty(); // means browser default
61      }
62  
63      // ===================================================================================
64      //                                                                      Basic Override
65      //                                                                      ==============
66      @Override
67      public String toString() {
68          final String hash = Integer.toHexString(hashCode());
69          return DfTypeUtil.toClassTitle(this) + ":{acceptCookieLocale=" + isAcceptCookieLocale() + "}@" + hash;
70      }
71  }