Persoenliche Mediazentrale
ulrich
2021-04-10 005d7a23659592b32d2dce25ae0b08850f29a241
commit | author | age
86bbf7 1 /*
b56bb3 2   Mediazentrale - Personal Media Center
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/>.
86bbf7 17  */
U 18 package de.uhilger.mediaz.api;
19
8d7d35 20 import com.sun.net.httpserver.HttpExchange;
U 21 import com.sun.net.httpserver.HttpHandler;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.util.logging.Logger;
28
86bbf7 29
U 30 /**
31  *
b56bb3 32  * @author Ulrich Hilger
U 33  * @version 1, 8.4.2021
86bbf7 34  */
8d7d35 35 public abstract class AbstractHandler extends JsonHelper implements HttpHandler {
86bbf7 36   
8d7d35 37   private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName());
U 38
39   /** Name der HTTP Methode GET */
40   public static final String HTTP_GET = "GET";
41   
42   /** Name der HTTP Methode PUT */
43   public static final String HTTP_PUT = "PUT";
44   
45   /** Name der HTTP Methode POST */
46   public static final String HTTP_POST = "POST";
47   
48   /** Name der HTTP Methode DELETE */
49   public static final String HTTP_DELETE = "DELETE";
50   
0e9cd3 51   public static final int RTC_OK = 200;
U 52   public static final int RTC_NOT_FOUND = 404;
53   
54   protected int returnCode;
55
56   public AbstractHandler() {
57     this.returnCode = RTC_OK;
58   }
59   
8d7d35 60   @Override
U 61   public void handle(HttpExchange e) throws IOException {
62     String method = e.getRequestMethod();
63     String response = "";
64     switch(method) {
65       case HTTP_GET:
66         String json = get(e);
67         if(json != null) {
68           response = json;
69         } else {
70           response = "nicht gefunden";
0e9cd3 71           returnCode = RTC_NOT_FOUND;
8d7d35 72         }
U 73         break;
74         
75       case HTTP_PUT:
76         response = put(e);
77         break;
78         
79       case HTTP_POST:
80         response = post(e);
81         break;
82         
83       case HTTP_DELETE:
84         boolean geloescht = delete(e);
85         if(geloescht) {
86           response = "geloescht";
87         } else {
88           response = "nicht geloescht";
89         }
90         break;
91     }
0e9cd3 92     e.sendResponseHeaders(getReturnCode(), response.length());
8d7d35 93     OutputStream os = e.getResponseBody();
U 94     os.write(response.getBytes());
95     os.close();        
0e9cd3 96   }
U 97   
98   protected void setReturnCode(int code) {
99     this.returnCode = code;
100   }
101   
102   protected int getReturnCode() {
103     return returnCode;
86bbf7 104   }
U 105   
8d7d35 106   protected String bodyLesen(HttpExchange e) throws IOException {
U 107     InputStream is = e.getRequestBody();
108     BufferedReader r = new BufferedReader(new InputStreamReader(is));
86bbf7 109     StringBuilder sb = new StringBuilder();
8d7d35 110     String line = r.readLine();
U 111     while(line != null) {
112       sb.append(line);
113       line = r.readLine();
114     }
115     r.close();
116     String json = sb.toString();
117     return json;
86bbf7 118   }
8d7d35 119
0e9cd3 120   protected String put(HttpExchange e) throws IOException {
U 121     setReturnCode(RTC_NOT_FOUND);
122     return "nicht unterstuetzt";
123   }
124
125   protected String post(HttpExchange e) {
126     setReturnCode(RTC_NOT_FOUND);
127     return "nicht unterstuetzt";
128   }
129
130   protected boolean delete(HttpExchange e) {
131     setReturnCode(RTC_NOT_FOUND);
132     return false;
133   }
134   
8d7d35 135   
U 136   protected abstract String get(HttpExchange e);
0e9cd3 137   /*
8d7d35 138   protected abstract String put(HttpExchange e) throws IOException;
U 139   protected abstract String post(HttpExchange e);
140   protected abstract boolean delete(HttpExchange e);
0e9cd3 141   */
86bbf7 142   
U 143 }