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.file.Files; |
|
28 |
import java.text.SimpleDateFormat; |
|
29 |
import java.util.Date; |
|
30 |
|
|
31 |
/** |
90f2d3
|
32 |
* Helfer zur Beantwortung von HTTP-Anfragen |
U |
33 |
* |
|
34 |
* @author Ulrich Hilger |
|
35 |
* @version 1, 03.06.2021 |
069fd4
|
36 |
*/ |
U |
37 |
public class HttpResponder { |
|
38 |
|
|
39 |
/* Headernamen */ |
|
40 |
public static final String ACCEPT_RANGES_HEADER = "Accept-Ranges"; |
|
41 |
public static final String CONTENT_LENGTH = "Content-Length"; |
|
42 |
public static final String CONTENT_TYPE = "Content-Type"; |
|
43 |
public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified"; |
|
44 |
|
|
45 |
/* Statuscodes */ |
|
46 |
public static final int SC_OK = 200; |
|
47 |
public static final int SC_NOT_FOUND = 404; |
|
48 |
|
|
49 |
/* HTTP Methoden */ |
|
50 |
public static final String HTTP_GET = "GET"; |
|
51 |
|
|
52 |
/* String Konstanten */ |
|
53 |
public static final String STR_BYTES = "bytes"; |
|
54 |
public static final String STR_NOT_FOUND = " not found."; |
|
55 |
public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz"; |
|
56 |
|
|
57 |
/** |
|
58 |
* Den Inhalt einer Datei ausliefern |
|
59 |
* |
|
60 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
61 |
* Anfertigen und Senden der Antwort |
|
62 |
* @param file die Datei, deren Inhalt ausgeliefert werden soll |
|
63 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
64 |
*/ |
|
65 |
public void serveFile(HttpExchange e, File file) throws IOException { |
|
66 |
if (file.exists()) { |
|
67 |
setHeaders(e, file); |
|
68 |
e.getResponseHeaders().set(CONTENT_LENGTH, Long.toString(file.length())); |
|
69 |
e.sendResponseHeaders(SC_OK, file.length()); |
|
70 |
if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) { |
|
71 |
InputStream in = new FileInputStream(file); |
bc820e
|
72 |
OutputStream os = e.getResponseBody(); |
U |
73 |
write(in, os); |
|
74 |
finish(in, os); |
069fd4
|
75 |
} |
U |
76 |
} else { |
|
77 |
sendNotFound(e, file.getName()); |
|
78 |
} |
|
79 |
} |
|
80 |
|
bc820e
|
81 |
public void write(InputStream in, OutputStream out) throws IOException { |
U |
82 |
byte[] b = new byte[4096]; |
|
83 |
int bytesRead = in.read(b); |
|
84 |
while (bytesRead > -1) { |
|
85 |
out.write(b, 0, bytesRead); |
|
86 |
bytesRead = in.read(b); |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
public void finish(InputStream in, OutputStream out) throws IOException { |
|
91 |
in.close(); |
|
92 |
out.flush(); |
|
93 |
out.close(); |
|
94 |
} |
|
95 |
|
069fd4
|
96 |
/** |
U |
97 |
* Die Header erzeugen, die unabhängig davon, ob der ganze |
|
98 |
* Inhalt oder nur Teile davon ausgeliefert werden sollen, in der |
|
99 |
* Antwort stehen sollen |
|
100 |
* |
|
101 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
102 |
* Anfertigen und Senden der Antwort |
|
103 |
* @param file die Datei, für die die Header gelten |
|
104 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
105 |
*/ |
786e8c
|
106 |
public void setHeaders(HttpExchange e, File file) throws IOException { |
069fd4
|
107 |
Headers resHeaders = e.getResponseHeaders(); |
U |
108 |
resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES); |
|
109 |
String mimeType = Files.probeContentType(file.toPath()); |
|
110 |
if (mimeType != null) { |
|
111 |
resHeaders.add(CONTENT_TYPE, mimeType); |
|
112 |
} |
|
113 |
SimpleDateFormat sdf = new SimpleDateFormat(LM_PATTERN); |
|
114 |
Date date = new Date(file.lastModified()); |
|
115 |
resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date)); |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* Eine nicht gefunden Antwort senden |
|
120 |
* |
|
121 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
122 |
* Anfertigen und Senden der Antwort |
|
123 |
* @param fname Name der Datei, die nicht gefunden wurde |
|
124 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
125 |
*/ |
786e8c
|
126 |
public void sendNotFound(HttpExchange e, String fname) throws IOException { |
5e0e7a
|
127 |
antwortSenden(e, SC_NOT_FOUND, fname + STR_NOT_FOUND); |
069fd4
|
128 |
} |
U |
129 |
|
|
130 |
public void antwortSenden(HttpExchange exchange, int code, String antwort) throws IOException { |
|
131 |
byte[] bytes = antwort.getBytes(); |
|
132 |
exchange.sendResponseHeaders(code, bytes.length); |
|
133 |
OutputStream os = exchange.getResponseBody(); |
|
134 |
os.write(bytes); |
|
135 |
os.flush(); |
|
136 |
os.close(); |
|
137 |
} |
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
} |