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.http;
    21
    22  import java.nio.ByteBuffer;
    23  import java.util.Collections;
    24  import java.util.List;
    25  import java.util.Map;
    26
    27  import org.apache.mina.http.api.HttpMethod;
    28  import org.apache.mina.http.api.HttpPduEncodingVisitor;
    29  import org.apache.mina.http.api.HttpRequest;
    30  import org.apache.mina.http.api.HttpVersion;
    31
    32  /**
    33   *
    34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    35   */
    36  public class HttpRequestImpl implements HttpRequest {
    37
    38      private final HttpVersion version;
    39
    40      private final HttpMethod method;
    41
    42      private final String requestedPath;
    43
    44      private final Map<String, String> headers;
    45
    46      public HttpRequestImpl(HttpVersion version, HttpMethod method, String requestedPath, Map<String, String> headers) {
    47          this.version = version;
    48          this.method = method;
    49          this.requestedPath = requestedPath;
    50          this.headers = Collections.unmodifiableMap(headers);
    51      }
    52
    53      /**
    54       * {@inheritDoc}
    55       */
    56      @Override
    57      public HttpVersion getProtocolVersion() {
    58          return version;
    59      }
    60
    61      /**
    62       * {@inheritDoc}
    63       */
    64      @Override
    65      public String getContentType() {
    66          return headers.get("content-type");
    67      }
    68
    69      /**
    70       * {@inheritDoc}
    71       */
    72      @Override
    73      public boolean isKeepAlive() {
    74          // TODO check headers
    75          return false;
    76      }
    77
    78      /**
    79       * {@inheritDoc}
    80       */
    81      @Override
    82      public String getHeader(String name) {
    83          return headers.get(name);
    84      }
    85
    86      /**
    87       * {@inheritDoc}
    88       */
    89      @Override
    90      public boolean containsHeader(String name) {
    91          return headers.containsKey(name);
    92      }
    93
    94      /**
    95       * {@inheritDoc}
    96       */
    97      @Override
    98      public Map<String, String> getHeaders() {
    99          return headers;
    100     }
    101
    102     /**
    103      * {@inheritDoc}
    104      */
    105     @Override
    106     public boolean containsParameter(String name) {
    107         // TODO Auto-generated method stub
    108         return false;
    109     }
    110
    111     /**
    112      * {@inheritDoc}
    113      */
    114     @Override
    115     public String getParameter(String name) {
    116         // TODO Auto-generated method stub
    117         return null;
    118     }
    119
    120     /**
    121      * {@inheritDoc}
    122      */
    123     @Override
    124     public Map<String, List<String>> getParameters() {
    125         // TODO Auto-generated method stub
    126         return null;
    127     }
    128
    129     /**
    130      * {@inheritDoc}
    131      */
    132     @Override
    133     public HttpMethod getMethod() {
    134         return method;
    135     }
    136
    137     @Override
    138     public ByteBuffer encode(HttpPduEncodingVisitor visitor) {
    139         return visitor.visit(this);
    140     }
    141
    142     /**
    143      * {@inheritDoc}
    144      */
    145     @Override
    146     public String toString() {
    147         StringBuilder sb = new StringBuilder();
    148
    149         sb.append("HTTP REQUEST METHOD: ").append(method).append('\n');
    150         sb.append("VERSION: ").append(version).append('\n');
    151         sb.append("PATH: ").append(requestedPath).append('\n');
    152
    153         sb.append("--- HEADER --- \n");
    154
    155         for (String key : headers.keySet()) {
    156             String value = headers.get(key);
    157             sb.append(key).append(':').append(value).append('\n');
    158         }
    159
    160         return sb.toString();
    161     }
    162 }