|
| 1 | +package de.quoss.example.httpexample; |
| 2 | + |
| 3 | +import java.io.FileWriter; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.OutputStream; |
| 7 | +import java.net.InetSocketAddress; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.logging.Level; |
| 11 | +import java.util.logging.Logger; |
| 12 | + |
| 13 | +import com.sun.net.httpserver.Headers; |
| 14 | +import com.sun.net.httpserver.HttpExchange; |
| 15 | +import com.sun.net.httpserver.HttpHandler; |
| 16 | +import com.sun.net.httpserver.HttpServer; |
| 17 | + |
| 18 | +class HttpServer2Example { |
| 19 | + |
| 20 | + private static final String CLASS_NAME = HttpServer2Example.class.getName(); |
| 21 | + |
| 22 | + private static final Logger LOGGER = Logger.getLogger(CLASS_NAME); |
| 23 | + |
| 24 | + private static final String REQUEST_METHOD_GET = "GET"; |
| 25 | + |
| 26 | + private static final String REQUEST_METHOD_POST = "POST"; |
| 27 | + |
| 28 | + private HttpServer2Example() throws Exception { |
| 29 | + super(); |
| 30 | + HttpServer httpServer = HttpServer.create(new InetSocketAddress(8080), 0); |
| 31 | + httpServer.createContext("/test", new HttpHandlerExample()); |
| 32 | + httpServer.start(); |
| 33 | + } |
| 34 | + |
| 35 | + private class HttpHandlerExample implements HttpHandler { |
| 36 | + |
| 37 | + @Override |
| 38 | + public void handle(HttpExchange httpExchange) throws IOException { |
| 39 | + String requestMethod = httpExchange.getRequestMethod(); |
| 40 | + LOGGER.log(Level.INFO, " [requestMethod={0}]", new Object[] { requestMethod }); |
| 41 | + Headers headers = httpExchange.getRequestHeaders(); |
| 42 | + LOGGER.log(Level.INFO, " [headers.entrySet()={0}]", new Object[] { headers.entrySet() }); |
| 43 | + String response; |
| 44 | + int responseCode; |
| 45 | + if (REQUEST_METHOD_GET.equals(requestMethod)) { |
| 46 | + Map<String, String> query = queryToMap(httpExchange.getRequestURI().getQuery()); |
| 47 | + LOGGER.log(Level.INFO, " [query={0}]", new Object[] { query }); |
| 48 | + response = "This is the response"; |
| 49 | + responseCode = 200; |
| 50 | + httpExchange.sendResponseHeaders(200, response.length()); |
| 51 | + OutputStream outputStream = httpExchange.getResponseBody(); |
| 52 | + outputStream.write(response.getBytes()); |
| 53 | + outputStream.close(); |
| 54 | + } else if (REQUEST_METHOD_POST.equals(requestMethod)) { |
| 55 | + Map<String, String> query = queryToMap(httpExchange.getRequestURI().getQuery()); |
| 56 | + LOGGER.log(Level.INFO, " [query={0}]", new Object[] { query }); |
| 57 | + String filename = query.get("filename"); |
| 58 | + InputStream inputStream = httpExchange.getRequestBody(); |
| 59 | + int bytesRead; |
| 60 | + byte[] b = new byte[1024]; |
| 61 | + StringBuilder stringBuilder = new StringBuilder(); |
| 62 | + while ((bytesRead = inputStream.read(b)) != -1) { |
| 63 | + stringBuilder.append(new String(b).substring(0, bytesRead)); |
| 64 | + } |
| 65 | + LOGGER.log(Level.INFO, " [stringBuilder={0}]", new Object[] { stringBuilder.toString() }); |
| 66 | + if (filename != null) { |
| 67 | + FileWriter fileWriter = new FileWriter("output\\".concat(filename)); |
| 68 | + fileWriter.write(stringBuilder.toString()); |
| 69 | + fileWriter.close(); |
| 70 | + response = String.format("File received: %s", filename); |
| 71 | + } else { |
| 72 | + response = "No file received."; |
| 73 | + } |
| 74 | + responseCode = 200; |
| 75 | + } else { |
| 76 | + LOGGER.log(Level.INFO, "not supported [requestMethod={0}]", new Object[] { requestMethod }); |
| 77 | + response = String.format("Request method not supported: %s", requestMethod); |
| 78 | + responseCode = 501; |
| 79 | + } |
| 80 | + httpExchange.sendResponseHeaders(responseCode, response.length()); |
| 81 | + OutputStream outputStream = httpExchange.getResponseBody(); |
| 82 | + outputStream.write(response.getBytes()); |
| 83 | + outputStream.close(); |
| 84 | + } |
| 85 | + |
| 86 | + private Map<String, String> queryToMap(String query) { |
| 87 | + Map<String, String> result = new HashMap<String, String>(); |
| 88 | + for (String param : query.split("&")) { |
| 89 | + String[] pair = param.split("="); |
| 90 | + if (pair.length > 1) { |
| 91 | + result.put(pair[0], pair[1]); |
| 92 | + } else { |
| 93 | + result.put(pair[0], ""); |
| 94 | + } |
| 95 | + } |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public static void main(String[] args) throws Exception { |
| 102 | + new HttpServer2Example(); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments