Skip to content

Commit

Permalink
merge handling of routes into single handler
Browse files Browse the repository at this point in the history
  • Loading branch information
thmarx committed Nov 17, 2024
1 parent 01aefc1 commit 29872c7
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 164 deletions.
12 changes: 3 additions & 9 deletions cms-server/src/main/java/com/condation/cms/server/VHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,18 @@
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;
import com.condation.cms.filesystem.FileDB;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -237,7 +232,6 @@ public Handler buildHttpHandler() {
initContextHandler,
routeHandler,
routesHandler,
extensionRouteHandler,
viewHandler,
taxonomyHandler,
contentHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;

/**
*
Expand All @@ -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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.condation.cms.server.handler.extensions;
package com.condation.cms.server.handler.http;

/*-
* #%L
Expand All @@ -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;
Expand All @@ -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<HttpHandlerExtension> 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<Mapping> 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<HttpHandlerExtension> findHttpHandler = httpExtensions.findHttpHandler(method, route);

if (findHttpHandler.isPresent()) {
return new JettyHttpHandlerWrapper(findHttpHandler.get().handler()).handle(request, response, callback);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

This file was deleted.

0 comments on commit 29872c7

Please sign in to comment.