Example usage for org.apache.commons.net.smtp SMTPSClient getReplyString

List of usage examples for org.apache.commons.net.smtp SMTPSClient getReplyString

Introduction

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

Prototype

public String getReplyString() 

Source Link

Document

Returns the entire text of the last SMTP server response exactly as it was received, including all end of line markers in NETASCII format.

Usage

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

/**
 * SMTPS????????//from  w  ww.  j  a v  a2  s .  co  m
 * 
 * @param addressText
 * @return SMTPS
 */
@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 SMTPS Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        SMTPSClient client = new SMTPSClient(true);

        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();

                int reply = client.getReplyCode();

                if (SMTPReply.isPositiveCompletion(reply)) {
                    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;
                } else {
                    retry = false;
                    isReachable = false;
                }

            } 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 + "(SMTPS/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.warn("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage(), e);

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

        return false;
    }
}

From source file:me.schiz.jmeter.protocol.smtp.sampler.SMTPSampler.java

private SampleResult sampleCommand(SampleResult sr) {
    SocketClient soclient = SessionStorage.getInstance().getClient(getSOClient());
    SMTPClient client = null;//from  w w w.  j a v  a2s .  c  om
    int responseCode = 0;
    if (soclient instanceof SMTPClient)
        client = (SMTPClient) soclient;

    String request = "COMMAND\n";
    request += "Client : " + getClient() + "\n";
    request += "Command : " + getCommand() + "\n";
    sr.setRequestHeaders(request);
    if (client == null) {
        sr.setResponseCode("404");
        sr.setResponseData(("Client `" + getClient() + "` not found").getBytes());
        sr.setSuccessful(false);
        return sr;
    } else {
        synchronized (client) {
            sr.sampleStart();
            try {
                responseCode = client.sendCommand(getCommand());
                sr.setResponseCode(String.valueOf(responseCode));
                sr.setSuccessful(SMTPReply.isPositiveIntermediate(responseCode));
                String response = client.getReplyString();
                setSuccessfulByResponseCode(sr, client.getReplyCode());

                if (SessionStorage.getInstance()
                        .getClientType(getSOClient()) == SessionStorage.proto_type.STARTTLS) {
                    String command;
                    if (getCommand().indexOf(' ') != -1)
                        command = getCommand().substring(0, getCommand().indexOf(' '));
                    else
                        command = getCommand();
                    if ((command.equalsIgnoreCase("lhlo") || command.equalsIgnoreCase("ehlo")
                            || command.equalsIgnoreCase("helo")) && getUseSTARTTLS()
                            && client instanceof SMTPSClient) {
                        SMTPSClient sclient = (SMTPSClient) client;
                        if (sclient.execTLS() == false) {
                            sr.setSuccessful(false);
                            sr.setResponseCode("403");
                            ;
                            response += sclient.getReplyString();
                            log.error("client `" + client + "` STARTTLS failed");
                            removeClient();
                        } else {
                            response += "\nSTARTTLS OK";
                        }
                    }
                }
                sr.setResponseData(response.getBytes());
            } catch (IOException e) {
                sr.setSuccessful(false);
                sr.setResponseData(e.toString().getBytes());
                sr.setResponseCode(e.getClass().getName());
                log.error("client `" + client + "` ", e);
                removeClient();
            }
            sr.sampleEnd();
        }
    }
    return sr;
}