universe@7: /* universe@7: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@34: * universe@24: * Copyright 2018 Mike Becker. All rights reserved. universe@34: * universe@7: * Redistribution and use in source and binary forms, with or without universe@7: * modification, are permitted provided that the following conditions are met: universe@7: * universe@7: * 1. Redistributions of source code must retain the above copyright universe@7: * notice, this list of conditions and the following disclaimer. universe@7: * universe@7: * 2. Redistributions in binary form must reproduce the above copyright universe@7: * notice, this list of conditions and the following disclaimer in the universe@7: * documentation and/or other materials provided with the distribution. universe@7: * universe@7: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@7: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@7: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@7: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@7: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@7: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@7: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@7: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@7: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@7: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@7: * POSSIBILITY OF SUCH DAMAGE. universe@34: * universe@7: */ universe@7: package de.uapcore.lightpit.modules; universe@7: universe@31: import de.uapcore.lightpit.*; universe@86: import de.uapcore.lightpit.viewmodel.LanguageView; universe@31: import org.slf4j.Logger; universe@31: import org.slf4j.LoggerFactory; universe@31: universe@31: import javax.servlet.ServletException; universe@7: import javax.servlet.annotation.WebServlet; universe@11: import javax.servlet.http.HttpServletRequest; universe@11: import javax.servlet.http.HttpServletResponse; universe@31: import java.util.*; universe@7: universe@7: @WebServlet( universe@11: name = "LanguageModule", universe@11: urlPatterns = "/language/*" universe@7: ) universe@11: public final class LanguageModule extends AbstractLightPITServlet { universe@34: universe@13: private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class); universe@34: universe@15: private final List languages = new ArrayList<>(); universe@13: universe@13: @Override universe@78: protected String getResourceBundleName() { universe@78: return "localization.language"; universe@78: } universe@78: universe@78: @Override universe@13: public void init() throws ServletException { universe@13: super.init(); universe@34: universe@13: Optional langs = Functions.availableLanguages(getServletContext()); universe@13: if (langs.isPresent()) { universe@13: for (String lang : langs.get()) { universe@13: try { universe@13: Locale locale = Locale.forLanguageTag(lang); universe@13: if (locale.getLanguage().isEmpty()) { universe@13: throw new IllformedLocaleException(); universe@13: } universe@13: languages.add(locale); universe@13: } catch (IllformedLocaleException ex) { universe@39: LOG.warn("Specified language {} in context parameter cannot be mapped to an existing locale - skipping.", lang); universe@13: } universe@13: } universe@34: universe@13: } else { universe@13: languages.add(Locale.ENGLISH); universe@39: LOG.warn("Context parameter 'available-languages' not found. Only english will be available."); universe@13: } universe@13: } universe@15: universe@15: @Override universe@15: public void destroy() { universe@15: super.destroy(); universe@15: languages.clear(); universe@15: } universe@34: universe@11: @RequestMapping(method = HttpMethod.GET) universe@42: public ResponseType handle(HttpServletRequest req) { universe@13: universe@86: final var viewModel = new LanguageView(); universe@86: viewModel.setLanguages(languages); universe@86: viewModel.setBrowserLanguage(req.getLocale()); universe@86: viewModel.setCurrentLanguage((Locale)req.getSession().getAttribute(Constants.SESSION_ATTR_LANGUAGE)); universe@34: universe@86: setViewModel(req, viewModel); universe@13: setStylesheet(req, "language"); universe@74: setContentPage(req, "language"); universe@43: return ResponseType.HTML; universe@11: } universe@34: universe@15: @RequestMapping(method = HttpMethod.POST) universe@42: public ResponseType switchLanguage(HttpServletRequest req, HttpServletResponse resp) { universe@34: universe@15: Optional chosenLanguage = Optional.ofNullable(req.getParameter("language")) universe@15: .map(Locale::forLanguageTag) universe@15: .filter((l) -> !l.getLanguage().isEmpty()); universe@34: universe@15: chosenLanguage.ifPresent((l) -> req.getSession().setAttribute(Constants.SESSION_ATTR_LANGUAGE, l)); universe@15: chosenLanguage.ifPresent(resp::setLocale); universe@34: universe@42: return handle(req); universe@15: } universe@7: }