001/*
002 * This file is part of the JDrupes non-blocking HTTP Codec
003 * Copyright (C) 2016,2018  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.websocket;
020
021import java.nio.CharBuffer;
022import java.util.Optional;
023
024/**
025 * Represents a WebSocket close frame.
026 * 
027 * Note that status code and reason are modeled as part of the header in 
028 * this API although they are handled like payload by the "wire protocol".
029 */
030public class WsCloseFrame extends WsFrameHeader {
031
032        private Integer statusCode;
033        private String reason;
034        
035        /**
036         * Creates a new close control frame.
037         * 
038         * @param statusCode the status code (if any)
039         * @param reason the reason (if any)
040         */
041        public WsCloseFrame(Integer statusCode, CharBuffer reason) {
042                super();
043                this.statusCode = statusCode;
044                this.reason = reason != null ? reason.toString() : null;
045        }
046        
047        /* (non-Javadoc)
048         * @see org.jdrupes.httpcodec.MessageHeader#hasPayload()
049         */
050        @Override
051        public boolean hasPayload() {
052                return false;
053        }
054
055        /* (non-Javadoc)
056         * @see org.jdrupes.httpcodec.MessageHeader#isFinal()
057         */
058        @Override
059        public boolean isFinal() {
060                return true;
061        }
062
063        /**
064         * @return the statusCode
065         */
066        public Optional<Integer> statusCode() {
067                return Optional.ofNullable(statusCode);
068        }
069        
070        /**
071         * @return the reason
072         */
073        public Optional<String> reason() {
074                return Optional.ofNullable(reason);
075        }
076
077        /* (non-Javadoc)
078         * @see java.lang.Object#toString()
079         */
080        @Override
081        public String toString() {
082                StringBuilder builder = new StringBuilder();
083                builder.append(this.getClass().getSimpleName());
084                builder.append(" [");
085                if (statusCode != null) {
086                        builder.append("statusCode=");
087                        builder.append(statusCode);
088                        builder.append(", ");
089                }
090                if (reason != null) {
091                        builder.append("reason=");
092                        builder.append(reason);
093                }
094                builder.append("]");
095                return builder.toString();
096        }
097        
098}