Basisklassen zum Modul jdk.httpserver
ulrich
2021-07-03 f9b15dc42ba641cefef8360e4bea6035d9f7e26e
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   //public static final String RANGE_HEADER = "Range";
46   //public static final String CONTENT_RANGE_HEADER = "Content-Range";
47
48   /* Statuscodes */
49   public static final int SC_OK = 200;
50   public static final int SC_NOT_FOUND = 404;
51   
52   //public static final int SC_PARTIAL_CONTENT = 206;
53
54   /* HTTP Methoden */
55   public static final String HTTP_GET = "GET";
56   
57   /* String Konstanten */
58   public static final String STR_BYTES = "bytes";
59   public static final String STR_NOT_FOUND = " not found.";
60   public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
61   
62   //public static final String STR_BLANK = " ";
63   //public static final String STR_DASH = "-";
64   //public static final String STR_COMMA = ",";
65   //public static final String STR_DOT = ".";
66   //public static final String RANGE_PATTERN = "[^\\d-,]";
67   //public static final String WELCOME_FILE = "index.html";
68   
69   /**
70    * Den Inhalt einer Datei ausliefern
71    *
72    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
73    * Anfertigen und Senden der Antwort
74    * @param file die Datei, deren Inhalt ausgeliefert werden soll
75    * @throws IOException falls etwas schief geht entsteht dieser Fehler
76    */
77   public void serveFile(HttpExchange e, File file) throws IOException {
78     if (file.exists()) {
79       setHeaders(e, file);
80       e.getResponseHeaders().set(CONTENT_LENGTH, Long.toString(file.length()));
81       e.sendResponseHeaders(SC_OK, file.length());
82       if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) {
83         InputStream in = new FileInputStream(file);
bc820e 84         OutputStream os = e.getResponseBody();        
U 85         write(in, os);
86         finish(in, os);
069fd4 87       }
U 88     } else {
89       sendNotFound(e, file.getName());
90     }
91   }
92
bc820e 93   public void write(InputStream in, OutputStream out) throws IOException {
U 94     byte[] b = new byte[4096];
95     int bytesRead = in.read(b);
96     while (bytesRead > -1) {
97       out.write(b, 0, bytesRead);
98       bytesRead = in.read(b);
99     }
100   }
101   
102   public void finish(InputStream in, OutputStream out) throws IOException {
103     in.close();
104     out.flush();
105     out.close();
106   }
107   
069fd4 108   /**
U 109    * Die Header erzeugen, die unabh&auml;ngig davon, ob der ganze 
110    * Inhalt oder nur Teile davon ausgeliefert werden sollen, in der 
111    * Antwort stehen sollen 
112    * 
113    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
114    * Anfertigen und Senden der Antwort
115    * @param file  die Datei, f&uuml;r die die Header gelten
116    * @throws IOException falls etwas schief geht entsteht dieser Fehler
117    */
786e8c 118   public void setHeaders(HttpExchange e, File file) throws IOException {
069fd4 119     Headers resHeaders = e.getResponseHeaders();
U 120     resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES);
121     String mimeType = Files.probeContentType(file.toPath());
122     if (mimeType != null) {
123       resHeaders.add(CONTENT_TYPE, mimeType);
124     }
125     SimpleDateFormat sdf = new SimpleDateFormat(LM_PATTERN);
126     Date date = new Date(file.lastModified());
127     resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date));
128   }
129
130   /**
131    * Eine nicht gefunden Antwort senden
132    *
133    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
134    * Anfertigen und Senden der Antwort
135    * @param fname Name der Datei, die nicht gefunden wurde
136    * @throws IOException falls etwas schief geht entsteht dieser Fehler
137    */
786e8c 138   public void sendNotFound(HttpExchange e, String fname) throws IOException {
5e0e7a 139     antwortSenden(e, SC_NOT_FOUND, fname + STR_NOT_FOUND);
069fd4 140   }  
U 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 }