All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.net.tcp.secure.jdk.JdkSecureSessionFactory Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.net.tcp.secure.jdk;

import com.firefly.net.*;
import com.firefly.utils.lang.Pair;

import javax.net.ssl.SSLEngine;
import java.io.IOException;
import java.util.List;

/**
 * @author Pengtao Qiu
 */
public class JdkSecureSessionFactory implements SecureSessionFactory {

    private SSLContextFactory clientSSLContextFactory = new NoCheckJdkSSLContextFactory();
    private SSLContextFactory serverSSLContextFactory = new DefaultCredentialJdkSSLContextFactory();

    private List supportedProtocols;

    public JdkSecureSessionFactory() {
    }

    public JdkSecureSessionFactory(SSLContextFactory clientSSLContextFactory, SSLContextFactory serverSSLContextFactory) {
        this.clientSSLContextFactory = clientSSLContextFactory;
        this.serverSSLContextFactory = serverSSLContextFactory;
    }

    public SSLContextFactory getClientSSLContextFactory() {
        return clientSSLContextFactory;
    }

    public void setClientSSLContextFactory(SSLContextFactory clientSSLContextFactory) {
        this.clientSSLContextFactory = clientSSLContextFactory;
    }

    public SSLContextFactory getServerSSLContextFactory() {
        return serverSSLContextFactory;
    }

    public void setServerSSLContextFactory(SSLContextFactory serverSSLContextFactory) {
        this.serverSSLContextFactory = serverSSLContextFactory;
    }

    @Override
    public SecureSession create(Session session, boolean clientMode, SecureSessionHandshakeListener secureSessionHandshakeListener) throws IOException {
        SSLContextFactory sslContextFactory = from(clientMode);
        sslContextFactory.setSupportedProtocols(supportedProtocols);
        Pair p = sslContextFactory.createSSLEngine(clientMode);
        return new JdkSSLSession(session, p.first, p.second, secureSessionHandshakeListener);
    }

    @Override
    public SecureSession create(Session session, boolean clientMode, String peerHost, int peerPort, SecureSessionHandshakeListener secureSessionHandshakeListener) throws IOException {
        SSLContextFactory sslContextFactory = from(clientMode);
        sslContextFactory.setSupportedProtocols(supportedProtocols);
        Pair p = sslContextFactory.createSSLEngine(clientMode, peerHost, peerPort);
        return new JdkSSLSession(session, p.first, p.second, secureSessionHandshakeListener);
    }

    protected SSLContextFactory from(boolean clientMode) {
        return clientMode ? clientSSLContextFactory : serverSSLContextFactory;
    }

    @Override
    public List getSupportedProtocols() {
        return supportedProtocols;
    }

    @Override
    public void setSupportedProtocols(List supportedProtocols) {
        this.supportedProtocols = supportedProtocols;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy