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
102 changes: 9 additions & 93 deletions cms-api/src/main/java/com/condation/cms/api/hooks/HookSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
import com.condation.cms.api.annotations.Filter;
import com.condation.cms.api.annotations.Action;
import com.condation.cms.api.utils.AnnotationsUtil;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -39,78 +29,21 @@
*
* @author t.marx
*/
@Slf4j
public class HookSystem {
public interface HookSystem {

Multimap<String, ActionHook> actions = ArrayListMultimap.create();
void register(Object sourceObject);

Multimap<String, FilterHook> filters = ArrayListMultimap.create();
public <T> void registerAction(final String name, final ActionFunction<T> hookFunction);

public HookSystem () {

}
public HookSystem(HookSystem source) {
this.actions.putAll(source.actions);
this.filters.putAll(source.filters);
}
public <T> void registerAction(final String name, final ActionFunction<T> hookFunction, int priority);

public void register(Object sourceObject) {
// Action-Methoden registrieren
List<AnnotationsUtil.CMSAnnotation<Action, Void>> actionMethods
= AnnotationsUtil.process(sourceObject, Action.class, List.of(ActionContext.class), Void.class);
public <T> void registerFilter(final String name, final FilterFunction<T> hookFunction);

for (AnnotationsUtil.CMSAnnotation<Action, Void> ann : actionMethods) {
Action annotation = ann.annotation();
registerAction(annotation.value(), context -> ann.invoke(context), annotation.priority());
}
public <T> void registerFilter(final String name, final FilterFunction<T> hookFunction, int priority);

// Filter-Methoden registrieren
List<AnnotationsUtil.CMSAnnotation<Filter, Object>> filterMethods
= AnnotationsUtil.process(sourceObject, Filter.class, List.of(FilterContext.class), Object.class);
public ActionContext<Object> doAction(final String name);

for (AnnotationsUtil.CMSAnnotation<Filter, Object> ann : filterMethods) {
Filter annotation = ann.annotation();
registerFilter(annotation.value(), context -> ann.invoke(context), annotation.priority());
}
}

public <T> void registerAction(final String name, final ActionFunction<T> hookFunction) {
registerAction(name, hookFunction, 10);
}

public <T> void registerAction(final String name, final ActionFunction<T> hookFunction, int priority) {
actions.put(name, new ActionHook<>(name, priority, hookFunction));
}

public <T> void registerFilter(final String name, final FilterFunction<T> hookFunction) {
registerFilter(name, hookFunction, 10);
}

public <T> void registerFilter(final String name, final FilterFunction<T> hookFunction, int priority) {
filters.put(name, new FilterHook<>(name, priority, hookFunction));
}

public ActionContext<Object> execute(final String name) {
return execute(name, Map.of());
}

public ActionContext<Object> execute(final String name, final Map<String, Object> arguments) {
var context = new ActionContext(new HashMap<>(arguments), new ArrayList<>());
actions.get(name).stream()
.sorted((h1, h2) -> Integer.compare(h1.priority(), h2.priority()))
.map((action) -> {
try {
return action.function().apply(context);
} catch (Exception e) {
log.error("error executing action", e);
}
return null;
})
.filter(value -> value != null)
.forEach(context.results()::add);

return context;
}
public ActionContext<Object> doAction(final String name, final Map<String, Object> arguments);

/**
* calls all filters with the given parameters, if no filter is executed,
Expand All @@ -121,22 +54,5 @@ public ActionContext<Object> execute(final String name, final Map<String, Object
* @param parameters
* @return
*/
public <T> FilterContext<T> filter(final String name, final T parameters) {
final FilterContext<T> returnContext = new FilterContext(
parameters
);
filters.get(name).stream()
.sorted((h1, h2) -> Integer.compare(h1.priority(), h2.priority()))
.forEach((var action) -> {
try {
var context = new FilterContext(returnContext.value());
var result = action.function().apply(context);
returnContext.value((T) result);
} catch (Exception e) {
log.error("error on filter", e);
}
});

return returnContext;
}
public <T> FilterContext<T> doFilter(final String name, final T parameters);
}
2 changes: 2 additions & 0 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,6 +34,8 @@ public enum Hooks {
/* content */
CONTENT_TAGS("system/content/tags"),
CONTENT_FILTER("system/content/filter"),
CONTENT_SLOT_HEADER("system/content/slot/header"),
CONTENT_SLOT_FOOTER("system/content/slot/footer"),
/*query*/
DB_QUERY_OPERATIONS("system/db/query/operations"),
/* scheduler */
Expand Down
2 changes: 1 addition & 1 deletion cms-content/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</dependency>
<dependency>
<groupId>com.condation.cms</groupId>
<artifactId>cms-core</artifactId>
<artifactId>cms-hooksystem</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
import com.condation.cms.content.pipeline.ContentPipelineFactory;
import com.condation.cms.content.views.model.View;
import com.condation.cms.api.content.MapAccess;
import com.condation.cms.extensions.hooks.DBHooks;
import com.condation.cms.extensions.hooks.TemplateHooks;
import com.condation.cms.content.pipeline.HTMLPipeline;
import com.condation.cms.hooksystem.extensions.DBHooks;
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.list.NodeListFunctionBuilder;
Expand Down Expand Up @@ -218,7 +219,10 @@ public String render(final ReadOnlyFile contentFile, final RequestContext contex

model.values.putAll(namespace.getNamespaces());

return templates.get().render((String) meta.get("template"), model);
var htmlContent = templates.get().render((String) meta.get("template"), model);

HTMLPipeline htmlPipeline = new HTMLPipeline(context.get(HookSystemFeature.class).hookSystem());
return htmlPipeline.process(htmlContent);
}

protected MarkdownFunction createMarkdownFunction(final RequestContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ protected void init() {
});

}

public String process(String rawContent) {
return hookSystem.filter(Hooks.CONTENT_FILTER.hook(), rawContent).value();
return hookSystem.doFilter(Hooks.CONTENT_FILTER.hook(), rawContent).value();
}

private String processMarkdown(FilterContext<String> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.condation.cms.api.hooks.HookSystem;
import com.condation.cms.api.request.RequestContext;
import com.condation.cms.api.template.TemplateEngine;
import com.condation.cms.hooksystem.CMSHookSystem;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

Expand All @@ -37,8 +38,8 @@ public final class ContentPipelineFactory {

public static ContentPipeline create (final RequestContext requestContext, final TemplateEngine.Model model) {

var hookSystem = requestContext.get(HookSystemFeature.class).hookSystem();
var pipeline = new ContentPipeline(new HookSystem(hookSystem), requestContext, model);
var hookSystem = (CMSHookSystem)requestContext.get(HookSystemFeature.class).hookSystem();
var pipeline = new ContentPipeline(new CMSHookSystem(hookSystem), requestContext, model);
pipeline.init();

return pipeline;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.condation.cms.content.pipeline;

/*-
* #%L
* CMS Content
* %%
* 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%
*/
import com.condation.cms.api.hooks.HookSystem;
import com.condation.cms.api.hooks.Hooks;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
*
* @author thmar
*/
@Slf4j
@RequiredArgsConstructor
public class HTMLPipeline {

private final HookSystem hookSystem;

public String process(String rawContent) {
rawContent = updateSlot(Hooks.CONTENT_SLOT_HEADER, "</head>", rawContent);
return updateSlot(Hooks.CONTENT_SLOT_FOOTER, "</body>", rawContent);
}

public String updateSlot (Hooks hook, String elementName, String rawContent) {

if (!rawContent.contains(elementName)) {
log.warn("No {} found, skipping header slot injection", elementName);
return rawContent;
}

var result = hookSystem.doAction(hook.hook());

var hookValues = result.results().stream()
.filter(Objects::nonNull)
.filter(String.class::isInstance)
.map(String.class::cast)
.toList();
if (hookValues.isEmpty()) {
return rawContent;
}


var mergedValue = String.join("\n\n", hookValues);

return rawContent.replace(elementName, mergedValue + "\n" + elementName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public ActionContext<Object> execute (String name) {
return execute(name, Map.of());
}
public ActionContext<Object> execute (String name, Map<String, Object> arguments) {
return hookSystem.execute(name, arguments);
return hookSystem.doAction(name, arguments);
}

public FilterContext<Object> filter (String name) {
return filter(name, List.of());
}
public FilterContext<Object> filter (String name, List<Object> arguments) {
return hookSystem.filter(name, arguments);
return hookSystem.doFilter(name, arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public List<NavNode> path() {
navNodes = navNodes.reversed();

if (name != null) {
var hookContext = hookSystem.filter(Hooks.NAVIGATION_PATH.hook(name), navNodes);
var hookContext = hookSystem.doFilter(Hooks.NAVIGATION_PATH.hook(name), navNodes);
navNodes = hookContext.value();
}

Expand Down Expand Up @@ -153,7 +153,7 @@ private List<NavNode> getNodes(final String start, final int depth) {

}
if (name != null) {
var hookContext = hookSystem.filter(Hooks.NAVIGATION_LIST.hook(name), navNodes);
var hookContext = hookSystem.doFilter(Hooks.NAVIGATION_LIST.hook(name), navNodes);
navNodes = hookContext.value();
}
return navNodes;
Expand Down
Loading