changeset 1828:09e90d94b7b5

add DomainFilter
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 15 Sep 2024 19:51:16 -0600
parents 50e570b598b2
children 0eb615de1f80
files src/luan/modules/http/DomainFilter.java src/luan/modules/http/LuanDomainHandler.java
diffstat 2 files changed, 35 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/http/DomainFilter.java	Sun Sep 15 19:51:16 2024 -0600
@@ -0,0 +1,33 @@
+package luan.modules.http;
+
+import goodjava.webserver.Handler;
+import goodjava.webserver.handlers.DomainHandler;
+import luan.Luan;
+import luan.LuanFunction;
+import luan.LuanException;
+import luan.LuanRuntimeException;
+
+
+public class DomainFilter implements DomainHandler.Factory {
+	private final DomainHandler domainHandler = new DomainHandler(this);
+	private final LuanFunction filter;
+	private final Handler next;
+
+	public DomainFilter(LuanFunction filter,Handler next) throws LuanException {
+		if(filter==null) throw new NullPointerException("filter required");
+		this.filter = filter;
+		this.next = next;
+	}
+
+	@Override public Handler newHandler(String domain) {
+		try {
+			Luan luan = new Luan();
+			Object rtn = filter.call(luan,domain);
+			if( !(rtn instanceof Boolean) )
+				throw new LuanException("filter must return boolean");
+			return (Boolean)rtn ? next : null;
+		} catch(LuanException e) {
+			throw new LuanRuntimeException(e);
+		}
+	}
+}
--- a/src/luan/modules/http/LuanDomainHandler.java	Sun Sep 15 10:36:46 2024 -0600
+++ b/src/luan/modules/http/LuanDomainHandler.java	Sun Sep 15 19:51:16 2024 -0600
@@ -24,17 +24,13 @@
 
 	@Override public Handler newHandler(String domain) {
 		Luan luan = newLuan(domain);
-		return luan==null ? null : new LuanHandler(luan,domain);
+		return new LuanHandler(luan,domain);
 	}
 
 	protected Luan newLuan(final String domain) {
 		try {
 			Luan luan = new Luan();
-			Object rtn = init.call(luan,domain);
-			if( !(rtn instanceof Boolean) )
-				throw new LuanException("init must return boolean");
-			if( !(Boolean)rtn )
-				return null;
+			init.call(luan,domain);
 			LuanFunction reset_luan = new LuanFunction() {
 				@Override public Object call(Luan luan,Object[] args) {
 					domainHandler.removeHandler(domain);