Example usage for org.apache.commons.net.smtp SMTPReply isPositiveIntermediate

List of usage examples for org.apache.commons.net.smtp SMTPReply isPositiveIntermediate

Introduction

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

Prototype

public static boolean isPositiveIntermediate(int reply) 

Source Link

Document

Determine if a reply code is a positive intermediate response.

Usage

From source file:de.burlov.ultracipher.core.mail.AuthenticatingSMTPClient.java

/**
 * Authenticate to the SMTP server by sending the AUTH command with the
 * selected mechanism, using the given username and the given password.
 * <p/>//from   w ww. j av  a  2s . co  m
 *
 * @return True if successfully completed, false if not.
 * @throws SMTPConnectionClosedException              If the SMTP server prematurely closes the connection as a
 *                                                    result of the client being idle or some other reason
 *                                                    causing the server to send SMTP reply code 421. This
 *                                                    exception may be caught either as an IOException or
 *                                                    independently as itself.
 * @throws java.io.IOException                        If an I/O error occurs while either sending a command to
 *                                                    the server or receiving a reply from the server.
 * @throws java.security.NoSuchAlgorithmException     If the CRAM hash algorithm cannot be instantiated by the
 *                                                    Java runtime system.
 * @throws java.security.InvalidKeyException          If the CRAM hash algorithm failed to use the given
 *                                                    password.
 * @throws java.security.spec.InvalidKeySpecException If the CRAM hash algorithm failed to use the given
 *                                                    password.
 *                                                    *
 */
public boolean auth(AUTH_METHOD method, String username, String password)
        throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    if (!SMTPReply.isPositiveIntermediate(sendCommand(SMTPCommand.AUTH, AUTH_METHOD.getAuthName(method)))) {
        return false;
    }

    if (method.equals(AUTH_METHOD.PLAIN)) {
        // the server sends an empty response ("334 "), so we don't have to
        // read it.
        return SMTPReply.isPositiveCompletion(sendCommand(
                new String(Base64.encodeBase64(("\000" + username + "\000" + password).getBytes()))));
    } else if (method.equals(AUTH_METHOD.CRAM_MD5)) {
        // get the CRAM challenge
        byte[] serverChallenge = Base64.decodeBase64(getReplyString().substring(4).trim());
        // get the Mac instance
        Mac hmac_md5 = Mac.getInstance("HmacMD5");
        hmac_md5.init(new SecretKeySpec(password.getBytes(), "HmacMD5"));
        // compute the result:
        byte[] hmacResult = _convertToHexString(hmac_md5.doFinal(serverChallenge)).getBytes();
        // join the byte arrays to form the reply
        byte[] usernameBytes = username.getBytes();
        byte[] toEncode = new byte[usernameBytes.length + 1 /* the space */ + hmacResult.length];
        System.arraycopy(usernameBytes, 0, toEncode, 0, usernameBytes.length);
        toEncode[usernameBytes.length] = ' ';
        System.arraycopy(hmacResult, 0, toEncode, usernameBytes.length + 1, hmacResult.length);
        // send the reply and read the server code:
        return SMTPReply.isPositiveCompletion(sendCommand(new String(Base64.encodeBase64(toEncode))));
    } else if (method.equals(AUTH_METHOD.LOGIN)) {
        // the server sends fixed responses (base64("Username") and
        // base64("Password")), so we don't have to read them.
        if (!SMTPReply
                .isPositiveIntermediate(sendCommand(new String(Base64.encodeBase64(username.getBytes()))))) {
            return false;
        }
        return SMTPReply
                .isPositiveCompletion(sendCommand(new String(Base64.encodeBase64(password.getBytes()))));
    } else {
        return false; // safety check
    }
}

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;// w w w  .j  a  v  a 2 s.co  m
    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;
}

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

private void setSuccessfulByResponseCode(SampleResult sr, int replyCode) {
    if (SMTPReply.isPositiveCompletion(replyCode) || SMTPReply.isPositiveIntermediate(replyCode)
            || SMTPReply.isPositivePreliminary(replyCode)) {
        sr.setSuccessful(true);//from w  ww  .  j  a va2s.c o  m
    } else
        sr.setSuccessful(false);
}