View Javadoc

    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;
    23  import java.io.BufferedWriter;
    24  import java.io.IOException;
    25  import java.io.InputStream;
    26  import java.io.InputStreamReader;
    27  import java.io.OutputStream;
    28  import java.io.OutputStreamWriter;
    29  import java.io.PrintWriter;
    30  import java.util.Iterator;
    31  import java.util.Map;
    32  import java.util.TreeMap;
    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 (Fri, 13 Jul 2007) $
    44   */
    45  public class HttpProtocolHandler extends StreamIoHandler {
    46      protected void processStreamIo(IoSession session, InputStream in,
    47              OutputStream 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 {
    53          private final InputStream in;
    54
    55          private final OutputStream out;
    56
    57          public Worker(InputStream in, OutputStream out) {
    58              setDaemon(true);
    59              this.in = in;
    60              this.out = out;
    61          }
    62
    63          public void run() {
    64              String url;
    65              Map<String, String> headers = new TreeMap<String, String>();
    66              BufferedReader in = new BufferedReader(new InputStreamReader(
    67                      this.in));
    68              PrintWriter out = new PrintWriter(new BufferedWriter(
    69                      new OutputStreamWriter(this.out)));
    70
    71              try {
    72                  // Get request URL.
    73                  url = in.readLine().split(" ")[1];
    74
    75                  // Read header
    76                  String line;
    77                  while ((line = in.readLine()) != null && !line.equals("")) {
    78                      String[] 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 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 e) {
    109                 e.printStackTrace();
    110             } finally {
    111                 out.flush();
    112                 out.close();
    113                 try {
    114                     in.close();
    115                 } catch (IOException e) {
    116                 }
    117             }
    118         }
    119     }
    120 }