1 | /* |
2 | * @(#) $Id: HttpProtocolHandler.java 210062 2005-07-11 03:52:38Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.examples.httpserver; |
20 | |
21 | import java.io.BufferedReader; |
22 | import java.io.BufferedWriter; |
23 | import java.io.IOException; |
24 | import java.io.InputStream; |
25 | import java.io.InputStreamReader; |
26 | import java.io.OutputStream; |
27 | import java.io.OutputStreamWriter; |
28 | import java.io.PrintWriter; |
29 | import java.util.Iterator; |
30 | import java.util.Map; |
31 | import java.util.TreeMap; |
32 | import java.util.Map.Entry; |
33 | |
34 | import org.apache.mina.io.IoSession; |
35 | import org.apache.mina.io.handler.StreamIoHandler; |
36 | |
37 | /** |
38 | * A simplistic HTTP protocol handler that replies back the URL and headers |
39 | * which a client requested. |
40 | * |
41 | * @author The Apache Directory Project (dev@directory.apache.org) |
42 | * @author Trustin Lee (trustin@apache.org) |
43 | * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $ |
44 | */ |
45 | public class HttpProtocolHandler extends StreamIoHandler |
46 | { |
47 | protected void processStreamIo( IoSession session, InputStream in, |
48 | OutputStream out ) |
49 | { |
50 | // You *MUST* execute stream I/O logic in a separate thread. |
51 | new Worker( in, out ).start(); |
52 | } |
53 | |
54 | private static class Worker extends Thread |
55 | { |
56 | private final InputStream in; |
57 | private final OutputStream out; |
58 | |
59 | public Worker( InputStream in, OutputStream out ) |
60 | { |
61 | setDaemon( true ); |
62 | this.in = in; |
63 | this.out = out; |
64 | } |
65 | |
66 | public void run() |
67 | { |
68 | String url; |
69 | Map headers = new TreeMap(); |
70 | BufferedReader in = new BufferedReader( |
71 | new InputStreamReader( this.in ) ); |
72 | PrintWriter out = new PrintWriter( |
73 | new BufferedWriter( new OutputStreamWriter( this.out ) ) ); |
74 | |
75 | try |
76 | { |
77 | // Get request URL. |
78 | url = in.readLine().split( " " )[1]; |
79 | |
80 | // Read header |
81 | String line; |
82 | while ( ( line = in.readLine() ) != null && !line.equals( "" ) ) |
83 | { |
84 | String[] tokens = line.split(": "); |
85 | headers.put( tokens[0], tokens[1] ); |
86 | } |
87 | |
88 | // Write header |
89 | out.println( "HTTP/1.0 200 OK" ); |
90 | out.println( "Content-Type: text/html" ); |
91 | out.println( "Server: MINA Example" ); |
92 | out.println(); |
93 | |
94 | // Write content |
95 | out.println( "<html><head></head><body>" ); |
96 | out.println( "<h3>Request Summary for: " + url + "</h3>" ); |
97 | out.println( "<table border=\"1\"><tr><th>Key</th><th>Value</th></tr>" ); |
98 | |
99 | Iterator it = headers.entrySet().iterator(); |
100 | while( it.hasNext() ) |
101 | { |
102 | Entry e = ( Entry ) it.next(); |
103 | out.println( "<tr><td>" + e.getKey() + "</td><td>" + e.getValue() + "</td></tr>" ); |
104 | } |
105 | |
106 | out.println( "</table>" ); |
107 | out.println( "</body></html>" ); |
108 | } |
109 | catch( Exception e ) |
110 | { |
111 | e.printStackTrace(); |
112 | } |
113 | finally |
114 | { |
115 | out.flush(); |
116 | out.close(); |
117 | try |
118 | { |
119 | in.close(); |
120 | } |
121 | catch( IOException e ) |
122 | { |
123 | } |
124 | } |
125 | } |
126 | } |
127 | } |