Ein minimalistischer HTTP-Server
ulrich
2021-03-27 ee6a3e9057bff6e30e6deff43100bcf2bf9bfba5
commit | author | age
9c7249 1 /*
678b07 2   mini-server - Ein minimalistischer HTTP-Server
U 3   Copyright (C) 2021  Ulrich Hilger
9c7249 4
678b07 5   This program is free software: you can redistribute it and/or modify
U 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.
9c7249 9
678b07 10   This program is distributed in the hope that it will be useful,
U 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.
9c7249 14
678b07 15   You should have received a copy of the GNU Affero General Public License
U 16   along with this program.  If not, see <https://www.gnu.org/licenses/>.
ee6a3e 17  */
9c7249 18 package de.uhilger.minsrv.handler;
U 19
20 import com.sun.net.httpserver.HttpExchange;
21 import com.sun.net.httpserver.HttpHandler;
22 import de.uhilger.minsrv.App;
23 import java.io.IOException;
24 import java.io.OutputStream;
ee6a3e 25 import java.util.Timer;
U 26 import java.util.TimerTask;
9c7249 27 import java.util.logging.Logger;
U 28
29 /**
d0bb21 30  * Ein HTTP-Handler zum Stoppen der Anwendung
ee6a3e 31  *
8abbcf 32  * @author Ulrich Hilger
9c7249 33  */
U 34 public class StopServerHandler implements HttpHandler {
35
36   @Override
ee6a3e 37   public void handle(HttpExchange e) throws IOException {
U 38     Logger.getLogger(StopServerHandler.class.getName()).info(e.getRequestURI().toString());
9c7249 39     String response = "Server stopped";
ee6a3e 40     e.sendResponseHeaders(200, response.length());
U 41     OutputStream os = e.getResponseBody();
9c7249 42     os.write(response.getBytes());
U 43     os.flush();
44     os.close();
ee6a3e 45     Logger.getLogger(StopServerHandler.class.getName()).info("stopping server.");
U 46     e.getHttpContext().getServer().stop(1);
47     Timer timer = new Timer();
48     timer.schedule(new AppStopper(), 2000);
9c7249 49   }
ee6a3e 50
U 51   class AppStopper extends TimerTask {
52
53     @Override
54     public void run() {
55       Logger.getLogger(StopServerHandler.class.getName()).info("Mini-Server beendet.");
56       App.stop();
57     }
58   }
59
9c7249 60 }