annotate src/goodjava/webserver/examples/Headers.java @ 1504:f443542d8650

threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 May 2020 11:13:16 -0600
parents 27efb1fcbcb5
children fa066aaa068c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
1 package goodjava.webserver.examples;
1141
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.Writer;
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.OutputStreamWriter;
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.IOException;
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.Map;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
7 import goodjava.webserver.Handler;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
8 import goodjava.webserver.Request;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
9 import goodjava.webserver.Response;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
10 import goodjava.webserver.ResponseOutputStream;
1141
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 public final class Headers implements Handler {
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public Response handle(Request request) {
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 Response response = new Response();
1165
668f29bc52ea clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents: 1148
diff changeset
17 response.headers.put( "content-type", "text/plain; charset=utf-8" );
1141
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 try {
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1141
diff changeset
20 for( Map.Entry<String,Object> entry : request.headers.entrySet() ) {
1141
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 writer.write(entry.getKey()+": "+entry.getValue()+"\n");
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 writer.close();
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 } catch(IOException e) {
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 throw new RuntimeException(e);
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 return response;
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 }
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
14927c34cbb7 webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }