diff --git a/net-http-server-application-config/src/main/java/module-info.java b/net-http-server-application-config/src/main/java/module-info.java index 46df99e..1edb78c 100644 --- a/net-http-server-application-config/src/main/java/module-info.java +++ b/net-http-server-application-config/src/main/java/module-info.java @@ -1,6 +1,3 @@ -import org.xbib.net.http.server.ApplicationModule; -import org.xbib.net.http.server.application.config.ConfigApplicationModule; - module org.xbib.net.http.server.application.config { uses org.xbib.config.ConfigLogger; exports org.xbib.net.http.server.application.config; @@ -8,5 +5,4 @@ module org.xbib.net.http.server.application.config { requires org.xbib.net.http.server; requires org.xbib.config; requires java.logging; - provides ApplicationModule with ConfigApplicationModule; } diff --git a/net-http-server-application-config/src/main/java/org/xbib/net/http/server/application/config/ConfigApplicationModule.java b/net-http-server-application-config/src/main/java/org/xbib/net/http/server/application/config/ConfigApplicationModule.java index 10455d4..853f30d 100644 --- a/net-http-server-application-config/src/main/java/org/xbib/net/http/server/application/config/ConfigApplicationModule.java +++ b/net-http-server-application-config/src/main/java/org/xbib/net/http/server/application/config/ConfigApplicationModule.java @@ -30,16 +30,12 @@ public class ConfigApplicationModule extends BaseApplicationModule { private Settings settings; - public ConfigApplicationModule() { + public ConfigApplicationModule(Application application, String name, Settings settings) { + super(application, name, settings); } @Override - public String getName() { - return "config"; - } - - @Override - public void onOpen(Application application) throws Exception { + public void onOpen(Application application, Settings settings) { String profile = System.getProperty("application.profile"); if (profile == null) { profile = "developer"; diff --git a/net-http-server-application-database/src/main/java/module-info.java b/net-http-server-application-database/src/main/java/module-info.java index 9971f71..0ba609b 100644 --- a/net-http-server-application-database/src/main/java/module-info.java +++ b/net-http-server-application-database/src/main/java/module-info.java @@ -1,14 +1,11 @@ -import org.xbib.net.http.server.ApplicationModule; -import org.xbib.net.http.server.application.database.DatabaseApplicationModule; - module org.xbib.net.http.server.application.database { requires org.xbib.net; requires org.xbib.net.http; requires org.xbib.net.http.server; + requires org.xbib.config; requires org.xbib.jdbc.query; requires org.xbib.jdbc.connection.pool; requires org.xbib.datastructures.tiny; requires java.logging; requires java.sql; - provides ApplicationModule with DatabaseApplicationModule; } diff --git a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/DatabaseApplicationModule.java b/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/DatabaseApplicationModule.java index ce3de09..4033a11 100644 --- a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/DatabaseApplicationModule.java +++ b/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/DatabaseApplicationModule.java @@ -9,13 +9,14 @@ import org.xbib.net.http.server.BaseApplicationModule; import org.xbib.net.http.server.HttpRequest; import org.xbib.net.http.server.HttpServerContext; import org.xbib.net.http.server.HttpService; +import org.xbib.settings.Settings; import javax.sql.DataSource; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; -public class DatabaseApplicationModule extends BaseApplicationModule { +public class DatabaseApplicationModule extends BaseApplicationModule { private static final Logger logger = Logger.getLogger(DatabaseApplicationModule.class.getName()); @@ -23,16 +24,12 @@ public class DatabaseApplicationModule extends BaseApplicationModule { private DatabaseProvider databaseProvider; - public DatabaseApplicationModule() { + public DatabaseApplicationModule(Application application, String name, Settings settings) { + super(application, name, settings); } @Override - public String getName() { - return "database"; - } - - @Override - public void onOpen(Application application) throws Exception { + public void onOpen(Application application, Settings settings) throws Exception { this.dataSource = createDataSource(); String flavor = System.getProperty("database.flavor"); this.databaseProvider = flavor != null ? diff --git a/net-http-server-application-web/build.gradle b/net-http-server-application-web/build.gradle index e0cfffe..9429461 100644 --- a/net-http-server-application-web/build.gradle +++ b/net-http-server-application-web/build.gradle @@ -5,8 +5,6 @@ dependencies { api project(':net-http-server-application-config') api project(':net-http-template-groovy') api libs.jdbc.query - implementation libs.settings.datastructures.json - implementation libs.settings.datastructures.yaml implementation libs.webjars.bootstrap implementation libs.webjars.jquery implementation libs.webjars.fontawesome diff --git a/net-http-server-application-web/src/main/java/module-info.java b/net-http-server-application-web/src/main/java/module-info.java index 13d7064..1e104ce 100644 --- a/net-http-server-application-web/src/main/java/module-info.java +++ b/net-http-server-application-web/src/main/java/module-info.java @@ -9,7 +9,6 @@ module org.xbib.net.http.server.application.web { requires org.xbib.net.http.server.netty.secure; requires org.xbib.net.http.template.groovy; requires org.xbib.datastructures.tiny; - requires org.xbib.datastructures.json.tiny; requires org.xbib.jdbc.query; requires org.xbib.config; requires java.logging; diff --git a/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplication.java b/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplication.java index bf77c51..a3f237b 100644 --- a/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplication.java +++ b/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplication.java @@ -10,11 +10,15 @@ import org.xbib.net.http.server.session.IncomingSessionHandler; import org.xbib.net.http.server.session.OutgoingSessionHandler; import org.xbib.net.http.server.session.Session; import org.xbib.net.http.server.session.file.FileJsonSessionCodec; +import org.xbib.settings.Settings; public class WebApplication extends BaseApplication { + private final WebApplicationBuilder builder; + protected WebApplication(WebApplicationBuilder builder) { super(builder); + this.builder = builder; } public static WebApplicationBuilder builder() { diff --git a/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplicationBuilder.java b/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplicationBuilder.java index 3c1a476..16f4ae4 100644 --- a/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplicationBuilder.java +++ b/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplicationBuilder.java @@ -1,7 +1,6 @@ package org.xbib.net.http.server.application.web; import org.xbib.net.http.server.BaseApplicationBuilder; -import org.xbib.net.http.server.Application; import org.xbib.settings.Settings; @@ -11,14 +10,18 @@ public class WebApplicationBuilder extends BaseApplicationBuilder { protected String name; - protected Settings settings; - - protected WebApplicationBuilder() throws Exception { + protected WebApplicationBuilder() { super(); this.profile = System.getProperty("application.profile"); this.name = System.getProperty("application.name"); } + @Override + public WebApplicationBuilder setSettings(Settings settings) { + super.setSettings(settings); + return this; + } + public WebApplicationBuilder setProfile(String profile) { this.profile = profile; return this; @@ -29,13 +32,8 @@ public class WebApplicationBuilder extends BaseApplicationBuilder { return this; } - public WebApplicationBuilder setSettings(Settings settings) { - this.settings = settings; - return this; - } - @Override - public Application build() { + public WebApplication build() { WebApplication webApplication = new WebApplication(this); setupApplication(webApplication); return webApplication; diff --git a/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/HttpResponse.java b/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/HttpResponse.java index cc3c949..0e676b1 100644 --- a/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/HttpResponse.java +++ b/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/HttpResponse.java @@ -23,13 +23,11 @@ public class HttpResponse extends BaseHttpResponse { @Override public void close() throws IOException { - //logger.log(Level.FINE, "close"); - //builder.internalClose(); + builder.close(); } @Override public void flush() throws IOException { - //logger.log(Level.FINE, "flush"); - //builder.internalFlush(); + builder.flush(); } } diff --git a/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/HttpResponseBuilder.java b/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/HttpResponseBuilder.java index 32a1a8a..07269cf 100644 --- a/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/HttpResponseBuilder.java +++ b/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/HttpResponseBuilder.java @@ -124,12 +124,17 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder { return this; } + public void flush() { + internalBufferWrite(Unpooled.buffer(0)); + } + + public void close() { + ctx.close(); + } + @Override public HttpResponse build() { Objects.requireNonNull(ctx); - //if (shouldFlush()) { - // internalFlush(); - //} if (body != null) { internalWrite(body); } else if (charBuffer != null && charset != null) { @@ -141,19 +146,13 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder { } else if (inputStream != null) { internalWrite(inputStream, bufferSize, true); } + if (shouldFlush()) { + // really server flush? + //flush(); + } return new HttpResponse(this); } - //void internalFlush() { - // logger.log(Level.FINE, "internal flush"); - // internalBufferWrite(Unpooled.buffer(0)); - //} - - //void internalClose() { - // logger.log(Level.FINE, "internal close"); - // ctx.close(); - //} - private void internalWrite(String body) { internalWrite(dataBufferFactory.wrap(StandardCharsets.UTF_8.encode(body))); } diff --git a/net-http-server/build.gradle b/net-http-server/build.gradle index 5929d8a..e3032b3 100644 --- a/net-http-server/build.gradle +++ b/net-http-server/build.gradle @@ -1,3 +1,6 @@ dependencies { api project(':net-http') + api libs.config + implementation libs.settings.datastructures.json + implementation libs.settings.datastructures.yaml } diff --git a/net-http-server/src/main/java/module-info.java b/net-http-server/src/main/java/module-info.java index ccd3996..49fae31 100644 --- a/net-http-server/src/main/java/module-info.java +++ b/net-http-server/src/main/java/module-info.java @@ -2,6 +2,7 @@ import org.xbib.net.http.server.ApplicationModule; module org.xbib.net.http.server { uses ApplicationModule; + uses org.xbib.config.ConfigLogger; exports org.xbib.net.http.server; exports org.xbib.net.http.server.auth; exports org.xbib.net.http.server.cookie; @@ -23,6 +24,7 @@ module org.xbib.net.http.server { requires org.xbib.net.mime; requires org.xbib.net.http; requires org.xbib.datastructures.common; + requires org.xbib.config; requires java.logging; requires java.naming; requires java.sql; diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/Application.java b/net-http-server/src/main/java/org/xbib/net/http/server/Application.java index 9c8a4c1..8fa1636 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/Application.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/Application.java @@ -3,6 +3,7 @@ package org.xbib.net.http.server; import org.xbib.net.http.HttpAddress; import org.xbib.net.http.HttpResponseStatus; import org.xbib.net.http.server.session.SessionListener; +import org.xbib.settings.Settings; import java.io.Closeable; import java.io.IOException; @@ -26,6 +27,8 @@ public interface Application extends SessionListener, Resolver, Closeable String getContextPath(); + Settings getSettings(); + Collection getModules(); /** diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/ApplicationBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/ApplicationBuilder.java index 2e4d64c..91c13cf 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/ApplicationBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/ApplicationBuilder.java @@ -26,7 +26,5 @@ public interface ApplicationBuilder { ApplicationBuilder setZoneId(ZoneId zoneId); - ApplicationBuilder addModule(ApplicationModule module); - Application build(); } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/ApplicationModule.java b/net-http-server/src/main/java/org/xbib/net/http/server/ApplicationModule.java index 04322a1..ebba3bf 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/ApplicationModule.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/ApplicationModule.java @@ -1,12 +1,13 @@ package org.xbib.net.http.server; import org.xbib.net.http.server.session.Session; +import org.xbib.settings.Settings; -public interface ApplicationModule extends Comparable { +public interface ApplicationModule { String getName(); - void onOpen(Application application) throws Exception; + void onOpen(Application application, Settings settings) throws Exception; void onOpen(Application application, HttpServerContext httpServerContext); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplication.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplication.java index b79146a..4aeac31 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplication.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplication.java @@ -14,6 +14,7 @@ import org.xbib.net.http.server.session.Session; import org.xbib.net.http.server.util.BlockingThreadPoolExecutor; import org.xbib.net.http.server.validate.HttpRequestValidator; import org.xbib.net.util.NamedThreadFactory; +import org.xbib.settings.Settings; import java.io.Closeable; import java.io.IOException; @@ -55,7 +56,7 @@ public class BaseApplication implements Application { protected BaseApplication(BaseApplicationBuilder builder) { this.builder = builder; this.executor = new BlockingThreadPoolExecutor(builder.blockingThreadCount, builder.blockingQueueCount, - new NamedThreadFactory("org-xbib-net-hhtp-server-application")); + new NamedThreadFactory("org-xbib-net-http-server-application")); this.executor.setRejectedExecutionHandler((runnable, threadPoolExecutor) -> logger.log(Level.SEVERE, "rejected " + runnable + " for thread pool executor = " + threadPoolExecutor)); this.httpRequestValidator = buildRequestValidator(); @@ -86,6 +87,11 @@ public class BaseApplication implements Application { return builder.contextPath; } + @Override + public Settings getSettings() { + return builder.settings; + } + public HttpRouter getRouter() { return builder.router; } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplicationBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplicationBuilder.java index 8654ba4..2827c47 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplicationBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplicationBuilder.java @@ -1,7 +1,12 @@ package org.xbib.net.http.server; +import org.xbib.config.ConfigLoader; +import org.xbib.config.ConfigLogger; +import org.xbib.config.ConfigParams; +import org.xbib.config.SystemConfigLogger; import org.xbib.datastructures.common.ImmutableSet; import org.xbib.net.http.server.route.HttpRouter; +import org.xbib.settings.Settings; import java.nio.file.Path; import java.nio.file.Paths; @@ -9,6 +14,8 @@ import java.time.ZoneId; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Optional; import java.util.ServiceLoader; import java.util.Set; import java.util.logging.Level; @@ -18,6 +25,17 @@ public class BaseApplicationBuilder implements ApplicationBuilder { private static final Logger logger = Logger.getLogger(BaseApplicationBuilder.class.getName()); + private static final ConfigLogger bootLogger; + + static { + // early loading of boot logger during static initialization block + ServiceLoader serviceLoader = ServiceLoader.load(ConfigLogger.class); + Optional optionalBootLogger = serviceLoader.findFirst(); + bootLogger = optionalBootLogger.orElse(new SystemConfigLogger()); + } + + protected ClassLoader classLoader; + protected int blockingThreadCount; protected int blockingQueueCount; @@ -36,11 +54,18 @@ public class BaseApplicationBuilder implements ApplicationBuilder { protected ZoneId zoneId; - protected List applicationModuleList; - protected Set staticFileSuffixes; + protected ConfigParams configParams; + + protected ConfigLoader configLoader; + + protected Settings settings; + + protected List applicationModuleList; + protected BaseApplicationBuilder() { + this.classLoader = getClass().getClassLoader(); this.blockingThreadCount = Runtime.getRuntime().availableProcessors(); this.blockingQueueCount = Integer.MAX_VALUE; this.home = Paths.get(System.getProperties().containsKey("application.home") ? System.getProperty("application.home") : "."); @@ -52,6 +77,11 @@ public class BaseApplicationBuilder implements ApplicationBuilder { this.applicationModuleList = new ArrayList<>(); } + public BaseApplicationBuilder setSettings(Settings settings) { + this.settings = settings; + return this; + } + @Override public BaseApplicationBuilder setThreadCount(int blockingThreadCount) { this.blockingThreadCount = blockingThreadCount; @@ -77,19 +107,19 @@ public class BaseApplicationBuilder implements ApplicationBuilder { } @Override - public BaseApplicationBuilder setSecret(String secret) { + public ApplicationBuilder setSecret(String secret) { this.secret = secret; return this; } @Override - public BaseApplicationBuilder setSessionsEnabled(boolean sessionsEnabled) { + public ApplicationBuilder setSessionsEnabled(boolean sessionsEnabled) { this.sessionsEnabled = sessionsEnabled; return this; } @Override - public BaseApplicationBuilder setRouter(HttpRouter router) { + public ApplicationBuilder setRouter(HttpRouter router) { this.router = router; return this; } @@ -106,15 +136,6 @@ public class BaseApplicationBuilder implements ApplicationBuilder { return this; } - @Override - public ApplicationBuilder addModule(ApplicationModule module) { - if (module != null) { - applicationModuleList.add(module); - logger.log(Level.FINE, "module " + module + " added"); - } - return this; - } - public ApplicationBuilder addStaticSuffixes(String... suffixes) { ImmutableSet.Builder builder = ImmutableSet.builder(); for (String suffix : suffixes) { @@ -124,6 +145,11 @@ public class BaseApplicationBuilder implements ApplicationBuilder { return this; } + public ApplicationBuilder registerModule(ApplicationModule applicationModule) { + applicationModuleList.add(applicationModule); + return this; + } + @Override public Application build() { Application application = new BaseApplication(this); @@ -132,19 +158,41 @@ public class BaseApplicationBuilder implements ApplicationBuilder { } protected void setupApplication(Application application) { - ServiceLoader serviceLoader = ServiceLoader.load(ApplicationModule.class); - for (ApplicationModule module : serviceLoader) { - applicationModuleList.add(module); - logger.log(Level.FINE, "module " + module + " added"); + String profile = System.getProperty("application.profile"); + if (profile == null) { + profile = "developer"; } - applicationModuleList.forEach(module -> { - try { - module.onOpen(application); - logger.log(Level.FINE, "module " + module + " opened"); - } catch (Exception e) { - throw new IllegalStateException(e); + String[] args = profile.split(";"); + this.configParams = new ConfigParams() + .withArgs(args) + .withDirectoryName("application") + .withFileNamesWithoutSuffix(args[0]) + .withSystemEnvironment() + .withSystemProperties(); + this.configLoader = ConfigLoader.getInstance() + .withLogger(bootLogger); + this.settings = configLoader.load(configParams); + for (Map.Entry entry : settings.getGroups("module").entrySet()) { + String moduleName = entry.getKey(); + Settings moduleSettings = entry.getValue(); + if (moduleSettings.getAsBoolean("enabled", true)) { + try { + String className = moduleSettings.get("class"); + @SuppressWarnings("unchecked") + Class clazz = + (Class) Class.forName(className, true, classLoader); + ApplicationModule applicationModule = clazz.getConstructor(Application.class, String.class, Settings.class) + .newInstance(application, moduleName, moduleSettings); + applicationModuleList.add(applicationModule); + applicationModule.onOpen(application, moduleSettings); + } catch (Exception e) { + logger.log(Level.WARNING, e.getMessage(), e); + throw new IllegalArgumentException("class not found or not loadable: " + e.getMessage()); + } + } else { + logger.log(Level.WARNING, "disabled module: " + moduleName); } - }); + } if (router != null) { router.setApplication(application); } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplicationModule.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplicationModule.java index 4149035..4975ba1 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplicationModule.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseApplicationModule.java @@ -1,14 +1,29 @@ package org.xbib.net.http.server; import org.xbib.net.http.server.session.Session; +import org.xbib.settings.Settings; public abstract class BaseApplicationModule implements ApplicationModule { - public BaseApplicationModule() { + protected final Application application; + + protected final String name; + + protected final Settings settings; + + public BaseApplicationModule(Application application, String name, Settings settings) { + this.application = application; + this.name = name; + this.settings = settings; } @Override - public void onOpen(Application application) throws Exception { + public String getName() { + return name != null ? name : settings.get("name", "undefined"); + } + + @Override + public void onOpen(Application application, Settings settings) throws Exception { } @Override @@ -38,9 +53,4 @@ public abstract class BaseApplicationModule implements ApplicationModule { @Override public void onClose(Application application) { } - - @Override - public int compareTo(ApplicationModule o) { - return getName().compareTo(o.getName()); - } } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpResponseBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpResponseBuilder.java index 51b9d0b..158ffa6 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpResponseBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpResponseBuilder.java @@ -80,7 +80,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder { public void reset() { this.version = HttpVersion.HTTP_1_1; - this.status = null; + this.status = HttpResponseStatus.OK; this.headers = new HttpHeaders(); this.trailingHeaders = new HttpHeaders(); this.contentType = HttpHeaderValues.APPLICATION_OCTET_STREAM; @@ -302,6 +302,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder { if (httpServerConfig != null && httpServerConfig.getServerName() != null) { headers.add(HttpHeaderNames.SERVER, httpServerConfig.getServerName()); } + logger.log(Level.FINER, "headers built: " + headers); } public CharBuffer wrapHeaders() { diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/resource/AbstractResourceHandler.java b/net-http-server/src/main/java/org/xbib/net/http/server/resource/AbstractResourceHandler.java index 09f5683..2970291 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/resource/AbstractResourceHandler.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/resource/AbstractResourceHandler.java @@ -82,12 +82,14 @@ public abstract class AbstractResourceHandler implements HttpHandler { context.response() .addHeader(HttpHeaderNames.LOCATION, resource.getIndexFileName()) .setResponseStatus(HttpResponseStatus.TEMPORARY_REDIRECT) - .build().flush(); // flush is important + .build() + .flush(); // write headers } else { // send forbidden, we do not allow directory access context.response() .setResponseStatus(HttpResponseStatus.FORBIDDEN) - .build().flush(); // fluish is important + .build() + .flush(); // write status } context.done(); } else { @@ -291,7 +293,7 @@ public abstract class AbstractResourceHandler implements HttpHandler { HttpServerContext context, long offset, long size) throws IOException { if (resource instanceof HttpServerResource) { - logger.log(Level.FINE, "we have a server resource, render by resource"); + logger.log(Level.FINER, "we have a server resource, render by resource"); ((HttpServerResource) resource).render(context); return; } diff --git a/net-http-template-groovy/src/main/java/module-info.java b/net-http-template-groovy/src/main/java/module-info.java index ce1e8c3..5714436 100644 --- a/net-http-template-groovy/src/main/java/module-info.java +++ b/net-http-template-groovy/src/main/java/module-info.java @@ -1,13 +1,10 @@ -import org.xbib.net.http.server.ApplicationModule; -import org.xbib.net.http.template.groovy.GroovyTemplateApplicationModule; - module org.xbib.net.http.template.groovy { exports org.xbib.net.http.template.groovy; requires org.xbib.net; requires org.xbib.net.http; requires org.xbib.net.http.server; + requires org.xbib.config; requires org.apache.groovy; requires org.apache.groovy.templates; requires java.logging; - provides ApplicationModule with GroovyTemplateApplicationModule; } diff --git a/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/GroovyTemplateApplicationModule.java b/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/GroovyTemplateApplicationModule.java index 5a316e8..ac53bde 100644 --- a/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/GroovyTemplateApplicationModule.java +++ b/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/GroovyTemplateApplicationModule.java @@ -8,6 +8,7 @@ import org.xbib.net.http.server.HttpServerContext; import java.io.IOException; import java.io.UncheckedIOException; import org.xbib.net.http.server.HttpService; +import org.xbib.settings.Settings; public class GroovyTemplateApplicationModule extends BaseApplicationModule { @@ -15,16 +16,12 @@ public class GroovyTemplateApplicationModule extends BaseApplicationModule { private GroovyTemplateRenderer groovyTemplateRenderer; - public GroovyTemplateApplicationModule() { + public GroovyTemplateApplicationModule(Application application, String name, Settings settings) { + super(application, name, settings); } @Override - public String getName() { - return "groovy-template"; - } - - @Override - public void onOpen(Application application) { + public void onOpen(Application application, Settings settings) { this.groovyMarkupTemplateHandler = new GroovyMarkupTemplateHandler(application); this.groovyTemplateRenderer = new GroovyTemplateRenderer(); } @@ -40,7 +37,9 @@ public class GroovyTemplateApplicationModule extends BaseApplicationModule { @Override public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) { + httpServerContext.attributes().put("request", httpRequest); httpServerContext.attributes().put("params", httpRequest.getParameter().asSingleValuedMap()); + application.getModules().forEach(module -> httpServerContext.attributes().put(module.getName(), module)); } @Override diff --git a/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/GroovyTemplateResource.java b/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/GroovyTemplateResource.java index 5ad1815..5a670d9 100644 --- a/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/GroovyTemplateResource.java +++ b/net-http-template-groovy/src/main/java/org/xbib/net/http/template/groovy/GroovyTemplateResource.java @@ -98,10 +98,10 @@ public class GroovyTemplateResource extends HtmlTemplateResource { Template template = templates.get(templatePath); Logger templateLogger = Logger.getLogger("template." + getName().replace('/', '.')); Binding binding = new Binding(); - binding.setVariable("variables", binding.getVariables()); httpServerContext.attributes().forEach(binding::setVariable); binding.setVariable("logger", templateLogger); binding.setVariable("log", templateLogger); + application.getModules().forEach(m -> binding.setVariable(m.getName(), m)); DefaultTemplateResolver templateResolver = httpServerContext.attributes().get(DefaultTemplateResolver.class, "templateresolver"); if (templateResolver != null) { // handle programmatic locale change plus template making under lock so no other request/response can interrupt us