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.util.Map;
23  
24  /**
25   * The default implementation for the HTTP response element.
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      /**
38       * Creates a new DefaultHttpResponse instance
39       * 
40       * @param version The HTTP version
41       * @param status The HTTP status
42       * @param headers The HTTP headers
43       */
44      public DefaultHttpResponse(HttpVersion version, HttpStatus status, Map<String, String> headers) {
45          this.version = version;
46          this.status = status;
47          this.headers = headers;
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public HttpVersion getProtocolVersion() {
55          return version;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public String getContentType() {
63          return headers.get("content-type");
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public boolean isKeepAlive() {
71          // TODO check header and version for keep alive
72          return false;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public String getHeader(String name) {
80          return headers.get(name);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public boolean containsHeader(String name) {
88          return headers.containsKey(name);
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public Map<String, String> getHeaders() {
96          return headers;
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public HttpStatus getStatus() {
104         return status;
105     }
106 
107     @Override
108     public String toString() {
109         StringBuilder sb = new StringBuilder();
110         sb.append("HTTP RESPONSE STATUS: " ).append(status).append('\n');
111         sb.append("VERSION: ").append(version).append('\n');
112         
113         sb.append("-- HEADER --- \n");
114         
115         for (Map.Entry<String, String> entry : headers.entrySet()) {
116             sb.append(entry.getKey()).append(':').append(entry.getValue()).append('\n');
117         }
118 
119         return sb.toString();
120     }
121 }