author | dfuchs |
Tue, 11 Apr 2017 16:32:38 +0100 | |
changeset 44639 | 5c2838d882a5 |
parent 42460 | 7133f144981a |
permissions | -rw-r--r-- |
37720 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, 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.*; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
26 |
import jdk.incubator.http.*; |
37720 | 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; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
36 |
import static jdk.incubator.http.HttpRequest.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
37 |
import static jdk.incubator.http.HttpResponse.*; |
37720 | 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 |
public EchoHandler() {} |
|
44 |
||
45 |
@Override |
|
46 |
public void handle(HttpExchange t) |
|
47 |
throws IOException { |
|
48 |
try { |
|
49 |
System.err.println("EchoHandler received request to " + t.getRequestURI()); |
|
50 |
InputStream is = t.getRequestBody(); |
|
51 |
Headers map = t.getRequestHeaders(); |
|
52 |
Headers map1 = t.getResponseHeaders(); |
|
53 |
map1.add("X-Hello", "world"); |
|
54 |
map1.add("X-Bye", "universe"); |
|
55 |
String fixedrequest = map.getFirst("XFixed"); |
|
56 |
File outfile = File.createTempFile("foo", "bar"); |
|
57 |
FileOutputStream fos = new FileOutputStream(outfile); |
|
58 |
int count = (int) is.transferTo(fos); |
|
59 |
is.close(); |
|
60 |
fos.close(); |
|
61 |
InputStream is1 = new FileInputStream(outfile); |
|
62 |
OutputStream os = null; |
|
63 |
// return the number of bytes received (no echo) |
|
64 |
String summary = map.getFirst("XSummary"); |
|
65 |
if (fixedrequest != null && summary == null) { |
|
66 |
t.sendResponseHeaders(200, count); |
|
67 |
os = t.getResponseBody(); |
|
68 |
is1.transferTo(os); |
|
69 |
} else { |
|
70 |
t.sendResponseHeaders(200, 0); |
|
71 |
os = t.getResponseBody(); |
|
72 |
is1.transferTo(os); |
|
73 |
||
74 |
if (summary != null) { |
|
75 |
String s = Integer.toString(count); |
|
76 |
os.write(s.getBytes()); |
|
77 |
} |
|
78 |
} |
|
79 |
outfile.delete(); |
|
80 |
os.close(); |
|
81 |
is1.close(); |
|
82 |
} catch (Throwable e) { |
|
83 |
e.printStackTrace(); |
|
84 |
throw new IOException(e); |
|
85 |
} |
|
86 |
} |
|
87 |
} |