org.nervousync.mail.protocol.BaseProtocol Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nervousync.mail.protocol;
import java.io.Serializable;
import java.security.Security;
import java.util.*;
import org.nervousync.commons.Globals;
import org.nervousync.proxy.ProxyConfig;
import org.nervousync.enumerations.mail.MailProtocol;
import org.nervousync.mail.config.MailConfig;
import org.nervousync.security.factory.SecureFactory;
import org.nervousync.utils.StringUtils;
/**
* Abstract class of JavaMail protocol
* JavaMail的协议抽象类
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0.0 $ $Date: Jul 31, 2012 19:07:08 $
*/
public abstract class BaseProtocol implements Serializable {
/**
* Serial version UID
* 序列化UID
*/
private static final long serialVersionUID = 6441571927997267674L;
/**
* Default SSL socket factory class, using for connect to ssl mail server
* 默认的安全套接字工厂类,用于连接到电子邮件服务器时使用安全连接
*/
private static final String SSL_FACTORY_CLASS = "javax.net.ssl.SSLSocketFactory";
/**
* Protocol key name of connect to mail server store
* 连接到电子邮件服务器的通讯协议类型键值名
*/
private static final String MAIL_STORE_PROTOCOL = "mail.store.protocol";
/**
* Protocol key name of connect to mail server transport
* 连接到电子邮件服务器的传输协议类型键值名
*/
private static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
/**
* Connect timeout value
* 连接超时时间
*/
protected String connectionTimeoutParam;
/**
* Mail server domain name
* 邮件服务器域名
*/
protected String hostParam;
/**
* Mail server port
* 邮件服务器端口号
*/
protected String portParam;
/**
* Process timeout value
* 操作超时时间
*/
protected String timeoutParam;
/**
* Secure config name
* 安全配置名称
*/
private final String secureName;
/**
* Proxy configure information
* 代理服务器配置信息
*/
private final ProxyConfig proxyConfig;
/**
* Constructor method for BaseProtocol
* BaseProtocol构造方法
*
* @param secureName Secure config name
* 安全配置名称
* @param proxyConfig Proxy configure information
* 代理服务器配置信息
*/
protected BaseProtocol(final String secureName, final ProxyConfig proxyConfig) {
this.secureName = secureName;
this.proxyConfig = proxyConfig;
}
/**
* Convert given e-mail server configure instance to Properties instance
* Generated Properties instance is using for connect to E-mail server
* 转换给定的电子邮件配置实例对象为Properties实例对象
* 生成的Properties实例对象用于连接到电子邮件服务器
*
* @param serverConfig Server configure information
* 服务器配置
*
* @return Generated Properties instance
* 生成的Properties实例对象
*/
public final Properties readConfig(final MailConfig.ServerConfig serverConfig) {
Properties properties = new Properties();
properties.setProperty(this.hostParam, serverConfig.getHostName());
int port = serverConfig.getHostPort();
if (port != Globals.DEFAULT_VALUE_INT) {
properties.setProperty(portParam, Integer.toString(port));
}
if (serverConfig.getConnectionTimeout() > 0) {
properties.setProperty(connectionTimeoutParam,
Integer.toString(serverConfig.getConnectionTimeout() * 1000));
}
if (serverConfig.getProcessTimeout() > 0) {
properties.setProperty(timeoutParam, Integer.toString(serverConfig.getConnectionTimeout() * 1000));
}
if (serverConfig.isSsl()) {
Security.addProvider(Security.getProvider("SunJSSE"));
}
switch (serverConfig.getProtocolOption()) {
case IMAP:
properties.setProperty(MAIL_STORE_PROTOCOL, "imap");
if (serverConfig.isAuthLogin()) {
properties.setProperty("mail.imap.auth.plain.disable", Boolean.TRUE.toString());
properties.setProperty("mail.imap.auth.login.disable", Boolean.TRUE.toString());
}
if (serverConfig.isSsl()) {
properties.setProperty(MAIL_STORE_PROTOCOL, "imaps");
properties.setProperty("mail.imap.socketFactory.class", SSL_FACTORY_CLASS);
if (port != Globals.DEFAULT_VALUE_INT) {
properties.setProperty("mail.imap.socketFactory.port", Integer.toString(port));
}
properties.setProperty("mail.imap.starttls.enable", Boolean.TRUE.toString());
}
break;
case SMTP:
properties.setProperty(MAIL_STORE_PROTOCOL, "smtp");
properties.setProperty(MAIL_TRANSPORT_PROTOCOL, "smtp");
if (serverConfig.isAuthLogin()) {
properties.setProperty("mail.smtp.auth", Boolean.TRUE.toString());
}
if (serverConfig.isSsl()) {
properties.setProperty(MAIL_STORE_PROTOCOL, "smtps");
properties.setProperty("mail.smtp.ssl.enable", Boolean.TRUE.toString());
properties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY_CLASS);
properties.setProperty("mail.smtp.socketFactory.fallback", Boolean.FALSE.toString());
if (port != Globals.DEFAULT_VALUE_INT) {
properties.setProperty("mail.smtp.socketFactory.port", Integer.toString(port));
}
properties.setProperty("mail.smtp.starttls.enable", Boolean.TRUE.toString());
}
break;
case POP3:
properties.setProperty(MAIL_STORE_PROTOCOL, "pop3");
properties.setProperty(MAIL_TRANSPORT_PROTOCOL, "pop3");
if (serverConfig.isSsl()) {
properties.setProperty(MAIL_STORE_PROTOCOL, "pop3s");
properties.setProperty(MAIL_TRANSPORT_PROTOCOL, "pop3s");
properties.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY_CLASS);
if (port != 0) {
properties.setProperty("mail.pop3.socketFactory.port", Integer.toString(port));
}
properties.setProperty("mail.pop3.disabletop", Boolean.TRUE.toString());
properties.setProperty("mail.pop3.ssl.enable", Boolean.TRUE.toString());
properties.setProperty("mail.pop3.useStartTLS", Boolean.TRUE.toString());
}
break;
default:
return new Properties();
}
this.configProxy(serverConfig.getProtocolOption(), properties);
return properties;
}
/**
* Add proxy configure information to target Properties instance
* 添加代理服务器信息到给定的Properties实例对象中
*
* @param mailProtocol Enumeration value of MailProtocol
* 电子邮件协议枚举值
* @param properties Given Properties instance
* 给定的Properties实例对象
*/
private void configProxy(final MailProtocol mailProtocol, final Properties properties) {
final String configPrefix;
switch (mailProtocol) {
case SMTP:
configPrefix = "mail.smtp";
break;
case IMAP:
configPrefix = "mail.imap";
break;
case POP3:
configPrefix = "mail.pop3";
break;
default:
return;
}
switch (this.proxyConfig.getProxyType()) {
case HTTP:
properties.setProperty(configPrefix + ".proxy.host", this.proxyConfig.getProxyAddress());
if (this.proxyConfig.getProxyPort() != Globals.DEFAULT_VALUE_INT) {
properties.setProperty(configPrefix + ".proxy.port",
Integer.toString(this.proxyConfig.getProxyPort()));
}
if (StringUtils.notBlank(this.proxyConfig.getUserName())
&& StringUtils.notBlank(this.proxyConfig.getPassword())) {
properties.setProperty(configPrefix + ".proxy.user", this.proxyConfig.getUserName());
properties.setProperty(configPrefix + ".proxy.password",
SecureFactory.decrypt(this.secureName, this.proxyConfig.getPassword()));
}
break;
case SOCKS:
properties.setProperty(configPrefix + ".socks.host", this.proxyConfig.getProxyAddress());
properties.setProperty(configPrefix + ".socks.port", Integer.toString(this.proxyConfig.getProxyPort()));
break;
}
}
}