universe@7: /*
universe@7: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@7: *
universe@7: * Copyright 2017 Mike Becker. All rights reserved.
universe@7: *
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@7: *
universe@7: */
universe@7: package de.uapcore.lightpit;
universe@7:
universe@11: import java.util.Optional;
universe@11: import javax.servlet.http.HttpServletRequest;
universe@10: import org.slf4j.Logger;
universe@10: import org.slf4j.LoggerFactory;
universe@10:
universe@7: /**
universe@7: * Contains common static functions.
universe@7: */
universe@10: public final class Functions {
universe@10:
universe@10: private static final Logger LOG = LoggerFactory.getLogger(Functions.class);
universe@7:
universe@7: public static String jspPath(String filename) {
universe@7: return Constants.JSP_PATH_PREFIX + filename;
universe@7: }
universe@10:
universe@11: public static String fqn(String base, String name) {
universe@10: return base+"."+name;
universe@10: }
universe@10:
universe@11: public static String fqn(Class clazz, String name) {
universe@11: return fqn(clazz.getName(), name);
universe@11: }
universe@11:
universe@11: public static String fullPath(LightPITModule module, RequestMapping mapping) {
universe@11: StringBuilder sb = new StringBuilder();
universe@11: sb.append(module.modulePath());
universe@11: sb.append('/');
universe@11: if (!mapping.requestPath().isEmpty()) {
universe@11: sb.append(mapping.requestPath().isEmpty());
universe@11: sb.append('/');
universe@11: }
universe@11: return sb.toString();
universe@11: }
universe@11:
universe@11: public static String fullPath(HttpServletRequest req) {
universe@11: return req.getServletPath() + Optional.ofNullable(req.getPathInfo()).orElse("");
universe@10: }
universe@10:
universe@10: /**
universe@10: * Returns the module path of the given LightPIT Servlet module.
universe@10: *
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@10: *
universe@10: * @param clazz the servlet class
universe@10: * @return the module path
universe@10: */
universe@10: public static String modulePathOf(Class extends AbstractLightPITServlet> 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@10:
universe@10: /**
universe@10: * This class is not instantiatable.
universe@10: */
universe@10: private Functions() {}
universe@7: }