1 /*
2 * This file is part of Waarp Project (named also Waarp or GG).
3 *
4 * Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5 * tags. See the COPYRIGHT.txt in the distribution for a full listing of
6 * individual contributors.
7 *
8 * All Waarp Project is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * Waarp . If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 package org.waarp.openr66.protocol.http.restv2.utils;
22
23 import io.netty.handler.codec.http.HttpRequest;
24 import org.glassfish.jersey.message.internal.AcceptableLanguageTag;
25 import org.glassfish.jersey.message.internal.HttpHeaderReader;
26
27 import java.text.ParseException;
28 import java.util.List;
29 import java.util.Locale;
30
31 import static javax.ws.rs.core.HttpHeaders.*;
32
33 /**
34 * A series of utility methods shared by all handlers of the RESTv2 API.
35 */
36 public final class RestUtils {
37
38 /**
39 * Makes the default constructor of this utility class inaccessible.
40 */
41 private RestUtils() throws InstantiationException {
42 throw new InstantiationException(
43 getClass().getName() + " cannot be instantiated.");
44 }
45
46 // ######################### PUBLIC METHODS #################################
47
48 /**
49 * Returns the language of the given request.
50 *
51 * @param request the HTTP request
52 *
53 * @return the request's language
54 */
55 public static Locale getLocale(final HttpRequest request) {
56 final String langHead = request.headers().get(ACCEPT_LANGUAGE);
57
58 try {
59 final List<AcceptableLanguageTag> acceptableLanguages =
60 HttpHeaderReader.readAcceptLanguage(langHead);
61 AcceptableLanguageTag bestMatch = acceptableLanguages.get(0);
62 for (final AcceptableLanguageTag acceptableLanguage : acceptableLanguages) {
63 if (acceptableLanguage.getQuality() > bestMatch.getQuality()) {
64 bestMatch = acceptableLanguage;
65 }
66 }
67 return bestMatch.getAsLocale();
68 } catch (final ParseException e) {
69 return Locale.getDefault();
70 }
71 }
72
73 /**
74 * Converts a String into its' corresponding boolean value.
75 *
76 * @param string the String to convert
77 *
78 * @return the corresponding boolean value
79 *
80 * @throws IllegalArgumentException If the String does not represent
81 * a valid boolean value.
82 */
83 public static boolean stringToBoolean(final String string) {
84 if ("true".equalsIgnoreCase(string)) {
85 return true;
86 } else if ("false".equalsIgnoreCase(string)) {
87 return false;
88 } else {
89 throw new IllegalArgumentException();
90 }
91 }
92
93 }