Persoenliche Mediazentrale
undisclosed
2023-01-23 03b95f100b605458e5bf2995e955882d9aa51868
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
  Tango - Personal Media Center
  Copyright (C) 2021  Ulrich Hilger
 
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.
 
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.
 
  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.uhilger.tango.api;
 
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import de.uhilger.tango.App;
import de.uhilger.tango.entity.Einstellung;
import de.uhilger.tango.entity.Entity;
import de.uhilger.tango.store.Storage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ResourceBundle;
import java.util.logging.Logger;
 
 
/**
 *
 * @author Ulrich Hilger
 * @version 1, 8.4.2021
 */
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";
  
  public static final int RTC_OK = 200;
  public static final int RTC_NOT_FOUND = 404;
  
  protected int returnCode;
  
  private ResourceBundle rb;
 
  public AbstractHandler() {
    this.returnCode = RTC_OK;
  }
  
  @Override
  public void handle(HttpExchange e) throws IOException {
    String method = e.getRequestMethod();
    String response = "";
    switch(method) {
      case HTTP_GET:
        String json = get(e);
        if(json != null) {
          response = json;
        } else {
          response = "nicht gefunden";
          //returnCode = RTC_NOT_FOUND;
        }
        break;
        
      case HTTP_PUT:
        response = put(e);
        break;
        
      case HTTP_POST:
        response = post(e);
        break;
        
      case HTTP_DELETE:
        boolean geloescht = delete(e);
        if(geloescht) {
          response = "geloescht";
        } else {
          response = "nicht geloescht";
        }
        break;
    }
    logger.fine(response);
    Headers headers = e.getResponseHeaders();
    headers.add("Content-Type", "application/json");
    e.sendResponseHeaders(getReturnCode(), response.length());
    OutputStream os = e.getResponseBody();
    os.write(response.getBytes());
    os.close();        
  }
  
  protected void setReturnCode(int code) {
    this.returnCode = code;
  }
  
  protected int getReturnCode() {
    return returnCode;
  }
  
  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();
    return json;
  }
 
  protected String put(HttpExchange e) throws IOException {
    setReturnCode(RTC_NOT_FOUND);
    return "nicht unterstuetzt";
  }
 
  protected String post(HttpExchange e) throws IOException {
    setReturnCode(RTC_NOT_FOUND);
    return "nicht unterstuetzt";
  }
 
  protected boolean delete(HttpExchange e) {
    setReturnCode(RTC_NOT_FOUND);
    return false;
  }
  
  
  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);
  */
  
  protected String getResString(String key) {
    if(rb == null) {
      rb = ResourceBundle.getBundle(App.RB_NAME);
    }
    return rb.getString(key);
  }
  
  protected String getEinstellung(Storage s, String key, String standardWert) {
    Entity entity = s.read(Einstellung.class.getSimpleName(), key);
    if (entity instanceof Einstellung) {
      Einstellung einstellung = (Einstellung) entity;
      Object o = einstellung.getValue();
      if(o instanceof String) {
        return o.toString();
      } else {
        return standardWert;
      }
    } else {
      return standardWert;
    }
  }
  
  
}