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