Mercurial Hosting > luan
annotate src/goodjava/webserver/examples/Example.java @ 1445:f6075d7a36f2
better range query
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 05 Feb 2020 18:06:26 -0700 |
parents | 27efb1fcbcb5 |
children | 219f2b937f2b |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 package goodjava.webserver.examples; |
1137 | 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; | |
1143
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
8 import org.apache.log4j.EnhancedPatternLayout; |
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
9 import org.apache.log4j.ConsoleAppender; |
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
10 import org.apache.log4j.Logger; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
11 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
12 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
13 import goodjava.webserver.Response; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
14 import goodjava.webserver.ResponseOutputStream; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
15 import goodjava.webserver.Server; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
16 import goodjava.webserver.handlers.MapHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
17 import goodjava.webserver.handlers.SafeHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
18 import goodjava.webserver.handlers.LogHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
19 import goodjava.webserver.handlers.FileHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
20 import goodjava.webserver.handlers.DirHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
21 import goodjava.webserver.handlers.ListHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
22 import goodjava.webserver.handlers.ContentTypeHandler; |
1137 | 23 |
24 | |
25 public class Example implements Handler { | |
26 | |
27 public Response handle(Request request) { | |
28 Response response = new Response(); | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
29 response.headers.put( "content-type", "text/plain; charset=utf-8" ); |
1137 | 30 try { |
31 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); | |
32 writer.write("Hello World\n"); | |
33 writer.close(); | |
34 } catch(IOException e) { | |
35 throw new RuntimeException("shouldn't happen",e); | |
36 } | |
37 return response; | |
38 } | |
39 | |
40 public static void simple() throws IOException { | |
41 Handler handler = new Example(); | |
42 new Server(8080,handler).start(); | |
43 } | |
44 | |
45 public static void fancy() throws IOException { | |
46 Map<String,Handler> map = new HashMap<String,Handler>(); | |
1141
14927c34cbb7
webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
47 map.put( "/hello", new Example() ); |
14927c34cbb7
webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
48 map.put( "/headers", new Headers() ); |
1143
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
49 map.put( "/params", new Params() ); |
1146
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
1143
diff
changeset
|
50 map.put( "/cookies", new Cookies() ); |
1168 | 51 Handler mapHandler = new MapHandler(map); |
52 FileHandler fileHandler = new FileHandler(); | |
53 Handler dirHandler = new DirHandler(fileHandler); | |
54 Handler handler = new ListHandler( mapHandler, fileHandler, dirHandler ); | |
1146
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
1143
diff
changeset
|
55 handler = new ContentTypeHandler(handler); |
1137 | 56 handler = new SafeHandler(handler); |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1137
diff
changeset
|
57 handler = new LogHandler(handler); |
1137 | 58 new Server(8080,handler).start(); |
59 } | |
60 | |
1143
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
61 public static void initLogging() { |
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
62 // Logger.getRootLogger().setLevel(Level.INFO); |
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
63 EnhancedPatternLayout layout = new EnhancedPatternLayout("%d{HH:mm:ss} %-5p %c - %m%n"); |
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
64 ConsoleAppender appender = new ConsoleAppender(layout,"System.err"); |
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
65 Logger.getRootLogger().addAppender(appender); |
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
66 } |
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
67 |
1137 | 68 public static void main(String[] args) throws Exception { |
1143
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
69 initLogging(); |
1137 | 70 fancy(); |
71 } | |
72 } |