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

org.nervousync.mail.config.builder.AbstractMailConfigBuilder Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * 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.config.builder;

import org.nervousync.builder.AbstractBuilder;
import org.nervousync.commons.Globals;
import org.nervousync.commons.RegexGlobals;
import org.nervousync.proxy.AbstractProxyConfigBuilder;
import org.nervousync.proxy.ProxyConfig;
import org.nervousync.enumerations.mail.MailProtocol;
import org.nervousync.exceptions.builder.BuilderException;
import org.nervousync.mail.config.MailConfig;
import org.nervousync.security.factory.SecureConfig;
import org.nervousync.security.factory.SecureFactory;
import org.nervousync.utils.FileUtils;
import org.nervousync.utils.StringUtils;

import java.security.PrivateKey;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Optional;

/**
 * 

Abstract mail configure builder for Generics Type

*

* Current abstract class is using to integrate to another builder * which configure contains mail configure information. *

*

拥有父构造器的电子邮件配置信息抽象构造器

*

当前抽象构建器用于整合到包含邮件配置信息的其他配置构建器

* * @param Generics Type Class * 泛型类 * * @author Steven Wee [email protected] * @version $Revision: 1.0.0 $ $Date: Jul 31, 2022 16:27:18 $ */ public abstract class AbstractMailConfigBuilder extends AbstractBuilder { /** *

Current mail configure information

*

当前邮件配置信息

*/ protected final MailConfig mailConfig; /** *

Protected constructor for AbstractMailConfigBuilder

*

AbstractMailConfigBuilder的构造函数

* * @param parentBuilder Generics Type instance * 泛型类实例对象 * @param mailConfig Mail configure information * 邮件配置信息 */ protected AbstractMailConfigBuilder(final T parentBuilder, final MailConfig mailConfig) { super(parentBuilder); this.mailConfig = (mailConfig == null) ? new MailConfig() : mailConfig; } /** *

Configure secure name

* Builder will decrypt (if current configure contains secure name) and encrypt (using new secure name) password automatically *

设置安全名称

* 构造器会解密(如果当前配置信息中包含安全名称)并加密(使用新的安全名称)密码信息 * * @param secureName New secure name * 新的安全名称 * * @return Current builder instance * 当前构造器实例对象 */ public AbstractMailConfigBuilder secureName(final String secureName) { if (StringUtils.notBlank(secureName) && SecureFactory.registeredConfig(secureName)) { if (StringUtils.notBlank(this.mailConfig.getPassword())) { String newPassword = SecureFactory.update(this.mailConfig.getPassword(), this.mailConfig.getSecureName(), secureName); this.mailConfig.setPassword(newPassword); } Optional.ofNullable(this.mailConfig.getProxyConfig()) .filter(proxyConfig -> StringUtils.notBlank(proxyConfig.getPassword())) .ifPresent(proxyConfig -> { String newPassword = SecureFactory.update(proxyConfig.getPassword(), this.mailConfig.getSecureName(), secureName); proxyConfig.setPassword(newPassword); this.mailConfig.setProxyConfig(proxyConfig); }); this.mailConfig.setSecureName(secureName); } return this; } /** *

Configure custom secure config

* * Builder will decrypt (if current configure contains secure name or secure config) * and encrypt (using new secure config) password automatically * *

设置安全名称

* 构造器会解密(如果当前配置信息中包含安全名称)并加密(使用新的安全配置)密码信息 * * @param secureName New secure name * 新的安全名称 * @param secureConfig Custom secure config * 新的安全配置 * * @return Current builder instance * 当前构造器实例对象 */ public AbstractMailConfigBuilder secureConfig(final String secureName, final SecureConfig secureConfig) { if (StringUtils.notBlank(secureName) && secureConfig != null) { if (StringUtils.notBlank(this.mailConfig.getPassword())) { String newPassword; if (SecureFactory.registeredConfig(secureName)) { SecureFactory.register(Globals.DEFAULT_TEMPORARY_SECURE_NAME, secureConfig); newPassword = SecureFactory.update(this.mailConfig.getPassword(), this.mailConfig.getSecureName(), Globals.DEFAULT_TEMPORARY_SECURE_NAME); SecureFactory.deregister(Globals.DEFAULT_TEMPORARY_SECURE_NAME); SecureFactory.register(secureName, secureConfig); } else { SecureFactory.register(secureName, secureConfig); newPassword = SecureFactory.update(this.mailConfig.getPassword(), this.mailConfig.getSecureName(), secureName); } this.mailConfig.setPassword(newPassword); } this.mailConfig.setSecureName(secureName); this.mailConfig.setSecureConfig(secureConfig); } return this; } /** *

Configure authenticate information

*

设置身份认证信息

* * @param userName Mail account username * 邮件账户用户名 * @param password Mail account password * 邮件账户密码 * * @return Current builder instance * 当前构造器实例对象 * * @throws BuilderException the builder exception * If username string not a valid e-mail address * 当用户名不是合法的电子邮件地址时 */ public AbstractMailConfigBuilder authentication(final String userName, final String password) throws BuilderException { if (!StringUtils.matches(userName, RegexGlobals.EMAIL_ADDRESS)) { throw new BuilderException(0x0000000E0001L, "Username_Invalid_Mail_Error"); } String encPassword = Globals.DEFAULT_VALUE_STRING; if (StringUtils.notBlank(password)) { if (StringUtils.notBlank(this.mailConfig.getSecureName()) && SecureFactory.registeredConfig(this.mailConfig.getSecureName())) { encPassword = SecureFactory.encrypt(this.mailConfig.getSecureName(), password); } else { encPassword = password; } } this.mailConfig.setUserName(userName); this.mailConfig.setPassword(encPassword); return this; } /** *

Using current proxy configure information to create ProxyConfigBuilder instance

*

使用当前的代理服务器配置信息生成代理服务器配置构建器实例对象

* * @return ProxyConfigBuilder instance * 代理服务器配置构建器实例对象 */ public ProxyConfigBuilder proxyConfig() { return new ProxyConfigBuilder<>(this, this.mailConfig.getSecureName(), this.mailConfig.getProxyConfig()); } /** *

Using current send server configure information to create ServerConfigBuilder instance

*

使用当前的发送邮件服务器配置信息生成邮件服务器配置构建器实例对象

* * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public ServerConfigBuilder sendConfig() { return new ServerConfigBuilder<>(this, Boolean.TRUE, this.mailConfig.getSendConfig()); } /** *

Using current receive server configure information to create ServerConfigBuilder instance

*

使用当前的接收邮件服务器配置信息生成邮件服务器配置构建器实例对象

* * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public ServerConfigBuilder receiveConfig() { return new ServerConfigBuilder<>(this, Boolean.FALSE, this.mailConfig.getReceiveConfig()); } /** *

Configure save path of mail attachment files

*

设置电子邮件附件的保存地址

* * @param storagePath Local save path * 本地保存地址 * * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 * * @throws BuilderException * If storage path is empty string or folder not exists * 如果本地保存地址为空字符串或目录不存在 */ public AbstractMailConfigBuilder storagePath(String storagePath) throws BuilderException { if (StringUtils.isEmpty(storagePath) || !FileUtils.isExists(storagePath)) { throw new BuilderException(0x0000000E0002L, "Storage_Path_NotFound_Mail_Error"); } this.mailConfig.setStoragePath(storagePath); return this; } /** *

Configure the x509 certificate and private key for mail signature

*

设置用于电子邮件签名及验签的x509证书及私钥

* * @param x509Certificate x509 certificate using for verify signature * x509证书,用于验证电子签名 * @param privateKey Private key instance using for generate signature * 私钥对象实例,用于生成电子签名 * * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public AbstractMailConfigBuilder signer(final X509Certificate x509Certificate, final PrivateKey privateKey) { if (x509Certificate != null && privateKey != null) { try { this.mailConfig.setCertificate(StringUtils.base64Encode(x509Certificate.getEncoded())); this.mailConfig.setPrivateKey(StringUtils.base64Encode(privateKey.getEncoded())); } catch (CertificateEncodingException e) { this.mailConfig.setCertificate(Globals.DEFAULT_VALUE_STRING); this.mailConfig.setPrivateKey(Globals.DEFAULT_VALUE_STRING); } } return this; } /** *

Confirm proxy configure

*

确认代理服务器配置

* * @param proxyConfig Proxy configure information * 代理服务器配置 */ private void proxyConfig(final ProxyConfig proxyConfig) { Optional.ofNullable(proxyConfig).ifPresent(this.mailConfig::setProxyConfig); } /** *

Confirm mail server configure

*

确认邮件服务器配置

* * @param sendConfig Is send server configure * 是发送服务器配置信息 * @param serverConfig Server configure information * 服务器配置 */ private void serverConfig(final boolean sendConfig, final MailConfig.ServerConfig serverConfig) { Optional.ofNullable(serverConfig).ifPresent(config -> { if (sendConfig) { this.mailConfig.setSendConfig(config); } else { this.mailConfig.setReceiveConfig(config); } }); } /** *

Proxy configure builder

*

代理服务器配置信息抽象构造器

* * @author Steven Wee [email protected] * @version $Revision: 1.0.0 $ $Date: Jul 31, 2022 16:33:28 $ */ public static final class ProxyConfigBuilder extends AbstractProxyConfigBuilder> { /** *

Private constructor for ProxyConfigBuilder

*

ProxyConfigBuilder的私有构造函数

* * @param parentBuilder Mail configure builder instance * 电子邮件配置构造器实例 * @param secureName New secure name * 新的安全名称 * @param proxyConfig Proxy configure information * 代理服务器配置信息 */ private ProxyConfigBuilder(final AbstractMailConfigBuilder parentBuilder, final String secureName, final ProxyConfig proxyConfig) { super(parentBuilder, secureName, proxyConfig); } /** *

Confirm current configure information

*

确认当前配置信息

*/ protected void build() { super.parentBuilder.proxyConfig(this.proxyConfig); } } /** *

Mail server configure builder

*

电子邮件服务器配置信息抽象构造器

* * @author Steven Wee [email protected] * @version $Revision: 1.0.0 $ $Date: Jul 31, 2022 16:35:16 $ */ public static final class ServerConfigBuilder extends AbstractBuilder> { /** * Is send server configure * 是发送服务器配置信息 */ private final boolean sendConfig; /** * Mail server domain name * 邮件服务器域名 */ private String hostName; /** * Mail server port * 邮件服务器端口号 */ private int hostPort = Globals.DEFAULT_VALUE_INT; /** * Using secure connection to host server * 使用安全连接到邮件服务器 */ private boolean ssl; /** * Host server authenticate login * 邮件服务器需要身份验证 */ private boolean authLogin; /** * Mail server protocol * 邮件服务器协议 */ private MailProtocol protocolOption = MailProtocol.UNKNOWN; /** * Connection timeout(Unit: seconds) * 连接超时时间(单位:秒) */ private int connectionTimeout = 5; /** * Process timeout(Unit: seconds) * 操作超时时间(单位:秒) */ private int processTimeout = 5; /** *

Private constructor for ServerConfigBuilder

*

ServerConfigBuilder的私有构造函数

* * @param parentBuilder Mail configure builder instance * 电子邮件配置构造器实例 * @param sendConfig Is send server configure * 是发送服务器配置信息 * @param serverConfig Server configure information * 服务器配置 */ private ServerConfigBuilder(final AbstractMailConfigBuilder parentBuilder, final boolean sendConfig, final MailConfig.ServerConfig serverConfig) { super(parentBuilder); this.sendConfig = sendConfig; if (serverConfig != null) { this.hostName = serverConfig.getHostName(); this.hostPort = serverConfig.getHostPort(); this.ssl = serverConfig.isSsl(); this.authLogin = serverConfig.isAuthLogin(); this.protocolOption = serverConfig.getProtocolOption(); this.connectionTimeout = serverConfig.getConnectionTimeout(); this.processTimeout = serverConfig.getProcessTimeout(); } } /** *

Configure host server information

*

设置服务器信息

* * @param hostAddress Mail server domain name * 邮件服务器域名 * @param hostPort Mail server port * 邮件服务器端口号 * * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public ServerConfigBuilder configHost(String hostAddress, int hostPort) { this.hostName = hostAddress; if (hostPort > 0) { this.hostPort = hostPort; } return this; } /** *

Configure using secure connection to host server

*

设置使用安全连接到邮件服务器

* * @param useSSL Using secure connection to host server * 使用安全连接到邮件服务器 * * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public ServerConfigBuilder useSSL(boolean useSSL) { this.ssl = useSSL; return this; } /** *

Configure using secure connection to host server

*

设置使用安全连接到邮件服务器

* * @param authLogin the auth login * Host server authenticate login * 邮件服务器需要身份验证 * * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public ServerConfigBuilder authLogin(boolean authLogin) { this.authLogin = authLogin; return this; } /** *

Configure mail server protocol

*

设置邮件服务器协议

* * @param protocolOption Mail server protocol * 邮件服务器协议 * * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public ServerConfigBuilder mailProtocol(final MailProtocol protocolOption) { if (!MailProtocol.UNKNOWN.equals(protocolOption)) { this.protocolOption = protocolOption; } return this; } /** *

Configure connection timeout(Unit: seconds)

*

设置连接超时时间(单位:秒)

* * @param connectionTimeout Connection timeout(Unit: seconds) * 连接超时时间(单位:秒) * * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public ServerConfigBuilder connectionTimeout(int connectionTimeout) { if (connectionTimeout > 0) { this.connectionTimeout = connectionTimeout; } return this; } /** *

Configure process timeout(Unit: seconds)

*

设置操作超时时间(单位:秒)

* * @param processTimeout Process timeout(Unit: seconds) * 操作超时时间(单位:秒) * * @return ServerConfigBuilder instance * 邮件服务器配置构建器实例对象 */ public ServerConfigBuilder processTimeout(int processTimeout) { if (processTimeout > 0) { this.processTimeout = processTimeout; } return this; } /** *

Confirm current configure information

*

确认当前配置信息

* * @throws BuilderException the builder exception */ protected void build() throws BuilderException { if (StringUtils.isEmpty(this.hostName)) { throw new BuilderException(0x0000000E0003L, "Host_Address_Unknown_Mail_Error"); } if (MailProtocol.UNKNOWN.equals(this.protocolOption)) { throw new BuilderException(0x0000000E0004L, "Protocol_Unknown_Mail_Error"); } MailConfig.ServerConfig serverConfig = new MailConfig.ServerConfig(); serverConfig.setHostName(this.hostName); serverConfig.setHostPort(this.hostPort); serverConfig.setSsl(this.ssl); serverConfig.setAuthLogin(this.authLogin); serverConfig.setProtocolOption(this.protocolOption); serverConfig.setConnectionTimeout(this.connectionTimeout); serverConfig.setProcessTimeout(this.processTimeout); super.parentBuilder.serverConfig(this.sendConfig, serverConfig); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy