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;
022import org.jdrupes.httpcodec.protocols.http.HttpRequest;
023import org.jgrapes.http.HttpServer;
024
025/**
026 * Indicates that a component has accepted a {@link Request.In.Get} with
027 * a header that requested an upgrade to another protocol.
028 * 
029 * The {@link HttpServer} component listens for such events and 
030 * automatically creates the required {@link Response} event.
031 */
032public class ProtocolSwitchAccepted extends MessageReceived<Void> {
033
034    private final Request.In requestEvent;
035    private final String protocol;
036
037    /**
038     * Creates a new event. The request event passed in as parameter
039     * is used by the {@link HttpServer} to build the response message
040     * and link an existing session to the web socket.
041     * 
042     * To be precise, the {@link HttpServer} retrieves the {@link HttpRequest}
043     * from the request event and uses the prepared response provided by 
044     * {@link HttpRequest#response()} to build the response. The default
045     * information contained in this prepared response is sufficient to
046     * build the actual response. If required, the accepting component 
047     * can add special header fields to the prepared response.
048     *
049     * @param request the base response data
050     * @param protocol the accepted protocol
051     */
052    public ProtocolSwitchAccepted(Request.In request, String protocol) {
053        this.requestEvent = request;
054        this.protocol = protocol;
055    }
056
057    /**
058     * Returns the resource for which the socket was opened.
059     * 
060     * @return the value
061     */
062    public URI resourceName() {
063        return requestEvent.requestUri();
064    }
065
066    /**
067     * Returns the original request. 
068     * 
069     * @return the value
070     */
071    public Request.In requestEvent() {
072        return requestEvent;
073    }
074
075    /**
076     * The accepted protocol.
077     *
078     * @return the protocol
079     */
080    public String protocol() {
081        return protocol;
082    }
083}