Example usage for org.apache.commons.net.pop3 POP3Client getReplyString

List of usage examples for org.apache.commons.net.pop3 POP3Client getReplyString

Introduction

In this page you can find the example usage for org.apache.commons.net.pop3 POP3Client getReplyString.

Prototype

public String getReplyString() 

Source Link

Document

Returns the reply to the last command sent to the server.

Usage

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

/**
 * POP3????????/*from  w w w. jav  a 2 s  .  c  om*/
 * 
 * @param addressText
 * @return POP3
 */
@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 POP3 Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        POP3Client client = new POP3Client();

        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 + "(POP3/" + 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:ProtocolRunner.java

private static void handlePOP3(
    POP3Client client, 
    ProtocolRunner runner, /*w  w w . j av a2s .c o m*/
    String commandString) 
    throws IOException {
        
    if(commandString == null) { // means just connected 
            return;            
    } else { // need to handle a command
        client.sendCommand(commandString);
    }
        
    runner.getTCPServerResponse().append(client.getReplyString());
}

From source file:me.schiz.jmeter.protocol.pop3.sampler.POP3Sampler.java

private SampleResult sampleConnect(SampleResult sr) {
    POP3Client client;

    if (getUseSSL()) {
        client = new POP3SClient(true);
        //      } else if(getUseSTARTTLS()) {
        //         client = new POP3SClient(false);
    } else {//w  w  w .  j av  a2  s  . c om
        client = new POP3Client();
    }

    StringBuilder requestBuilder = new StringBuilder();
    try {
        //String request = "CONNECT \n";
        requestBuilder.append("CONNECT\n");
        requestBuilder.append("Host : " + getHostname() + ":" + getPort() + "\n");
        requestBuilder.append("Connect Timeout: " + getConnectionTimeout() + "\n");
        requestBuilder.append("Socket Timeout: " + getSoTimeout() + "\n");
        requestBuilder.append("Client : " + getClient() + "\n");
        if (getUseSSL())
            requestBuilder.append("SSL : true\n");
        else
            requestBuilder.append("SSL : false\n");
        //         if(getUseSTARTTLS())    request += "STARTTLS : true\n";
        //         else request += "STARTTLS : false\n";

        sr.setRequestHeaders(requestBuilder.toString());
        sr.sampleStart();
        client.setConnectTimeout(getConnectionTimeout());
        client.connect(getHostname(), getPort());
        if (client.isConnected()) {
            SessionStorage.proto_type protoType = SessionStorage.proto_type.PLAIN;
            if (getUseSSL())
                protoType = SessionStorage.proto_type.SSL;
            //            if(getUseSSL() && !getUseSTARTTLS()) protoType = SessionStorage.proto_type.SSL;
            //            if(!getUseSSL() && getUseSTARTTLS()) protoType = SessionStorage.proto_type.STARTTLS;

            SessionStorage.getInstance().putClient(getSOClient(), client, protoType);
            client.setSoTimeout(getSoTimeout());
            client.setTcpNoDelay(getTcpNoDelay());
            sr.setResponseCode(RC_200);
            sr.setResponseData(client.getReplyString().getBytes());
            sr.setSuccessful(true);
        } else {
            sr.setResponseCode(RC_500);
            sr.setSuccessful(false);
        }
    } catch (SocketException se) {
        sr.setResponseMessage(se.toString());
        sr.setSuccessful(false);
        sr.setResponseCode(se.getClass().getName());
        log.error("client `" + client + "` ", se);
    } catch (IOException ioe) {
        sr.setResponseMessage(ioe.toString());
        sr.setSuccessful(false);
        sr.setResponseCode(ioe.getClass().getName());
        log.error("client `" + client + "` ", ioe);
    }
    sr.sampleEnd();
    return sr;
}

From source file:org.apache.james.protocols.pop3.AbstractPOP3ServerTest.java

@Test
public void testAPop() throws Exception {
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;/*from   w w  w. ja  v  a  2 s  .c om*/
    try {
        TestApopCmdHandler handler = new TestApopCmdHandler();
        server = createServer(createProtocol(handler), address);
        server.bind();

        POP3Client client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        String welcomeMessage = client.getReplyString();

        // check for valid syntax that include all info needed for APOP
        assertThat(welcomeMessage.trim()).matches(Pattern.compile("\\+OK \\<-?\\d+\\.\\d+@.+\\> .+"));

        assertThat(client.sendCommand("APOP invalid invalid")).isEqualTo(POP3Reply.ERROR);

        handler.add("valid", new MockMailbox("id"));
        assertThat(client.sendCommand("APOP valid valid")).isEqualTo(POP3Reply.OK);

        assertThat(client.logout()).isTrue();

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}