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; universe@7: universe@33: import org.slf4j.Logger; universe@33: import org.slf4j.LoggerFactory; universe@33: universe@13: import javax.servlet.ServletContext; universe@11: import javax.servlet.http.HttpServletRequest; universe@33: import java.util.Optional; universe@10: universe@7: /** universe@7: * Contains common static functions. universe@7: */ universe@10: public final class Functions { universe@34: universe@10: private static final Logger LOG = LoggerFactory.getLogger(Functions.class); universe@7: universe@13: public static Optional availableLanguages(ServletContext ctx) { universe@13: return Optional.ofNullable(ctx.getInitParameter(Constants.CTX_ATTR_LANGUAGES)).map((x) -> x.split("\\s*,\\s*")); universe@13: } universe@34: universe@13: public static String enforceExt(String filename, String ext) { universe@13: return filename.endsWith(ext) ? filename : filename + ext; universe@13: } universe@13: universe@7: public static String jspPath(String filename) { universe@13: return enforceExt(Constants.JSP_PATH_PREFIX + filename, ".jsp"); universe@13: } universe@34: universe@13: public static String dynFragmentPath(String filename) { universe@13: return enforceExt(Constants.DYN_FRAGMENT_PATH_PREFIX + filename, ".jsp"); universe@7: } universe@34: universe@11: public static String fqn(String base, String name) { universe@34: return base + "." + name; universe@10: } universe@33: universe@33: public static String fqn(Class clazz, String name) { universe@11: return fqn(clazz.getName(), name); universe@11: } universe@34: universe@47: public static String baseHref(HttpServletRequest req) { universe@47: return String.format("%s://%s:%d%s/", universe@47: req.getScheme(), universe@47: req.getServerName(), universe@47: req.getServerPort(), universe@47: req.getContextPath()); universe@11: } universe@34: universe@11: public static String fullPath(HttpServletRequest req) { universe@11: return req.getServletPath() + Optional.ofNullable(req.getPathInfo()).orElse(""); universe@10: } universe@34: universe@10: /** universe@10: * Returns the module path of the given LightPIT Servlet module. universe@34: *

universe@10: * If you specify a malformed LightPIT servlet, which does not have a module universe@10: * annotation, the path to the /error/404.html page is returned. universe@34: * universe@10: * @param clazz the servlet class universe@10: * @return the module path universe@10: */ universe@10: public static String modulePathOf(Class clazz) { universe@11: Optional moduleInfo = Optional.ofNullable(clazz.getAnnotation(LightPITModule.class)); universe@11: if (moduleInfo.isPresent()) { universe@11: return moduleInfo.get().modulePath(); universe@11: } else { universe@10: LOG.warn( universe@10: "{} is a LightPIT Servlet but is missing the module annotation.", universe@10: clazz.getName() universe@10: ); universe@10: return "/error/404.html"; universe@10: } universe@10: } universe@34: universe@10: /** universe@10: * This class is not instantiatable. universe@10: */ universe@34: private Functions() { universe@34: } universe@7: }