Mercurial Hosting > luan
comparison src/goodjava/webserver/examples/Cookies.java @ 1402:27efb1fcbcb5
move luan.lib to goodjava
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 17 Sep 2019 01:35:01 -0400 |
parents | src/luan/lib/webserver/examples/Cookies.java@643cf1c37723 |
children | fa066aaa068c |
comparison
equal
deleted
inserted
replaced
1401:ef1620aa99cb | 1402:27efb1fcbcb5 |
---|---|
1 package goodjava.webserver.examples; | |
2 | |
3 import java.io.Writer; | |
4 import java.io.OutputStreamWriter; | |
5 import java.io.IOException; | |
6 import java.util.Map; | |
7 import java.util.HashMap; | |
8 import goodjava.webserver.Handler; | |
9 import goodjava.webserver.Request; | |
10 import goodjava.webserver.Response; | |
11 import goodjava.webserver.ResponseOutputStream; | |
12 | |
13 | |
14 public final class Cookies implements Handler { | |
15 | |
16 public Response handle(Request request) { | |
17 Response response = new Response(); | |
18 String name = (String)request.parameters.get("name"); | |
19 if( name != null ) { | |
20 Map<String,String> attributes = new HashMap<String,String>(); | |
21 String value = (String)request.parameters.get("value"); | |
22 if( value != null ) { | |
23 response.setCookie(name,value,attributes); | |
24 } else { | |
25 attributes.put("Max-Age","0"); | |
26 response.setCookie(name,"delete",attributes); | |
27 } | |
28 } | |
29 response.headers.put( "content-type", "text/plain; charset=utf-8" ); | |
30 try { | |
31 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); | |
32 for( Map.Entry<String,String> entry : request.cookies.entrySet() ) { | |
33 writer.write(entry.getKey()+" = "+entry.getValue()+"\n"); | |
34 } | |
35 writer.close(); | |
36 } catch(IOException e) { | |
37 throw new RuntimeException(e); | |
38 } | |
39 return response; | |
40 } | |
41 | |
42 } |