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;
}
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;
}
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;
}
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;
}
}
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);
}
Aggregations