Persoenliche Mediazentrale
ulrich
2021-04-08 8e2578a22a4cf2749d6d884e231335287069f490
commit | author | age
86bbf7 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.mediaz.api;
7
8d7d35 8 import com.sun.net.httpserver.HttpExchange;
U 9 import com.sun.net.httpserver.HttpHandler;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.io.OutputStream;
15 import java.util.logging.Logger;
16
86bbf7 17
U 18 /**
19  *
20  * @author ulrich
21  */
8d7d35 22 public abstract class AbstractHandler extends JsonHelper implements HttpHandler {
86bbf7 23   
8d7d35 24   private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName());
U 25
26   /** Name der HTTP Methode GET */
27   public static final String HTTP_GET = "GET";
28   
29   /** Name der HTTP Methode PUT */
30   public static final String HTTP_PUT = "PUT";
31   
32   /** Name der HTTP Methode POST */
33   public static final String HTTP_POST = "POST";
34   
35   /** Name der HTTP Methode DELETE */
36   public static final String HTTP_DELETE = "DELETE";
37   
38   @Override
39   public void handle(HttpExchange e) throws IOException {
40     String method = e.getRequestMethod();
41     String response = "";
42     int code = 200;
43     switch(method) {
44       case HTTP_GET:
45         String json = get(e);
46         if(json != null) {
47           response = json;
48         } else {
49           response = "nicht gefunden";
50           code = 404;
51         }
52         break;
53         
54       case HTTP_PUT:
55         response = put(e);
56         break;
57         
58       case HTTP_POST:
59         response = post(e);
60         code = 404;
61         break;
62         
63       case HTTP_DELETE:
64         boolean geloescht = delete(e);
65         if(geloescht) {
66           response = "geloescht";
67         } else {
68           response = "nicht geloescht";
69         }
70         break;
71     }
72     e.sendResponseHeaders(code, response.length());
73     OutputStream os = e.getResponseBody();
74     os.write(response.getBytes());
75     os.close();        
86bbf7 76   }
U 77   
8d7d35 78   protected String bodyLesen(HttpExchange e) throws IOException {
U 79     InputStream is = e.getRequestBody();
80     BufferedReader r = new BufferedReader(new InputStreamReader(is));
86bbf7 81     StringBuilder sb = new StringBuilder();
8d7d35 82     String line = r.readLine();
U 83     while(line != null) {
84       sb.append(line);
85       line = r.readLine();
86     }
87     r.close();
88     String json = sb.toString();
89     return json;
86bbf7 90   }
8d7d35 91
U 92   
93   protected abstract String get(HttpExchange e);
94   protected abstract String put(HttpExchange e) throws IOException;
95   protected abstract String post(HttpExchange e);
96   protected abstract boolean delete(HttpExchange e);
86bbf7 97   
U 98 }