Mercurial Hosting > luan
annotate src/goodjava/webserver/examples/Cookies.java @ 1771:c62324841dfb
improve NumberFieldParser and add key map lucene query
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 25 Jun 2023 15:05:09 -0600 |
parents | fa066aaa068c |
children |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 package goodjava.webserver.examples; |
1146
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 import java.io.Writer; |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 import java.io.OutputStreamWriter; |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 import java.io.IOException; |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 import java.util.Map; |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 import java.util.HashMap; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
8 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
9 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
10 import goodjava.webserver.Response; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
11 import goodjava.webserver.ResponseOutputStream; |
1146
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 public final class Cookies implements Handler { |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 public Response handle(Request request) { |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 Response response = new Response(); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 String name = (String)request.parameters.get("name"); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 if( name != null ) { |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 Map<String,String> attributes = new HashMap<String,String>(); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 String value = (String)request.parameters.get("value"); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 if( value != null ) { |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 response.setCookie(name,value,attributes); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 } else { |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 attributes.put("Max-Age","0"); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 response.setCookie(name,"delete",attributes); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 } |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 } |
1607 | 29 response.headers.put( "Content-Type", "text/plain; charset=utf-8" ); |
1146
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 try { |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 for( Map.Entry<String,String> entry : request.cookies.entrySet() ) { |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 writer.write(entry.getKey()+" = "+entry.getValue()+"\n"); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 } |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 writer.close(); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 } catch(IOException e) { |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 throw new RuntimeException(e); |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 } |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 return response; |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 } |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 |
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 } |