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

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

Introduction

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

Prototype

void writeLine(String s) throws IOException;

Source Link

Document

Copies content of the given string into this buffer as one line of text including a line delimiter.

Usage

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);
            }//from w  w w .j  a v a 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 www .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);
    }
}