Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rafactor theme context path
update request context creation
  • Loading branch information
Thorsten Marx committed Jan 30, 2024
1 parent 86df562 commit 86f3774
Show file tree
Hide file tree
Showing 29 changed files with 254 additions and 166 deletions.
2 changes: 1 addition & 1 deletion cms-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>4.7.0</version>
<version>4.8.0</version>
</parent>
<artifactId>cms-api</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-filesystem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>4.7.0</version>
<version>4.8.0</version>
</parent>
<artifactId>cms-filesystem</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-git/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>4.7.0</version>
<version>4.8.0</version>
</parent>
<artifactId>cms-git</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-media/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>4.7.0</version>
<version>4.8.0</version>
</parent>
<artifactId>cms-media</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-sandbox/cms-markdown/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-sandbox</artifactId>
<version>4.7.0</version>
<version>4.8.0</version>
</parent>
<artifactId>cms-markdown</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-sandbox/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>4.7.0</version>
<version>4.8.0</version>
</parent>
<artifactId>cms-sandbox</artifactId>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>4.7.0</version>
<version>4.8.0</version>
</parent>
<artifactId>cms-server</artifactId>
<packaging>jar</packaging>
Expand Down
12 changes: 5 additions & 7 deletions cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.github.thmarx.cms.server.jetty.modules.SiteHandlerModule;
import com.github.thmarx.cms.server.jetty.modules.SiteModule;
import com.github.thmarx.cms.server.jetty.modules.ThemeModule;
import com.github.thmarx.cms.utils.SiteUtils;
import com.github.thmarx.modules.api.ModuleManager;
import com.google.inject.Guice;
import com.google.inject.Injector;
Expand Down Expand Up @@ -149,12 +150,9 @@ public void init(Path modulesPath) throws IOException {
}

