Clover icon

Magnolia REST Content Delivery 2.1.3

  1. Project Clover database Fri Oct 25 2019 12:39:35 CEST
  2. Package info.magnolia.rest.delivery.jcr.i18n

File I18nContainerRequestFilter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
50% of files have more coverage

Code metrics

16
23
5
1
120
66
15
0.65
4.6
5
3

Classes

Class Line # Actions
I18nContainerRequestFilter 62 23 0% 15 44
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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  0 toggle public I18nContainerRequestFilter() {
69  0 this.i18nContentSupport = Components.getComponent(I18nContentSupport.class);
70    }
71   
 
72  0 toggle @Override
73    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
74  0 if (ALL_LANGUAGE_PARAM.equalsIgnoreCase(containerRequestContext.getUriInfo().getQueryParameters().getFirst(LANGUAGE_PARAM))) {
75  0 return;
76    }
77  0 Locale preferredLocale = determineLocaleFromQueryParameter(containerRequestContext.getUriInfo().getQueryParameters());
78  0 if (preferredLocale == null) {
79  0 preferredLocale = determineLocaleFromRequest(containerRequestContext);
80    }
81   
82  0 if (preferredLocale != null) {
83  0 i18nContentSupport.setLocale(preferredLocale);
84    }
85    }
86   
 
87  0 toggle private Locale determineLocaleFromQueryParameter(MultivaluedMap<String, String> queryParameters) {
88  0 if (!queryParameters.containsKey(LANGUAGE_PARAM)) {
89  0 return null;
90    }
91  0 String localeString = queryParameters.getFirst(LANGUAGE_PARAM);
92    // Allows to request single language only.
93  0 if (!isValidLanguageTag(localeString)) {
94  0 throw new BadRequestException(String.format("Language parameter: %s is not allowed", localeString));
95    }
96  0 List<LanguageRange> ranges = LanguageRange.parse(localeString);
97  0 return Locale.lookup(ranges, i18nContentSupport.getLocales());
98    }
99   
 
100  0 toggle private Locale determineLocaleFromRequest(ContainerRequestContext request) {
101  0 if (CollectionUtils.isEmpty(i18nContentSupport.getLocales()) || CollectionUtils.isEmpty(request.getAcceptableLanguages())) {
102  0 return null;
103    }
104    // Accepts only for available languages.
105  0 List<Variant> variantList = Variant.VariantListBuilder.newInstance().languages(i18nContentSupport.getLocales().toArray(new Locale[] {})).build();
106  0 Variant bestMatch = request.getRequest().selectVariant(variantList);
107   
108  0 if (bestMatch != null && bestMatch.getLanguage() != null) {
109  0 return bestMatch.getLanguage();
110    }
111  0 return null;
112    }
113   
 
114  0 toggle private boolean isValidLanguageTag(String languageTag) {
115  0 return StringUtils.isBlank(languageTag) ?
116    false :
117    LocaleUtils.availableLocaleList().stream()
118    .anyMatch(locale -> locale.toLanguageTag().equalsIgnoreCase(languageTag));
119    }
120    }