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