001/*
002 * Copyright (C) 2016 Neo Visionaries Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing,
011 * software distributed under the License is distributed on an
012 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific
014 * language governing permissions and limitations under the
015 * License.
016 */
017package NeoVisionaries.WebSockets;
018
019
020/**
021 * HTTP status line returned from an HTTP server.
022 * 
023 * <EMBED CLASS='external-html' DATA-FILE-ID=LICENSE>
024 *
025 * @since 1.19
026 */
027public class StatusLine
028{
029    /**
030     * HTTP version.
031     */
032    private final String mHttpVersion;
033
034
035    /**
036     * Status code.
037     */
038    private final int mStatusCode;
039
040
041    /**
042     * Reason phrase.
043     */
044    private final String mReasonPhrase;
045
046
047    /**
048     * String representation of this instance (= the raw status line).
049     */
050    private final String mString;
051
052
053    /**
054     * Constructor with a raw status line.
055     *
056     * @param line
057     *         A status line.
058     *
059     * @throws NullPointerException
060     *         {@code line} is {@code null}
061     *
062     * @throws IllegalArgumentException
063     *         The number of elements in {@code line} is less than 2.
064     *
065     * @throws NumberFormatException
066     *         Failed to parse the second element in {@code line}
067     *         as an integer.
068     */
069    StatusLine(String line)
070    {
071        // HTTP-Version Status-Code Reason-Phrase
072        String[] elements = line.split(" +", 3);
073
074        if (elements.length < 2)
075        {
076            throw new IllegalArgumentException();
077        }
078
079        mHttpVersion  = elements[0];
080        mStatusCode   = Integer.parseInt(elements[1]);
081        mReasonPhrase = (elements.length == 3) ? elements[2] : null;
082        mString       = line;
083    }
084
085
086    /**
087     * Get the HTTP version.
088     *
089     * @return
090     *         The HTTP version. For example, {@code "HTTP/1.1"}.
091     */
092    public String getHttpVersion()
093    {
094        return mHttpVersion;
095    }
096
097
098    /**
099     * Get the status code.
100     *
101     * @return
102     *         The status code. For example, {@code 404}.
103     */
104    public int getStatusCode()
105    {
106        return mStatusCode;
107    }
108
109
110    /**
111     * Get the reason phrase.
112     *
113     * @return
114     *         The reason phrase. For example, {@code "Not Found"}.
115     */
116    public String getReasonPhrase()
117    {
118        return mReasonPhrase;
119    }
120
121
122    /**
123     * Get the string representation of this instance, which is
124     * equal to the raw status line.
125     *
126     * @return
127     *         The raw status line. For example,
128     *         {@code "HTTP/1.1 404 Not Found"}.
129     */
130    @Override
131    public String toString()
132    {
133        return mString;
134    }
135}