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.util.Iterator;
    23  import java.util.Map;
    24  import java.util.Map.Entry;
    25
    26  /**
    27   * A HTTP request message.
    28   *
    29   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    30   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    31   */
    32  public class HttpRequestMessage {
    33      /** Map<String, String[]> */
    34      private Map headers = null;
    35
    36      public void setHeaders(Map headers) {
    37          this.headers = headers;
    38      }
    39
    40      public Map getHeaders() {
    41          return headers;
    42      }
    43
    44      public String getContext() {
    45          String[] context = (String[]) headers.get("Context");
    46          return context == null ? "" : context[0];
    47      }
    48
    49      public String getParameter(String name) {
    50          String[] param = (String[]) headers.get("@".concat(name));
    51          return param == null ? "" : param[0];
    52      }
    53
    54      public String[] getParameters(String name) {
    55          String[] param = (String[]) headers.get("@".concat(name));
    56          return param == null ? new String[] {} : param;
    57      }
    58
    59      public String[] getHeader(String name) {
    60          return (String[]) headers.get(name);
    61      }
    62
    63      public String toString() {
    64          StringBuilder str = new StringBuilder();
    65
    66          Iterator it = headers.entrySet().iterator();
    67          while (it.hasNext()) {
    68              Entry e = (Entry) it.next();
    69              str.append(e.getKey() + " : "
    70                      + arrayToString((String[]) e.getValue(), ',') + "\n");
    71          }
    72          return str.toString();
    73      }
    74
    75      public static String arrayToString(String[] s, char sep) {
    76          if (s == null || s.length == 0)
    77              return "";
    78          StringBuffer buf = new StringBuffer();
    79          if (s != null) {
    80              for (int i = 0; i < s.length; i++) {
    81                  if (i > 0)
    82                      buf.append(sep);
    83                  buf.append(s[i]);
    84              }
    85          }
    86          return buf.toString();
    87      }
    88  }