-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thorsten Marx
committed
Jun 26, 2024
1 parent
bdea380
commit 8c77eac
Showing
14 changed files
with
306 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/HttpHandlerWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/ServerHooks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/ShortCodesWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.