Basisklassen zum Modul jdk.httpserver
ulrich
2021-07-03 f9b15dc42ba641cefef8360e4bea6035d9f7e26e
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;
73b5b8 21 import de.uhilger.httpserver.base.handler.FileHandler;
069fd4 22 import java.io.BufferedReader;
73b5b8 23 import java.io.File;
069fd4 24 import java.io.IOException;
U 25 import java.io.InputStream;
26 import java.io.InputStreamReader;
f9b15d 27 import java.util.Map;
069fd4 28
U 29 /**
90f2d3 30  * Helfer fuer HTTP-Methoden
U 31  * 
069fd4 32  * @author Ulrich Hilger
U 33  * @version 1, 01.06.2021
34  */
35 public class HttpHelper {
36
37   /* HTTP Methoden */
38   public static final String HTTP_GET = "GET";
e81655 39   public static final String HTTP_POST = "POST";
U 40   public static final String HTTP_PUT = "PUT";
41   public static final String HTTP_DELETE = "DELETE";
069fd4 42   
U 43   
44   public static final String CONTENT_TYPE = "Content-Type";
45
46   public static final String CT_JSON = "application/json; charset=UTF-8";
47   public static final String CT_TEXT_HTML = "text/html";
48   
49   /**
50    * Den Namen der gew&uuml;nschten Datei aus der HTTP-Anfrage ermitteln
51    * 
52    * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
53    * Anfertigen und Senden der Antwort
54    * @return Name der gew&uuml;nschten Datei
55    */
56   public String getFileName(HttpExchange e) {
57     String ctxPath = e.getHttpContext().getPath();
58     String uriPath = e.getRequestURI().getPath();
59     return uriPath.substring(ctxPath.length());
60   }
61   
62   public String bodyLesen(HttpExchange exchange) throws IOException {
63     StringBuilder sb = new StringBuilder();
64     InputStream is = exchange.getRequestBody();
65     BufferedReader in = new BufferedReader(new InputStreamReader(is));
66     String line = in.readLine();
67     while (line != null) {
68       sb.append(line);
69       line = in.readLine();
70     }
71     return sb.toString();
72   }
73   
f9b15d 74   public String getAttrStr(Map attributes, String key, String defaultValue) {
U 75     Object value = attributes.get(key);
76     if(value instanceof String) {
77       return value.toString();
78     } else {
79       return defaultValue;
80     }
81   } 
82   
73b5b8 83   public File tryWelcomeFiles(HttpExchange e, String fName) {
U 84     boolean notFound = true;
85     File file = null;
f9b15d 86     String fileBase = e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString();
U 87     Object welcomeFiles = e.getHttpContext().getAttributes().get(FileHandler.ATTR_WELCOME_FILES);
88     if(welcomeFiles instanceof String) {
89       String[] fileNames = welcomeFiles.toString().split(FileHandler.STR_COMMA);
90       int i = -1;
91       while(notFound && ++i < fileNames.length) {
92         file = new File(fileBase, fName + fileNames[i]);
93         if(file.exists()) {
94           notFound = false;
95         }
73b5b8 96       }
U 97     }
98     if(notFound) {
99       file = new File(fileBase, fName + FileHandler.WELCOME_FILE);
100     }
101     return file;
102   }
069fd4 103   
U 104 }