EMMA Coverage Report (generated Sat Sep 03 11:42:34 KST 2005)
[all classes][org.apache.mina.examples.httpserver]

COVERAGE SUMMARY FOR SOURCE FILE [HttpProtocolHandler.java]

nameclass, %method, %block, %line, %
HttpProtocolHandler.java0%   (0/2)0%   (0/4)0%   (0/175)0%   (0/40)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class HttpProtocolHandler0%   (0/1)0%   (0/2)0%   (0/10)0%   (0/4)
HttpProtocolHandler (): void 0%   (0/1)0%   (0/3)0%   (0/2)
processStreamIo (IoSession, InputStream, OutputStream): void 0%   (0/1)0%   (0/7)0%   (0/2)
     
class HttpProtocolHandler$Worker0%   (0/1)0%   (0/2)0%   (0/165)0%   (0/36)
HttpProtocolHandler$Worker (InputStream, OutputStream): void 0%   (0/1)0%   (0/12)0%   (0/5)
run (): void 0%   (0/1)0%   (0/153)0%   (0/31)

1/*
2 *   @(#) $Id: HttpProtocolHandler.java 264677 2005-08-30 02:44:35Z 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 */
19package org.apache.mina.examples.httpserver;
20 
21import java.io.BufferedReader;
22import java.io.BufferedWriter;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.InputStreamReader;
26import java.io.OutputStream;
27import java.io.OutputStreamWriter;
28import java.io.PrintWriter;
29import java.util.Iterator;
30import java.util.Map;
31import java.util.TreeMap;
32import java.util.Map.Entry;
33 
34import org.apache.mina.io.IoSession;
35import 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: 264677 $, $Date: 2005-08-30 11:44:35 +0900 $
44 */
45public 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}

[all classes][org.apache.mina.examples.httpserver]
EMMA 2.0.4217 (C) Vladimir Roubtsov