Persoenliche Mediazentrale
ulrich
2021-04-08 8d7d357497e80b87f1d3be2357cb9cb2e853e582
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
/*
 * 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.mediaz.api;
 
import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
 
 
/**
 *
 * @author ulrich
 */
public abstract class AbstractHandler extends JsonHelper implements HttpHandler {
  
  private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName());
 
  /** Name der HTTP Methode GET */
  public static final String HTTP_GET = "GET";
  
  /** Name der HTTP Methode PUT */
  public static final String HTTP_PUT = "PUT";
  
  /** Name der HTTP Methode POST */
  public static final String HTTP_POST = "POST";
  
  /** Name der HTTP Methode DELETE */
  public static final String HTTP_DELETE = "DELETE";
  
  @Override
  public void handle(HttpExchange e) throws IOException {
    String method = e.getRequestMethod();
    String response = "";
    int code = 200;
    switch(method) {
      case HTTP_GET:
        String json = get(e);
        if(json != null) {
          response = json;
        } else {
          response = "nicht gefunden";
          code = 404;
        }
        break;
        
      case HTTP_PUT:
        response = put(e);
        break;
        
      case HTTP_POST:
        response = post(e);
        code = 404;
        break;
        
      case HTTP_DELETE:
        boolean geloescht = delete(e);
        if(geloescht) {
          response = "geloescht";
        } else {
          response = "nicht geloescht";
        }
        break;
    }
    logger.fine(response);
    e.sendResponseHeaders(code, response.length());
    OutputStream os = e.getResponseBody();
    os.write(response.getBytes());
    os.close();        
  }
  
  protected String bodyLesen(HttpExchange e) throws IOException {
    InputStream is = e.getRequestBody();
    BufferedReader r = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = r.readLine();
    while(line != null) {
      sb.append(line);
      line = r.readLine();
    }
    r.close();
    String json = sb.toString();
    logger.log(Level.FINE, "json: {0}", json);
    return json;
  }
 
  
  protected abstract String get(HttpExchange e);
  protected abstract String put(HttpExchange e) throws IOException;
  protected abstract String post(HttpExchange e);
  protected abstract boolean delete(HttpExchange e);
  
}