Basisklassen zum Modul jdk.httpserver
ulrich
2021-07-03 73b5b85c1c7caf1095ae004cb8efb7b73f360099
Angabe fuer welcome files erweitert
2 files modified
46 ■■■■ changed files
src/de/uhilger/httpserver/base/HttpHelper.java 20 ●●●●● patch | view | raw | blame | history
src/de/uhilger/httpserver/base/handler/FileHandler.java 26 ●●●●● patch | view | raw | blame | history
src/de/uhilger/httpserver/base/HttpHelper.java
@@ -18,7 +18,9 @@
package de.uhilger.httpserver.base;
import com.sun.net.httpserver.HttpExchange;
import de.uhilger.httpserver.base.handler.FileHandler;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -68,5 +70,23 @@
    return sb.toString();
  }
  
  public File tryWelcomeFiles(HttpExchange e, String fName) {
    String fileBase = e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString();
    String welcomeFiles = e.getHttpContext().getAttributes().get(FileHandler.ATTR_WELCOME_FILES).toString();
    String[] fileNames = welcomeFiles.split(FileHandler.STR_COMMA);
    boolean notFound = true;
    int i = -1;
    File file = null;
    while(notFound && ++i < fileNames.length) {
      file = new File(fileBase, fName + fileNames[i]);
      if(file.exists()) {
        notFound = false;
      }
    }
    if(notFound) {
      file = new File(fileBase, fName + FileHandler.WELCOME_FILE);
    }
    return file;
  }
  
}
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;
@@ -74,6 +75,7 @@
  //protected final String fileBase;
  
  public static final String ATTR_FILE_BASE = "fileBase";
  public static final String ATTR_WELCOME_FILES = "welcomeFiles";
  /**
   * Ein neues Objekt der Klasse FileHandler erzeugen
@@ -99,7 +101,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);
@@ -109,11 +111,16 @@
        FileActor fa = new FileActor();
        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(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), 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);
      }
    }
  }
@@ -125,15 +132,12 @@
   * Anfertigen und Senden der Antwort
   * @return Name der gew&uuml;nschten Datei
   */
  /*
  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();
  }*/
  }
  */
}