Search in sources :

Example 61 with SocketFactory

use of javax.net.SocketFactory in project zm-mailbox by Zimbra.

the class MailConnection method newSocket.

private Socket newSocket() throws IOException {
    SocketFactory sf = config.getSecurity() != MailConfig.Security.SSL ? getSocketFactory() : getSSLSocketFactory();
    Socket sock = sf.createSocket();
    int connectTimeout = (int) Math.min(config.getConnectTimeout() * 1000L, Integer.MAX_VALUE);
    int readTimeout = (int) Math.min(config.getReadTimeout() * 1000L, Integer.MAX_VALUE);
    sock.setSoTimeout(readTimeout > 0 ? readTimeout : Integer.MAX_VALUE);
    sock.connect(new InetSocketAddress(config.getHost(), config.getPort()), connectTimeout > 0 ? connectTimeout : Integer.MAX_VALUE);
    return sock;
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 62 with SocketFactory

use of javax.net.SocketFactory in project zm-mailbox by Zimbra.

the class TestSocks method connect.

private Socket connect(ProxySelector ps) throws IOException {
    SocketFactory sf = SocketFactories.proxySelectorSocketFactory(ps);
    Socket sock = sf.createSocket();
    assertFalse(sock.isConnected());
    assertFalse(sock.isBound());
    sock.bind(new InetSocketAddress(0));
    sock.connect(new InetSocketAddress("www.news.com", 80));
    assertTrue(sock.isConnected());
    return sock;
}
Also used : SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 63 with SocketFactory

use of javax.net.SocketFactory in project zm-mailbox by Zimbra.

the class SmtpTransport method protocolConnect.

@Override
protected boolean protocolConnect(String host, int port, String user, String passwd) throws MessagingException {
    boolean auth = PropUtil.getBooleanSessionProperty(session, "mail." + protocol + ".auth", false);
    String authMechanism = session.getProperty("mail." + protocol + ".sasl.mechanisms");
    if (authMechanism != null && SaslAuthenticator.XOAUTH2.equalsIgnoreCase(authMechanism)) {
        passwd = session.getProperty("mail." + protocol + ".sasl.mechanisms.oauth2.oauthToken");
    }
    if (auth && (user == null || passwd == null)) {
        return false;
    }
    if (port < 0) {
        port = PropUtil.getIntSessionProperty(session, "mail." + protocol + ".port", ssl ? SmtpConfig.DEFAULT_SSL_PORT : SmtpConfig.DEFAULT_PORT);
    }
    if (Strings.isNullOrEmpty(host)) {
        host = "localhost";
    }
    SmtpConfig config = new SmtpConfig();
    config.setHost(host);
    config.setPort(port);
    config.setDomain(session.getProperty("mail." + protocol + ".localhost"));
    config.setSecurity(ssl ? SmtpConfig.Security.SSL : PropUtil.getBooleanSessionProperty(session, "mail.smtp.starttls.enable", false) ? SmtpConfig.Security.TLS_IF_AVAILABLE : SmtpConfig.Security.NONE);
    config.setAllowPartialSend(PropUtil.getBooleanSessionProperty(session, "mail." + protocol + ".sendpartial", false));
    config.setConnectTimeout(PropUtil.getIntSessionProperty(session, "mail." + protocol + ".connectiontimeout", 0) / // msec to sec
    1000);
    config.setReadTimeout(PropUtil.getIntSessionProperty(session, "mail." + protocol + ".timeout", 0) / // msec to sec
    1000);
    config.setDsn(session.getProperty("mail." + protocol + ".dsn.notify"));
    Properties props = session.getProperties();
    Object socketFactory = props.get("mail." + protocol + ".socketFactory");
    if (socketFactory instanceof SocketFactory) {
        config.setSocketFactory((SocketFactory) socketFactory);
    }
    Object sslSocketFactory = props.get("mail." + protocol + ".ssl.socketFactory");
    if (sslSocketFactory instanceof SSLSocketFactory) {
        config.setSSLSocketFactory((SSLSocketFactory) sslSocketFactory);
    }
    if (authMechanism != null && SaslAuthenticator.XOAUTH2.equalsIgnoreCase(authMechanism)) {
        config.setMechanism(SaslAuthenticator.XOAUTH2);
        HashMap<String, String> map = new HashMap<String, String>();
        JMSession.addOAuth2Properties(passwd, map, config.getProtocol());
        config.setSaslProperties(map);
    }
    if (user != null && passwd != null) {
        config.setAuthenticationId(user);
    }
    connection = new SmtpConnection(config);
    try {
        connection.connect();
    } catch (IOException e) {
        throw new MessagingException(e.getMessage(), e);
    }
    if (auth || (user != null && passwd != null)) {
        try {
            connection.authenticate(passwd);
        } catch (LoginException e) {
            throw new AuthenticationFailedException(e.getMessage());
        } catch (IOException e) {
            throw new AuthenticationFailedException(e.getMessage());
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) IOException(java.io.IOException) Properties(java.util.Properties) LoginException(javax.security.auth.login.LoginException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 64 with SocketFactory

use of javax.net.SocketFactory in project cloudstack by apache.

the class EasySSLProtocolSocketFactory method createSocket.

/**
     * Attempts to get a new socket connection to the given host within the given time limit.
     * <p>
     * To circumvent the limitations of older JREs that do not support connect timeout a
     * controller thread is executed. The controller thread attempts to create a new socket
     * within the given limit of time. If socket constructor does not return until the
     * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
     * </p>
     *
     * @param host the host name/IP
     * @param port the port on the host
     * @param localAddress the local host name/IP to bind the socket to
     * @param localPort the port on the local machine
     * @param params {@link HttpConnectionParams Http connection parameters}
     *
     * @return Socket a new socket
     *
     * @throws IOException if an I/O error occurs while creating the socket
     * @throws UnknownHostException if the IP address of the host cannot be
     * determined
     */
@Override
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        Socket socket = socketfactory.createSocket(host, port, localAddress, localPort);
        if (socket instanceof SSLSocket) {
            ((SSLSocket) socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket) socket).getEnabledProtocols()));
        }
        return socket;
    } else {
        Socket socket = socketfactory.createSocket();
        if (socket instanceof SSLSocket) {
            ((SSLSocket) socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket) socket).getEnabledProtocols()));
        }
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}
Also used : SocketFactory(javax.net.SocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 65 with SocketFactory

use of javax.net.SocketFactory in project geode by apache.

the class SecurityTestUtils method clearStaticSSLContext.

/**
   * This is a hack using reflection to clear the static objects in JSSE since otherwise changing
   * the javax.* store related properties has no effect during the course of running dunit suite
   * unless the VMs are restarted.
   */
protected static void clearStaticSSLContext() {
    ServerSocketFactory defaultServerFact = SSLServerSocketFactory.getDefault();
    // Get the class of this and use reflection to blank out any static SSLContext objects inside
    Map<Field, Object> contextMap = getSSLFields(defaultServerFact, new Class[] { SSLContext.class, SSLContextSpi.class });
    makeNullSSLFields(defaultServerFact, contextMap);
    for (Iterator contextObjsIter = contextMap.values().iterator(); contextObjsIter.hasNext(); ) {
        Object contextObj = contextObjsIter.next();
        Map<Field, Object> contextObjsMap = getSSLFields(contextObj, new Class[] { TrustManager.class, KeyManager.class, TrustManager[].class, KeyManager[].class });
        makeNullSSLFields(contextObj, contextObjsMap);
    }
    makeNullStaticField(SSLServerSocketFactory.class);
    // Do the same for normal SSL socket factory
    SocketFactory defaultFact = SSLSocketFactory.getDefault();
    contextMap = getSSLFields(defaultFact, new Class[] { SSLContext.class, SSLContextSpi.class });
    makeNullSSLFields(defaultFact, contextMap);
    for (Iterator contextObjsIter = contextMap.values().iterator(); contextObjsIter.hasNext(); ) {
        Object contextObj = contextObjsIter.next();
        Map<Field, Object> contextObjsMap = getSSLFields(contextObj, new Class[] { TrustManager.class, KeyManager.class, TrustManager[].class, KeyManager[].class });
        makeNullSSLFields(contextObj, contextObjsMap);
    }
    makeNullStaticField(SSLSocketFactory.class);
    makeNullStaticField(SSLContext.class);
}
Also used : Field(java.lang.reflect.Field) SSLContextSpi(javax.net.ssl.SSLContextSpi) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) Iterator(java.util.Iterator) SSLContext(javax.net.ssl.SSLContext) KeyManager(javax.net.ssl.KeyManager) TrustManager(javax.net.ssl.TrustManager)

Aggregations

SocketFactory (javax.net.SocketFactory)66 Socket (java.net.Socket)25 Test (org.junit.Test)25 IOException (java.io.IOException)18 InetSocketAddress (java.net.InetSocketAddress)14 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 SSLSocket (javax.net.ssl.SSLSocket)10 OutputStream (java.io.OutputStream)9 ServerSocket (java.net.ServerSocket)9 SocketAddress (java.net.SocketAddress)6 Configuration (org.apache.hadoop.conf.Configuration)5 ServerSocketFactory (javax.net.ServerSocketFactory)4 InputStream (java.io.InputStream)3 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)3 StandardSocketFactory (org.apache.hadoop.net.StandardSocketFactory)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 SocketException (java.net.SocketException)2