1 | /* |
2 | * @(#) $Id: HttpProtocolHandler.java 332218 2005-11-10 03:52:42Z 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 | * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $ |
43 | */ |
44 | public class HttpProtocolHandler extends StreamIoHandler |
45 | { |
46 | protected void processStreamIo( IoSession session, InputStream in, |
47 | OutputStream out ) |
48 | { |
49 | // You *MUST* execute stream I/O logic in a separate thread. |
50 | new Worker( in, out ).start(); |
51 | } |
52 | |
53 | private static class Worker extends Thread |
54 | { |
55 | private final InputStream in; |
56 | private final OutputStream out; |
57 | |
58 | public Worker( InputStream in, OutputStream out ) |
59 | { |
60 | setDaemon( true ); |
61 | this.in = in; |
62 | this.out = out; |
63 | } |
64 | |
65 | public void run() |
66 | { |
67 | String url; |
68 | Map headers = new TreeMap(); |
69 | BufferedReader in = new BufferedReader( |
70 | new InputStreamReader( this.in ) ); |
71 | PrintWriter out = new PrintWriter( |
72 | new BufferedWriter( new OutputStreamWriter( this.out ) ) ); |
73 | |
74 | try |
75 | { |
76 | // Get request URL. |
77 | url = in.readLine().split( " " )[1]; |
78 | |
79 | // Read header |
80 | String line; |
81 | while ( ( line = in.readLine() ) != null && !line.equals( "" ) ) |
82 | { |
83 | String[] tokens = line.split(": "); |
84 | headers.put( tokens[0], tokens[1] ); |
85 | } |
86 | |
87 | // Write header |
88 | out.println( "HTTP/1.0 200 OK" ); |
89 | out.println( "Content-Type: text/html" ); |
90 | out.println( "Server: MINA Example" ); |
91 | out.println(); |
92 | |
93 | // Write content |
94 | out.println( "<html><head></head><body>" ); |
95 | out.println( "<h3>Request Summary for: " + url + "</h3>" ); |
96 | out.println( "<table border=\"1\"><tr><th>Key</th><th>Value</th></tr>" ); |
97 | |
98 | Iterator it = headers.entrySet().iterator(); |
99 | while( it.hasNext() ) |
100 | { |
101 | Entry e = ( Entry ) it.next(); |
102 | out.println( "<tr><td>" + e.getKey() + "</td><td>" + e.getValue() + "</td></tr>" ); |
103 | } |
104 | |
105 | out.println( "</table>" ); |
106 | out.println( "</body></html>" ); |
107 | } |
108 | catch( Exception e ) |
109 | { |
110 | e.printStackTrace(); |
111 | } |
112 | finally |
113 | { |
114 | out.flush(); |
115 | out.close(); |
116 | try |
117 | { |
118 | in.close(); |
119 | } |
120 | catch( IOException e ) |
121 | { |
122 | } |
123 | } |
124 | } |
125 | } |
126 | } |