📄 emailconnection.java
字号:
/*------------------------------------------------------------------------------Name: EmailConnection.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: Handles connection to xmlBlaster with plain emailsAuthor: xmlBlaster@marcelruff.info------------------------------------------------------------------------------*/package org.xmlBlaster.client.protocol.email;import org.xmlBlaster.util.Global;import org.xmlBlaster.util.Timestamp;import org.xmlBlaster.client.qos.ConnectReturnQos;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.def.ErrorCode;import org.xmlBlaster.util.def.MethodName;import org.xmlBlaster.util.def.Constants;import org.xmlBlaster.util.plugin.I_PluginConfig;import org.xmlBlaster.util.plugin.PluginInfo;import org.xmlBlaster.util.protocol.email.EmailExecutor;import org.xmlBlaster.util.protocol.socket.SocketExecutor;import org.xmlBlaster.util.MsgUnitRaw;import org.xmlBlaster.util.qos.address.Address;import org.xmlBlaster.client.protocol.I_XmlBlasterConnection;import java.io.IOException;import java.util.HashMap;import java.util.Properties;import java.util.logging.Level;import java.util.logging.Logger;/** * This driver sends emails to the xmlBlaster server, the return QOS are polled via POP3. * <p /> * This "email" driver needs to be registered in xmlBlaster.properties * and will be started on xmlBlaster startup: * <pre> * ClientProtocolPlugin[email][1.0]=org.xmlBlaster.client.protocol.email.EmailConnection * </pre> * <p /> * All adjustable parameters are explained in {@link org.xmlBlaster.client.protocol.email.EmailConnection#usage()} * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/protocol.email.html">The protocol.email requirement</a> * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>. */public class EmailConnection extends EmailExecutor implements I_XmlBlasterConnection{ private static Logger log = Logger.getLogger(EmailConnection.class.getName()); private String ME = "EmailConnection"; private Global glob; protected String loginName = "dummyLoginName"; private PluginInfo pluginInfo; private boolean isLoggedIn; private boolean isInitialized; /** * Called by plugin loader which calls init(Global, PluginInfo) thereafter. */ public EmailConnection() { } /** */ public String getLoginName() { return this.loginName; } /** Enforced by I_Plugin */ public String getType() { return getProtocol(); } /** Enforced by I_Plugin */ public String getVersion() { return (this.pluginInfo == null) ? "1.0" : this.pluginInfo.getVersion(); } /** * This method is called by the PluginManager (enforced by I_Plugin). * @see org.xmlBlaster.util.plugin.I_Plugin#init(org.xmlBlaster.util.Global,org.xmlBlaster.util.plugin.PluginInfo) */ public void init(org.xmlBlaster.util.Global glob, PluginInfo pluginInfo) throws XmlBlasterException { this.glob = (glob == null) ? Global.instance() : glob; this.pluginInfo = pluginInfo; if (log.isLoggable(Level.FINE)) log.fine("Entering init()"); } /** * Connects to xmlBlaster with one email connection. * @see I_XmlBlasterConnection#connectLowlevel(Address) */ public void connectLowlevel(Address address) throws XmlBlasterException { if (!this.isInitialized) { super.init(glob, address, this.pluginInfo); super.setEmailSessionId(""+new Timestamp().getTimestamp()); // Initially until the secret session id is known // Who are we? // We need to correct the mail addresses from EmailExecutor // as it assumes server side operation if (super.fromAddress == null) { throw new XmlBlasterException(glob, ErrorCode.USER_ILLEGALARGUMENT, ME, "Please configure a 'from' address with 'mail.smtp.from=xy@somehost.com'"); } // The email address to reach the xmlBlaster server //super.setTo(this.glob.get("mail.smtp.to", "xmlBlaster@localhost", null, // this.pluginInfo)); this.isInitialized = true; log.info("Initialized email connection from='" + super.fromAddress.toString() + "' to='" + super.toAddress.toString() + "'"); } } /** * Reset the driver on problems */ public void resetConnection() { shutdown(); } /** * @see I_XmlBlasterConnection#setConnectReturnQos(ConnectReturnQos) */ public void setConnectReturnQos(ConnectReturnQos connectReturnQos) { super.setSecretSessionId(connectReturnQos.getSecretSessionId()); super.setEmailSessionId(connectReturnQos.getSecretSessionId()); //super.setEmailSessionId(connectReturnQos.getSessionName().getRelativeName()); this.loginName = connectReturnQos.getSessionName().getLoginName(); this.ME = "EmailConnection-"+loginName; this.isLoggedIn = true; } /** * Login to the server. * <p /> * @param connectQos The encrypted connect QoS * @exception XmlBlasterException if login fails */ public String connect(String connectQos) throws XmlBlasterException { if (connectQos == null) throw new XmlBlasterException(glob, ErrorCode.USER_ILLEGALARGUMENT, ME+".connect()", "Please specify a valid QoS"); if (log.isLoggable(Level.FINER)) log.finer("Entering connect("+connectQos+")"); if (isLoggedIn()) { log.warning("You are already logged in, we try again"); Thread.dumpStack(); } connectLowlevel(null); try { // sessionId is usually null on login, on reconnect != null String ret = (String)super.sendEmail(connectQos, MethodName.CONNECT, SocketExecutor.WAIT_ON_RESPONSE); //super.setEmailSessionId(super.getSecretSessionId()); return ret;/* MsgInfo parser = new MsgInfo(glob, MsgInfo.INVOKE_BYTE, MethodName.CONNECT, sessionId); parser.addQos(connectQos); return (String)super.execute(parser, SocketExecutor.WAIT_ON_RESPONSE, SocketUrl.SOCKET_TCP); */ } catch (XmlBlasterException e) { throw e; } catch (Throwable e) { if (!(e instanceof IOException) && !(e instanceof java.net.ConnectException)) e.printStackTrace(); if (log.isLoggable(Level.FINE)) log.fine(e.toString()); throw new XmlBlasterException(glob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, "login failed", e); } } /** * Returns the protocol type. * @return "email" */ public final String getProtocol() { return (this.pluginInfo == null) ? "email" : this.pluginInfo.getType(); } /** * Does a logout and removes the callback server. * <p /> * @param sessionId The client sessionId */ public boolean disconnect(String qos) throws XmlBlasterException { if (log.isLoggable(Level.FINER)) log.finer("Entering logout/disconnect: id=" + getSecretSessionId()); if (!isLoggedIn()) { log.warning("You are not logged in, no logout possible."); return false; } try { super.sendEmail(qos, MethodName.DISCONNECT, SocketExecutor.WAIT_ON_RESPONSE); //MsgInfo parser = new MsgInfo(glob, MsgInfo.INVOKE_BYTE, MethodName.DISCONNECT, sessionId); //parser.addQos((qos==null)?"":qos); //super.execute(parser, SocketExecutor.WAIT_ON_RESPONSE/*ONEWAY*/, SocketUrl.SOCKET_TCP); return true; } catch (XmlBlasterException e) { throw new XmlBlasterException(glob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, "disconnect", e); } finally { // shutdown(); // the callback server // sessionId = null; } } /** * Shut down the callback server. * Is called by logout() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -