1 /* 2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import com.sun.net.httpserver.*; 25 import java.net.*; 26 import java.net.http.*; 27 import java.io.*; 28 import java.util.concurrent.*; 29 import javax.net.ssl.*; 30 import java.nio.file.*; 31 import java.util.HashSet; 32 import java.util.LinkedList; 33 import java.util.List; 34 import java.util.Random; 35 import jdk.testlibrary.SimpleSSLContext; 36 import static java.net.http.HttpRequest.*; 37 import static java.net.http.HttpResponse.*; 38 import java.util.logging.ConsoleHandler; 39 import java.util.logging.Level; 40 import java.util.logging.Logger; 41 42 public class HttpEchoHandler implements HttpHandler { 43 static final Path CWD = Paths.get("."); 44 HttpEchoHandler()45 public HttpEchoHandler() {} 46 47 @Override handle(HttpExchange t)48 public void handle(HttpExchange t) 49 throws IOException { 50 try { 51 System.err.println("EchoHandler received request to " + t.getRequestURI()); 52 InputStream is = t.getRequestBody(); 53 Headers map = t.getRequestHeaders(); 54 Headers map1 = t.getResponseHeaders(); 55 map1.add("X-Hello", "world"); 56 map1.add("X-Bye", "universe"); 57 String fixedrequest = map.getFirst("XFixed"); 58 File outfile = Files.createTempFile(CWD, "foo", "bar").toFile(); 59 FileOutputStream fos = new FileOutputStream(outfile); 60 int count = (int) is.transferTo(fos); 61 is.close(); 62 fos.close(); 63 InputStream is1 = new FileInputStream(outfile); 64 OutputStream os = null; 65 // return the number of bytes received (no echo) 66 String summary = map.getFirst("XSummary"); 67 if (fixedrequest != null && summary == null) { 68 t.sendResponseHeaders(200, count); 69 os = t.getResponseBody(); 70 is1.transferTo(os); 71 } else { 72 t.sendResponseHeaders(200, 0); 73 os = t.getResponseBody(); 74 is1.transferTo(os); 75 76 if (summary != null) { 77 String s = Integer.toString(count); 78 os.write(s.getBytes()); 79 } 80 } 81 outfile.delete(); 82 os.close(); 83 is1.close(); 84 } catch (Throwable e) { 85 e.printStackTrace(); 86 throw new IOException(e); 87 } 88 } 89 } 90