simplemaillistener.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 559 行 · 第 1/2 页

JAVA
559
字号
/*
 * Licensed to the Apache Software Foundation (ASF) 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.apache.axis2.transport.mail;

import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.builder.BuilderUtil;
import org.apache.axis2.context.*;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.*;
import org.apache.axis2.i18n.Messages;
import org.apache.axis2.transport.TransportListener;
import org.apache.axis2.transport.TransportUtils;
import org.apache.axis2.util.Utils;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

/**
 * This is the implementation for Mail Listener in Axis2. It has the full capability
 * of connecting to a POP3 or IMPA server with SSL or regualar connection. This listener intend
 * to use as a server in client side as well with the involcation is Async with addressing.
 */


public class SimpleMailListener implements Runnable, TransportListener {
    private static final Log log = LogFactory.getLog(SimpleMailListener.class);

    private ConfigurationContext configurationContext = null;

    private boolean running = true;
    /*password and replyTo is Axis2 specific*/
    private String user = "";
    private String replyTo = "";

    /*This hold properties for pop3 or impa server connection*/
    private Properties pop3Properties = new Properties();

    private final EmailReceiver receiver ;

    /**
     * Time has been put from best guest. Let the default be 3 mins.
     * This value is configuralble from Axis2.xml. Under mail transport listener
     * simply set the following parameter.
     * <parameter name="transport.listener.interval">[custom listener interval]</parameter>
     */
    private int listenerWaitInterval = 1000 * 60 * 3;

    public SimpleMailListener() {
          receiver = new EmailReceiver();
    }

    public void init(ConfigurationContext configurationContext, TransportInDescription transportIn)
            throws AxisFault {
        this.configurationContext = configurationContext;

        ArrayList mailParameters = transportIn.getParameters();

        String password = "";
        String host = "";
        String protocol = "";
        String port = "";
        URLName urlName;

        for (Iterator iterator = mailParameters.iterator(); iterator.hasNext();) {
            Parameter param = (Parameter) iterator.next();
            String paramKey = param.getName();
            String paramValue = Utils.getParameterValue(param);
            if (paramKey == null || paramValue == null) {
                String error = Messages.getMessage("canNotBeNull", "Parameter name and value");
                log.error(error);
                throw new AxisFault(error);

            }
            pop3Properties.setProperty(paramKey, paramValue);
            if (paramKey.equals(org.apache.axis2.transport.mail.Constants.POP3_USER)) {
                user = paramValue;
            }
            if (paramKey.equals(org.apache.axis2.transport.mail.Constants.POP3_PASSWORD)) {
                password = paramValue;
            }
            if (paramKey.equals(org.apache.axis2.transport.mail.Constants.POP3_HOST)) {
                host = paramValue;
            }
            if (paramKey.equals(org.apache.axis2.transport.mail.Constants.STORE_PROTOCOL)) {
                protocol = paramValue;
            }
            if (paramKey.equals(org.apache.axis2.transport.mail.Constants.POP3_PORT)) {
                port = paramValue;
            }

            //Transport specific
            if (paramKey.equals(org.apache.axis2.transport.mail.Constants.REPLY_TO)) {
                replyTo = paramValue;
            }
            if (paramKey.equals(org.apache.axis2.transport.mail.Constants.LISTENER_INTERVAL)) {
                listenerWaitInterval = Integer.parseInt(paramValue);
            }

        }
        if (password.length() == 0 || user.length() == 0 || host.length() == 0 ||
            protocol.length() == 0) {
            String error = SimpleMailListener.class.getName() +
                           " one or more of Password, User, Host and Protocol are null or empty";
            log.error(error);
            throw new AxisFault(error);
        }

        if (port.length() == 0) {
            urlName = new URLName(protocol, host, -1, "", user, password);
        } else {
            urlName = new URLName(protocol, host, Integer.parseInt(port), "", user, password);
        }

        receiver.setPop3Properties(pop3Properties);
        receiver.setUrlName(urlName);
        Object obj = configurationContext.
                getProperty(org.apache.axis2.transport.mail.Constants.MAPPING_TABLE);
        if (obj == null) {
            configurationContext.setProperty(
                    org.apache.axis2.transport.mail.Constants.MAPPING_TABLE, new Hashtable());
        }

        Object callBackTable = configurationContext.
                getProperty(org.apache.axis2.transport.mail.Constants.CALLBACK_TABLE);
        if (callBackTable == null) {
            configurationContext.setProperty(
                    org.apache.axis2.transport.mail.Constants.CALLBACK_TABLE, new Hashtable());
        }


    }

