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