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(); |
9d3717
|
92 |
finish(out); |
U |
93 |
} |
|
94 |
|
|
95 |
public void finish(OutputStream out) throws IOException { |
bc820e
|
96 |
out.flush(); |
U |
97 |
out.close(); |
|
98 |
} |
|
99 |
|
069fd4
|
100 |
/** |
U |
101 |
* Die Header erzeugen, die unabhängig davon, ob der ganze |
|
102 |
* Inhalt oder nur Teile davon ausgeliefert werden sollen, in der |
|
103 |
* Antwort stehen sollen |
|
104 |
* |
|
105 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
106 |
* Anfertigen und Senden der Antwort |
|
107 |
* @param file die Datei, für die die Header gelten |
|
108 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
109 |
*/ |
786e8c
|
110 |
public void setHeaders(HttpExchange e, File file) throws IOException { |
069fd4
|
111 |
Headers resHeaders = e.getResponseHeaders(); |
U |
112 |
resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES); |
|
113 |
String mimeType = Files.probeContentType(file.toPath()); |
|
114 |
if (mimeType != null) { |
|
115 |
resHeaders.add(CONTENT_TYPE, mimeType); |
|
116 |
} |
|
117 |
SimpleDateFormat sdf = new SimpleDateFormat(LM_PATTERN); |
|
118 |
Date date = new Date(file.lastModified()); |
|
119 |
resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date)); |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Eine nicht gefunden Antwort senden |
|
124 |
* |
|
125 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
126 |
* Anfertigen und Senden der Antwort |
|
127 |
* @param fname Name der Datei, die nicht gefunden wurde |
|
128 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
129 |
*/ |
786e8c
|
130 |
public void sendNotFound(HttpExchange e, String fname) throws IOException { |
5e0e7a
|
131 |
antwortSenden(e, SC_NOT_FOUND, fname + STR_NOT_FOUND); |
069fd4
|
132 |
} |
U |
133 |
|
|
134 |
public void antwortSenden(HttpExchange exchange, int code, String antwort) throws IOException { |
|
135 |
byte[] bytes = antwort.getBytes(); |
|
136 |
exchange.sendResponseHeaders(code, bytes.length); |
|
137 |
OutputStream os = exchange.getResponseBody(); |
|
138 |
os.write(bytes); |
9d3717
|
139 |
finish(os); |
069fd4
|
140 |
} |
U |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
} |