⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emailservice.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF)                              *//* (C) Copyright IBM Corp. 2002, 2004, 2005                                  *//*                                                                           *//* This software is licensed under the Common Public License (CPL) V1.0.     *//*****************************************************************************/package com.ibm.staf.service.email;import com.ibm.staf.*;import com.ibm.staf.service.*;import java.io.*;import java.util.*;import java.net.*;import org.apache.commons.codec.binary.Base64;public class EmailService implements STAFServiceInterfaceLevel30{    private STAFHandle fHandle;    private final String kVersion = "3.2.0";    private String fServiceName = "";    private String fLocalMachineName = "";    private STAFCommandParser fSendParser;    private STAFCommandParser fListParser;    private STAFCommandParser fParmsParser;    private STAFCommandParser fSetParser;    private String fLineSep;    private STAFMapClassDefinition fSettingsMapClass;    private static Socket fSocket;    private static BufferedReader fInStream;    private static PrintStream fOutStream;    private static String fHostname;    private Base64 base64 = new Base64();    private static String fMailserver = "";    private static int fMailport = 25;    private static boolean fResolveMessage = true;    static final String sSettingsMapClassName = new String(        "STAF/Service/Email/Settings");    static final String sTextPlainContentType = "text/plain";    static final String sTextHtmlContentType = "text/html";    private static String fContentType = sTextPlainContentType;    private static String fLineEnd = "";    public static String lineSep;    private final String sPlainHeader =        "******************************************************************\n" +        "* DO NOT RESPOND TO THE SERVICE MACHINE THAT GENERATED THIS NOTE *\n" +        "*****************************************************" +        "*************\n";    private final String sHtmlHeader =        "<pre>*****************************************************" +        "*************\n"+        "* DO NOT RESPOND TO THE SERVICE MACHINE THAT GENERATED THIS NOTE *\n" +        "*****************************************************" +        "*************\n</pre>";    public static final int IOEXCEPTION = 4001;    private static final String IOEXCEPTIONInfo = "IO Exception";    private static final String IOEXCEPTIONDesc =       "An IO Exception occured while sending the email.";    public EmailService() {}    public STAFResult init(STAFServiceInterfaceLevel30.InitInfo info)    {        int rc = STAFResult.Ok;        try        {            fHandle = new STAFHandle("STAF/SERVICE/" + info.name);        }        catch (STAFException e)        {            return new STAFResult(STAFResult.STAFRegistrationError, e.toString());        }        STAFResult res = new STAFResult();        // Resolve the line separator variable for the local machine        res = STAFUtil.resolveInitVar("{STAF/Config/Sep/Line}", fHandle);        if (res.rc != STAFResult.Ok) return res;        lineSep = res.result;        fServiceName = info.name;        // Send parser        fSendParser = new STAFCommandParser(0, false);        fSendParser.addOption("SEND", 1,                              STAFCommandParser.VALUENOTALLOWED);        fSendParser.addOption("TO", 0,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("FROM", 1,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("SUBJECT", 1,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("MESSAGE", 1,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("FILE", 1,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("MACHINE", 1,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("NOHEADER", 1,                              STAFCommandParser.VALUENOTALLOWED);        fSendParser.addOption("CONTENTTYPE", 1,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("TEXTATTACHMENT", 0,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("BINARYATTACHMENT", 0,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("ATTACHMENTMACHINE", 1,                              STAFCommandParser.VALUEREQUIRED);        fSendParser.addOption("RESOLVEMESSAGE", 1,                              STAFCommandParser.VALUENOTALLOWED);        fSendParser.addOption("NORESOLVEMESSAGE", 1,                              STAFCommandParser.VALUENOTALLOWED);        fSendParser.addOptionGroup("RESOLVEMESSAGE NORESOLVEMESSAGE", 0, 1);        fSendParser.addOptionGroup("MESSAGE FILE", 1, 1);        fSendParser.addOptionNeed("MACHINE", "FILE");        fSendParser.addOptionNeed("ATTACHMENTMACHINE",                                   "TEXTATTACHMENT BINARYATTACHMENT");        // List parser        fListParser = new STAFCommandParser(0, false);        fListParser.addOption("LIST", 1, STAFCommandParser.VALUENOTALLOWED);        fListParser.addOption("SETTINGS", 1, STAFCommandParser.VALUENOTALLOWED);        fListParser.addOptionNeed("SETTINGS", "LIST");        // Set parser        fSetParser = new STAFCommandParser(0, false);        fSetParser.addOption("SET", 1,                             STAFCommandParser.VALUENOTALLOWED);        fSetParser.addOption("MAILSERVER", 1,                             STAFCommandParser.VALUEREQUIRED);        fSetParser.addOption("PORT", 1,                             STAFCommandParser.VALUEREQUIRED);        fSetParser.addOption("CONTENTTYPE", 1,                             STAFCommandParser.VALUEREQUIRED);        fSetParser.addOption("RESOLVEMESSAGE", 1,                             STAFCommandParser.VALUENOTALLOWED);        fSetParser.addOption("NORESOLVEMESSAGE", 1,                             STAFCommandParser.VALUENOTALLOWED);        fSetParser.addOption("LINEEND", 1,                             STAFCommandParser.VALUEREQUIRED);        fSetParser.addOptionGroup("RESOLVEMESSAGE NORESOLVEMESSAGE", 0, 1);        // Resolve the line separator variable for the local machine                 res = STAFUtil.resolveInitVar("{STAF/Config/Sep/Line}", fHandle);        if (res.rc != STAFResult.Ok) return res;                fLineSep = res.result;                 // Resolve the machine name variable for the local machine        res = STAFUtil.resolveInitVar("{STAF/Config/Machine}", fHandle);        if (res.rc != STAFResult.Ok) return res;                fLocalMachineName = res.result;        // Parse the parameters specified when registering the service        if (info.parms == null)        {            return new STAFResult(                STAFResult.ServiceConfigurationError,                "MAILSERVER is a required parameter for the STAFEmail service");        }        else        {            fParmsParser = new STAFCommandParser(0, false);            fParmsParser.addOption("MAILSERVER", 1,                STAFCommandParser.VALUEREQUIRED);            fParmsParser.addOption("PORT", 1,                STAFCommandParser.VALUEREQUIRED);            fParmsParser.addOption("CONTENTTYPE", 1,                STAFCommandParser.VALUEREQUIRED);            fParmsParser.addOption("LINEEND", 1,                STAFCommandParser.VALUEREQUIRED);            fParmsParser.addOption("RESOLVEMESSAGE", 1,                STAFCommandParser.VALUENOTALLOWED);            fParmsParser.addOption("NORESOLVEMESSAGE", 1,                STAFCommandParser.VALUENOTALLOWED);            // DEBUG is deprecated            fParmsParser.addOption("DEBUG", 1,                STAFCommandParser.VALUENOTALLOWED);            fParmsParser.addOptionGroup("RESOLVEMESSAGE NORESOLVEMESSAGE", 0, 1);            res = handleParms(info);            if (res.rc != STAFResult.Ok)            {                return new STAFResult(                    STAFResult.ServiceConfigurationError,                    "Error validating parameters: RC=" + res.rc +                    ", Result=" + res.result);            }            String message = fServiceName + " service initialized, using " +                "mailserver " + fMailserver + " port " + fMailport;            fHandle.submit2("local", "LOG", "LOG MACHINE LOGNAME " +                        fServiceName + " LEVEL info MESSAGE " +                        STAFUtil.wrapData(message));        }        /* Register RCs with the HELP service */        rc = this.registerHelp(info.name);        if (rc != STAFResult.Ok)            return new STAFResult(rc,                                    "Error registering RCs with HELP service.");        // Construct map-class for list settings information        fSettingsMapClass = new STAFMapClassDefinition(            sSettingsMapClassName);        fSettingsMapClass.addKey("mailServer", "Mail Server");        fSettingsMapClass.addKey("port", "Port");        fSettingsMapClass.addKey("contentType", "Content Type");        fSettingsMapClass.addKey("resolveMessage", "Resolve Message");        return new STAFResult(rc);    }        private int registerHelp(String name)    {        try        {            String request = "REGISTER SERVICE " + name + " ERROR " +                IOEXCEPTION + " INFO \"" + IOEXCEPTIONInfo + "\" DESCRIPTION \""                + IOEXCEPTIONDesc + "\"";            fHandle.submit("LOCAL", "HELP", request);        }        catch(STAFException se)        {            return se.rc;        }        return STAFResult.Ok;    }    private STAFResult handleParms(STAFServiceInterfaceLevel30.InitInfo info)    {        STAFCommandParseResult parseResult= fParmsParser.parse(info.parms);        String errmsg = "ERROR:  Service Configuration Error for Service " +                        fServiceName + lineSep +                        "EmailService::handleParms() - ";        if (parseResult.rc != STAFResult.Ok)        {            return new STAFResult(STAFResult.InvalidRequestString,                                  parseResult.errorBuffer);        }        if (parseResult.optionTimes("MAILSERVER") == 0)        {            String errorMsg = "MAILSERVER is a required parameter for the " +                "STAFEmail service";            return new STAFResult(                STAFResult.ServiceConfigurationError, errorMsg);        }        else        {            STAFResult resolvedResult = STAFUtil.resolveInitVar(                parseResult.optionValue("MAILSERVER"), fHandle);            if (resolvedResult.rc != STAFResult.Ok)            {                System.out.println(                    errmsg + "Error resolving MAILSERVER.  RC=" +                    resolvedResult.rc + " Result=" + resolvedResult.result);                return resolvedResult;            }            fMailserver = resolvedResult.result;        }        if (parseResult.optionTimes("LINEEND") > 0)        {            STAFResult resolvedResult = STAFUtil.resolveInitVar(                parseResult.optionValue("LINEEND"), fHandle);            if (resolvedResult.rc != STAFResult.Ok)            {                System.out.println(                    errmsg + "Error resolving LINEEND.  RC=" +                    resolvedResult.rc + " Result=" + resolvedResult.result);                return resolvedResult;            }            fLineEnd = resolvedResult.result;        }        if (parseResult.optionTimes("RESOLVEMESSAGE") > 0)        {            fResolveMessage = true;        }        else if (parseResult.optionTimes("NORESOLVEMESSAGE") > 0)        {            fResolveMessage = false;        }        // DEBUG parm is deprecated        if (parseResult.optionTimes("PORT") > 0)        {            STAFResult resolvedResult = STAFUtil.resolveInitVar(                parseResult.optionValue("PORT"), fHandle);            if (resolvedResult.rc != STAFResult.Ok)            {                System.out.println(                    errmsg + "Error resolving PORT.  RC=" +                    resolvedResult.rc + " Result=" + resolvedResult.result);                return resolvedResult;            }            fMailport = (new Integer(resolvedResult.result).intValue());        }        if (parseResult.optionTimes("CONTENTTYPE") > 0)        {            STAFResult resolvedResult = STAFUtil.resolveInitVar(                parseResult.optionValue("CONTENTTYPE"), fHandle);            if (resolvedResult.rc != STAFResult.Ok)            {                System.out.println(                    errmsg + "Error resolving CONTENTTYPE.  RC=" +                    resolvedResult.rc + " Result=" + resolvedResult.result);                return resolvedResult;            }             if (!(resolvedResult.result.equals(sTextPlainContentType)) &&                !(resolvedResult.result.equals(sTextHtmlContentType)))            {                return new STAFResult(STAFResult.InvalidValue,                                       resolvedResult.result);            }            fContentType = resolvedResult.result;        }        return new STAFResult(STAFResult.Ok);    }    public STAFResult acceptRequest(STAFServiceInterfaceLevel30.RequestInfo info)    {        String lowerRequest = info.request.toLowerCase();        // call the appropriate method to handle the command        if (lowerRequest.startsWith("send"))        {            return handleSend(info);        }        else if (lowerRequest.startsWith("list"))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -