View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.handler.codec.http;
    17
    18  import java.util.List;
    19  import java.util.Map;
    20  import java.util.Set;
    21
    22  import org.jboss.netty.buffer.ChannelBuffer;
    23  import org.jboss.netty.buffer.ChannelBuffers;
    24  import org.jboss.netty.util.internal.StringUtil;
    25
    26  /**
    27   * The default {@link HttpMessage} implementation.
    28   */
    29  public class DefaultHttpMessage implements HttpMessage {
    30
    31      private final HttpHeaders headers = new HttpHeaders();
    32      private HttpVersion version;
    33      private ChannelBuffer content = ChannelBuffers.EMPTY_BUFFER;
    34      private boolean chunked;
    35
    36      /**
    37       * Creates a new instance.
    38       */
    39      protected DefaultHttpMessage(final HttpVersion version) {
    40          setProtocolVersion(version);
    41      }
    42
    43      public void addHeader(final String name, final Object value) {
    44          headers.addHeader(name, value);
    45      }
    46
    47      public void setHeader(final String name, final Object value) {
    48          headers.setHeader(name, value);
    49      }
    50
    51      public void setHeader(final String name, final Iterable<?> values) {
    52          headers.setHeader(name, values);
    53      }
    54
    55      public void removeHeader(final String name) {
    56          headers.removeHeader(name);
    57      }
    58
    59      @Deprecated
    60      public long getContentLength() {
    61          return HttpHeaders.getContentLength(this);
    62      }
    63
    64      @Deprecated
    65      public long getContentLength(long defaultValue) {
    66          return HttpHeaders.getContentLength(this, defaultValue);
    67      }
    68
    69      public boolean isChunked() {
    70          if (chunked) {
    71              return true;
    72          } else {
    73              return HttpCodecUtil.isTransferEncodingChunked(this);
    74          }
    75      }
    76
    77      public void setChunked(boolean chunked) {
    78          this.chunked = chunked;
    79          if (chunked) {
    80              setContent(ChannelBuffers.EMPTY_BUFFER);
    81          }
    82      }
    83
    84      @Deprecated
    85      public boolean isKeepAlive() {
    86          return HttpHeaders.isKeepAlive(this);
    87      }
    88
    89      public void clearHeaders() {
    90          headers.clearHeaders();
    91      }
    92
    93      public void setContent(ChannelBuffer content) {
    94          if (content == null) {
    95              content = ChannelBuffers.EMPTY_BUFFER;
    96          }
    97          if (content.readable() && isChunked()) {
    98              throw new IllegalArgumentException(
    99                      "non-empty content disallowed if this.chunked == true");
    100         }
    101         this.content = content;
    102     }
    103
    104     public String getHeader(final String name) {
    105         return headers.getHeader(name);
    106     }
    107
    108     public List<String> getHeaders(final String name) {
    109         return headers.getHeaders(name);
    110     }
    111
    112     public List<Map.Entry<String, String>> getHeaders() {
    113         return headers.getHeaders();
    114     }
    115
    116     public boolean containsHeader(final String name) {
    117         return headers.containsHeader(name);
    118     }
    119
    120     public Set<String> getHeaderNames() {
    121         return headers.getHeaderNames();
    122     }
    123
    124     public HttpVersion getProtocolVersion() {
    125         return version;
    126     }
    127
    128     public void setProtocolVersion(HttpVersion version) {
    129         if (version == null) {
    130             throw new NullPointerException("version");
    131         }
    132         this.version = version;
    133     }
    134
    135     public ChannelBuffer getContent() {
    136         return content;
    137     }
    138
    139     @Override
    140     public String toString() {
    141         StringBuilder buf = new StringBuilder();
    142         buf.append(getClass().getSimpleName());
    143         buf.append("(version: ");
    144         buf.append(getProtocolVersion().getText());
    145         buf.append(", keepAlive: ");
    146         buf.append(isKeepAlive());
    147         buf.append(", chunked: ");
    148         buf.append(isChunked());
    149         buf.append(')');
    150         buf.append(StringUtil.NEWLINE);
    151         appendHeaders(buf);
    152
    153         // Remove the last newline.
    154         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
    155         return buf.toString();
    156     }
    157
    158     void appendHeaders(StringBuilder buf) {
    159         for (Map.Entry<String, String> e: getHeaders()) {
    160             buf.append(e.getKey());
    161             buf.append(": ");
    162             buf.append(e.getValue());
    163             buf.append(StringUtil.NEWLINE);
    164         }
    165     }
    166 }