Basisklassen zum Modul jdk.httpserver
ulrich
2021-07-03 f9b15dc42ba641cefef8360e4bea6035d9f7e26e
commit | author | age
069fd4 1 /*
90f2d3 2   http-base - Extensions to jdk.httpserver
069fd4 3   Copyright (C) 2021  Ulrich Hilger
U 4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU Affero General Public License as
7   published by the Free Software Foundation, either version 3 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU Affero General Public License for more details.
14
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18 package de.uhilger.httpserver.base.handler;
19
786e8c 20 import de.uhilger.httpserver.base.HttpResponder;
069fd4 21 import com.sun.net.httpserver.Headers;
U 22 import com.sun.net.httpserver.HttpExchange;
23 import com.sun.net.httpserver.HttpHandler;
73b5b8 24 import de.uhilger.httpserver.base.HttpHelper;
786e8c 25 import de.uhilger.httpserver.base.actor.FileActor;
069fd4 26 import java.io.File;
U 27 import java.io.IOException;
28 import java.util.logging.Logger;
29
30 /**
31  * Die Klasse FileHandler dient zur Auslieferung von Dateiinhalten &uuml;ber
32  * HTTP.
33  *
34  * F&uuml;r das Streaming &uuml;ber HTTP wird die Auslieferung von Teilinhalten
35  * mit dem Accept-Ranges-Header angeboten und via Range-Header unterst&uuml;tzt.
36  * (vgl. https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests)
37  *
38  * @author Ulrich Hilger
90f2d3 39  * @version 1, 03.06.2021, (seit 25. M&auml;rz 2021)
069fd4 40  */
U 41 public class FileHandler implements HttpHandler {
42
43   /* Der Logger fuer diesen FileHandler */
44   private static final Logger logger = Logger.getLogger(FileHandler.class.getName());
45
46   /* Headernamen */
47   public static final String RANGE_HEADER = "Range";
48   public static final String CONTENT_RANGE_HEADER = "Content-Range";
49   //public static final String ACCEPT_RANGES_HEADER = "Accept-Ranges";
50   //public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified";
51   public static final String CONTENT_TYPE = "Content-Type";
52   public static final String CONTENT_LENGTH = "Content-Length";
53
54   /* Statuscodes */
55   public static final int SC_OK = 200;
56   public static final int SC_PARTIAL_CONTENT = 206;
e88b7b 57   public static final int SC_FORBIDDEN = 403;
069fd4 58   public static final int SC_NOT_FOUND = 404;
U 59   public static final int SC_METHOD_NOT_ALLOWED = 405;
60   public static final int SC_UNPROCESSABLE_ENTITY = 422;
61
62   /* String Konstanten */
63   //public static final String STR_BYTES = "bytes";
64   public static final String STR_SLASH = "/";
65   public static final String STR_BLANK = " ";
66   public static final String STR_DASH = "-";
67   public static final String STR_COMMA = ",";
68   public static final String STR_DOT = ".";
69   //public static final String STR_NOT_FOUND = " not found.";
70   //public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
71   public static final String RANGE_PATTERN = "[^\\d-,]";
72   public static final String WELCOME_FILE = "index.html";
73
74   /* Ablageort fuer Webinhalte */
a488df 75   //protected final String fileBase;
U 76   
77   public static final String ATTR_FILE_BASE = "fileBase";
73b5b8 78   public static final String ATTR_WELCOME_FILES = "welcomeFiles";
069fd4 79
U 80   /**
81    * Ein neues Objekt der Klasse FileHandler erzeugen
82    *
83    * @param absoluteDirectoryPathAndName der absolute Pfad und Name des 
84    * Ordners im Dateisystem, der die Inhalte enthaelt, die von diesem 
85    * Handler ausgeliefert werden sollen
86    */
a488df 87   /*
069fd4 88   public FileHandler(String absoluteDirectoryPathAndName) {
U 89     this.fileBase = absoluteDirectoryPathAndName;
90   }
a488df 91   */
069fd4 92
U 93   /**
94    * Die Datei ermitteln, die sich aus dem angefragten URL ergibt, pr&uuml;fen,
95    * ob die Datei existiert und den Inhalt der Datei abh&auml;ngig davon, ob ein
96    * Range-Header vorhanden ist, ganz oder teilweise ausliefern.
97    *
98    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
99    * Anfertigen und Senden der Antwort
100    * @throws IOException falls etwas schief geht entsteht dieser Fehler
101    */
102   @Override
103   public void handle(HttpExchange e) throws IOException {
73b5b8 104     String fName = new HttpHelper().getFileName(e);
069fd4 105     if (fName.startsWith(STR_DOT)) {
U 106       HttpResponder fs = new HttpResponder();
107       fs.sendNotFound(e, fName);
108     } else {
109       Headers headers = e.getRequestHeaders();
110       if (headers.containsKey(RANGE_HEADER)) {
786e8c 111         FileActor fa = new FileActor();
a488df 112         fa.serveFileParts(e, new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName));
069fd4 113       } else {
U 114         HttpResponder fs = new HttpResponder();
73b5b8 115         File file = new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName);
U 116         if (fName.length() < 1 || fName.endsWith(STR_SLASH)) {
117           HttpHelper helper = new HttpHelper();
118           File welcomeFile = helper.tryWelcomeFiles(e, fName);
119           if(welcomeFile != null) {
120             file = welcomeFile;
121           }
122         }
123         fs.serveFile(e, file);
069fd4 124       }
U 125     }
126   }
127
128   /**
129    * Den Namen der gew&uuml;nschten Datei aus der HTTP-Anfrage ermitteln
130    * 
131    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
132    * Anfertigen und Senden der Antwort
133    * @return Name der gew&uuml;nschten Datei
134    */
73b5b8 135   /*
9efb60 136   public String getFileName(HttpExchange e) {
069fd4 137     String ctxPath = e.getHttpContext().getPath();
U 138     String uriPath = e.getRequestURI().getPath();
139     logger.info(uriPath);
140     return uriPath.substring(ctxPath.length());
73b5b8 141   }  
U 142   */
069fd4 143 }