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

Wed, 06 Jan 2021 15:39:56 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Jan 2021 15:39:56 +0100
changeset 180
009700915269
parent 179
623c340058f3
permissions
-rw-r--r--

merge resource bundles

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 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     public void init() throws ServletException {
    58         super.init();
    60         Optional<String[]> langs = availableLanguages();
    61         if (langs.isPresent()) {
    62             for (String lang : langs.get()) {
    63                 try {
    64                     Locale locale = Locale.forLanguageTag(lang);
    65                     if (locale.getLanguage().isEmpty()) {
    66                         throw new IllformedLocaleException();
    67                     }
    68                     languages.add(locale);
    69                 } catch (IllformedLocaleException ex) {
    70                     LOG.warn("Specified language {} in context parameter cannot be mapped to an existing locale - skipping.", lang);
    71                 }
    72             }
    74         } else {
    75             languages.add(Locale.ENGLISH);
    76             LOG.warn("Context parameter 'available-languages' not found. Only english will be available.");
    77         }
    78     }
    80     @Override
    81     public void destroy() {
    82         super.destroy();
    83         languages.clear();
    84     }
    86     @RequestMapping(method = HttpMethod.GET)
    87     public void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    89         final var viewModel = new LanguageView();
    90         viewModel.setLanguages(languages);
    91         viewModel.setBrowserLanguage(req.getLocale());
    92         viewModel.setCurrentLanguage((Locale)req.getSession().getAttribute(Constants.SESSION_ATTR_LANGUAGE));
    94         setViewModel(req, viewModel);
    95         setStylesheet(req, "language");
    96         setContentPage(req, "language");
    98         renderSite(req, resp);
    99     }
   101     @RequestMapping(method = HttpMethod.POST)
   102     public void switchLanguage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   104         Optional<Locale> chosenLanguage = Optional.ofNullable(req.getParameter("language"))
   105                 .map(Locale::forLanguageTag)
   106                 .filter((l) -> !l.getLanguage().isEmpty());
   108         chosenLanguage.ifPresent((l) -> req.getSession().setAttribute(Constants.SESSION_ATTR_LANGUAGE, l));
   109         chosenLanguage.ifPresent(resp::setLocale);
   111         handle(req, resp);
   112     }
   113 }

mercurial