1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35
36 public final class RestUtils {
37
38
39
40
41 private RestUtils() throws InstantiationException {
42 throw new InstantiationException(
43 getClass().getName() + " cannot be instantiated.");
44 }
45
46
47
48
49
50
51
52
53
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
75
76
77
78
79
80
81
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 }