From f5728b1d24410f076777c1a6cd409c5e65d72051 Mon Sep 17 00:00:00 2001 From: ulrich Date: Sat, 03 Jul 2021 08:27:56 +0000 Subject: [PATCH] FileHandler aufgeraeumt --- src/de/uhilger/httpserver/base/handler/FileHandler.java | 50 +++++++++++++++++--------------------------------- 1 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/de/uhilger/httpserver/base/handler/FileHandler.java b/src/de/uhilger/httpserver/base/handler/FileHandler.java index 9ab3690..b939b2f 100644 --- a/src/de/uhilger/httpserver/base/handler/FileHandler.java +++ b/src/de/uhilger/httpserver/base/handler/FileHandler.java @@ -21,6 +21,7 @@ import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; +import de.uhilger.httpserver.base.HttpHelper; import de.uhilger.httpserver.base.actor.FileActor; import java.io.File; import java.io.IOException; @@ -53,6 +54,7 @@ /* Statuscodes */ public static final int SC_OK = 200; public static final int SC_PARTIAL_CONTENT = 206; + public static final int SC_FORBIDDEN = 403; public static final int SC_NOT_FOUND = 404; public static final int SC_METHOD_NOT_ALLOWED = 405; public static final int SC_UNPROCESSABLE_ENTITY = 422; @@ -70,18 +72,10 @@ public static final String WELCOME_FILE = "index.html"; /* Ablageort fuer Webinhalte */ - protected final String fileBase; - - /** - * Ein neues Objekt der Klasse FileHandler erzeugen - * - * @param absoluteDirectoryPathAndName der absolute Pfad und Name des - * Ordners im Dateisystem, der die Inhalte enthaelt, die von diesem - * Handler ausgeliefert werden sollen - */ - public FileHandler(String absoluteDirectoryPathAndName) { - this.fileBase = absoluteDirectoryPathAndName; - } + //protected final String fileBase; + + public static final String ATTR_FILE_BASE = "fileBase"; + public static final String ATTR_WELCOME_FILES = "welcomeFiles"; /** * Die Datei ermitteln, die sich aus dem angefragten URL ergibt, prüfen, @@ -94,7 +88,7 @@ */ @Override public void handle(HttpExchange e) throws IOException { - String fName = getFileName(e); + String fName = new HttpHelper().getFileName(e); if (fName.startsWith(STR_DOT)) { HttpResponder fs = new HttpResponder(); fs.sendNotFound(e, fName); @@ -102,29 +96,19 @@ Headers headers = e.getRequestHeaders(); if (headers.containsKey(RANGE_HEADER)) { FileActor fa = new FileActor(); - fa.serveFileParts(e, new File(fileBase, fName)); + fa.serveFileParts(e, new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName)); } else { - if (fName.length() < 1 || fName.endsWith(STR_SLASH)) { - fName += WELCOME_FILE; - } HttpResponder fs = new HttpResponder(); - fs.serveFile(e, new File(fileBase, fName)); + File file = new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName); + if (fName.length() < 1 || fName.endsWith(STR_SLASH)) { + HttpHelper helper = new HttpHelper(); + File welcomeFile = helper.tryWelcomeFiles(e, fName); + if(welcomeFile != null) { + file = welcomeFile; + } + } + fs.serveFile(e, file); } } } - - /** - * Den Namen der gewünschten Datei aus der HTTP-Anfrage ermitteln - * - * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum - * Anfertigen und Senden der Antwort - * @return Name der gewünschten Datei - */ - protected String getFileName(HttpExchange e) { - String ctxPath = e.getHttpContext().getPath(); - String uriPath = e.getRequestURI().getPath(); - logger.info(uriPath); - return uriPath.substring(ctxPath.length()); - } - } -- Gitblit v1.9.3