KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > example > httpserver > stream > HttpProtocolHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. 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,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.mina.example.httpserver.stream;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.TreeMap JavaDoc;
33 import java.util.Map.Entry;
34
35 import org.apache.mina.common.IoSession;
36 import org.apache.mina.handler.StreamIoHandler;
37
38 /**
39  * A simplistic HTTP protocol handler that replies back the URL and headers
40  * which a client requested.
41  *
42  * @author The Apache Directory Project (mina-dev@directory.apache.org)
43  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $
44  */

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