Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API endpoint for extensions and modules #330

Merged
merged 13 commits into from
Nov 28, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public ReadOnlyFile getParent() {

@Override
public List<ReadOnlyFile> children() throws IOException {
return Files.list(file).map(path -> new NIOReadOnlyFile(file, basePath)).map(ReadOnlyFile.class::cast).toList();
return Files.list(file).map(child -> new NIOReadOnlyFile(child, basePath)).map(ReadOnlyFile.class::cast).toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author t.marx
*/
@Deprecated(since = "7.3.0", forRemoval = true)
public abstract class HttpRouteExtensionPoint extends AbstractExtensionPoint {
abstract public String getRoute ();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.condation.cms.api.extensions.http;

/*-
* #%L
* cms-api
* %%
* 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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.api.extensions.AbstractExtensionPoint;

public abstract class APIHandlerExtensionPoint extends AbstractExtensionPoint {
abstract public PathMapping getMapping();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.condation.cms.api.extensions.http;

/*-
* #%L
* cms-api
* %%
* 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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.Callback;

/**
*
* @author t.marx
*/
public interface HttpHandler {

boolean handle (Request request, Response response, Callback callback) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.condation.cms.api.extensions.http;

/*-
* #%L
* cms-api
* %%
* 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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.eclipse.jetty.http.pathmap.PathSpec;

public class PathMapping {

private List<Mapping> mappings;

public PathMapping () {
mappings = new ArrayList<>();
}

public void add (PathSpec pathSpec, String method, HttpHandler handler) {
mappings.add(new Mapping(pathSpec, method, handler));
}

public Optional<HttpHandler> getMatchingHandler (String uri, String method) {
return mappings.stream().filter(entry ->
entry.pathSpec.matches(uri) && entry.method.equalsIgnoreCase(method)
).map(entry -> entry.handler).findFirst();
}


private static record Mapping (PathSpec pathSpec, String method, HttpHandler handler){};
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum Hooks {
SCHEDULER_REMOVE("system/scheduler/remove"),
HTTP_EXTENSION("system/server/http/extension"),
HTTP_ROUTE("system/server/http/route"),
API_ROUTE("system/server/api/route"),
TEMPLATE_SUPPLIER("system/template/supplier"),
TEMPLATE_FUNCTION("system/template/function")
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class ContentResolver {
private final DB db;

public Optional<ContentResponse> getStaticContent (String uri) {
if (uri.endsWith(".md")) {
return Optional.empty();
}
if (uri.startsWith("/")) {
uri = uri.substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.api.scheduler.CronJob;
import com.condation.cms.api.scheduler.CronJobContext;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import lombok.extern.slf4j.Slf4j;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
Expand All @@ -36,19 +37,29 @@
*
* @author t.marx
*/
@Slf4j
@DisallowConcurrentExecution
public class SingleCronJobRunner implements Job {

public static final String DATA_CRONJOB = "cronjob";
public static final String DATA_CONTEXT = "context";

public static final Semaphore LOCK = new Semaphore(1, true);

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
CronJobContext jobContext = (CronJobContext) context.getJobDetail().getJobDataMap().get(DATA_CONTEXT);
CronJob cronJob = (CronJob) context.getJobDetail().getJobDataMap().get(DATA_CRONJOB);
try {
LOCK.acquire();
CronJobContext jobContext = (CronJobContext) context.getJobDetail().getJobDataMap().get(DATA_CONTEXT);
CronJob cronJob = (CronJob) context.getJobDetail().getJobDataMap().get(DATA_CRONJOB);

if (cronJob != null && jobContext != null) {
cronJob.accept(jobContext);
if (cronJob != null && jobContext != null) {
cronJob.accept(jobContext);
}
} catch (Exception e) {
log.error("", e);
} finally {
LOCK.release();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ public HttpHandlerWrapper getHttpRoutes () {

return httpExtensions;
}

public HttpHandlerWrapper getAPIRoutes () {
var httpExtensions = new HttpHandlerWrapper();
requestContext.get(HookSystemFeature.class).hookSystem()
.execute(Hooks.API_ROUTE.hook(), Map.of("apiRoutes", httpExtensions));

return httpExtensions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/


import com.condation.cms.media.FileMediaService;
import java.io.IOException;
import java.nio.file.Path;
import org.assertj.core.api.Assertions;
Expand Down
5 changes: 5 additions & 0 deletions cms-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>cms-auth</artifactId>
</dependency>

<dependency>
<groupId>com.condation.cms.modules</groupId>
<artifactId>cms-system-modules</artifactId>
</dependency>

<dependency>
<groupId>com.condation.cms</groupId>
<artifactId>cms-test</artifactId>
Expand Down
27 changes: 19 additions & 8 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 @@ -80,12 +77,12 @@
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 @@ -226,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 @@ -236,7 +232,6 @@ public Handler buildHttpHandler() {
initContextHandler,
routeHandler,
routesHandler,
extensionRouteHandler,
viewHandler,
taxonomyHandler,
contentHandler
Expand Down Expand Up @@ -271,6 +266,10 @@ public Handler buildHttpHandler() {
createExtensionHandler()
);

pathMappingsHandler.addMapping(PathSpec.from("/" + APIHandler.PATH + "/*"),
createAPIHandler()
);

ContextHandler defaultContextHandler = new ContextHandler(
pathMappingsHandler,
injector.getInstance(SiteProperties.class).contextPath()
Expand Down Expand Up @@ -301,6 +300,18 @@ public Handler buildHttpHandler() {
return hostHandler;
}

private Handler.Wrapper createAPIHandler() {
var authHandler = injector.getInstance(JettyAuthenticationHandler.class);
var initContextHandler = injector.getInstance(InitRequestContextFilter.class);
var apiHandler = injector.getInstance(APIHandler.class);
var handlerSequence = new Handler.Sequence(
authHandler,
initContextHandler,
apiHandler
);
return requestContextFilter(handlerSequence, injector);
}

private Handler.Wrapper createExtensionHandler() {
var authHandler = injector.getInstance(JettyAuthenticationHandler.class);
var initContextHandler = injector.getInstance(InitRequestContextFilter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,19 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.condation.cms.api.ServerProperties;
import com.condation.cms.api.SiteProperties;
import com.condation.cms.api.configuration.Configuration;
import com.condation.cms.api.eventbus.EventBus;
import com.condation.cms.api.extensions.MarkdownRendererProviderExtensionPoint;
import com.condation.cms.api.extensions.TemplateEngineProviderExtensionPoint;
import com.condation.cms.api.extensions.TemplateEngineProviderExtentionPoint;
import com.condation.cms.api.feature.features.ConfigurationFeature;
import com.condation.cms.api.feature.features.CronJobSchedulerFeature;
import com.condation.cms.api.feature.features.DBFeature;
import com.condation.cms.api.feature.features.EventBusFeature;
import com.condation.cms.api.feature.features.MessagingFeature;
import com.condation.cms.api.feature.features.ModuleManagerFeature;
import com.condation.cms.api.feature.features.ServerPropertiesFeature;
import com.condation.cms.api.feature.features.SitePropertiesFeature;
import com.condation.cms.api.feature.features.ThemeFeature;
import com.condation.cms.api.hooks.HookSystem;
import com.condation.cms.api.markdown.MarkdownRenderer;
import com.condation.cms.api.messaging.Messaging;
import com.condation.cms.api.module.CMSModuleContext;
import com.condation.cms.api.module.CMSRequestContext;
import com.condation.cms.api.request.ThreadLocalRequestContext;
import com.condation.cms.api.template.TemplateEngine;
import com.condation.cms.api.theme.Theme;
import com.condation.cms.content.markdown.module.CMSMarkdownRenderer;
import com.condation.cms.core.scheduler.SiteCronJobScheduler;
import com.condation.cms.filesystem.FileDB;
import com.condation.modules.api.ModuleManager;
import com.condation.modules.api.ModuleRequestContextFactory;
Expand Down
Loading