src/main/java/de/uapcore/lightpit/modules/LanguageModule.java

Tue, 05 Jan 2021 19:19:31 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 05 Jan 2021 19:19:31 +0100
changeset 179
623c340058f3
parent 158
4f912cd42876
child 180
009700915269
permissions
-rw-r--r--

migrates the utility classes for the AbstractServlet

     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;
    31 import de.uapcore.lightpit.AbstractServlet;
    32 import de.uapcore.lightpit.Constants;
    33 import de.uapcore.lightpit.HttpMethod;
    34 import de.uapcore.lightpit.RequestMapping;
    35 import de.uapcore.lightpit.viewmodel.LanguageView;
    36 import org.slf4j.Logger;
    37 import org.slf4j.LoggerFactory;
    39 import javax.servlet.ServletException;
    40 import javax.servlet.annotation.WebServlet;
    41 import javax.servlet.http.HttpServletRequest;
    42 import javax.servlet.http.HttpServletResponse;
    43 import java.io.IOException;
    44 import java.util.*;
    46 @WebServlet(
    47         name = "LanguageModule",
    48         urlPatterns = "/language/*"
    49 )
    50 public final class LanguageModule extends AbstractServlet {
    52     private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class);
    54     private final List<Locale> languages = new ArrayList<>();
    56     @Override
    57     protected String getResourceBundleName() {
    58         return "localization.language";
    59     }
    61     @Override
    62     public void init() throws ServletException {
    63         super.init();
    65         Optional<String[]> langs = availableLanguages();
    66         if (langs.isPresent()) {
    67             for (String lang : langs.get()) {
    68                 try {
    69                     Locale locale = Locale.forLanguageTag(lang);
    70                     if (locale.getLanguage().isEmpty()) {
    71                         throw new IllformedLocaleException();
    72                     }
    73                     languages.add(locale);
    74                 } catch (IllformedLocaleException ex) {
    75                     LOG.warn("Specified language {} in context parameter cannot be mapped to an existing locale - skipping.", lang);
    76                 }
    77             }
    79         } else {
    80             languages.add(Locale.ENGLISH);
    81             LOG.warn("Context parameter 'available-languages' not found. Only english will be available.");
    82         }
    83     }
    85     @Override
    86     public void destroy() {
    87         super.destroy();
    88         languages.clear();
    89     }
    91     @RequestMapping(method = HttpMethod.GET)
    92     public void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    94         final var viewModel = new LanguageView();
    95         viewModel.setLanguages(languages);
    96         viewModel.setBrowserLanguage(req.getLocale());
    97         viewModel.setCurrentLanguage((Locale)req.getSession().getAttribute(Constants.SESSION_ATTR_LANGUAGE));
    99         setViewModel(req, viewModel);
   100         setStylesheet(req, "language");
   101         setContentPage(req, "language");
   103         renderSite(req, resp);
   104     }
   106     @RequestMapping(method = HttpMethod.POST)
   107     public void switchLanguage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   109         Optional<Locale> chosenLanguage = Optional.ofNullable(req.getParameter("language"))
   110                 .map(Locale::forLanguageTag)
   111                 .filter((l) -> !l.getLanguage().isEmpty());
   113         chosenLanguage.ifPresent((l) -> req.getSession().setAttribute(Constants.SESSION_ATTR_LANGUAGE, l));
   114         chosenLanguage.ifPresent(resp::setLocale);
   116         handle(req, resp);
   117     }
   118 }

mercurial