Basisklassen zum Modul jdk.httpserver
ulrich
2021-06-30 4664926567a401a176938d958cb53baf7ada89e4
src/de/uhilger/httpserver/base/handler/FileHandler.java
@@ -53,6 +53,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,7 +71,9 @@
  public static final String WELCOME_FILE = "index.html";
  /* Ablageort fuer Webinhalte */
  protected final String fileBase;
  //protected final String fileBase;
  public static final String ATTR_FILE_BASE = "fileBase";
  /**
   * Ein neues Objekt der Klasse FileHandler erzeugen
@@ -79,9 +82,11 @@
   * Ordners im Dateisystem, der die Inhalte enthaelt, die von diesem 
   * Handler ausgeliefert werden sollen
   */
  /*
  public FileHandler(String absoluteDirectoryPathAndName) {
    this.fileBase = absoluteDirectoryPathAndName;
  }
  */
  /**
   * Die Datei ermitteln, die sich aus dem angefragten URL ergibt, prüfen,
@@ -102,13 +107,13 @@
      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));
        fs.serveFile(e, new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName));
      }
    }
  }
@@ -120,11 +125,15 @@
   * Anfertigen und Senden der Antwort
   * @return Name der gew&uuml;nschten Datei
   */
  protected String getFileName(HttpExchange e) {
  public String getFileName(HttpExchange e) {
    String ctxPath = e.getHttpContext().getPath();
    String uriPath = e.getRequestURI().getPath();
    logger.info(uriPath);
    return uriPath.substring(ctxPath.length());
  }
  
  /*public String getFileBase() {
    return this.e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString();
  }*/
}