| | |
| | | /* Der Logger fuer diesen FileHandler */ |
| | | private static final Logger logger = Logger.getLogger(FileHandler.class.getName()); |
| | | |
| | | /* Header Namen */ |
| | | /* Headernamen */ |
| | | public static final String RANGE_HEADER = "Range"; |
| | | public static final String CONTENT_RANGE_HEADER = "Content-Range"; |
| | | public static final String ACCEPT_RANGES_HEADER = "Accept-Ranges"; |
| | | public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified"; |
| | | public static final String CONTENT_TYPE = "Content-Type"; |
| | | |
| | | /* Status Codes */ |
| | | /* Statuscodes */ |
| | | public static final int SC_OK = 200; |
| | | public static final int SC_PARTIAL_CONTENT = 206; |
| | | public static final int SC_NOT_FOUND = 404; |
| | | |
| | | /* HTTP Methoden */ |
| | | public static final String HTTP_GET = "GET"; |
| | | |
| | | /* String Konstanten */ |
| | | public static final String STR_BYTES = "bytes"; |
| | |
| | | public static final String WELCOME_FILE = "index.html"; |
| | | |
| | | /* Ablageort fuer Webinhalte */ |
| | | private final String basePath; |
| | | private final String fileBase; |
| | | |
| | | /** |
| | | * Ein neues Objekt der Klasse FileHandler erzeugen |
| | | * |
| | | * @param basePath der Pfad zu Inhalten, die von diesem Handler ausgeliefert |
| | | * werden |
| | | * @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 basePath) { |
| | | this.basePath = basePath; |
| | | public FileHandler(String absoluteDirectoryPathAndName) { |
| | | this.fileBase = absoluteDirectoryPathAndName; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void handle(HttpExchange e) throws IOException { |
| | | String ctxPath = e.getHttpContext().getPath(); |
| | | String uriPath = e.getRequestURI().getPath(); |
| | | logger.info(uriPath); |
| | | String fName = uriPath.substring(ctxPath.length()); |
| | | String fName = getFileName(e); |
| | | if (fName.startsWith(STR_DOT)) { |
| | | sendNotFound(e, fName); |
| | | } else { |
| | | Headers headers = e.getRequestHeaders(); |
| | | if (headers.containsKey(RANGE_HEADER)) { |
| | | serveFileParts(e, new File(basePath, fName)); |
| | | serveFileParts(e, new File(fileBase, fName)); |
| | | } else { |
| | | if (fName.endsWith(Server.STR_SLASH)) { |
| | | fName += WELCOME_FILE; |
| | | } |
| | | serveFile(e, new File(basePath, fName)); |
| | | serveFile(e, new File(fileBase, fName)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | protected String getFileName(HttpExchange e) { |
| | | String ctxPath = e.getHttpContext().getPath(); |
| | | String uriPath = e.getRequestURI().getPath(); |
| | | logger.info(uriPath); |
| | | return uriPath.substring(ctxPath.length()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param file die Datei, deren Inhalt ausgeliefert werden soll |
| | | * @throws IOException falls etwas schief geht entsteht dieser Fehler |
| | | */ |
| | | private void serveFile(HttpExchange e, File file) throws IOException { |
| | | protected void serveFile(HttpExchange e, File file) throws IOException { |
| | | if (file.exists()) { |
| | | OutputStream os = e.getResponseBody(); |
| | | Headers headers = e.getResponseHeaders(); |
| | | setCommonHeaders(headers, file); |
| | | setHeaders(e, file); |
| | | e.sendResponseHeaders(SC_OK, file.length()); |
| | | if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) { |
| | | InputStream in = new FileInputStream(file); |
| | | int b = in.read(); |
| | | while (b > -1) { |
| | |
| | | in.close(); |
| | | os.flush(); |
| | | os.close(); |
| | | } |
| | | } else { |
| | | sendNotFound(e, file.getName()); |
| | | } |
| | |
| | | */ |
| | | /* |
| | | */ |
| | | private void serveFileParts(HttpExchange e, File file) throws IOException { |
| | | protected void serveFileParts(HttpExchange e, File file) throws IOException { |
| | | if (file.exists()) { |
| | | InputStream is = new FileInputStream(file); |
| | | OutputStream os = e.getResponseBody(); |
| | | Headers resHeaders = e.getResponseHeaders(); |
| | | setCommonHeaders(resHeaders, file); |
| | | setHeaders(e, file); |
| | | long responseLength = 0; |
| | | long start = 0; |
| | | long end; |
| | | RangeGroup rangeGroup = parseRanges(e, file); |
| | | Iterator<Range> i = rangeGroup.getRanges(); |
| | | Headers resHeaders = e.getResponseHeaders(); |
| | | while (i.hasNext()) { |
| | | Range range = i.next(); |
| | | start = range.getStart(); |
| | |
| | | responseLength += (end - start); |
| | | } |
| | | e.sendResponseHeaders(SC_PARTIAL_CONTENT, responseLength); |
| | | if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) { |
| | | if (start > 0) { |
| | | is.skip(start); |
| | | } |
| | |
| | | os.flush(); |
| | | os.close(); |
| | | is.close(); |
| | | } |
| | | } else { |
| | | sendNotFound(e, file.getName()); |
| | | } |
| | |
| | | * @param file die Datei, deren Inhalt ausgeliefert werden soll |
| | | * @return die angefragten Byte-Ranges |
| | | */ |
| | | private RangeGroup parseRanges(HttpExchange e, File file) { |
| | | protected RangeGroup parseRanges(HttpExchange e, File file) { |
| | | RangeGroup ranges = new RangeGroup(); |
| | | String rangeHeader = e.getRequestHeaders().get(RANGE_HEADER).toString(); |
| | | |
| | |
| | | * bezeichnet wird |
| | | * @return der Inhalt des Content-Range Headers |
| | | */ |
| | | private String contentRangeHdr(Range range, File file) { |
| | | protected String contentRangeHdr(Range range, File file) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append(STR_BYTES); |
| | | sb.append(STR_BLANK); |
| | |
| | | * Inhalt oder nur Teile davon ausgeliefert werden sollen, in der |
| | | * Antwort stehen sollen |
| | | * |
| | | * @param resHeaders das Objekt, in das die Header erzeugt werden |
| | | * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
| | | * Anfertigen und Senden der Antwort |
| | | * @param file die Datei, für die die Header gelten |
| | | * @throws IOException falls etwas schief geht entsteht dieser Fehler |
| | | */ |
| | | private void setCommonHeaders(Headers resHeaders, File file) throws IOException { |
| | | protected void setHeaders(HttpExchange e, File file) throws IOException { |
| | | Headers resHeaders = e.getResponseHeaders(); |
| | | resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES); |
| | | String mimeType = Files.probeContentType(file.toPath()); |
| | | if (mimeType != null) { |
| | |
| | | * @param fname Name der Datei, die nicht gefunden wurde |
| | | * @throws IOException falls etwas schief geht entsteht dieser Fehler |
| | | */ |
| | | private void sendNotFound(HttpExchange e, String fname) throws IOException { |
| | | protected void sendNotFound(HttpExchange e, String fname) throws IOException { |
| | | OutputStream os = e.getResponseBody(); |
| | | String response = fname + STR_NOT_FOUND; |
| | | byte[] bytes = response.getBytes(StandardCharsets.UTF_8); |