Example usage for org.apache.http.nio.reactor SessionOutputBuffer hasData

List of usage examples for org.apache.http.nio.reactor SessionOutputBuffer hasData

Introduction

In this page you can find the example usage for org.apache.http.nio.reactor SessionOutputBuffer hasData.

Prototype

boolean hasData();

Source Link

Document

Determines if the buffer contains data.

Usage

From source file:com.ok2c.lightmtp.impl.protocol.SimpleSendEnvelopCodec.java

@Override
public void produceData(final IOSession iosession, final ClientState sessionState)
        throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");
    if (sessionState.getRequest() == null) {
        if (sessionState.isTerminated()) {
            this.codecState = CodecState.COMPLETED;
        }//from   ww w.ja v  a2s  .  co  m
        return;
    }

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();
    DeliveryRequest request = sessionState.getRequest();

    switch (this.codecState) {
    case MAIL_REQUEST_READY:
        SMTPCommand mailFrom = new SMTPCommand("MAIL", "FROM:<" + request.getSender() + ">");
        this.writer.write(mailFrom, buf);

        this.codecState = CodecState.MAIL_RESPONSE_EXPECTED;
        iosession.setEvent(SelectionKey.OP_READ);
        break;
    case RCPT_REQUEST_READY:
        String recipient = this.recipients.getFirst();

        SMTPCommand rcptTo = new SMTPCommand("RCPT", "TO:<" + recipient + ">");
        this.writer.write(rcptTo, buf);

        this.codecState = CodecState.RCPT_RESPONSE_EXPECTED;
        break;
    case DATA_REQUEST_READY:
        SMTPCommand data = new SMTPCommand("DATA");
        this.writer.write(data, buf);
        this.codecState = CodecState.DATA_RESPONSE_EXPECTED;
        break;
    }

    if (buf.hasData()) {
        buf.flush(iosession.channel());
    }
    if (!buf.hasData()) {
        iosession.clearEvent(SelectionKey.OP_WRITE);
    }
}

From source file:com.ok2c.lightmtp.impl.protocol.SendDataCodec.java

@Override
public void produceData(final IOSession iosession, final ClientState sessionState)
        throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    switch (this.codecState) {
    case CONTENT_READY:
        int bytesRead = 0;
        while (buf.length() < LIMIT) {
            if (!this.contentBuf.hasData()) {
                bytesRead = this.contentBuf.fill(this.contentChannel);
            }/* w  ww  .j  a va  2 s .co m*/

            boolean lineComplete = this.contentBuf.readLine(this.lineBuf, bytesRead == -1);
            if (this.maxLineLen > 0 && (this.lineBuf.length() > this.maxLineLen
                    || (!lineComplete && this.contentBuf.length() > this.maxLineLen))) {
                throw new SMTPProtocolException("Maximum line length limit exceeded");
            }
            if (lineComplete) {
                if (this.lineBuf.length() > 0 && this.lineBuf.charAt(0) == '.') {
                    buf.write(PERIOD);
                }
                buf.writeLine(this.lineBuf);
                this.lineBuf.clear();
            } else {
                bytesRead = this.contentBuf.fill(this.contentChannel);
            }
            if (bytesRead == -1 && !this.contentBuf.hasData()) {

                this.lineBuf.clear();
                this.lineBuf.append('.');
                buf.writeLine(this.lineBuf);
                this.lineBuf.clear();

                this.content.reset();
                this.contentSent = true;
                this.codecState = CodecState.CONTENT_RESPONSE_EXPECTED;
                break;
            }
            if (bytesRead == 0 && !lineComplete) {
                break;
            }
        }
    }

    if (buf.hasData()) {
        buf.flush(iosession.channel());
    }
    if (!buf.hasData() && this.contentSent) {
        iosession.clearEvent(SelectionKey.OP_WRITE);
    }
}

From source file:com.ok2c.lightmtp.impl.protocol.AuthCodec.java

@Override
public void produceData(final IOSession iosession, final ClientState state)
        throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(state, "Session state");

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    switch (this.codecState) {
    case AUTH_READY:
        AuthMode mode = null;//from  w  w w  .j a va  2  s.  c o m
        for (final String extension : state.getExtensions()) {
            if (extension.startsWith(ProtocolState.AUTH.name())) {
                String types = extension.substring(ProtocolState.AUTH.name().length() + 1);
                mode = getAuthMode(types);
                if (mode != null) {
                    break;
                }
            }
        }
        if (mode == null) {
            // TODO: Maybe we should just skip auth then and call the next codec in the chain
            throw new SMTPProtocolException("Unsupported AUTH types");
        } else {
            iosession.setAttribute(AUTH_TYPE, mode);
        }

        SMTPCommand auth = new SMTPCommand("AUTH", mode.name());
        this.writer.write(auth, buf);
        this.codecState = CodecState.AUTH_RESPONSE_READY;
        break;

    case AUTH_PLAIN_INPUT_READY:
        byte[] authdata = Base64.encodeBase64(("\0" + username + "\0" + password).getBytes(AUTH_CHARSET));
        lineBuf.append(authdata, 0, authdata.length);
        this.codecState = CodecState.AUTH_PLAIN_INPUT_RESPONSE_EXPECTED;
        break;

    case AUTH_LOGIN_USERNAME_INPUT_READY:
        byte[] authUserData = Base64.encodeBase64(username.getBytes(AUTH_CHARSET));
        lineBuf.append(authUserData, 0, authUserData.length);

        this.codecState = CodecState.AUTH_LOGIN_USERNAME_INPUT_RESPONSE_EXPECTED;
        break;

    case AUTH_LOGIN_PASSWORD_INPUT_READY:
        byte[] authPassData = Base64.encodeBase64(password.getBytes(AUTH_CHARSET));
        lineBuf.append(authPassData, 0, authPassData.length);

        this.codecState = CodecState.AUTH_LOGIN_PASSWORD_INPUT_RESPONSE_EXPECTED;
        break;
    }

    if (!lineBuf.isEmpty()) {
        buf.writeLine(lineBuf);
        lineBuf.clear();
    }
    if (buf.hasData()) {
        buf.flush(iosession.channel());
    }
    if (!buf.hasData()) {
        iosession.clearEvent(SelectionKey.OP_WRITE);
    }
}