Skip to content

Commit

Permalink
#218 introduce server hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jun 26, 2024
1 parent bdea380 commit 8c77eac
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -74,7 +73,7 @@ public ActionContext<Object> execute(final String name, final Map<String, Object
try {
return action.function().apply(context);
} catch (Exception e) {
log.error("error executing action");
log.error("error executing action", e);
}
return null;
})
Expand Down Expand Up @@ -106,7 +105,7 @@ public <T> FilterContext<T> filter(final String name, final List<T> parameters)
returnContext.values().clear();
returnContext.values().addAll((List<T>)result);
} catch (Exception e) {
log.error("error on filter");
log.error("error on filter", e);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ShortCodeParser {

private static final String SHORTCODE_REGEX = "\\[\\[(\\w+)([^\\]]*)\\]\\](.*?)\\[\\[\\/\\1\\]\\]|\\[\\[(\\w+)([^\\]]*)\\/\\]\\]";
Expand Down Expand Up @@ -76,7 +78,11 @@ public static String replace (String content, Codes codes) {

newContent += content.substring(lastPosition, match.getStart());

newContent += codes.get(match.getName()).apply(match.getParameters());
try {
newContent += codes.get(match.getName()).apply(match.getParameters());
} catch (Exception e) {
log.error("error executing shortcode", e);
}

lastPosition = match.getEnd();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@
*/

import com.github.thmarx.cms.api.model.Parameter;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -41,10 +36,6 @@
@RequiredArgsConstructor
public class ShortCodes {

public static final Pattern TAG_PARAMS_PATTERN_SHORT = Pattern.compile("\\[{2}(?<tag>[a-z_A-Z0-9]+)( (?<params>.*?))?\\p{Blank}*/\\]{2}");

public static final Pattern TAG_PARAMS_PATTERN_LONG = Pattern.compile("\\[{2}(?<tag>[a-z_A-Z0-9]+)( (?<params>.*?))?\\]{2}(?<content>.*?)\\[{2}/\\k<tag>\\]{2}");

private final ShortCodeParser.Codes codes;

public ShortCodes (Map<String, Function<Parameter, String>> codes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
import com.github.thmarx.cms.api.ServerProperties;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.feature.features.HookSystemFeature;
import com.github.thmarx.cms.api.request.RequestContext;
import com.github.thmarx.cms.extensions.request.RequestExtensions;
import com.github.thmarx.cms.api.theme.Theme;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.thmarx.cms.extensions.hooks;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* 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.github.thmarx.cms.extensions.HttpHandlerExtension;
import com.github.thmarx.cms.extensions.http.ExtensionHttpHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.Getter;

/**
*
* @author t.marx
*/
public class HttpHandlerWrapper {

@Getter
private final List<HttpHandlerExtension> httpHandlerExtensions = new ArrayList<>();

public void add(final String method, final String path, final ExtensionHttpHandler handler) {
httpHandlerExtensions.add(new HttpHandlerExtension(method, path, handler));
}

public Optional<HttpHandlerExtension> findHttpHandler(final String method, final String path) {
return httpHandlerExtensions.stream().filter(handler -> handler.method().equalsIgnoreCase(method) && handler.path().equalsIgnoreCase(path)).findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.thmarx.cms.extensions.hooks;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* 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.github.thmarx.cms.api.annotations.Experimental;
import com.github.thmarx.cms.api.feature.Feature;
import com.github.thmarx.cms.api.feature.features.HookSystemFeature;
import com.github.thmarx.cms.api.model.Parameter;
import com.github.thmarx.cms.api.request.RequestContext;
import java.util.Map;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;

/**
*
* @author t.marx
*/
@Experimental
@RequiredArgsConstructor
public class ServerHooks implements Feature {

private final RequestContext requestContext;


public HttpHandlerWrapper getHttpExtensions () {
var httpExtensions = new HttpHandlerWrapper();
requestContext.get(HookSystemFeature.class).hookSystem()
.execute("http/extension/register", Map.of("httpExtensions", httpExtensions));

return httpExtensions;
}

public HttpHandlerWrapper getHttpRoutes () {
var httpExtensions = new HttpHandlerWrapper();
requestContext.get(HookSystemFeature.class).hookSystem()
.execute("http/route/register", Map.of("httpRoutes", httpExtensions));

return httpExtensions;
}

public ShortCodesWrapper getShortCodes (Map<String, Function<Parameter, String>> codes) {
var codeWrapper = new ShortCodesWrapper(codes);
requestContext.get(HookSystemFeature.class).hookSystem()
.execute("content/shortcodes/filter", Map.of("shortCodes", codeWrapper));

return codeWrapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.thmarx.cms.extensions.hooks;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* 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.github.thmarx.cms.api.model.Parameter;
import java.util.Map;
import java.util.function.Function;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
*
* @author t.marx
*/
@RequiredArgsConstructor
public class ShortCodesWrapper {

@Getter
private final Map<String, Function<Parameter, String>> shortCodes;

public void put(final String shortCode, final Function<Parameter, String> function) {
shortCodes.put(shortCode, function);
}

public boolean contains(final String shortCode) {
return shortCodes.containsKey(shortCode);
}

public void remove(final String shortCode) {
shortCodes.remove(shortCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,19 @@ public void registerQueryOperation (final String operation, final BiPredicate<Ob
queryOperations.put(operation, predicate);
}

@Deprecated
public void registerHttpExtension(final String method, final String path, final ExtensionHttpHandler handler) {
httpHandlerExtensions.add(new HttpHandlerExtension(method, path, handler));
}

@Deprecated
public Optional<HttpHandlerExtension> findHttpHandler (final String method, final String path) {
return httpHandlerExtensions.stream().filter(handler -> handler.method().equalsIgnoreCase(method) && handler.path().equalsIgnoreCase(path)).findFirst();
}

@Deprecated
public void registerHttpRouteExtension(final String method, final String path, final ExtensionHttpHandler handler) {
httpRouteHandlerExtensions.add(new HttpHandlerExtension(method, path, handler));
}

@Deprecated
public Optional<HttpHandlerExtension> findHttpRouteHandler (final String method, final String path) {
return httpRouteHandlerExtensions.stream().filter(handler -> handler.method().equalsIgnoreCase(method) && handler.path().equalsIgnoreCase(path)).findFirst();
}
Expand All @@ -91,7 +92,7 @@ public void registerTemplateSupplier(final String path, final Supplier<?> suppli
public void registerTemplateFunction(final String path, final Function<?, ?> function) {
registerTemplateFunctions.add(new TemplateFunctionExtension(path, function));
}

@Deprecated
public void registerShortCode(final String shortCode, final Function<Parameter, String> function) {
shortCodes.put(shortCode, function);
}
Expand Down
35 changes: 35 additions & 0 deletions cms-server/hosts/features/extensions/content.extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { $hooks } from 'system/hooks.mjs';
import { $shortcodes } from 'system/shortcodes.mjs';


$shortcodes.register(
"hello",
(params) => `Hello ${params.get("name")}, I'm a TAG!`
)

/*
$shortcodes.register(
"name_age",
(params) => `Hello ${params.get("name")}, your age is ${params.get("age")}!`
)
*/

/*
$hooks.registerFilter("content/shortcodes/filter", (context) => {
context.values().getFirst().put(
"name_age",
(params) => `Hello ${params.get("name")}, your age is ${params.get("age")}!`
)
return context.values()
})
*
*/
$hooks.registerAction("content/shortcodes/filter", (context) => {
context.arguments().get("shortCodes").put(
"name_age",
(params) => `Hello ${params.get("name")}, your age is ${params.get("age")}!`
)
return null;
})

39 changes: 39 additions & 0 deletions cms-server/hosts/features/extensions/http.extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { UTF_8 } from 'system/charsets.mjs';
import { $hooks } from 'system/hooks.mjs';
import { $http } from 'system/http.mjs';
import { $routes } from 'system/routes.mjs';

// callable via /extensions/test
$http.get("/test", (request, response) => {
response.addHeader("Content-Type", "text/html; charset=utf-8")
response.write("ich bin einen test extension!öäü", UTF_8)
})

$hooks.registerAction("http/extension/register", (context) => {
context.arguments().get("httpExtensions").add(
"GET",
"/test2",
(request, response) => {
response.addHeader("Content-Type", "text/html; charset=utf-8")
response.write("ich bin einen test extension, registered via hook!", UTF_8)
}
)
return null;
})

$routes.get("/hello-extension", (request, response) => {
response.addHeader("Content-Type", "text/html; charset=utf-8")
response.write("extension route", UTF_8)
})

$hooks.registerAction("http/route/register", (context) => {
context.arguments().get("httpRoutes").add(
"GET",
"/hello-route",
(request, response) => {
response.addHeader("Content-Type", "text/html; charset=utf-8")
response.write("route via hook!", UTF_8)
}
)
return null;
})
16 changes: 0 additions & 16 deletions cms-server/hosts/features/extensions/test.extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ import { UTF_8 } from 'system/charsets.mjs';
import { $http } from 'system/http.mjs';
import { $routes } from 'system/routes.mjs';
import { $template } from 'system/template.mjs';
import { $shortcodes } from 'system/shortcodes.mjs';
import { getLogger } from 'system/logging.mjs';

const logger = getLogger("extensions");
if (ENV === "dev"){
logger.info("dev debug log from test extension");
}

// callable via /extensions/test
$http.get("/test", (request, response) => {
response.addHeader("Content-Type", "text/html; charset=utf-8")
response.write("ich bin einen test extension!öäü", UTF_8)
})
$http.post("/form", (request, response) => {
const body = JSON.parse(request.getBody(UTF_8))
console.log("body", request.getBody(UTF_8))
Expand All @@ -36,14 +30,4 @@ $template.registerTemplateSupplier(
$template.registerTemplateFunction(
"getHello",
(name) => "Hello " + name + "!"
)

$shortcodes.register(
"hello",
(params) => `Hello ${params.get("name")}, I'm a TAG!`
)

$shortcodes.register(
"name_age",
(params) => `Hello ${params.get("name")}, your age is ${params.get("age")}!`
)
Loading

0 comments on commit 8c77eac

Please sign in to comment.