001/*
002 * This file is part of the JDrupes non-blocking HTTP Codec
003 * Copyright (C) 2016, 2017  Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published
007 * by the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
013 * License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.httpcodec.protocols.http;
020
021import org.jdrupes.httpcodec.ProtocolException;
022
023import static org.jdrupes.httpcodec.protocols.http.HttpConstants.*;
024
025/**
026 * Represents a violation of the HTTP protocol. This kind of exception
027 * is thrown by the HTTP codecs when a problem is detected while encoding
028 * or decoding a message.
029 */
030public class HttpProtocolException extends ProtocolException {
031
032        private static final long serialVersionUID = 1L;
033
034        private HttpProtocol httpProtocol;
035        private int statusCode;
036        private String reasonPhrase;
037
038        /**
039         * Creates a new exception with the given values.
040         * 
041         * @param httpProtocol the HTTP version
042         * @param statusCode the status code
043         * @param reasonPhrase the reason phrase
044         */
045        public HttpProtocolException(HttpProtocol httpProtocol, int statusCode,
046                String reasonPhrase) {
047                super(String.format("%03d %s", statusCode, reasonPhrase));
048                this.httpProtocol = httpProtocol;
049                this.statusCode = statusCode;
050                this.reasonPhrase = reasonPhrase;
051        }
052
053        /**
054         * Creates a new exception with the standard reason phrase.
055         * 
056         * @param httpProtocol the HTTP version
057         * @param status the status
058         */
059        public HttpProtocolException(HttpProtocol httpProtocol, HttpStatus status) {
060                super(String.format("%03d %s", status.statusCode(),
061                                status.reasonPhrase()));
062                this.httpProtocol = httpProtocol;
063                this.statusCode = status.statusCode();
064                this.reasonPhrase = status.reasonPhrase();
065        }
066        
067        /**
068         * Returns the HTTP version.
069         * 
070         * @return the HTTP Version
071         */
072        public HttpProtocol httpVersion() {
073                return httpProtocol;
074        }
075        
076        /**
077         * Returns the status code.
078         * 
079         * @return the statusCode
080         */
081        public int statusCode() {
082                return statusCode;
083        }
084
085        /**
086         * Returns the reason phrase.
087         * 
088         * @return the reasonPhrase
089         */
090        public String reasonPhrase() {
091                return reasonPhrase;
092        }
093        
094}