Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
public @interface Action {
String value ();
int priority () default 10;
Scope scope () default Scope.REQUEST;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
public @interface Filter {
String value ();
int priority () default 10;
Scope scope () default Scope.REQUEST;
}
32 changes: 32 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/annotations/Scope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.condation.cms.api.annotations;

/*-
* #%L
* CMS Api
* %%
* Copyright (C) 2023 - 2026 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/

/**
*
* @author thmar
*/
public enum Scope {

REQUEST,
APPLICATION
}
4 changes: 2 additions & 2 deletions cms-api/src/main/java/com/condation/cms/api/hooks/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public enum Hooks {
/* content */
CONTENT_SHORTCODES("system/content/shortCodes"),
CONTENT_FILTER("system/content/filter"),
LAYOUT_HEADER("system/layout/header"),
LAYOUT_FOOTER("system/layout/footer"),
LAYOUT_HEADER("system/layout/html/header"),
LAYOUT_FOOTER("system/layout/html/footer"),
/*query*/
DB_QUERY_OPERATIONS("system/db/query/operations"),
/* scheduler */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.condation.cms.hooksystem.extensions.TemplateHooks;
import com.condation.cms.content.template.functions.LinkFunction;
import com.condation.cms.content.template.functions.MarkdownFunction;
import com.condation.cms.content.template.functions.hooks.HooksTemlateFunction;
import com.condation.cms.content.template.functions.list.NodeListFunctionBuilder;
import com.condation.cms.content.template.functions.navigation.NavigationFunction;
import com.condation.cms.content.template.functions.query.QueryFunction;
Expand Down Expand Up @@ -189,8 +190,6 @@ public String render(final ReadOnlyFile contentFile, final RequestContext contex
model.values.put("messages", theme.getMessages());
}

namespace.add(Constants.TemplateNamespaces.CMS, "hooks", context.get(HookSystemFeature.class).hookSystem());

namespace.add(Constants.TemplateNamespaces.CMS, "links", new LinkFunction(context));

model.values.put("PREVIEW_MODE", isPreview(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public String process(String rawContent) {
public String updateLayoutPosition (Hooks hook, String elementName, String rawContent) {

if (!rawContent.contains(elementName)) {
log.debug("No {} found, skipping layout position injection", elementName);
return rawContent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
import com.condation.cms.api.SiteProperties;
import com.condation.cms.api.scheduler.CronJob;
import com.condation.cms.api.scheduler.CronJobContext;
import com.condation.cms.api.scheduler.CronJobScheduler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* #L%
*/

import com.condation.cms.api.annotations.Scope;
import com.condation.cms.api.hooks.ActionFunction;
import com.condation.cms.api.hooks.FilterFunction;
import com.condation.cms.api.hooks.HookSystem;
Expand All @@ -45,17 +46,24 @@ public class CMSHookSystem implements HookSystem {
private final ActionExecutor actionExecutor;
private final FilterExecutor filterExecutor;
private final AnnotationHookRegistrar annotationRegistrar;

private Scope scope = null;

public CMSHookSystem() {
public CMSHookSystem (Scope scope) {
this.scope = scope;
this.actionRegistry = new ActionRegistry();
this.filterRegistry = new FilterRegistry();
this.actionExecutor = new ActionExecutor(actionRegistry);
this.filterExecutor = new FilterExecutor(filterRegistry);
this.annotationRegistrar = new AnnotationHookRegistrar(actionRegistry, filterRegistry);
this.annotationRegistrar = new AnnotationHookRegistrar(actionRegistry, filterRegistry, scope);
}

public CMSHookSystem() {
this(Scope.APPLICATION);
}

public CMSHookSystem(CMSHookSystem source) {
this();
this(Scope.REQUEST);
this.actionRegistry.putAll(source.actionRegistry);
this.filterRegistry.putAll(source.filterRegistry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.condation.cms.api.annotations.Action;
import com.condation.cms.api.annotations.Filter;
import com.condation.cms.api.annotations.Scope;
import com.condation.cms.api.hooks.ActionFunction;
import com.condation.cms.api.hooks.ActionContext;
import com.condation.cms.api.hooks.FilterContext;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class AnnotationHookRegistrar {

private final ActionRegistry actionRegistry;
private final FilterRegistry filterRegistry;
private final Scope scope;

public void register(Object source) {
for (Method method : source.getClass().getMethods()) {
Expand All @@ -74,6 +76,11 @@ public void register(Object source) {

private void registerAction(Object target, Method method) {
Action annotation = method.getAnnotation(Action.class);

if (!scope.equals(annotation.scope())) {
return;
}

ActionFunction<?> fn = buildActionFunction(target, method);
if (fn != null) {
actionRegistry.register(annotation.value(), fn, annotation.priority());
Expand All @@ -82,6 +89,11 @@ private void registerAction(Object target, Method method) {

private void registerFilter(Object target, Method method) {
Filter annotation = method.getAnnotation(Filter.class);

if (!scope.equals(annotation.scope())) {
return;
}

FilterFunction<?> fn = buildFilterFunction(target, method);
if (fn != null) {
filterRegistry.register(annotation.value(), fn, annotation.priority());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.condation.cms.hooksystem.ActionHook;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

Expand All @@ -37,7 +36,7 @@
public class ActionRegistry {

private final Multimap<String, ActionHook> hooks = ArrayListMultimap.create();

public <T> void register(String name, ActionFunction<T> function, int priority) {
hooks.put(name, new ActionHook<>(name, priority, function));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.condation.cms.api.annotations.Filter;
import com.condation.cms.api.annotations.Action;
import com.condation.cms.api.annotations.Param;
import com.condation.cms.api.annotations.Scope;
import com.condation.cms.api.hooks.ActionContext;
import com.condation.cms.api.hooks.FilterContext;
import java.util.ArrayList;
Expand All @@ -42,10 +43,13 @@
public class CMSHookSystemTest {

private CMSHookSystem hookSystem;

private CMSHookSystem globalSystem;

@BeforeEach
public void setup() {
hookSystem = new CMSHookSystem();
hookSystem = new CMSHookSystem(Scope.REQUEST);
globalSystem = new CMSHookSystem(Scope.APPLICATION);
}

@Test
Expand Down Expand Up @@ -101,6 +105,18 @@ void test_action_annotation() {
hookSystem.doAction("test/annotation/action1");
Assertions.assertThat(actionObject.counter).hasValue(2);
}

@Test
void test_action_scope() {
var actionObject = new MyActions();
hookSystem.register(actionObject);
globalSystem.register(actionObject);

hookSystem.doAction("test/annotation/count");
Assertions.assertThat(actionObject.counter).hasValue(1);
globalSystem.doAction("test/annotation/count");
Assertions.assertThat(actionObject.counter).hasValue(2);
}

@Test
void test_filter_annotations() {
Expand All @@ -109,6 +125,19 @@ void test_filter_annotations() {
Assertions.assertThat(hookSystem.doFilter("test/annotation/filter1", new ArrayList<>(List.of("1", "2", "3"))))
.containsExactly("1", "3");
}

@Test
void test_filter_scopes() {
var myFilters = new MyFilters();
hookSystem.register(myFilters);
globalSystem.register(myFilters);

Assertions.assertThat(hookSystem.doFilter("test/annotation/stringFilter", ""))
.isEqualTo("requestFilter");

Assertions.assertThat(globalSystem.doFilter("test/annotation/stringFilter", ""))
.isEqualTo("globalFilter");
}

@Test
void test_action_named_params() {
Expand All @@ -130,6 +159,16 @@ public List<String> filter(FilterContext<List<String>> context) {
context.value().remove("2");
return context.value();
}

@Filter(value = "test/annotation/stringFilter")
public String requestFilter(FilterContext<String> context) {
return "requestFilter";
}

@Filter(value = "test/annotation/stringFilter", scope = Scope.APPLICATION)
public String globalFilter(FilterContext<String> context) {
return "globalFilter";
}
}

public class MyActions {
Expand All @@ -144,6 +183,15 @@ public void action1(ActionContext<?> context) {
public void action2(ActionContext<?> context) {
counter.incrementAndGet();
}

@Action(value = "test/annotation/count", scope = Scope.APPLICATION)
public void globalAction(ActionContext<?> context) {
counter.incrementAndGet();
}
@Action(value = "test/annotation/count")
public void requestAction(ActionContext<?> context) {
counter.incrementAndGet();
}
}

public class MyNamedParamActions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.condation.cms.api.feature.features.ThemeFeature;
import com.condation.cms.api.messaging.Messaging;
import com.condation.cms.api.module.SiteModuleContext;
import com.condation.cms.api.scheduler.CronJobContext;
import com.condation.cms.api.theme.Theme;
import com.condation.cms.content.ContentResolver;
import com.condation.cms.core.scheduler.SiteCronJobScheduler;
Expand All @@ -64,7 +65,7 @@ public void init () {
}

private void initCronJobContext() {
var context = injector.getInstance(SiteModuleContext.class);
var context = injector.getInstance(CronJobContext.class);

context.add(SitePropertiesFeature.class, new SitePropertiesFeature(injector.getInstance(SiteProperties.class)));
context.add(ServerPropertiesFeature.class, new ServerPropertiesFeature(injector.getInstance(ServerProperties.class)));
Expand Down
Loading