Skip to content

Commit

Permalink
#53 add servlet support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Nov 5, 2023
1 parent a452a99 commit b45badf
Show file tree
Hide file tree
Showing 23 changed files with 1,022 additions and 56 deletions.
5 changes: 5 additions & 0 deletions cms-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>jetty-http</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author t.marx
*/
public abstract class AbstractExtensionEndpoint implements ExtensionPoint<CMSModuleContext> {
public abstract class AbstractExtensionPoint implements ExtensionPoint<CMSModuleContext> {
@Getter
protected ModuleConfiguration moduleConfiguration;
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author t.marx
*/
@Deprecated(since = "2.5.0")
public abstract class HttpHandlerExtensionPoint extends AbstractExtensionEndpoint implements ExtensionHttpHandler {
public abstract class HttpHandlerExtensionPoint extends AbstractExtensionPoint implements ExtensionHttpHandler {

abstract public boolean handles (String method, String uri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
* #L%
*/

import org.eclipse.jetty.server.Handler;

/**
*
* @author t.marx
*/
public abstract class JettyHttpHandlerExtensionPoint extends AbstractExtensionEndpoint {
public abstract class JettyHttpHandlerExtensionPoint extends AbstractExtensionPoint {

abstract public String getContextPath();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author t.marx
*/
public abstract class MarkdownRendererProviderExtentionPoint extends AbstractExtensionEndpoint {
public abstract class MarkdownRendererProviderExtentionPoint extends AbstractExtensionPoint {

public abstract String getName ();
public abstract MarkdownRenderer getRenderer ();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.thmarx.cms.api.extensions;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 Marx-Software
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

/**
*
* @author t.marx
*/
public abstract class ServletExtensionPoint extends AbstractExtensionPoint {

public abstract ServletMapping getMapping();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.thmarx.cms.api.extensions;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 Marx-Software
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import jakarta.servlet.Servlet;
import jakarta.servlet.http.HttpServlet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.Getter;
import org.eclipse.jetty.http.pathmap.PathSpec;

/**
*
* @author thmar
*/
public class ServletMapping {

@Getter
private Map<String, HttpServlet> servletMappings;

public ServletMapping () {
servletMappings = new HashMap<>();
}

public void add (String pathSpec, HttpServlet servlet) {
servletMappings.put(pathSpec, servlet);
}

public Optional<HttpServlet> getMatchingServlet (String uri) {
return servletMappings.entrySet().stream().filter(entry -> entry.getKey().matches(uri)).map(entry -> entry.getValue()).findFirst();
}

public List<HttpServlet> getServlets () {
return new ArrayList<>(servletMappings.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author t.marx
*/
public abstract class TemplateEngineProviderExtentionPoint extends AbstractExtensionEndpoint {
public abstract class TemplateEngineProviderExtentionPoint extends AbstractExtensionPoint {

public abstract String getName ();
public abstract TemplateEngine getTemplateEngine ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @author thmar
*/
public abstract class TemplateModelExtendingExtentionPoint extends AbstractExtensionEndpoint{
public abstract class TemplateModelExtendingExtentionPoint extends AbstractExtensionPoint{

public abstract void extendModel (TemplateEngine.Model model);

Expand Down
Binary file not shown.
5 changes: 5 additions & 0 deletions cms-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlet</artifactId>
<version>12.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
65 changes: 34 additions & 31 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 @@ -74,11 +74,11 @@ public class VHost {
private final EventBus eventBus;

protected SiteProperties siteProperties;

protected ModuleManager moduleManager;

protected final ServerProperties serverProperties;

public VHost(final Path hostBase, final ServerProperties serverProperties) {
this.eventBus = new DefaultEventBus();
this.fileSystem = new FileSystem(hostBase, eventBus);
Expand All @@ -97,44 +97,47 @@ public void shutdown() {
public void init(Path modules) throws IOException {

fileSystem.init();

var props = fileSystem.resolve("site.yaml");
siteProperties = PropertiesLoader.hostProperties(props);

var classLoader = new ModuleAPIClassLoader(ClassLoader.getSystemClassLoader(),
var classLoader = new ModuleAPIClassLoader(ClassLoader.getSystemClassLoader(),
List.of(
"org.slf4j",
"org.slf4j",
"com.github.thmarx.cms",
"org.apache.logging",
"org.graalvm.polyglot",
"org.graalvm.js",
"org.eclipse.jetty"
));
this.moduleManager = ModuleManagerImpl.create(modules.toFile(),
fileSystem.resolve("modules_data").toFile(),
new CMSModuleContext(siteProperties, serverProperties, fileSystem, eventBus),
"org.eclipse.jetty",
"jakarta.servlet"
));
this.moduleManager = ModuleManagerImpl.create(modules.toFile(),
fileSystem.resolve("modules_data").toFile(),
new CMSModuleContext(siteProperties, serverProperties, fileSystem, eventBus),
classLoader
);
siteProperties.activeModules().forEach(module_id -> {
try {
log.debug("activate module {}", module_id);
moduleManager.activateModule(module_id);
} catch (IOException ex) {
log.error(null, ex);
}
});

siteProperties.activeModules().stream()
.filter(module_id -> moduleManager.getModuleIds().contains(module_id))
.forEach(module_id -> {
try {
log.debug("activate module {}", module_id);
moduleManager.activateModule(module_id);
} catch (IOException ex) {
log.error(null, ex);
}
});

moduleManager.getModuleIds().stream()
.filter(id -> !siteProperties.activeModules().contains(id))
.forEach((module_id) -> {
try {
log.debug("deactivate module {}", module_id);
moduleManager.deactivateModule(module_id);
} catch (IOException ex) {
log.error(null, ex);
}
});
try {
log.debug("deactivate module {}", module_id);
moduleManager.deactivateModule(module_id);
} catch (IOException ex) {
log.error(null, ex);
}
});

hostname = siteProperties.hostname();

contentBase = fileSystem.resolve("content/");
Expand Down Expand Up @@ -163,10 +166,10 @@ public void init(Path modules) throws IOException {

protected TemplateEngine resolveTemplateEngine() {
var engine = this.siteProperties.templateEngine();

List<TemplateEngineProviderExtentionPoint> extensions = moduleManager.extensions(TemplateEngineProviderExtentionPoint.class);
Optional<TemplateEngineProviderExtentionPoint> extOpt = extensions.stream().filter((ext) -> ext.getName().equals(engine)).findFirst();

if (extOpt.isPresent()) {
return extOpt.get().getTemplateEngine();
} else {
Expand All @@ -176,10 +179,10 @@ protected TemplateEngine resolveTemplateEngine() {

protected MarkdownRenderer resolveMarkdownRenderer(final Context context) {
var engine = this.siteProperties.markdownEngine();

List<MarkdownRendererProviderExtentionPoint> extensions = moduleManager.extensions(MarkdownRendererProviderExtentionPoint.class);
Optional<MarkdownRendererProviderExtentionPoint> extOpt = extensions.stream().filter((ext) -> ext.getName().equals(engine)).findFirst();

if (extOpt.isPresent()) {
return extOpt.get().getRenderer();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
* limitations under the License.
* #L%
*/

import com.github.thmarx.cms.api.ServerProperties;
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.extensions.JettyHttpHandlerExtensionPoint;
import com.github.thmarx.cms.api.extensions.ServletExtensionPoint;
import com.github.thmarx.cms.server.jetty.handler.JettyDefaultHandler;
import com.github.thmarx.cms.server.jetty.handler.JettyExtensionHandler;
import com.github.thmarx.cms.server.VHost;
import com.github.thmarx.cms.server.jetty.handler.JettyModuleMappingHandler;
import com.github.thmarx.cms.server.jetty.handler.JettyServletModuleMappingHandler;
import jakarta.servlet.http.HttpServlet;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.http.pathmap.PathSpec;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.ContextHandler;
Expand All @@ -45,7 +48,6 @@
@Slf4j
public class JettyVHost extends VHost {


public JettyVHost(Path hostBase, ServerProperties serverProperties) {
super(hostBase, serverProperties);
}
Expand All @@ -54,7 +56,7 @@ public Handler httpHandler() {
var defaultHandler = new JettyDefaultHandler(contentResolver, extensionManager, (context) -> {
return resolveMarkdownRenderer(context);
});

log.debug("create assets handler for {}", assetBase.toString());
ResourceHandler assetsHandler = new ResourceHandler();
assetsHandler.setDirAllowed(false);
Expand All @@ -64,40 +66,60 @@ public Handler httpHandler() {
} else {
assetsHandler.setCacheControl("max-age=" + TimeUnit.HOURS.toSeconds(24));
}

ResourceHandler faviconHandler = new ResourceHandler();
faviconHandler.setDirAllowed(false);
faviconHandler.setBaseResource(new FileFolderPathResource(assetBase.resolve("favicon.ico")));

PathMappingsHandler pathMappingsHandler = new PathMappingsHandler();
pathMappingsHandler.addMapping(PathSpec.from("/"), defaultHandler);
pathMappingsHandler.addMapping(PathSpec.from("/assets/*"), assetsHandler);
pathMappingsHandler.addMapping(PathSpec.from("/favicon.ico"), faviconHandler);
pathMappingsHandler.addMapping(PathSpec.from("/favicon.ico"), faviconHandler);

ContextHandler defaultContextHandler = new ContextHandler(pathMappingsHandler, "/");
defaultContextHandler.setVirtualHosts(List.of(siteProperties.hostname()));



var moduleHandler = new JettyModuleMappingHandler(moduleManager, siteProperties);
moduleHandler.init();
ContextHandler moduleContextHandler = new ContextHandler(moduleHandler, "/module");
var extensionHandler = new JettyExtensionHandler(extensionManager);
ContextHandler extensionContextHandler = new ContextHandler(extensionHandler, "/extension");

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



GzipHandler gzipHandler = new GzipHandler(contextCollection);
gzipHandler.setMinGzipSize(1024);
gzipHandler.addIncludedMimeTypes("text/plain");
gzipHandler.addIncludedMimeTypes("text/html");
gzipHandler.addIncludedMimeTypes("text/html");
gzipHandler.addIncludedMimeTypes("text/css");
gzipHandler.addIncludedMimeTypes("application/javascript");

return gzipHandler;
}

private ServletContextHandler servletContexteHandler() {
ServletContextHandler contextHandler = new ServletContextHandler("/servlet-modules");
//contextHandler.addServlet(new JettyServletModuleMappingHandler(moduleManager, siteProperties), "/*");

siteProperties.activeModules().forEach((var moduleid) -> {
final com.github.thmarx.modules.api.Module module = moduleManager
.module(moduleid);
if (module.provides(ServletExtensionPoint.class)) {
List<ServletExtensionPoint> extensions = module.extensions(ServletExtensionPoint.class);
extensions.forEach(ext -> {
ext.getMapping().getServletMappings().forEach((path, servlet) -> {
contextHandler.addServlet(servlet, "/%s%s".formatted(moduleid, path));
});

});
}
});

return contextHandler;
}
}
Loading

0 comments on commit b45badf

Please sign in to comment.