001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-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 Affero General Public License as published by 
007 * 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 Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.http.events;
020
021import java.net.URI;
022
023import org.jdrupes.httpcodec.protocols.http.HttpRequest;
024import org.jgrapes.http.HttpServer;
025
026/**
027 * Indicates that a component has accepted a {@link Request.In.Get} with
028 * a header that requested an upgrade to another protocol.
029 * 
030 * The {@link HttpServer} component listens for such events and 
031 * automatically creates the required {@link Response} event.
032 */
033public class ProtocolSwitchAccepted extends MessageReceived<Void> {
034
035    private final Request.In requestEvent;
036    private final String protocol;
037
038    /**
039     * Creates a new event. The request event passed in as parameter
040     * is used by the {@link HttpServer} to build the response message
041     * and link an existing session to the web socket.
042     * 
043     * To be precise, the {@link HttpServer} retrieves the {@link HttpRequest}
044     * from the request event and uses the prepared response provided by 
045     * {@link HttpRequest#response()} to build the response. The default
046     * information contained in this prepared response is sufficient to
047     * build the actual response. If required, the accepting component 
048     * can add special header fields to the prepared response.
049     *
050     * @param request the base response data
051     * @param protocol the accepted protocol
052     */
053    public ProtocolSwitchAccepted(Request.In request, String protocol) {
054        this.requestEvent = request;
055        this.protocol = protocol;
056    }
057
058    /**
059     * Returns the resource for which the socket was opened.
060     * 
061     * @return the value
062     */
063    public URI resourceName() {
064        return requestEvent.requestUri();
065    }
066
067    /**
068     * Returns the original request. 
069     * 
070     * @return the value
071     */
072    public Request.In requestEvent() {
073        return requestEvent;
074    }
075
076    /**
077     * The accepted protocol.
078     *
079     * @return the protocol
080     */
081    public String protocol() {
082        return protocol;
083    }
084}