| | |
| | | */ |
| | | package de.uhilger.mediaz.api; |
| | | |
| | | import com.sun.net.httpserver.Headers; |
| | | import com.sun.net.httpserver.HttpExchange; |
| | | import com.sun.net.httpserver.HttpHandler; |
| | | import java.io.BufferedReader; |
| | |
| | | /** 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; |
| | | |
| | | public AbstractHandler() { |
| | | this.returnCode = RTC_OK; |
| | | } |
| | | |
| | | @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); |
| | |
| | | response = json; |
| | | } else { |
| | | response = "nicht gefunden"; |
| | | code = 404; |
| | | //returnCode = RTC_NOT_FOUND; |
| | | } |
| | | break; |
| | | |
| | |
| | | |
| | | case HTTP_POST: |
| | | response = post(e); |
| | | code = 404; |
| | | break; |
| | | |
| | | case HTTP_DELETE: |
| | |
| | | } |
| | | break; |
| | | } |
| | | e.sendResponseHeaders(code, response.length()); |
| | | 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 { |
| | |
| | | return json; |
| | | } |
| | | |
| | | protected String put(HttpExchange e) throws IOException { |
| | | setReturnCode(RTC_NOT_FOUND); |
| | | return "nicht unterstuetzt"; |
| | | } |
| | | |
| | | protected String post(HttpExchange e) { |
| | | 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); |
| | | */ |
| | | |
| | | } |