001/*
002 * JGrapes Event Driven Framework
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 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.net.events;
020
021import java.net.SocketAddress;
022import java.util.Collections;
023import java.util.List;
024
025import javax.net.ssl.SNIServerName;
026
027import org.jgrapes.core.Channel;
028import org.jgrapes.core.Components;
029import org.jgrapes.io.events.Opened;
030
031/**
032 * This event signals that a new connection has been made by a client.
033 */
034@SuppressWarnings("PMD.DataClass")
035public class Accepted extends Opened {
036
037    private final SocketAddress localAddress;
038    private final SocketAddress remoteAddress;
039    private final boolean secure;
040    private final List<SNIServerName> requestedServerNames;
041
042    /**
043     * Creates a new instance.
044     * 
045     * @param localAddress the local address
046     * @param remoteAddress the remote address
047     * @param secure indicates that this is a secure connection
048     * @param requestedServerNames the requested server names
049     * (in case of a TLS connection)
050     */
051    public Accepted(SocketAddress localAddress, SocketAddress remoteAddress,
052            boolean secure, List<SNIServerName> requestedServerNames) {
053        this.localAddress = localAddress;
054        this.remoteAddress = remoteAddress;
055        this.secure = secure;
056        this.requestedServerNames
057            = Collections.unmodifiableList(requestedServerNames);
058    }
059
060    /**
061     * @return the localAddress
062     */
063    public SocketAddress localAddress() {
064        return localAddress;
065    }
066
067    /**
068     * @return the remoteAddress
069     */
070    public SocketAddress remoteAddress() {
071        return remoteAddress;
072    }
073
074    /**
075     * @return if this is a secure connection
076     */
077    public boolean isSecure() {
078        return secure;
079    }
080
081    /**
082     * @return the requested server names
083     */
084    public List<SNIServerName> requestedServerNames() {
085        return requestedServerNames;
086    }
087
088    /*
089     * (non-Javadoc)
090     * 
091     * @see java.lang.Object#toString()
092     */
093    @Override
094    public String toString() {
095        StringBuilder builder = new StringBuilder(50);
096        builder.append(Components.objectName(this))
097            .append(" [")
098            .append(localAddress)
099            .append(" <― ")
100            .append(remoteAddress)
101            .append(", ");
102        if (channels().length > 0) {
103            builder.append("channels=");
104            builder.append(Channel.toString(channels()));
105        }
106        builder.append(", secure=")
107            .append(secure)
108            .append(']');
109        return builder.toString();
110    }
111}