diff --git a/cms-server/src/main/java/com/condation/cms/server/VHost.java b/cms-server/src/main/java/com/condation/cms/server/VHost.java index 97378549..ac220d3a 100644 --- a/cms-server/src/main/java/com/condation/cms/server/VHost.java +++ b/cms-server/src/main/java/com/condation/cms/server/VHost.java @@ -49,13 +49,11 @@ import com.condation.cms.api.eventbus.events.InvalidateTemplateCacheEvent; import com.condation.cms.api.eventbus.events.lifecycle.HostReloadedEvent; import com.condation.cms.api.eventbus.events.lifecycle.HostStoppedEvent; -import com.condation.cms.api.feature.features.ContentRenderFeature; import com.condation.cms.api.feature.features.ThemeFeature; import com.condation.cms.api.module.CMSModuleContext; import com.condation.cms.api.template.TemplateEngine; import com.condation.cms.api.theme.Theme; import com.condation.cms.api.utils.SiteUtil; -import com.condation.cms.content.ContentResolver; import com.condation.cms.core.configuration.ConfigManagement; import com.condation.cms.extensions.GlobalExtensions; import com.condation.cms.extensions.hooks.GlobalHooks; @@ -63,7 +61,6 @@ import com.condation.cms.media.MediaManager; import com.condation.cms.media.SiteMediaManager; import com.condation.cms.media.ThemeMediaManager; -import com.condation.cms.module.DefaultRenderContentFunction; import com.condation.cms.request.RequestContextFactory; import com.condation.cms.server.configs.ModulesModule; import com.condation.cms.server.configs.SiteConfigInitializer; @@ -75,18 +72,17 @@ import com.condation.cms.server.filter.InitRequestContextFilter; import com.condation.cms.server.filter.PooledRequestContextFilter; import com.condation.cms.server.filter.RequestLoggingFilter; -import com.condation.cms.server.handler.api.APIHandler; import com.condation.cms.server.handler.auth.JettyAuthenticationHandler; import com.condation.cms.server.handler.cache.CacheHandler; import com.condation.cms.server.handler.content.JettyContentHandler; import com.condation.cms.server.handler.content.JettyTaxonomyHandler; import com.condation.cms.server.handler.content.JettyViewHandler; -import com.condation.cms.server.handler.extensions.JettyExtensionRouteHandler; import com.condation.cms.server.handler.extensions.JettyHttpHandlerExtensionHandler; +import com.condation.cms.server.handler.http.APIHandler; +import com.condation.cms.server.handler.http.RoutesHandler; import com.condation.cms.server.handler.media.JettyMediaHandler; import com.condation.cms.server.handler.module.JettyModuleHandler; import com.condation.cms.server.handler.module.JettyRouteHandler; -import com.condation.cms.server.handler.module.JettyRoutesHandler; import com.condation.modules.api.ModuleManager; import com.google.inject.Injector; import com.google.inject.Key; @@ -227,8 +223,7 @@ public Handler buildHttpHandler() { var taxonomyHandler = injector.getInstance(JettyTaxonomyHandler.class); var viewHandler = injector.getInstance(JettyViewHandler.class); var routeHandler = injector.getInstance(JettyRouteHandler.class); - var routesHandler = injector.getInstance(JettyRoutesHandler.class); - var extensionRouteHandler = injector.getInstance(JettyExtensionRouteHandler.class); + var routesHandler = injector.getInstance(RoutesHandler.class); var authHandler = injector.getInstance(JettyAuthenticationHandler.class); var initContextHandler = injector.getInstance(InitRequestContextFilter.class); @@ -237,7 +232,6 @@ public Handler buildHttpHandler() { initContextHandler, routeHandler, routesHandler, - extensionRouteHandler, viewHandler, taxonomyHandler, contentHandler diff --git a/cms-server/src/main/java/com/condation/cms/server/configs/SiteHandlerModule.java b/cms-server/src/main/java/com/condation/cms/server/configs/SiteHandlerModule.java index 9b970c23..e89a9900 100644 --- a/cms-server/src/main/java/com/condation/cms/server/configs/SiteHandlerModule.java +++ b/cms-server/src/main/java/com/condation/cms/server/configs/SiteHandlerModule.java @@ -1,5 +1,13 @@ package com.condation.cms.server.configs; +import java.io.IOException; +import java.nio.file.Path; +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.eclipse.jetty.server.handler.ResourceHandler; + /*- * #%L * cms-server @@ -32,31 +40,25 @@ import com.condation.cms.auth.services.AuthService; import com.condation.cms.auth.services.UserService; import com.condation.cms.media.SiteMediaManager; -import com.condation.cms.server.handler.api.APIHandler; -import com.condation.cms.server.handler.auth.JettyAuthenticationHandler; +import com.condation.cms.server.FileFolderPathResource; import com.condation.cms.server.filter.InitRequestContextFilter; +import com.condation.cms.server.handler.auth.JettyAuthenticationHandler; import com.condation.cms.server.handler.content.JettyContentHandler; import com.condation.cms.server.handler.content.JettyTaxonomyHandler; import com.condation.cms.server.handler.content.JettyViewHandler; import com.condation.cms.server.handler.extensions.JettyHttpHandlerExtensionHandler; -import com.condation.cms.server.handler.extensions.JettyExtensionRouteHandler; +import com.condation.cms.server.handler.http.APIHandler; +import com.condation.cms.server.handler.http.RoutesHandler; import com.condation.cms.server.handler.media.JettyMediaHandler; import com.condation.cms.server.handler.module.JettyModuleHandler; import com.condation.cms.server.handler.module.JettyRouteHandler; -import com.condation.cms.server.handler.module.JettyRoutesHandler; -import com.condation.cms.server.FileFolderPathResource; import com.condation.modules.api.ModuleManager; import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.Singleton; import com.google.inject.name.Named; -import java.io.IOException; -import java.nio.file.Path; -import java.time.Duration; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; + import lombok.RequiredArgsConstructor; -import org.eclipse.jetty.server.handler.ResourceHandler; /** * @@ -72,9 +74,8 @@ protected void configure() { bind(JettyContentHandler.class).in(Singleton.class); bind(JettyTaxonomyHandler.class).in(Singleton.class); bind(JettyRouteHandler.class).in(Singleton.class); - bind(JettyRoutesHandler.class).in(Singleton.class); + bind(RoutesHandler.class).in(Singleton.class); bind(JettyHttpHandlerExtensionHandler.class).in(Singleton.class); - bind(JettyExtensionRouteHandler.class).in(Singleton.class); bind(InitRequestContextFilter.class).in(Singleton.class); bind(APIHandler.class).in(Singleton.class); diff --git a/cms-server/src/main/java/com/condation/cms/server/handler/api/APIRegistry.java b/cms-server/src/main/java/com/condation/cms/server/handler/api/APIRegistry.java deleted file mode 100644 index 9d3f8743..00000000 --- a/cms-server/src/main/java/com/condation/cms/server/handler/api/APIRegistry.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.condation.cms.server.handler.api; - -/*- - * #%L - * cms-server - * %% - * Copyright (C) 2023 - 2024 CondationCMS - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * . - * #L% - */ - -import java.util.HashMap; -import java.util.Map; -import java.util.function.BiFunction; - -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.server.Response; - -public class APIRegistry { - - private Map routes = new HashMap<>(); - - public void register (String path, String method, BiFunction handler) { - routes.put(path, new Route(path, method, handler)); - } - - public static record Route (String path, String method, BiFunction handler) {}; -} diff --git a/cms-server/src/main/java/com/condation/cms/server/handler/extensions/JettyHttpHandlerExtensionHandler.java b/cms-server/src/main/java/com/condation/cms/server/handler/extensions/JettyHttpHandlerExtensionHandler.java index a8ed7ec8..d952e265 100644 --- a/cms-server/src/main/java/com/condation/cms/server/handler/extensions/JettyHttpHandlerExtensionHandler.java +++ b/cms-server/src/main/java/com/condation/cms/server/handler/extensions/JettyHttpHandlerExtensionHandler.java @@ -42,6 +42,7 @@ */ @RequiredArgsConstructor @Slf4j +@Deprecated(since = "7.3.0", forRemoval = true) public class JettyHttpHandlerExtensionHandler extends Handler.Abstract { public static final String PATH = "extension"; diff --git a/cms-server/src/main/java/com/condation/cms/server/handler/api/APIHandler.java b/cms-server/src/main/java/com/condation/cms/server/handler/http/APIHandler.java similarity index 98% rename from cms-server/src/main/java/com/condation/cms/server/handler/api/APIHandler.java rename to cms-server/src/main/java/com/condation/cms/server/handler/http/APIHandler.java index 79a1d28a..9ac101e7 100644 --- a/cms-server/src/main/java/com/condation/cms/server/handler/api/APIHandler.java +++ b/cms-server/src/main/java/com/condation/cms/server/handler/http/APIHandler.java @@ -1,4 +1,4 @@ -package com.condation.cms.server.handler.api; +package com.condation.cms.server.handler.http; import com.condation.cms.api.extensions.HttpRoutesExtensionPoint; import com.condation.cms.api.extensions.Mapping; diff --git a/cms-server/src/main/java/com/condation/cms/server/handler/extensions/JettyExtensionRouteHandler.java b/cms-server/src/main/java/com/condation/cms/server/handler/http/RoutesHandler.java similarity index 54% rename from cms-server/src/main/java/com/condation/cms/server/handler/extensions/JettyExtensionRouteHandler.java rename to cms-server/src/main/java/com/condation/cms/server/handler/http/RoutesHandler.java index d018d1e8..51806962 100644 --- a/cms-server/src/main/java/com/condation/cms/server/handler/extensions/JettyExtensionRouteHandler.java +++ b/cms-server/src/main/java/com/condation/cms/server/handler/http/RoutesHandler.java @@ -1,4 +1,4 @@ -package com.condation.cms.server.handler.extensions; +package com.condation.cms.server.handler.http; /*- * #%L @@ -22,12 +22,16 @@ * #L% */ - +import com.condation.cms.api.extensions.HttpRoutesExtensionPoint; +import com.condation.cms.api.extensions.Mapping; import com.condation.cms.api.request.RequestContext; +import com.condation.cms.api.utils.RequestUtil; import com.condation.cms.extensions.HttpHandlerExtension; import com.condation.cms.extensions.hooks.ServerHooks; import com.condation.cms.extensions.http.JettyHttpHandlerWrapper; import com.condation.cms.server.filter.CreateRequestContextFilter; +import com.condation.modules.api.ModuleManager; +import com.google.inject.Inject; import java.util.Optional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -40,39 +44,64 @@ * * @author t.marx */ -@RequiredArgsConstructor +@RequiredArgsConstructor(onConstructor = @__({ + @Inject })) @Slf4j -public class JettyExtensionRouteHandler extends Handler.Abstract { +public class RoutesHandler extends Handler.Abstract { + + private final ModuleManager moduleManager; @Override public boolean handle(Request request, Response response, Callback callback) throws Exception { - var requestContext = (RequestContext) request.getAttribute(CreateRequestContextFilter.REQUEST_CONTEXT); - String extension = getExtensionName(request); - var method = request.getMethod(); - - var httpExtensions = requestContext.get(ServerHooks.class).getHttpRoutes(); - Optional findHttpHandler = httpExtensions.findHttpHandler(method, extension); - - if (findHttpHandler.isPresent()) { - return new JettyHttpHandlerWrapper(findHttpHandler.get().handler()).handle(request, response, callback); + try { + if (tryExtensionRoutes(request, response, callback)) { + return true; + } + + if (tryModuleRoutes(request, response, callback)) { + return true; + } + + Response.writeError(request, response, callback, 404); + return true; + } catch (Exception e) { + log.error(null, e); + callback.failed(e); + return true; } - return false; } - private String getExtensionName(Request request) { - var path = request.getHttpURI().getPath(); - var contextPath = request.getContext().getContextPath(); + private boolean tryModuleRoutes(Request request, Response response, Callback callback) throws Exception { + String route = "/" + RequestUtil.getContentPath(request); - if (!contextPath.endsWith("/")) { - contextPath += "/"; - } + Optional firstMatch = moduleManager.extensions(HttpRoutesExtensionPoint.class) + .stream() + .filter(extension -> extension.getMapping().getMatchingHandler(route).isPresent()) + .map(extension -> extension.getMapping()) + .findFirst(); - path = path.replace(contextPath, ""); - if (!path.startsWith("/")) { - path = "/" + path; + if (firstMatch.isPresent()) { + var mapping = firstMatch.get(); + var handler = mapping.getMatchingHandler(route).get(); + return handler.handle(request, response, callback); } - return path; + + return false; } + private boolean tryExtensionRoutes(Request request, Response response, Callback callback) throws Exception { + var requestContext = (RequestContext) request.getAttribute(CreateRequestContextFilter.REQUEST_CONTEXT); + + String route = "/" + RequestUtil.getContentPath(request); + var method = request.getMethod(); + + var httpExtensions = requestContext.get(ServerHooks.class).getHttpRoutes(); + Optional findHttpHandler = httpExtensions.findHttpHandler(method, route); + + if (findHttpHandler.isPresent()) { + return new JettyHttpHandlerWrapper(findHttpHandler.get().handler()).handle(request, response, callback); + } + return false; + } } diff --git a/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyModuleHandler.java b/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyModuleHandler.java index 115e733b..3b918396 100644 --- a/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyModuleHandler.java +++ b/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyModuleHandler.java @@ -41,6 +41,7 @@ */ @RequiredArgsConstructor @Slf4j +@Deprecated(since = "7.3.0", forRemoval = true) public class JettyModuleHandler extends Handler.Abstract { public static final String PATH = "module"; diff --git a/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyRouteHandler.java b/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyRouteHandler.java index a5c44a20..97a6cb28 100644 --- a/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyRouteHandler.java +++ b/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyRouteHandler.java @@ -42,6 +42,7 @@ @RequiredArgsConstructor(onConstructor = @__({ @Inject})) @Slf4j +@Deprecated(since = "7.3.0", forRemoval = true) public class JettyRouteHandler extends Handler.Abstract { private final ModuleManager moduleManager; diff --git a/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyRoutesHandler.java b/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyRoutesHandler.java deleted file mode 100644 index 93ae373e..00000000 --- a/cms-server/src/main/java/com/condation/cms/server/handler/module/JettyRoutesHandler.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.condation.cms.server.handler.module; - -/*- - * #%L - * cms-server - * %% - * Copyright (C) 2023 - 2024 CondationCMS - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * . - * #L% - */ - - -import com.condation.cms.api.extensions.HttpRoutesExtensionPoint; -import com.condation.cms.api.extensions.Mapping; -import com.condation.cms.api.utils.RequestUtil; -import com.condation.modules.api.ModuleManager; -import com.google.inject.Inject; -import java.util.Optional; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.Callback; - -/** - * - * @author t.marx - */ -@RequiredArgsConstructor(onConstructor = @__({ - @Inject})) -@Slf4j -public class JettyRoutesHandler extends Handler.Abstract { - - private final ModuleManager moduleManager; - - @Override - public boolean handle(Request request, Response response, Callback callback) throws Exception { - - try { - String route = "/" + RequestUtil.getContentPath(request); - - Optional firstMatch = moduleManager.extensions(HttpRoutesExtensionPoint.class) - .stream() - .filter(extension -> extension.getMapping().getMatchingHandler(route).isPresent()) - .map(extension -> extension.getMapping()) - .findFirst(); - - if (firstMatch.isPresent()) { - var mapping = firstMatch.get(); - var handler = mapping.getMatchingHandler(route).get(); - return handler.handle(request, response, callback); - } - - - return false; - } catch (Exception e) { - log.error(null, e); - callback.failed(e); - return true; - } - } -}