001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.mina.http.api;
021
022import java.util.Map;
023
024public class DefaultHttpResponse implements HttpResponse {
025
026    private final HttpVersion version;
027
028    private final HttpStatus status;
029
030    private final Map<String, String> headers;
031
032    public DefaultHttpResponse(HttpVersion version, HttpStatus status, Map<String, String> headers) {
033        this.version = version;
034        this.status = status;
035        this.headers = headers;
036    }
037
038    public HttpVersion getProtocolVersion() {
039        return version;
040    }
041
042    public String getContentType() {
043        return headers.get("content-type");
044    }
045
046    public boolean isKeepAlive() {
047        // TODO check header and version for keep alive
048        return false;
049    }
050
051    public String getHeader(String name) {
052        return headers.get(name);
053    }
054
055    public boolean containsHeader(String name) {
056        return headers.containsKey(name);
057    }
058
059    public Map<String, String> getHeaders() {
060        return headers;
061    }
062
063    public HttpStatus getStatus() {
064        return status;
065    }
066
067    @Override
068    public String toString() {
069        StringBuilder sb = new StringBuilder();
070        sb.append("HTTP RESPONSE STATUS: " ).append(status).append('\n');
071        sb.append("VERSION: ").append(version).append('\n');
072        
073        sb.append("-- HEADER --- \n");
074        
075        for (Map.Entry<String, String> entry : headers.entrySet()) {
076            sb.append(entry.getKey()).append(':').append(entry.getValue()).append('\n');
077        }
078
079        return sb.toString();
080    }
081}