View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.rest.delivery.jcr.i18n;
35  
36  import info.magnolia.cms.i18n.I18nContentSupport;
37  import info.magnolia.objectfactory.Components;
38  
39  import java.io.IOException;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Locale.LanguageRange;
43  
44  import javax.ws.rs.BadRequestException;
45  import javax.ws.rs.container.ContainerRequestContext;
46  import javax.ws.rs.container.ContainerRequestFilter;
47  import javax.ws.rs.core.MultivaluedMap;
48  import javax.ws.rs.core.Variant;
49  import javax.ws.rs.ext.Provider;
50  
51  import org.apache.commons.collections4.CollectionUtils;
52  import org.apache.commons.lang3.LocaleUtils;
53  import org.apache.commons.lang3.StringUtils;
54  
55  /**
56   * This filter determines a locale by detecting "Accept-Language" HTTP header and the "lang" request parameter.
57   * "lang" parameter has higher priority than "Accept-Language" header.
58   * If the locale can't be determined, {@link I18nContentSupport} will do the job.
59   */
60  @Provider
61  @I18n
62  public class I18nContainerRequestFilter implements ContainerRequestFilter {
63      private static final String LANGUAGE_PARAM = "lang";
64      private static final String ALL_LANGUAGE_PARAM = "all";
65  
66      private final I18nContentSupport i18nContentSupport;
67  
68      public I18nContainerRequestFilter() {
69          this.i18nContentSupport = Components.getComponent(I18nContentSupport.class);
70      }
71  
72      @Override
73      public void filter(ContainerRequestContext containerRequestContext) throws IOException {
74          if (ALL_LANGUAGE_PARAM.equalsIgnoreCase(containerRequestContext.getUriInfo().getQueryParameters().getFirst(LANGUAGE_PARAM))) {
75              return;
76          }
77          Locale preferredLocale = determineLocaleFromQueryParameter(containerRequestContext.getUriInfo().getQueryParameters());
78          if (preferredLocale == null) {
79              preferredLocale = determineLocaleFromRequest(containerRequestContext);
80          }
81  
82          if (preferredLocale != null) {
83              i18nContentSupport.setLocale(preferredLocale);
84          }
85      }
86  
87      private Locale determineLocaleFromQueryParameter(MultivaluedMap<String, String> queryParameters) {
88          if (!queryParameters.containsKey(LANGUAGE_PARAM)) {
89              return null;
90          }
91          String localeString = queryParameters.getFirst(LANGUAGE_PARAM);
92          // Allows to request single language only.
93          if (!isValidLanguageTag(localeString)) {
94              throw new BadRequestException(String.format("Language parameter: %s is not allowed", localeString));
95          }
96          List<LanguageRange> ranges = LanguageRange.parse(localeString);
97          return Locale.lookup(ranges, i18nContentSupport.getLocales());
98      }
99  
100     private Locale determineLocaleFromRequest(ContainerRequestContext request) {
101         if (CollectionUtils.isEmpty(i18nContentSupport.getLocales()) || CollectionUtils.isEmpty(request.getAcceptableLanguages())) {
102             return null;
103         }
104         // Accepts only for available languages.
105         List<Variant> variantList = Variant.VariantListBuilder.newInstance().languages(i18nContentSupport.getLocales().toArray(new Locale[] {})).build();
106         Variant bestMatch = request.getRequest().selectVariant(variantList);
107 
108         if (bestMatch != null && bestMatch.getLanguage() != null) {
109             return bestMatch.getLanguage();
110         }
111         return null;
112     }
113 
114     private boolean isValidLanguageTag(String languageTag) {
115         return StringUtils.isBlank(languageTag) ?
116                 false : 
117                 LocaleUtils.availableLocaleList().stream()
118                         .anyMatch(locale -> locale.toLanguageTag().equalsIgnoreCase(languageTag));
119     }
120 }