Skip to content

Commit

Permalink
#136 refactor supplier usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed May 13, 2024
1 parent a14ffa3 commit 7cec168
Showing 7 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.thmarx.cms.api.content;

/*-
* #%L
* cms-api
* %%
* 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 java.util.List;
import java.util.Map;
import java.util.Optional;

/**
*
* @author t.marx
*/
public interface RenderContentFunction {
Optional<ContentResponse> render(String uri, Map<String, List<String>> parameters);
}
Original file line number Diff line number Diff line change
@@ -23,20 +23,20 @@
*/

import com.github.thmarx.cms.api.content.ContentResponse;
import com.github.thmarx.cms.api.content.RenderContentFunction;
import com.github.thmarx.cms.api.feature.Feature;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;

/**
*
* @author t.marx
*/
public record ContentRenderFeature(BiFunction<String, Map<String, List<String>>, Optional<ContentResponse>> renderContentFunction) implements Feature {
public record ContentRenderFeature(RenderContentFunction renderContentFunction) implements Feature {

public Optional<ContentResponse> renderContentNode (String uri, Map<String, List<String>> params) {
return renderContentFunction().apply(uri, params);
return renderContentFunction().render(uri, params);
}

}
Original file line number Diff line number Diff line change
@@ -24,12 +24,11 @@

import com.github.thmarx.cms.content.ContentResolver;
import com.github.thmarx.cms.api.content.ContentResponse;
import com.github.thmarx.cms.api.content.RenderContentFunction;
import com.github.thmarx.cms.request.RequestContextFactory;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@@ -39,17 +38,17 @@
*/
@Slf4j
@RequiredArgsConstructor
public class RenderContentFunction implements BiFunction<String, Map<String, List<String>>, Optional<ContentResponse>> {
public class DefaultRenderContentFunction implements RenderContentFunction {

private final Supplier<ContentResolver> contentResolver;
private final Supplier<RequestContextFactory> requestContextFactory;
private final ContentResolver contentResolver;
private final RequestContextFactory requestContextFactory;

@Override
public Optional<ContentResponse> apply(String uri, Map<String, List<String>> parameters) {
public Optional<ContentResponse> render(String uri, Map<String, List<String>> parameters) {
try (
var requestContext = requestContextFactory.get().create(uri, parameters);) {
var requestContext = requestContextFactory.create(uri, parameters);) {

return contentResolver.get().getContent(requestContext);
return contentResolver.getContent(requestContext);
} catch (Exception e) {
log.error("", e);
}
Original file line number Diff line number Diff line change
@@ -57,7 +57,6 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import org.eclipse.jetty.server.Request;

@@ -68,7 +67,7 @@
@RequiredArgsConstructor
public class RequestContextFactory {

private final Supplier<MarkdownRenderer> markdownRenderer;
private final MarkdownRenderer markdownRenderer;
private final ExtensionManager extensionManager;
private final Theme theme;
private final SiteProperties siteProperties;
@@ -93,7 +92,7 @@ public RequestContext create(
RequestExtensions requestExtensions = extensionManager.newContext(theme, hookSystem);

RenderContext renderContext = new RenderContext(
markdownRenderer.get(),
markdownRenderer,
createShortCodes(requestExtensions),
theme);

Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
import com.github.thmarx.cms.api.theme.Theme;
import com.github.thmarx.cms.filesystem.FileDB;
import com.github.thmarx.cms.media.MediaManager;
import com.github.thmarx.cms.module.RenderContentFunction;
import com.github.thmarx.cms.module.DefaultRenderContentFunction;
import com.github.thmarx.cms.request.RequestContextFactory;
import com.github.thmarx.cms.server.jetty.FileFolderPathResource;
import com.github.thmarx.cms.server.jetty.filter.RequestContextFilter;
@@ -132,7 +132,7 @@ public void init(Path modulesPath) throws IOException {

cmsModuleContext.add(
ContentRenderFeature.class,
new ContentRenderFeature(new RenderContentFunction(() -> contentResolver, () -> requestContextFactory))
new ContentRenderFeature(new DefaultRenderContentFunction(contentResolver, requestContextFactory))
);

moduleManager.initModules();
Original file line number Diff line number Diff line change
@@ -137,6 +137,7 @@ public CMSMarkdownRenderer defaultMarkdownRenderer () {
* The markedjs markdown renderer is implemented using graaljs, so we need a fresh instance for every request
* @param siteProperties
* @param moduleManager
* @param defaultMarkdownRenderer
* @return
*/
@Provides
Original file line number Diff line number Diff line change
@@ -206,9 +206,9 @@ public MediaService mediaService(@Named("assets") Path assetBase) throws IOExcep
@Singleton
public RequestContextFactory requestContextFactory(
Injector injector, ExtensionManager extensionManager, Theme theme, SiteProperties siteProperties,
MediaService mediaService) {
MediaService mediaService, MarkdownRenderer markdownRenderer) {
return new RequestContextFactory(
() -> injector.getInstance(MarkdownRenderer.class),
markdownRenderer,
extensionManager,
theme,
siteProperties,

0 comments on commit 7cec168

Please sign in to comment.