1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2018 Mike Becker. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 * |
|
28 */ |
|
29 package de.uapcore.lightpit.modules; |
|
30 |
|
31 import de.uapcore.lightpit.LightPITModule; |
|
32 import de.uapcore.lightpit.AbstractLightPITServlet; |
|
33 import de.uapcore.lightpit.Constants; |
|
34 import de.uapcore.lightpit.Functions; |
|
35 import de.uapcore.lightpit.HttpMethod; |
|
36 import javax.servlet.annotation.WebServlet; |
|
37 import javax.servlet.http.HttpServletRequest; |
|
38 import javax.servlet.http.HttpServletResponse; |
|
39 import de.uapcore.lightpit.RequestMapping; |
|
40 import de.uapcore.lightpit.ResponseType; |
|
41 import java.util.ArrayList; |
|
42 import java.util.IllformedLocaleException; |
|
43 import java.util.List; |
|
44 import java.util.Locale; |
|
45 import java.util.Optional; |
|
46 import javax.servlet.ServletException; |
|
47 import org.slf4j.Logger; |
|
48 import org.slf4j.LoggerFactory; |
|
49 |
|
50 |
|
51 @LightPITModule( |
|
52 bundleBaseName = "de.uapcore.lightpit.resources.localization.language", |
|
53 modulePath = "language" |
|
54 ) |
|
55 @WebServlet( |
|
56 name = "LanguageModule", |
|
57 urlPatterns = "/language/*" |
|
58 ) |
|
59 public final class LanguageModule extends AbstractLightPITServlet { |
|
60 |
|
61 private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class); |
|
62 |
|
63 private final List<Locale> languages = new ArrayList<>(); |
|
64 |
|
65 @Override |
|
66 public void init() throws ServletException { |
|
67 super.init(); |
|
68 |
|
69 Optional<String[]> langs = Functions.availableLanguages(getServletContext()); |
|
70 if (langs.isPresent()) { |
|
71 for (String lang : langs.get()) { |
|
72 try { |
|
73 Locale locale = Locale.forLanguageTag(lang); |
|
74 if (locale.getLanguage().isEmpty()) { |
|
75 throw new IllformedLocaleException(); |
|
76 } |
|
77 languages.add(locale); |
|
78 } catch (IllformedLocaleException ex) { |
|
79 LOG.warn("Specified lanaguge {} in context parameter cannot be mapped to an existing locale - skipping.", lang); |
|
80 } |
|
81 } |
|
82 |
|
83 } else { |
|
84 languages.add(Locale.ENGLISH); |
|
85 LOG.warn("Context parameter 'available-languges' not found. Only english will be available."); |
|
86 } |
|
87 } |
|
88 |
|
89 @Override |
|
90 public void destroy() { |
|
91 super.destroy(); |
|
92 languages.clear(); |
|
93 } |
|
94 |
|
95 @RequestMapping(method = HttpMethod.GET) |
|
96 public ResponseType handle(HttpServletRequest req, HttpServletResponse resp) { |
|
97 |
|
98 req.setAttribute("languages", languages); |
|
99 req.setAttribute("browserLanguage", req.getLocale()); |
|
100 |
|
101 setStylesheet(req, "language"); |
|
102 setDynamicFragment(req, "language"); |
|
103 return ResponseType.HTML_FULL; |
|
104 } |
|
105 |
|
106 @RequestMapping(method = HttpMethod.POST) |
|
107 public ResponseType switchLanguage(HttpServletRequest req, HttpServletResponse resp) { |
|
108 |
|
109 Optional<Locale> chosenLanguage = Optional.ofNullable(req.getParameter("language")) |
|
110 .map(Locale::forLanguageTag) |
|
111 .filter((l) -> !l.getLanguage().isEmpty()); |
|
112 |
|
113 chosenLanguage.ifPresent((l) -> req.getSession().setAttribute(Constants.SESSION_ATTR_LANGUAGE, l)); |
|
114 chosenLanguage.ifPresent(resp::setLocale); |
|
115 |
|
116 return handle(req, resp); |
|
117 } |
|
118 } |
|