| | |
| | | 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; |
| | |
| | | //protected final String fileBase; |
| | | |
| | | public static final String ATTR_FILE_BASE = "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; |
| | | } |
| | | */ |
| | | public static final String ATTR_WELCOME_FILES = "welcomeFiles"; |
| | | |
| | | /** |
| | | * Die Datei ermitteln, die sich aus dem angefragten URL ergibt, prüfen, |
| | |
| | | */ |
| | | @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); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 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 |
| | | */ |
| | | 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(); |
| | | }*/ |
| | | |
| | | } |