2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6270015
|
|
27 |
* @summary Light weight HTTP server
|
|
28 |
*/
|
|
29 |
|
|
30 |
import com.sun.net.httpserver.*;
|
|
31 |
|
|
32 |
import java.util.*;
|
|
33 |
import java.util.concurrent.*;
|
|
34 |
import java.io.*;
|
|
35 |
import java.net.*;
|
|
36 |
import java.security.*;
|
|
37 |
import javax.security.auth.callback.*;
|
|
38 |
import javax.net.ssl.*;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Test filters
|
|
42 |
*/
|
|
43 |
|
|
44 |
public class Test14 extends Test {
|
|
45 |
|
|
46 |
static final String test_input = "Hello world";
|
|
47 |
static final String test_output = "Ifmmp!xpsme";
|
|
48 |
|
|
49 |
/* an outputstream which transforms the output data
|
|
50 |
* by adding one to each byte
|
|
51 |
*/
|
|
52 |
static class OffsetOutputStream extends FilterOutputStream {
|
|
53 |
OffsetOutputStream (OutputStream os) {
|
|
54 |
super (os);
|
|
55 |
}
|
|
56 |
public void write (int b) throws IOException {
|
|
57 |
super.write (b+1);
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
static class OffsetFilter extends Filter {
|
|
62 |
public String description() {
|
|
63 |
return "Translates outgoing data";
|
|
64 |
}
|
|
65 |
|
|
66 |
public void destroy(HttpContext c) {}
|
|
67 |
public void init(HttpContext c) {}
|
|
68 |
|
|
69 |
public void doFilter (HttpExchange exchange, Filter.Chain chain)
|
|
70 |
throws IOException {
|
|
71 |
exchange.setStreams (null, new OffsetOutputStream(
|
|
72 |
exchange.getResponseBody()
|
|
73 |
));
|
|
74 |
chain.doFilter (exchange);
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
public static void main (String[] args) throws Exception {
|
|
79 |
Handler handler = new Handler();
|
|
80 |
InetSocketAddress addr = new InetSocketAddress (0);
|
|
81 |
HttpServer server = HttpServer.create (addr, 0);
|
|
82 |
HttpContext ctx = server.createContext ("/test", handler);
|
|
83 |
|
|
84 |
File logfile = new File (
|
|
85 |
System.getProperty ("test.classes")+ "/log.txt"
|
|
86 |
);
|
|
87 |
|
|
88 |
ctx.getFilters().add (new OffsetFilter());
|
|
89 |
ctx.getFilters().add (new LogFilter(logfile));
|
|
90 |
if (ctx.getFilters().size() != 2) {
|
|
91 |
throw new RuntimeException ("wrong filter list size");
|
|
92 |
}
|
|
93 |
ExecutorService executor = Executors.newCachedThreadPool();
|
|
94 |
server.setExecutor (executor);
|
|
95 |
server.start ();
|
|
96 |
|
|
97 |
URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
|
|
98 |
System.out.print ("Test14: " );
|
|
99 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
|
|
100 |
InputStream is = urlc.getInputStream();
|
|
101 |
int x = 0;
|
|
102 |
String output="";
|
|
103 |
while ((x=is.read())!= -1) {
|
|
104 |
output = output + (char)x;
|
|
105 |
}
|
|
106 |
error = !output.equals (test_output);
|
|
107 |
server.stop(2);
|
|
108 |
executor.shutdown();
|
|
109 |
if (error ) {
|
|
110 |
throw new RuntimeException ("test failed error");
|
|
111 |
}
|
|
112 |
System.out.println ("OK");
|
|
113 |
|
|
114 |
}
|
|
115 |
|
|
116 |
public static boolean error = false;
|
|
117 |
|
|
118 |
static class Handler implements HttpHandler {
|
|
119 |
int invocation = 1;
|
|
120 |
public void handle (HttpExchange t)
|
|
121 |
throws IOException
|
|
122 |
{
|
|
123 |
InputStream is = t.getRequestBody();
|
|
124 |
Headers map = t.getRequestHeaders();
|
|
125 |
Headers rmap = t.getResponseHeaders();
|
|
126 |
while (is.read () != -1) ;
|
|
127 |
is.close();
|
|
128 |
String response = test_input;
|
|
129 |
t.sendResponseHeaders (200, response.length());
|
|
130 |
OutputStream os = t.getResponseBody();
|
|
131 |
os.write (response.getBytes());
|
|
132 |
t.close();
|
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|