BasicStatusLine.java revision 069490a5ca2fd1988d29daf45d892f47ad665115
1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java $
3 * $Revision: 604625 $
4 * $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http.message;
33
34import org.apache.http.HttpStatus;
35import org.apache.http.ProtocolVersion;
36import org.apache.http.StatusLine;
37
38
39
40/**
41 * Represents a status line as returned from a HTTP server.
42 * See <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a> section 6.1.
43 * This class is immutable and therefore inherently thread safe.
44 *
45 * @see HttpStatus
46 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
47 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
48 *
49 * @version $Id: BasicStatusLine.java 604625 2007-12-16 14:11:11Z olegk $
50 *
51 * @since 4.0
52 */
53public class BasicStatusLine implements StatusLine, Cloneable {
54
55    // ----------------------------------------------------- Instance Variables
56
57    /** The protocol version. */
58    private final ProtocolVersion protoVersion;
59
60    /** The status code. */
61    private final int statusCode;
62
63    /** The reason phrase. */
64    private final String reasonPhrase;
65
66    // ----------------------------------------------------------- Constructors
67    /**
68     * Creates a new status line with the given version, status, and reason.
69     *
70     * @param version           the protocol version of the response
71     * @param statusCode        the status code of the response
72     * @param reasonPhrase      the reason phrase to the status code, or
73     *                          <code>null</code>
74     */
75    public BasicStatusLine(final ProtocolVersion version, int statusCode,
76                           final String reasonPhrase) {
77        super();
78        if (version == null) {
79            throw new IllegalArgumentException
80                ("Protocol version may not be null.");
81        }
82        if (statusCode < 0) {
83            throw new IllegalArgumentException
84                ("Status code may not be negative.");
85        }
86        this.protoVersion = version;
87        this.statusCode   = statusCode;
88        this.reasonPhrase = reasonPhrase;
89    }
90
91    // --------------------------------------------------------- Public Methods
92
93    /**
94     * @return the Status-Code
95     */
96    public int getStatusCode() {
97        return this.statusCode;
98    }
99
100    /**
101     * @return the HTTP-Version
102     */
103    public ProtocolVersion getProtocolVersion() {
104        return this.protoVersion;
105    }
106
107    /**
108     * @return the Reason-Phrase
109     */
110    public String getReasonPhrase() {
111        return this.reasonPhrase;
112    }
113
114    public String toString() {
115        // no need for non-default formatting in toString()
116        return BasicLineFormatter.DEFAULT
117            .formatStatusLine(null, this).toString();
118    }
119
120    public Object clone() throws CloneNotSupportedException {
121        return super.clone();
122    }
123
124}
125