Basisklassen zum Modul jdk.httpserver
ulrich
2021-07-03 73b5b85c1c7caf1095ae004cb8efb7b73f360099
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;
  }
  
}