    public void initFromRuntime(Properties properties, MessageContext msgContext) throws AxisFault {

        this.configurationContext = msgContext.getConfigurationContext();

        String password = "";
        String host = "";
        String protocol = "";
        String port = "";
        URLName urlName;

        pop3Properties.clear();
        pop3Properties.putAll(properties);

        user = properties.getProperty(org.apache.axis2.transport.mail.Constants.POP3_USER);
        password = properties.getProperty(org.apache.axis2.transport.mail.Constants.POP3_PASSWORD);
        host = properties.getProperty(org.apache.axis2.transport.mail.Constants.POP3_HOST);
        protocol = properties.getProperty(org.apache.axis2.transport.mail.Constants.STORE_PROTOCOL);
        port = properties.getProperty(org.apache.axis2.transport.mail.Constants.POP3_PORT);
        replyTo = properties.getProperty(org.apache.axis2.transport.mail.Constants.REPLY_TO);
        String value =
                properties.getProperty(org.apache.axis2.transport.mail.Constants.LISTENER_INTERVAL);
        if (value != null) {
            listenerWaitInterval = Integer.parseInt(value);
        }

        if (password.length() == 0 || user.length() == 0 || host.length() == 0 ||
            protocol.length() == 0) {
            String error = SimpleMailListener.class.getName() + " one or more of Password, User," +
                           " Host and Protocol are null or empty" + "in runtime settings";
            log.error(error);
            throw new AxisFault(error);
        }

        if (port == null) {
            urlName = new URLName(protocol, host, -1, "", user, password);
        } else {
            urlName = new URLName(protocol, host, Integer.parseInt(port), "", user, password);
        }

        receiver.setPop3Properties(pop3Properties);
        receiver.setUrlName(urlName);
        Object obj = configurationContext.
                getProperty(org.apache.axis2.transport.mail.Constants.MAPPING_TABLE);
        if (obj == null) {
            configurationContext.setProperty(
                    org.apache.axis2.transport.mail.Constants.MAPPING_TABLE, new Hashtable());
        }
        Object callBackTable = configurationContext.
                getProperty(org.apache.axis2.transport.mail.Constants.CALLBACK_TABLE);
        if (callBackTable == null) {
            configurationContext.setProperty(
                    org.apache.axis2.transport.mail.Constants.CALLBACK_TABLE, new Hashtable());
        }
    }

    /**
     * Server process.
     */
    public static void main(String args[]) throws AxisFault {
        if (args.length < 2) {
            log.info("java SimpleMailListener <repository>");
            printUsage();
        } else {
            String path = args[0];
            String axis2xml = args[1];
            ConfigurationContext configurationContext;
            File repo = new File(path);
            if (repo.exists()) {
                configurationContext =
                        ConfigurationContextFactory
                                .createConfigurationContextFromFileSystem(path, axis2xml);
            } else {
                printUsage();
                throw new AxisFault("repository not found");
            }
            SimpleMailListener sas = new SimpleMailListener();
            TransportInDescription transportIn =
                    configurationContext.
                            getAxisConfiguration().getTransportIn(Constants.TRANSPORT_MAIL);
            if (transportIn != null) {
                sas.init(configurationContext, transportIn);
                log.info("Starting the SimpleMailListener with repository "
                         + new File(args[0]).getAbsolutePath());
                sas.start();
            } else {
                log.info(
                        "Startup failed, mail transport not configured, Configure the mail trnasport in the axis2.xml file");
            }
        }
    }

    private static void printUsage() {
        System.out.println("Please provide the repository location and axis2.xml location ");
    }

    /**
     * Accept requests from a given TCP port and send them through the Axis
     * engine for processing.
     */
    public void run() {

        // Accept and process requests from the socket
        if (running) {
            log.info("Mail listner strated to listen to the address " + user);
        }

        while (running) {
            log.info("Info started polling");
            try {
                synchronized (receiver) {
                    receiver.connect();

                    Message[] msgs = receiver.receiveMessages();

                    if ((msgs != null) && (msgs.length > 0)) {

⌨️ 快捷键说明

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