Basisklassen zum Modul jdk.httpserver
ulrich
2021-06-11 786e8ce44c5e7011a8f4ea6c478e9a85e6ba5591
commit | author | age
069fd4 1 /*
90f2d3 2   http-base - Extensions to jdk.httpserver
U 3   Copyright (C) 2021  Ulrich Hilger
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/>.
069fd4 17  */
786e8c 18 package de.uhilger.httpserver.base;
069fd4 19
U 20 import com.sun.net.httpserver.Headers;
21 import com.sun.net.httpserver.HttpExchange;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31
32 /**
90f2d3 33  * Helfer zur Beantwortung von HTTP-Anfragen
U 34  * 
35  * @author Ulrich Hilger
36  * @version 1, 03.06.2021
069fd4 37  */
U 38 public class HttpResponder {
39   
40   /* Headernamen */
41   public static final String ACCEPT_RANGES_HEADER = "Accept-Ranges";
42   public static final String CONTENT_LENGTH = "Content-Length";
43   public static final String CONTENT_TYPE = "Content-Type";
44   public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified";
45
46   //public static final String RANGE_HEADER = "Range";
47   //public static final String CONTENT_RANGE_HEADER = "Content-Range";
48
49   /* Statuscodes */
50   public static final int SC_OK = 200;
51   public static final int SC_NOT_FOUND = 404;
52   
53   //public static final int SC_PARTIAL_CONTENT = 206;
54
55   /* HTTP Methoden */
56   public static final String HTTP_GET = "GET";
57   
58   /* String Konstanten */
59   public static final String STR_BYTES = "bytes";
60   public static final String STR_NOT_FOUND = " not found.";
61   public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
62   
63   //public static final String STR_BLANK = " ";
64   //public static final String STR_DASH = "-";
65   //public static final String STR_COMMA = ",";
66   //public static final String STR_DOT = ".";
67   //public static final String RANGE_PATTERN = "[^\\d-,]";
68   //public static final String WELCOME_FILE = "index.html";
69   
70   /**
71    * Den Inhalt einer Datei ausliefern
72    *
73    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
74    * Anfertigen und Senden der Antwort
75    * @param file die Datei, deren Inhalt ausgeliefert werden soll
76    * @throws IOException falls etwas schief geht entsteht dieser Fehler
77    */
78   public void serveFile(HttpExchange e, File file) throws IOException {
79     if (file.exists()) {
80       setHeaders(e, file);
81       e.getResponseHeaders().set(CONTENT_LENGTH, Long.toString(file.length()));
82       e.sendResponseHeaders(SC_OK, file.length());
83       if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) {
84         InputStream in = new FileInputStream(file);
85         OutputStream os = e.getResponseBody();
86         byte[] b = new byte[4096];
87         int bytesRead = in.read(b);
88         //int b = in.read();
89         while (bytesRead > -1) {
90           os.write(b, 0, bytesRead);
91           bytesRead = in.read(b);
92         }
93         in.close();
94         os.flush();
95         os.close();
96       }
97     } else {
98       sendNotFound(e, file.getName());
99     }
100   }
101
102   /**
103    * Die Header erzeugen, die unabh&auml;ngig davon, ob der ganze 
104    * Inhalt oder nur Teile davon ausgeliefert werden sollen, in der 
105    * Antwort stehen sollen 
106    * 
107    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
108    * Anfertigen und Senden der Antwort
109    * @param file  die Datei, f&uuml;r die die Header gelten
110    * @throws IOException falls etwas schief geht entsteht dieser Fehler
111    */
786e8c 112   public void setHeaders(HttpExchange e, File file) throws IOException {
069fd4 113     Headers resHeaders = e.getResponseHeaders();
U 114     resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES);
115     String mimeType = Files.probeContentType(file.toPath());
116     if (mimeType != null) {
117       resHeaders.add(CONTENT_TYPE, mimeType);
118     }
119     SimpleDateFormat sdf = new SimpleDateFormat(LM_PATTERN);
120     Date date = new Date(file.lastModified());
121     resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date));
122   }
123
124   /**
125    * Eine nicht gefunden Antwort senden
126    *
127    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
128    * Anfertigen und Senden der Antwort
129    * @param fname Name der Datei, die nicht gefunden wurde
130    * @throws IOException falls etwas schief geht entsteht dieser Fehler
131    */
786e8c 132   public void sendNotFound(HttpExchange e, String fname) throws IOException {
069fd4 133     OutputStream os = e.getResponseBody();
U 134     String response = fname + STR_NOT_FOUND;
135     byte[] bytes = response.getBytes(StandardCharsets.UTF_8);
136     e.sendResponseHeaders(SC_NOT_FOUND, bytes.length);
137     os.write(bytes);
138     os.flush();
139     os.close();
140   }  
141
142   public void antwortSenden(HttpExchange exchange, int code, String antwort) throws IOException {
143     byte[] bytes = antwort.getBytes();
144     exchange.sendResponseHeaders(code, bytes.length);
145     OutputStream os = exchange.getResponseBody();
146     os.write(bytes);
147     os.flush();
148     os.close();
149   }
150
151
152   
153 }