diff src/goodjava/webserver/handlers/IndexHandler.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents src/luan/lib/webserver/handlers/IndexHandler.java@643cf1c37723
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/webserver/handlers/IndexHandler.java	Tue Sep 17 01:35:01 2019 -0400
@@ -0,0 +1,33 @@
+package goodjava.webserver.handlers;
+
+import goodjava.webserver.Handler;
+import goodjava.webserver.Request;
+import goodjava.webserver.Response;
+
+
+public final class IndexHandler implements Handler {
+	private final Handler handler;
+	private final String indexName;
+
+	public IndexHandler(Handler handler) {
+		this(handler,"index.html");
+	}
+
+	public IndexHandler(Handler handler,String indexName) {
+		this.handler = handler;
+		this.indexName = indexName;
+	}
+
+	public Response handle(Request request) {
+		if( request.path.endsWith("/") ) {
+			String path = request.path;
+			try {
+				request.path += indexName;
+				return handler.handle(request);
+			} finally {
+				request.path = path;
+			}
+		} else
+			return handler.handle(request);
+	}
+}