Basisklassen zum Modul jdk.httpserver
ulrich
2021-06-24 bf550a92498fc5f253ffa0daefa03022d0a39724
commit | author | age
069fd4 1 /*
90f2d3 2   http-base - Extensions to jdk.httpserver
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/>.
069fd4 17  */
786e8c 18 package de.uhilger.httpserver.base;
069fd4 19
U 20 import com.sun.net.httpserver.HttpExchange;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25
26 /**
90f2d3 27  * Helfer fuer HTTP-Methoden
U 28  * 
069fd4 29  * @author Ulrich Hilger
U 30  * @version 1, 01.06.2021
31  */
32 public class HttpHelper {
33
34   /* HTTP Methoden */
35   public static final String HTTP_GET = "GET";
e81655 36   public static final String HTTP_POST = "POST";
U 37   public static final String HTTP_PUT = "PUT";
38   public static final String HTTP_DELETE = "DELETE";
069fd4 39   
U 40   
41   public static final String CONTENT_TYPE = "Content-Type";
42
43   public static final String CT_JSON = "application/json; charset=UTF-8";
44   public static final String CT_TEXT_HTML = "text/html";
45   
46   /**
47    * Den Namen der gew&uuml;nschten Datei aus der HTTP-Anfrage ermitteln
48    * 
49    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
50    * Anfertigen und Senden der Antwort
51    * @return Name der gew&uuml;nschten Datei
52    */
53   public String getFileName(HttpExchange e) {
54     String ctxPath = e.getHttpContext().getPath();
55     String uriPath = e.getRequestURI().getPath();
56     return uriPath.substring(ctxPath.length());
57   }
58   
59   public String bodyLesen(HttpExchange exchange) throws IOException {
60     StringBuilder sb = new StringBuilder();
61     InputStream is = exchange.getRequestBody();
62     BufferedReader in = new BufferedReader(new InputStreamReader(is));
63     String line = in.readLine();
64     while (line != null) {
65       sb.append(line);
66       line = in.readLine();
67     }
68     return sb.toString();
69   }
70   
71   
72 }