diff --git a/src/fr/devinsy/kiss4web/dispatcher/hooks/RewriteHook.java b/src/fr/devinsy/kiss4web/dispatcher/hooks/RewriteHook.java new file mode 100644 index 0000000..f8dbc96 --- /dev/null +++ b/src/fr/devinsy/kiss4web/dispatcher/hooks/RewriteHook.java @@ -0,0 +1,88 @@ +/** + * Copyright (C) 2024 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.dispatcher.hooks; + +import java.io.IOException; +import java.util.regex.Pattern; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.kiss4web.dispatcher.KissDispatcherFactory; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +/** + * The Class RewriteHook. + */ +public class RewriteHook extends HookCore +{ + private static Logger logger = LoggerFactory.getLogger(RewriteHook.class); + + private Pattern pattern; + private String targetClassPath; + + /** + * Instantiates a new XHTML hook. + */ + public RewriteHook(final String regex, final String targetClassPath) + { + super(); + this.pattern = Pattern.compile(regex); + this.targetClassPath = targetClassPath; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean matches(final ServletContext servletContext, final HttpServletRequest request) + { + boolean result; + + String urlPath = request.getPathInfo(); + + if ((this.pattern.matcher(urlPath).matches()) && (!KissDispatcherFactory.instance().isAvailablePath(urlPath))) + { + result = true; + } + else + { + result = false; + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public void process(final ServletConfig servletConfig, final ServletContext servletContext, final HttpServletRequest request, + final HttpServletResponse response) throws IOException, ServletException + { + logger.debug("Doing catch."); + + KissDispatcherFactory.instance().dispatchPathToServlet(servletConfig, request, response, this.targetClassPath); + } +}