EMMA Coverage Report (generated Fri Oct 21 16:16:13 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 327113 2005-10-21 06:59:15Z 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 * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +0900 $
43 */
44public 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}

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