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 EchoHandler implements HttpHandler {
43     static final Path CWD = Paths.get(".");
EchoHandler()44     public EchoHandler() {}
45 
46     @Override
handle(HttpExchange t)47     public void handle(HttpExchange t)
48             throws IOException {
49         try {
50             System.err.println("EchoHandler received request to " + t.getRequestURI());
51             InputStream is = t.getRequestBody();
52             Headers map = t.getRequestHeaders();
53             Headers map1 = t.getResponseHeaders();
54             map1.add("X-Hello", "world");
55             map1.add("X-Bye", "universe");
56             String fixedrequest = map.getFirst("XFixed");
57             File outfile = Files.createTempFile(CWD, "foo", "bar").toFile();
58             FileOutputStream fos = new FileOutputStream(outfile);
59             int count = (int) is.transferTo(fos);
60             is.close();
61             fos.close();
62             InputStream is1 = new FileInputStream(outfile);
63             OutputStream os = null;
64             // return the number of bytes received (no echo)
65             String summary = map.getFirst("XSummary");
66             if (fixedrequest != null && summary == null) {
67                 t.sendResponseHeaders(200, count);
68                 os = t.getResponseBody();
69                 is1.transferTo(os);
70             } else {
71                 t.sendResponseHeaders(200, 0);
72                 os = t.getResponseBody();
73                 is1.transferTo(os);
74 
75                 if (summary != null) {
76                     String s = Integer.toString(count);
77                     os.write(s.getBytes());
78                 }
79             }
80             outfile.delete();
81             os.close();
82             is1.close();
83         } catch (Throwable e) {
84             e.printStackTrace();
85             throw new IOException(e);
86         }
87     }
88 }
89