diff --git a/src/fr/devinsy/kiss4web/BuildInformation.java b/src/fr/devinsy/kiss4web/BuildInformation.java new file mode 100644 index 0000000..db1e19f --- /dev/null +++ b/src/fr/devinsy/kiss4web/BuildInformation.java @@ -0,0 +1,146 @@ +/** + * Copyright (C) 2013-2016 Christian Pierre MOMON + * + * This file is part of Kiss4web. + * + * Kiss4web is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Kiss4web is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Kiss4web. If not, see + */ +package fr.devinsy.kiss4web; + +import java.io.IOException; +import java.util.Properties; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * + * @author Christian P. Momon + */ +public class BuildInformation +{ + private static final Logger logger = LoggerFactory.getLogger(BuildInformation.class); + + private String productName; + private String majorRevision; + private String minorRevision; + private String buildNumber; + private String buildDate; + private String generator; + private String author; + + /** + * + */ + public BuildInformation() + { + Properties build = new Properties(); + + try + { + // + build.load(BuildInformation.class.getResource("/build_information.properties").openStream()); + + // + this.productName = build.getProperty("product.name", "n/a"); + this.majorRevision = build.getProperty("product.revision.major", "n/a"); + this.minorRevision = build.getProperty("product.revision.minor", "n/a"); + this.buildNumber = build.getProperty("product.revision.build", "n/a"); + this.buildDate = build.getProperty("product.revision.date", "n/a"); + this.generator = build.getProperty("product.revision.generator", "n/a"); + this.author = build.getProperty("product.revision.author", "n/a"); + + } + catch (IOException exception) + { + // + logger.error("Error loading the build.properties file: " + exception.getMessage()); + logger.error(ExceptionUtils.getStackTrace(exception)); + + // + this.productName = "n/a"; + this.majorRevision = "n/a"; + this.minorRevision = "n/a"; + this.buildNumber = "n/a"; + this.buildDate = "n/a"; + this.generator = "n/a"; + this.author = "n/a"; + } + } + + public String author() + { + return this.author; + } + + public String buildDate() + { + return this.buildDate; + } + + public String buildNumber() + { + return this.buildNumber; + } + + public String generator() + { + return this.generator; + } + + public String majorRevision() + { + return this.majorRevision; + } + + public String minorRevision() + { + return this.minorRevision; + } + + public String productName() + { + return this.productName; + } + + /** + * + */ + @Override + public String toString() + { + String result; + + result = String.format("%s %s.%s.%s built on %s by %s", this.productName, this.majorRevision, this.minorRevision, this.buildNumber, this.buildDate, this.author); + + // + return result; + } + + /** + * + * @return + */ + public String version() + { + String result; + + result = String.format("%s.%s.%s", this.majorRevision, this.minorRevision, this.buildNumber); + + // + return result; + } +} diff --git a/src/fr/devinsy/kiss4web/EnvironmentInformation.java b/src/fr/devinsy/kiss4web/EnvironmentInformation.java new file mode 100644 index 0000000..e5eb0fc --- /dev/null +++ b/src/fr/devinsy/kiss4web/EnvironmentInformation.java @@ -0,0 +1,231 @@ +/** + * Copyright 2013-2015 Christian Pierre MOMON, DEVINSY, UMR 7186 LESC. + * + * christian.momon@devinsy.fr + * + * This file is part of kiss4web. This software (Kiwa) is a computer program whose + * purpose is to be the Kinsources Web Application, an open interactive platform + * for archiving, sharing, analyzing and comparing kinship data used in + * scientific inquiry. + * + * This software is governed by the CeCILL license under French law and abiding + * by the rules of distribution of free software. You can use, modify and/ or + * redistribute the software under the terms of the CeCILL license as circulated + * by CEA, CNRS and INRIA at the following URL "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, modify + * and redistribute granted by the license, users are provided only with a + * limited warranty and the software's author, the holder of the economic + * rights, and the successive licensors have only limited liability. + * + * In this respect, the user's attention is drawn to the risks associated with + * loading, using, modifying and/or developing or reproducing the software by + * the user in light of its specific status of free software, that may mean that + * it is complicated to manipulate, and that also therefore means that it is + * reserved for developers and experienced professionals having in-depth + * computer knowledge. Users are therefore encouraged to load and test the + * software's suitability as regards their requirements in conditions enabling + * the security of their systems and/or data to be ensured and, more generally, + * to use and operate it in the same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +package fr.devinsy.kiss4web; + +import javax.mail.Session; +import javax.naming.ConfigurationException; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.util.strings.StringList; +import fr.devinsy.util.strings.StringListUtils; + +/** + * + * + * @author Christian P. Momon + */ +public class EnvironmentInformation +{ + private static final Logger logger = LoggerFactory.getLogger(EnvironmentInformation.class);; + + private String name; + private String websiteName; + private String websiteUrl; + private String log4jFilePathname; + + /** + * @throws ConfigurationException + * + */ + public EnvironmentInformation() throws ConfigurationException + { + try + { + // + Context initialContext = new InitialContext(); + Context environment = (Context) initialContext.lookup("java:comp/env"); + + // + this.name = (String) environment.lookup("kiss4web.environment.name"); + + // + this.websiteName = (String) environment.lookup("kiss4web.website.name"); + + // + this.websiteUrl = (String) environment.lookup("kiss4web.website.url"); + if ((this.websiteUrl != null) && (!this.websiteUrl.endsWith("/"))) + { + this.websiteUrl = this.websiteUrl + "/"; + } + + // + this.log4jFilePathname = (String) environment.lookup("kiss4web.log4j.path"); + } + catch (NamingException exception) + { + logger.error("Error setting the java:comp/env context: ", exception.getMessage()); + logger.error(ExceptionUtils.getStackTrace(exception)); + logger.error("Environment information:"); + logger.error("- name=[" + this.name + "]"); + logger.error("- websiteUrl=[" + this.websiteUrl + "]"); + logger.error("- log4jPath=[" + this.log4jFilePathname + "]"); + + throw new ConfigurationException("Error setting environment information: " + exception.getMessage()); + } + + // Check availability of all fields. + if (isNotAvailable()) + { + logger.error("Environment information:"); + logger.error("- name=[" + this.name + "]"); + logger.error("- websiteUrl=[" + this.websiteUrl + "]"); + logger.error("- log4jPath=[" + this.log4jFilePathname + "]"); + + throw new ConfigurationException("Environment information is not fully available."); + } + } + + /** + * + * @param name + * @return + */ + public String getSessionToString(final Context environment, final String name) + { + String result; + + try + { + if (environment == null) + { + result = null; + } + else + { + Session session = (Session) environment.lookup(name); + + StringList buffer = new StringList(); + for (Object key : session.getProperties().keySet().toArray()) + { + buffer.append((String) key).append("=").append(session.getProperties().getProperty((String) key)).append(","); + } + buffer.removeLast(); + result = buffer.toString(); + } + } + catch (NamingException exception) + { + logger.info("Error: {}", exception.getMessage()); + result = null; + } + + // + return result; + } + + /** + * + * @param name + * @return + */ + public String getSessionToString(final String name) + { + String result; + + try + { + Context initialContext = new InitialContext(); + + Context environment = (Context) initialContext.lookup("java:comp/env"); + result = getSessionToString(environment, name); + } + catch (NamingException exception) + { + logger.info("Error: {}", exception.getMessage()); + result = null; + } + + return result; + } + + /** + * + * @return + */ + public boolean isAvailable() + { + boolean result; + + if (StringListUtils.containsBlank(this.name, this.websiteName, this.websiteUrl, this.log4jFilePathname)) + { + result = false; + } + else + { + result = true; + } + + // + return result; + } + + /** + * + * @return + */ + public boolean isNotAvailable() + { + boolean result; + + result = !isAvailable(); + + return result; + } + + public String log4jFilePathname() + { + return this.log4jFilePathname; + } + + public String name() + { + return this.name; + } + + public String websiteName() + { + return this.websiteName; + } + + public String websiteUrl() + { + return this.websiteUrl; + } +} diff --git a/src/fr/devinsy/kiss4web/Kiss4web.java b/src/fr/devinsy/kiss4web/Kiss4web.java new file mode 100644 index 0000000..1b259c1 --- /dev/null +++ b/src/fr/devinsy/kiss4web/Kiss4web.java @@ -0,0 +1,138 @@ +/** + * Copyright (C) 2013-2016 Christian Pierre MOMON + * + * This file is part of Kiss4web. + * + * Kiss4web is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Kiss4web is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Kiss4web. If not, see + */ +package fr.devinsy.kiss4web; + +import java.util.Date; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; + +/** + * + * + * @author Christian P. Momon + */ +public class Kiss4web +{ + private static class SingletonHolder + { + private static final Kiss4web instance = new Kiss4web(); + } + + public enum Status + { + INIT_FAILED, + OPENED, + MAINTENANCE + } + + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Kiss4web.class); + + private BuildInformation buildInformation; + private Status status; + + /** + * @throws Exception + * + */ + private Kiss4web() + { + // + String currentLog = "Kiwa"; + + // + try + { + // + currentLog = "Kiwa"; + logger.info("Kiwa initializing..."); + long startTime = new Date().getTime(); + logInit(currentLog, "STARTING"); + + // + currentLog = "BuildInformation"; + this.buildInformation = new BuildInformation(); + logger.info(" build information=[" + this.buildInformation.toString() + "]"); + logInit(currentLog, "PASSED"); + + // + currentLog = "Kiwa"; + logInit(currentLog, "STARTED"); + long endTime = new Date().getTime(); + logger.info("Kiss4web initialized in {}ms.", endTime - startTime); + + } + catch (Exception exception) + { + // + logInit(currentLog, "FAILED"); + + // + logger.warn("KISS4WEV INIT FAILED: " + exception.getMessage()); + + // + logger.warn(ExceptionUtils.getStackTrace(exception)); + + // + this.status = Kiss4web.Status.INIT_FAILED; + } + } + + public BuildInformation buildInformation() + { + return this.buildInformation; + } + + /** + * + * @return + */ + public Kiss4web.Status getStatus() + { + return this.status; + } + + /** + * + * @param status + */ + public void setStatus(final Status status) + { + this.status = status; + } + + /** + * + * @return + */ + public static Kiss4web instance() + { + return SingletonHolder.instance; + } + + /** + * + * @param currentLogItem + * @param status + */ + private static void logInit(final String currentLogItem, final String status) + { + logger.info(String.format("%s%s%s", currentLogItem, StringUtils.repeat('.', 40 - StringUtils.length(currentLogItem)), status)); + } +} diff --git a/src/fr/devinsy/kiss4web/KissDispatcher.java b/src/fr/devinsy/kiss4web/KissDispatcher.java index 87d0792..7cf0f38 100755 --- a/src/fr/devinsy/kiss4web/KissDispatcher.java +++ b/src/fr/devinsy/kiss4web/KissDispatcher.java @@ -21,11 +21,13 @@ package fr.devinsy.kiss4web; import java.io.IOException; import java.util.Date; +import javax.naming.ConfigurationException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,6 +79,8 @@ public class KissDispatcher extends HttpServlet { long startTime = new Date().getTime(); + request.setCharacterEncoding("UTF-8"); + logger.info("=================================================="); logger.info("getContextPath=[" + request.getContextPath() + "]"); logger.info("getPathInfo=[" + request.getPathInfo() + "]"); @@ -135,12 +139,11 @@ public class KissDispatcher extends HttpServlet * If not specified, ISO-8859-1 will be used. * */ - // String path = request.getRequestURI(); - String urlPath = request.getPathInfo(); + // String urlPath = request.getPathInfo(); if (BlankCatcher.matches(request)) { - BlankCatcher.doCatch(response, urlPath); + BlankCatcher.doCatch(response); } else if (RootCatcher.matches(request)) { @@ -164,7 +167,7 @@ public class KissDispatcher extends HttpServlet } else if (WebContentCatcher.matches(this.getServletContext(), request)) { - XHTMLCatcher.doCatch(this.getServletConfig(), request, response, webClassesRootPath); + WebContentCatcher.doCatch(this.getServletContext(), request, response); } else if (WebInfCatcher.matches(this.getServletContext(), request, webClassesRootPath)) { @@ -172,7 +175,7 @@ public class KissDispatcher extends HttpServlet } else { - logger.info("Request not satisfied [" + urlPath + "]"); + logger.info("Request not satisfied [" + request.getPathInfo() + "]"); response.sendError(HttpServletResponse.SC_NOT_FOUND); } @@ -224,29 +227,46 @@ public class KissDispatcher extends HttpServlet public void init() throws ServletException { super.init(); - logger.info("KissDispatcher init called."); - - // - this.webclassesRootPackage = getInitParameter("webClassesRootPackage"); - logger.info("webClassesRootPackage loaded=[{}]", this.webclassesRootPackage); // Set logger. - String logFilepathname = getInitParameter("log4j-init-file"); - if (logFilepathname != null) + String logFilePathname; + try + { + new EnvironmentInformation().toString(); + + logFilePathname = new EnvironmentInformation().log4jFilePathname(); + } + catch (ConfigurationException exception) + { + System.err.println("Error reading the environment information."); + exception.printStackTrace(); + logFilePathname = null; + } + + if (StringUtils.isBlank(logFilePathname)) + { + org.apache.log4j.BasicConfigurator.configure(); + logger.warn("Log configuration undefined, use of the basic configurator."); + } + else { try { - System.out.println("Log configuration found (" + logFilepathname + "), will use it."); - org.apache.log4j.PropertyConfigurator.configure(getServletContext().getRealPath("/") + logFilepathname); + System.out.println("Log configuration defined (" + logFilePathname + "), will use it."); + org.apache.log4j.PropertyConfigurator.configure(getServletContext().getRealPath("/") + logFilePathname); } catch (Exception exception) { - System.out.println("Log configuration FILE NOT FOUND (" + logFilepathname + "), use of the basic configurator."); + System.out.println("Log configuration FILE NOT FOUND (" + logFilePathname + "), use of the basic configurator."); org.apache.log4j.BasicConfigurator.configure(); } logger = LoggerFactory.getLogger(this.getClass()); logger.info("Log initialization done."); } + + // + this.webclassesRootPackage = getInitParameter("webClassesRootPackage"); + logger.info("webClassesRootPackage loaded=[{}]", this.webclassesRootPackage); } } diff --git a/src/fr/devinsy/kiss4web/catchers/BlankCatcher.java b/src/fr/devinsy/kiss4web/catchers/BlankCatcher.java index 8d68b12..d88d628 100644 --- a/src/fr/devinsy/kiss4web/catchers/BlankCatcher.java +++ b/src/fr/devinsy/kiss4web/catchers/BlankCatcher.java @@ -38,7 +38,7 @@ public class BlankCatcher * @throws IOException * */ - public static void doCatch(final HttpServletResponse response, final String urlPath) throws IOException + public static void doCatch(final HttpServletResponse response) throws IOException { logger.debug("Doing catch."); diff --git a/src/fr/devinsy/kiss4web/catchers/FolderCatcher.java b/src/fr/devinsy/kiss4web/catchers/FolderCatcher.java index 69c536c..3705889 100644 --- a/src/fr/devinsy/kiss4web/catchers/FolderCatcher.java +++ b/src/fr/devinsy/kiss4web/catchers/FolderCatcher.java @@ -25,7 +25,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,14 +62,15 @@ public class FolderCatcher // Note: as pathInfo starts always with a '/', the first // good token index is 1. - for (int tokenCounter = 1; tokenCounter < tokens.length - 1; tokenCounter++) + for (int tokenCounter = 1; tokenCounter <= tokens.length - 1; tokenCounter++) { buffer.append(tokens[tokenCounter]); buffer.append('.'); } - String lastToken = tokens[tokens.length - 1]; - buffer.append(StringUtils.capitalize(lastToken)).append("_xhtml"); + // String lastToken = tokens[tokens.length - 1]; + // buffer.append(StringUtils.capitalize(lastToken)).append("_xhtml"); + buffer.append("Index_xhtml"); className = buffer.toString(); } diff --git a/src/fr/devinsy/kiss4web/catchers/LongURLCatcher.java b/src/fr/devinsy/kiss4web/catchers/LongURLCatcher.java index 417c766..012700b 100644 --- a/src/fr/devinsy/kiss4web/catchers/LongURLCatcher.java +++ b/src/fr/devinsy/kiss4web/catchers/LongURLCatcher.java @@ -50,8 +50,8 @@ public class LongURLCatcher String urlPath = request.getPathInfo(); String rewritedURLPath = LongURLRewriter.unrewrite(urlPath); - - String className = KissDispatcherUtils.concatenatePackage(webClassesRootPackage, rewritedURLPath); + String className = XHTMLCatcher.translateToClassName(rewritedURLPath); + className = KissDispatcherUtils.concatenatePackage(webClassesRootPackage, className); logger.info("className=[" + className + "]"); KissDispatcherUtils.dispatchToServlet(servletConfig, request, response, className); diff --git a/src/fr/devinsy/kiss4web/catchers/LongURLRewriter.java b/src/fr/devinsy/kiss4web/catchers/LongURLRewriter.java index 3af80f5..c0c0c23 100644 --- a/src/fr/devinsy/kiss4web/catchers/LongURLRewriter.java +++ b/src/fr/devinsy/kiss4web/catchers/LongURLRewriter.java @@ -87,7 +87,7 @@ public class LongURLRewriter * Note: "-/" is used to indicate the beginning of parameters. * */ - public static String rewriteLongUrl(final String path, final String... parameters) + public static String rewrite(final String path, final String... parameters) { String result; diff --git a/src/fr/devinsy/kiss4web/catchers/ShortURLCatcher.java b/src/fr/devinsy/kiss4web/catchers/ShortURLCatcher.java index a0f55c9..4f7c3f3 100644 --- a/src/fr/devinsy/kiss4web/catchers/ShortURLCatcher.java +++ b/src/fr/devinsy/kiss4web/catchers/ShortURLCatcher.java @@ -51,7 +51,9 @@ public class ShortURLCatcher String rewritedURLPath = ShortURLRewriter.unrewrite(urlPath); - String className = KissDispatcherUtils.concatenatePackage(webClassesRootPackage, rewritedURLPath); + String className = XHTMLCatcher.translateToClassName(rewritedURLPath); + className = KissDispatcherUtils.concatenatePackage(webClassesRootPackage, className); + logger.info("className=[" + className + "]"); KissDispatcherUtils.dispatchToServlet(servletConfig, request, response, className); diff --git a/src/fr/devinsy/kiss4web/catchers/WebContentCatcher.java b/src/fr/devinsy/kiss4web/catchers/WebContentCatcher.java index 9da2a43..3d63230 100644 --- a/src/fr/devinsy/kiss4web/catchers/WebContentCatcher.java +++ b/src/fr/devinsy/kiss4web/catchers/WebContentCatcher.java @@ -43,8 +43,7 @@ public class WebContentCatcher * @throws ServletException * */ - public static void doCatch(final ServletContext servletContext, final HttpServletRequest request, final HttpServletResponse response, final String webClassesRootPackage) throws IOException, - ServletException + public static void doCatch(final ServletContext servletContext, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { logger.debug("Doing catch."); diff --git a/src/fr/devinsy/kiss4web/catchers/XHTMLCatcher.java b/src/fr/devinsy/kiss4web/catchers/XHTMLCatcher.java index 53c3987..4222a40 100644 --- a/src/fr/devinsy/kiss4web/catchers/XHTMLCatcher.java +++ b/src/fr/devinsy/kiss4web/catchers/XHTMLCatcher.java @@ -50,30 +50,7 @@ public class XHTMLCatcher String urlPath = request.getPathInfo(); - String className; - String[] tokens = urlPath.split("/"); - if (tokens.length == 1) - { - className = "Index_xhtml"; - } - else - { - StringList buffer = new StringList(); - - // Note: as pathInfo starts always with a '/', the first - // good token index is 1. - for (int tokenCounter = 1; tokenCounter < tokens.length - 1; tokenCounter++) - { - buffer.append(tokens[tokenCounter]); - buffer.append('.'); - } - - String lastToken = tokens[tokens.length - 1]; - buffer.append(StringUtils.capitalize(lastToken).replace('.', '_')).append("_xhtml"); - - className = buffer.toString(); - } - + String className = translateToClassName(urlPath); className = KissDispatcherUtils.concatenatePackage(webClassesRootPackage, className); logger.info("className=[" + className + "]"); @@ -103,4 +80,40 @@ public class XHTMLCatcher // return result; } + + /** + * + * @param urlPath + * @return + */ + public static String translateToClassName(final String urlPath) + { + String result; + + String[] tokens = urlPath.split("/"); + if (tokens.length == 1) + { + result = "Index_xhtml"; + } + else + { + StringList buffer = new StringList(); + + // Note: as pathInfo starts always with a '/', the first + // good token index is 1. + for (int tokenCounter = 1; tokenCounter < tokens.length - 1; tokenCounter++) + { + buffer.append(tokens[tokenCounter]); + buffer.append('.'); + } + + String lastToken = tokens[tokens.length - 1]; + buffer.append(StringUtils.capitalize(lastToken).replace('.', '_')); + + result = buffer.toString(); + } + + // + return result; + } }