Example usage for org.apache.commons.net.imap IMAPClient connect

List of usage examples for org.apache.commons.net.imap IMAPClient connect

Introduction

In this page you can find the example usage for org.apache.commons.net.imap IMAPClient connect.

Prototype

public void connect(InetAddress host, int port) throws SocketException, IOException 

Source Link

Document

Opens a Socket connected to a remote host at the specified port and originating from the current host at a system assigned port.

Usage

From source file:com.clustercontrol.port.protocol.ReachAddressIMAP.java

/**
 * IMAP????????//ww  w  . jav  a2s .  c  o m
 * 
 * @param addressText
 * @return IMAP
 */
@Override
protected boolean isRunning(String addressText) {

    m_message = "";
    m_messageOrg = "";
    m_response = -1;

    boolean isReachable = false;

    try {
        long start = 0; // 
        long end = 0; // 
        boolean retry = true; // ????(true:??false:???)

        StringBuffer bufferOrg = new StringBuffer(); // 
        String result = "";

        InetAddress address = InetAddress.getByName(addressText);

        bufferOrg.append("Monitoring the IMAP Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        IMAPClient client = new IMAPClient();

        for (int i = 0; i < m_sentCount && retry; i++) {
            try {
                bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: ");
                client.setDefaultTimeout(m_timeout);

                start = HinemosTime.currentTimeMillis();
                client.connect(address, m_portNo);
                end = HinemosTime.currentTimeMillis();

                m_response = end - start;

                result = client.getReplyString();

                if (m_response > 0) {
                    if (m_response < m_timeout) {
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    } else {
                        m_response = m_timeout;
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    }
                } else {
                    result = result + ("\n" + "Response Time < 1ms");
                }

                retry = false;
                isReachable = true;

            } catch (SocketException e) {
                result = (e.getMessage() + "[SocketException]");
                retry = true;
                isReachable = false;
            } catch (IOException e) {
                result = (e.getMessage() + "[IOException]");
                retry = true;
                isReachable = false;
            } finally {
                bufferOrg.append(result + "\n");
                if (client.isConnected()) {
                    try {
                        client.disconnect();
                    } catch (IOException e) {
                        m_log.warn("isRunning(): " + "socket disconnect failed: " + e.getMessage(), e);
                    }
                }
            }

            if (i < m_sentCount - 1 && retry) {
                try {
                    Thread.sleep(m_sentInterval);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

        m_message = result + "(IMAP/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage());

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage()
                + ")";

        return false;
    }
}

From source file:me.schiz.jmeter.protocol.imap.sampler.IMAPSampler.java

private SampleResult sampleConnect(SampleResult sr) {
    IMAPClient client;
    if (getUseSSL()) {
        client = new IMAPSClient(true);
    } else {/*  w  w  w  . ja  v a  2 s .c  om*/
        client = new IMAPClient();
    }
    try {
        String request = "CONNECT \n";
        request += "Host : " + getHostname() + ":" + getPort() + "\n";
        request += "Default Timeout : " + getDefaultTimeout() + "\n";
        request += "Connect Timeout : " + getConnectionTimeout() + "\n";
        request += "So Timeout : " + getSoTimeout() + "\n";
        request += "Client : " + getClient() + "\n";

        sr.setRequestHeaders(request);
        sr.sampleStart();
        client.setDefaultTimeout(getDefaultTimeout());
        client.setConnectTimeout(getConnectionTimeout());
        if (getLocalAddr().isEmpty())
            client.connect(getHostname(), getPort());
        else
            client.connect(getHostname(), getPort(), InetAddress.getByName(getLocalAddr()), 0);
        if (client.isConnected()) {
            log.info("imap client " + getClient() + " connected from " + client.getLocalAddress() + ":"
                    + client.getLocalPort());
            SessionStorage.proto_type protoType = SessionStorage.proto_type.PLAIN;
            if (getUseSSL())
                protoType = SessionStorage.proto_type.SSL;
            SessionStorage.getInstance().putClient(getSOClient(), client, protoType);
            client.setSoTimeout(getSoTimeout());
            sr.setSuccessful(true);
            sr.setResponseCodeOK();
            sr.setResponseData(client.getReplyString().getBytes());
        } else {
            client.close();
            sr.setSuccessful(false);
            sr.setResponseCode("java.net.ConnectException");
            sr.setResponseMessage("Not connected");
        }
    } catch (SocketException se) {
        sr.setResponseMessage(se.toString());
        sr.setSuccessful(false);
        sr.setResponseCode(se.getClass().getName());
        log.error("client `" + getClient() + "` ", se);
        removeClient();
    } catch (IOException ioe) {
        sr.setResponseMessage(ioe.toString());
        sr.setSuccessful(false);
        sr.setResponseCode(ioe.getClass().getName());
        log.error("client `" + getClient() + "` ", ioe);
        removeClient();
    } finally {
        sr.sampleEnd();
    }
    return sr;
}

From source file:org.apache.james.ESReporterTest.java

@Test
public void timeMetricsShouldBeReportedWhenImapCommandsReceived() throws Exception {
    IMAPClient client = new IMAPClient();
    client.connect(InetAddress.getLocalHost(), IMAP_PORT);
    client.login(USERNAME, PASSWORD);/*  w  w  w .  j a  v  a2s  . com*/

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            try {
                client.list("", "*");
            } catch (Exception e) {
                LOGGER.error("Error while sending LIST command", e);
            }
        }
    };
    timer.schedule(timerTask, DELAY_IN_MS, PERIOD_IN_MS);

    await().atMost(Duration.TEN_MINUTES).until(() -> checkMetricRecordedInElasticSearch());
}