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
020import java.util.List;
021import java.util.Map;
022
023
024/**
025 * An exception raised due to a violation against the WebSocket protocol.
026 * 
027 * <EMBED CLASS='external-html' DATA-FILE-ID=LICENSE>
028 *
029 * @since 1.19
030 */
031public class OpeningHandshakeException extends WebSocketException
032{
033    private static final long serialVersionUID = 1L;
034
035
036    private final StatusLine mStatusLine;
037    private final Map<String, List<String>> mHeaders;
038    private final byte[] mBody;
039
040
041    OpeningHandshakeException(
042            WebSocketError error, String message,
043            StatusLine statusLine, Map<String, List<String>> headers)
044    {
045        this(error, message, statusLine, headers, null);
046    }
047
048
049    OpeningHandshakeException(
050            WebSocketError error, String message,
051            StatusLine statusLine, Map<String, List<String>> headers, byte[] body)
052    {
053        super(error, message);
054
055        mStatusLine = statusLine;
056        mHeaders    = headers;
057        mBody       = body;
058    }
059
060
061    /**
062     * Get the status line contained in the WebSocket opening handshake
063     * response from the server.
064     *
065     * @return
066     *         The status line.
067     */
068    public StatusLine getStatusLine()
069    {
070        return mStatusLine;
071    }
072
073
074    /**
075     * Get the HTTP headers contained in the WebSocket opening handshake
076     * response from the server.
077     *
078     * @return
079     *         The HTTP headers. The returned map is an instance of
080     *         {@link java.util.TreeMap TreeMap} with {@link
081     *         String#CASE_INSENSITIVE_ORDER} comparator.
082     */
083    public Map<String, List<String>> getHeaders()
084    {
085        return mHeaders;
086    }
087
088
089    /**
090     * Get the response body contained in the WebSocket opening handshake
091     * response from the server.
092     *
093     * <p>
094     * This method returns a non-null value only when (1) the status code
095     * is not 101 (Switching Protocols), (2) the response from the server
096     * has a response body, (3) the response has "Content-Length" header,
097     * and (4) no error occurred during reading the response body. In other
098     * cases, this method returns {@code null}.
099     * </p>
100     *
101     * @return
102     *         The response body.
103     */
104    public byte[] getBody()
105    {
106        return mBody;
107    }
108}