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.api;
21  
22  import java.nio.ByteBuffer;
23  import java.util.Map;
24  
25  /**
26   * 
27   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
28   */
29  public class DefaultHttpResponse implements HttpResponse {
30  
31      private final HttpVersion version;
32  
33      private final HttpStatus status;
34  
35      private final Map<String, String> headers;
36  
37      public DefaultHttpResponse(HttpVersion version, HttpStatus status, Map<String, String> headers) {
38          this.version = version;
39          this.status = status;
40          this.headers = headers;
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      public HttpVersion getProtocolVersion() {
48          return version;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public String getContentType() {
56          return headers.get("content-type");
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public boolean isKeepAlive() {
64          // TODO check header and version for keep alive
65          return false;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public String getHeader(String name) {
73          return headers.get(name);
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public boolean containsHeader(String name) {
81          return headers.containsKey(name);
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public Map<String, String> getHeaders() {
89          return headers;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public HttpStatus getStatus() {
97          return status;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public ByteBuffer encode(HttpPduEncodingVisitor visitor) {
105         return visitor.visit(this);
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public String toString() {
113         StringBuilder sb = new StringBuilder();
114         sb.append("HTTP RESPONSE STATUS: ").append(status).append('\n');
115         sb.append("VERSION: ").append(version).append('\n');
116 
117         sb.append("--- HEADER --- \n");
118 
119         for (String key : headers.keySet()) {
120             String value = headers.get(key);
121             sb.append(key).append(':').append(value).append('\n');
122         }
123 
124         return sb.toString();
125     }
126 }