protected List<String> getActiveModules() {
List<String> activeModules = new ArrayList<>();
activeModules.addAll(injector.getInstance(SiteProperties.class).activeModules());
var theme = injector.getInstance(Theme.class);
if (!theme.empty()) {
activeModules.addAll(theme.properties().activeModules());
}
return activeModules;
return SiteUtils.getActiveModules(
injector.getInstance(SiteProperties.class),
injector.getInstance(Theme.class)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.github.thmarx.cms.server.jetty.filter.RequestLoggingFilter;
import com.github.thmarx.cms.server.jetty.filter.RequestContextFilter;
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.configuration.Configuration;
import com.github.thmarx.cms.api.eventbus.EventBus;
Expand All @@ -37,7 +39,6 @@
import com.github.thmarx.cms.server.jetty.handler.JettyRoutesHandler;
import com.github.thmarx.cms.server.jetty.handler.JettyTaxonomyHandler;
import com.github.thmarx.cms.server.jetty.handler.JettyViewHandler;
import com.github.thmarx.modules.api.ModuleManager;
import com.google.inject.Key;
import com.google.inject.name.Names;
import java.nio.file.Path;
Expand All @@ -63,18 +64,18 @@ public JettyVHost(Path hostBase, Configuration configuration, final ScheduledExe
}

public Handler httpHandler() {

var contentHandler = injector.getInstance(JettyContentHandler.class);
var taxonomyHandler = injector.getInstance(JettyTaxonomyHandler.class);
var viewHandler = injector.getInstance(JettyViewHandler.class);
var routeHandler = injector.getInstance(JettyRouteHandler.class);
var routesHandler = injector.getInstance(JettyRoutesHandler.class);

var defaultHandlerSequence = new Handler.Sequence(
routeHandler,
routeHandler,
routesHandler,
viewHandler,
taxonomyHandler,
viewHandler,
taxonomyHandler,
contentHandler
);

Expand All @@ -87,52 +88,45 @@ public Handler httpHandler() {
faviconHandler.setBaseResource(new FileFolderPathResource(assetBase.resolve("favicon.ico")));

PathMappingsHandler pathMappingsHandler = new PathMappingsHandler();
pathMappingsHandler.addMapping(PathSpec.from("/"), defaultHandlerSequence);
pathMappingsHandler.addMapping(
PathSpec.from("/"),
new RequestContextFilter(defaultHandlerSequence, injector.getInstance(RequestContextFactory.class))
);
pathMappingsHandler.addMapping(PathSpec.from("/assets/*"), assetsHandler);
pathMappingsHandler.addMapping(PathSpec.from("/favicon.ico"), faviconHandler);

var assetsMediaManager = this.injector.getInstance(Key.get(MediaManager.class, Names.named("site")));
injector.getInstance(EventBus.class).register(SitePropertiesChanged.class, assetsMediaManager);
final JettyMediaHandler mediaHandler = this.injector.getInstance(Key.get(JettyMediaHandler.class, Names.named("site")));

pathMappingsHandler.addMapping(PathSpec.from("/media/*"), mediaHandler);

ContextHandler defaultContextHandler = new ContextHandler(
pathMappingsHandler,
injector.getInstance(SiteProperties.class).contextPath()
pathMappingsHandler.addMapping(
PathSpec.from("/" + JettyModuleMappingHandler.PATH + "/*"),
new RequestContextFilter(injector.getInstance(JettyModuleMappingHandler.class), injector.getInstance(RequestContextFactory.class))
);

var moduleHandler = new JettyModuleMappingHandler(
injector.getInstance(ModuleManager.class),
getActiveModules(),
injector.getInstance(RequestContextFactory.class)
pathMappingsHandler.addMapping(
PathSpec.from("/" + JettyExtensionHandler.PATH + "/*"),
new RequestContextFilter(injector.getInstance(JettyExtensionHandler.class), injector.getInstance(RequestContextFactory.class))
);
ContextHandler moduleContextHandler = new ContextHandler(moduleHandler, appendContextIfNeeded("/module"));

var extensionHandler = new JettyExtensionHandler(
injector.getInstance(RequestContextFactory.class)
ContextHandler defaultContextHandler = new ContextHandler(
pathMappingsHandler,
injector.getInstance(SiteProperties.class).contextPath()
);
ContextHandler extensionContextHandler = new ContextHandler(extensionHandler, appendContextIfNeeded("/extension"));


defaultContextHandler.setVirtualHosts(injector.getInstance(SiteProperties.class).hostnames());
moduleContextHandler.setVirtualHosts(injector.getInstance(SiteProperties.class).hostnames());
extensionContextHandler.setVirtualHosts(injector.getInstance(SiteProperties.class).hostnames());


ContextHandlerCollection contextCollection = new ContextHandlerCollection(
defaultContextHandler,
moduleContextHandler,
extensionContextHandler
defaultContextHandler
);


if (!injector.getInstance(Theme.class).empty()) {
var themeContextHandler = themeContextHandler();
themeContextHandler.setVirtualHosts(injector.getInstance(SiteProperties.class).hostnames());
contextCollection.addHandler(themeContextHandler);
}

JettyLoggingFilterHandler logContextHandler = new JettyLoggingFilterHandler(contextCollection, injector.getInstance(SiteProperties.class));
RequestLoggingFilter logContextHandler = new RequestLoggingFilter(contextCollection, injector.getInstance(SiteProperties.class));

GzipHandler gzipHandler = new GzipHandler(logContextHandler);
gzipHandler.setMinGzipSize(1024);
Expand All @@ -143,14 +137,14 @@ public Handler httpHandler() {

return gzipHandler;
}
private String appendContextIfNeeded (final String path) {

private String appendContextIfNeeded(final String path) {
var contextPath = injector.getInstance(SiteProperties.class).contextPath();

if ("/".equals(contextPath)) {
return path;
}

return contextPath + path;
}

Expand All @@ -164,6 +158,6 @@ private ContextHandler themeContextHandler() {
pathMappingsHandler.addMapping(PathSpec.from("/assets/*"), assetsHandler);
pathMappingsHandler.addMapping(PathSpec.from("/media/*"), mediaHandler);

return new ContextHandler(pathMappingsHandler, appendContextIfNeeded("/themes/" + injector.getInstance(Theme.class).getName()));
return new ContextHandler(pathMappingsHandler, appendContextIfNeeded("/theme"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.github.thmarx.cms.server.jetty.filter;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 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.request.ThreadLocalRequestContext;
import com.github.thmarx.cms.request.RequestContextFactory;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.Callback;

/**
*
* @author t.marx
*/
@Slf4j
public class RequestContextFilter extends Handler.Wrapper {

private final RequestContextFactory requestContextFactory;

public static final String REQUEST_CONTEXT = "_requestContext";

public RequestContextFilter(final Handler handler, final RequestContextFactory requestContextFactory) {
super(handler);
this.requestContextFactory = requestContextFactory;
}

@Override
public boolean handle(Request rqst, Response rspns, Callback clbck) throws Exception {
try (
var requestContext = requestContextFactory.create(rqst)) {

ThreadLocalRequestContext.REQUEST_CONTEXT.set(requestContext);

rqst.setAttribute(REQUEST_CONTEXT, requestContext);

return super.handle(rqst, rspns, clbck);

} finally {
ThreadLocalRequestContext.REQUEST_CONTEXT.remove();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.thmarx.cms.server.jetty;
package com.github.thmarx.cms.server.jetty.filter;

/*-
* #%L
Expand Down Expand Up @@ -35,11 +35,11 @@
* @author t.marx
*/
@Slf4j
public class JettyLoggingFilterHandler extends Handler.Wrapper {
public class RequestLoggingFilter extends Handler.Wrapper {

private final SiteProperties siteProperties;

public JettyLoggingFilterHandler (final Handler handler, final SiteProperties siteProperties) {
public RequestLoggingFilter (final Handler handler, final SiteProperties siteProperties) {
super(handler);
this.siteProperties = siteProperties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
*/
import com.github.thmarx.cms.content.ContentResolver;
import com.github.thmarx.cms.api.content.ContentResponse;
import com.github.thmarx.cms.api.request.ThreadLocalRequestContext;
import com.github.thmarx.cms.api.request.RequestContext;
import com.github.thmarx.cms.request.RequestContextFactory;
import com.github.thmarx.cms.api.utils.HTTPUtil;
import com.github.thmarx.cms.api.utils.RequestUtil;
import com.github.thmarx.cms.server.jetty.filter.RequestContextFilter;
import com.google.inject.Inject;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -41,22 +42,22 @@
*
* @author t.marx
*/
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
@RequiredArgsConstructor(onConstructor = @__({
@Inject}))
@Slf4j
public class JettyContentHandler extends Handler.Abstract {

private final ContentResolver contentResolver;
private final RequestContextFactory requestContextFactory;

@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception {
// var uri = request.getHttpURI().getPath();
var uri = RequestUtil.getContentPath(request);
var queryParameters = HTTPUtil.queryParameters(request.getHttpURI().getQuery());
try (
var requestContext = requestContextFactory.create(request)) {

ThreadLocalRequestContext.REQUEST_CONTEXT.set(requestContext);

var requestContext = (RequestContext) request.getAttribute(RequestContextFilter.REQUEST_CONTEXT);

try {
Optional<ContentResponse> content = contentResolver.getContent(requestContext);
response.setStatus(200);

Expand All @@ -73,7 +74,7 @@ public boolean handle(Request request, Response response, Callback callback) thr
}

}

var contentResponse = content.get();
if (contentResponse.isRedirect()) {
response.getHeaders().add("Location", contentResponse.node().getRedirectLocation());
Expand All @@ -83,14 +84,12 @@ public boolean handle(Request request, Response response, Callback callback) thr
response.getHeaders().add("Content-Type", "%s; charset=utf-8".formatted(content.get().contentType()));
Content.Sink.write(response, true, content.get().content(), callback);
}

} catch (Exception e) {
log.error("", e);
response.setStatus(500);
response.getHeaders().add("Content-Type", "text/html; charset=utf-8");
callback.succeeded();
} finally {
ThreadLocalRequestContext.REQUEST_CONTEXT.remove();
}
return true;
}
Expand Down
Loading

0 comments on commit 86f3774

Please sign in to comment.