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.codec;
    21
    22  import java.io.ByteArrayOutputStream;
    23  import java.io.IOException;
    24  import java.text.SimpleDateFormat;
    25  import java.util.Date;
    26  import java.util.HashMap;
    27  import java.util.Map;
    28
    29  import org.apache.mina.common.ByteBuffer;
    30
    31  /**
    32   * A HTTP response message.
    33   *
    34   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    35   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    36   */
    37  public class HttpResponseMessage {
    38      /** HTTP response codes */
    39      public static final int HTTP_STATUS_SUCCESS = 200;
    40
    41      public static final int HTTP_STATUS_NOT_FOUND = 404;
    42
    43      /** Map<String, String> */
    44      private Map<String, String> headers = new HashMap<String, String>();
    45
    46      /** Storage for body of HTTP response. */
    47      private ByteArrayOutputStream body = new ByteArrayOutputStream(1024);
    48
    49      private int responseCode = HTTP_STATUS_SUCCESS;
    50
    51      public HttpResponseMessage() {
    52          headers.put("Server", "HttpServer (" + Server.VERSION_STRING + ')');
    53          headers.put("Cache-Control", "private");
    54          headers.put("Content-Type", "text/html; charset=iso-8859-1");
    55          headers.put("Connection", "keep-alive");
    56          headers.put("Keep-Alive", "200");
    57          headers.put("Date", new SimpleDateFormat(
    58                  "EEE, dd MMM yyyy HH:mm:ss zzz").format(new Date()));
    59          headers.put("Last-Modified", new SimpleDateFormat(
    60                  "EEE, dd MMM yyyy HH:mm:ss zzz").format(new Date()));
    61      }
    62
    63      public Map getHeaders() {
    64          return headers;
    65      }
    66
    67      public void setContentType(String contentType) {
    68          headers.put("Content-Type", contentType);
    69      }
    70
    71      public void setResponseCode(int responseCode) {
    72          this.responseCode = responseCode;
    73      }
    74
    75      public int getResponseCode() {
    76          return this.responseCode;
    77      }
    78
    79      public void appendBody(byte[] b) {
    80          try {
    81              body.write(b);
    82          } catch (IOException ex) {
    83              ex.printStackTrace();
    84          }
    85      }
    86
    87      public void appendBody(String s) {
    88          try {
    89              body.write(s.getBytes());
    90          } catch (IOException ex) {
    91              ex.printStackTrace();
    92          }
    93      }
    94
    95      public ByteBuffer getBody() {
    96          return ByteBuffer.wrap(body.toByteArray());
    97      }
    98
    99      public int getBodyLength() {
    100         return body.size();
    101     }
    102 }