package handlers; import com.google.gson.Gson; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import dao.DataAccessException; import result.ClearResult; import services.ClearService; import java.io.*; import java.net.HttpURLConnection; public class ClearHandler implements HttpHandler { public void handle(HttpExchange exchange) throws IOException { try { if (exchange.getRequestMethod().equals("POST")) { Gson gson = new Gson(); ClearService service = new ClearService(); ClearResult result = service.clear(); if(!result.isSuccess()) { exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_REQUEST, 0); } else { exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0); } DefaultHandler handler = new DefaultHandler(); OutputStream resBody = exchange.getResponseBody(); String jsonString = gson.toJson(result); handler.writeString(jsonString, resBody); resBody.close(); } } catch (IOException | DataAccessException e) { exchange.sendResponseHeaders(HttpURLConnection.HTTP_SERVER_ERROR, 0); exchange.getResponseBody().close(); e.printStackTrace(); } } }