Example usage for org.apache.http.nio.reactor IOSession channel

List of usage examples for org.apache.http.nio.reactor IOSession channel

Introduction

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

Prototype

ByteChannel channel();

Source Link

Document

Returns the underlying I/O channel associated with this session.

Usage

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

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

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    int bytesRead = buf.fill(iosession.channel());
    SMTPReply reply = this.parser.parse(buf, bytesRead == -1);

    if (reply != null) {
        switch (this.codecState) {
        case QUIT_RESPONSE_EXPECTED:
            iosession.close();/*from  w  w w. ja va2 s .  c o m*/
            this.codecState = CodecState.COMPLETED;
            sessionState.setReply(reply);
            break;
        default:
            throw new SMTPProtocolException("Unexpected reply: " + reply);
        }
    }
}

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

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

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    int bytesRead = buf.fill(iosession.channel());
    SMTPReply reply = this.parser.parse(buf, bytesRead == -1);

    if (reply != null) {
        switch (this.codecState) {
        case RSET_RESPONSE_EXPECTED:
            sessionState.reset(null);//from w  w w.  jav a2 s.c om
            sessionState.setReply(reply);
            this.codecState = CodecState.COMPLETED;
            break;
        default:
            throw new SMTPProtocolException("Unexpected reply: " + reply);
        }
    }
}

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

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

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    int bytesRead = buf.fill(iosession.channel());
    SMTPReply reply = this.parser.parse(buf, bytesRead == -1);

    if (reply != null) {
        switch (this.codecState) {
        case SERVICE_READY_EXPECTED:
            if (reply.getCode() == SMTPCodes.SERVICE_READY) {
                this.codecState = CodecState.HELO_READY;
                iosession.setEvent(SelectionKey.OP_WRITE);
            } else {
                this.codecState = CodecState.COMPLETED;
                sessionState.setReply(reply);
            }//from   ww w.jav a  2 s  .c  o m
            break;
        case HELO_RESPONSE_EXPECTED:
            this.codecState = CodecState.COMPLETED;
            sessionState.setReply(reply);
            break;
        default:
            if (reply.getCode() == SMTPCodes.ERR_TRANS_SERVICE_NOT_AVAILABLE) {
                sessionState.setReply(reply);
                this.codecState = CodecState.COMPLETED;
            } else {
                throw new SMTPProtocolException("Unexpected reply: " + reply);
            }
        }
    } else {
        if (bytesRead == -1 && !sessionState.isTerminated()) {
            throw new UnexpectedEndOfStreamException();
        }
    }
}

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

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

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    int bytesRead = buf.fill(iosession.channel());
    SMTPReply reply = this.parser.parse(buf, bytesRead == -1);

    if (reply != null) {
        switch (this.codecState) {
        case SERVICE_READY_EXPECTED:
            if (reply.getCode() == SMTPCodes.SERVICE_READY) {
                this.codecState = CodecState.LHLO_READY;
                iosession.setEvent(SelectionKey.OP_WRITE);
            } else {
                this.codecState = CodecState.COMPLETED;
                sessionState.setReply(reply);
            }//from   ww  w  . j  a  v  a2 s  . c o m
            break;
        case LHLO_RESPONSE_EXPECTED:
            if (reply.getCode() == SMTPCodes.OK) {

                Set<String> extensions = sessionState.getExtensions();

                List<String> lines = reply.getLines();
                if (lines.size() > 1) {
                    for (int i = 1; i < lines.size(); i++) {
                        String line = lines.get(i);
                        extensions.add(line.toUpperCase(Locale.US));
                    }
                }
            }
            this.codecState = CodecState.COMPLETED;
            sessionState.setReply(reply);
            break;
        default:
            if (reply.getCode() == SMTPCodes.ERR_TRANS_SERVICE_NOT_AVAILABLE) {
                sessionState.setReply(reply);
                this.codecState = CodecState.COMPLETED;
            } else {
                throw new SMTPProtocolException("Unexpected reply: " + reply);
            }
        }
    } else {
        if (bytesRead == -1 && !sessionState.isTerminated()) {
            throw new UnexpectedEndOfStreamException();
        }
    }
}

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

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

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    int bytesRead = buf.fill(iosession.channel());
    SMTPReply reply = this.parser.parse(buf, bytesRead == -1);

    if (reply != null) {
        switch (this.codecState) {
        case SERVICE_READY_EXPECTED:
            if (reply.getCode() == SMTPCodes.SERVICE_READY) {
                this.codecState = CodecState.EHLO_READY;
                iosession.setEvent(SelectionKey.OP_WRITE);
            } else {
                this.codecState = CodecState.COMPLETED;
                sessionState.setReply(reply);
            }/*w  w w  .ja v a2s  . c om*/
            break;
        case EHLO_RESPONSE_EXPECTED:
            if (reply.getCode() == SMTPCodes.OK) {

                Set<String> extensions = sessionState.getExtensions();

                List<String> lines = reply.getLines();
                if (lines.size() > 1) {
                    for (int i = 1; i < lines.size(); i++) {
                        String line = lines.get(i);
                        extensions.add(line.toUpperCase(Locale.US));
                    }
                }
                this.codecState = CodecState.COMPLETED;
                sessionState.setReply(reply);
            } else if (reply.getCode() == SMTPCodes.ERR_PERM_SYNTAX_ERR_COMMAND) {
                this.codecState = CodecState.HELO_READY;
                iosession.setEvent(SelectionKey.OP_WRITE);
            } else {
                this.codecState = CodecState.COMPLETED;
                sessionState.setReply(reply);
            }
            break;
        case HELO_RESPONSE_EXPECTED:
            this.codecState = CodecState.COMPLETED;
            sessionState.setReply(reply);
            break;
        default:
            if (reply.getCode() == SMTPCodes.ERR_TRANS_SERVICE_NOT_AVAILABLE) {
                sessionState.setReply(reply);
                this.codecState = CodecState.COMPLETED;
            } else {
                throw new SMTPProtocolException("Unexpected reply: " + reply);
            }
        }
    } else {
        if (bytesRead == -1 && !sessionState.isTerminated()) {
            throw new UnexpectedEndOfStreamException();
        }
    }
}

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

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

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    if (this.pendingReply != null) {
        this.writer.write(this.pendingReply, buf);
        this.pendingReply = null;
    }/*from   w  ww .  jav a2s  . c o m*/

    if (buf.hasData()) {
        buf.flush(iosession.channel());
    }
    if (!buf.hasData()) {
        this.completed = true;
        iosession.close();
    }
}

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

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

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    synchronized (sessionState) {
        for (;;) {
            int bytesRead = buf.fill(iosession.channel());
            try {
                SMTPCommand command = this.parser.parse(buf, bytesRead == -1);
                if (command == null) {
                    if (bytesRead == -1 && !sessionState.isTerminated() && this.pendingActions.isEmpty()) {
                        throw new UnexpectedEndOfStreamException();
                    } else {
                        break;
                    }//from www. j ava 2  s  .  com
                }
                Action<ServerState> action = this.commandHandler.handle(command);
                this.pendingActions.add(action);
            } catch (SMTPErrorException ex) {
                SMTPReply reply = new SMTPReply(ex.getCode(), ex.getEnhancedCode(), ex.getMessage());
                this.pendingActions.add(new SimpleAction(reply));
            } catch (SMTPProtocolException ex) {
                SMTPReply reply = new SMTPReply(SMTPCodes.ERR_PERM_SYNTAX_ERR_COMMAND, new SMTPCode(5, 3, 0),
                        ex.getMessage());
                this.pendingActions.add(new SimpleAction(reply));
            }
        }

        if (!this.pendingActions.isEmpty()) {
            iosession.setEvent(SelectionKey.OP_WRITE);
        }
    }
}

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

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

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    if (this.pendingReply != null) {
        this.writer.write(this.pendingReply, buf);
        this.pendingReply = null;
    }//from   www . ja va 2s . c  o m

    if (buf.hasData()) {
        buf.flush(iosession.channel());
    }
    if (!buf.hasData()) {
        this.completed = true;
        iosession.setEventMask(SelectionKey.OP_READ);
    }
}

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

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

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    while (this.codecState != CodecState.COMPLETED) {
        int bytesRead = buf.fill(iosession.channel());
        SMTPReply reply = this.parser.parse(buf, bytesRead == -1);

        if (reply == null) {
            if (bytesRead == -1 && !sessionState.isTerminated()) {
                throw new UnexpectedEndOfStreamException();
            } else {
                break;
            }/*from w  w  w. ja v  a 2s  .  co  m*/
        }

        switch (this.codecState) {
        case MAIL_RESPONSE_EXPECTED:
            this.codecState = CodecState.RCPT_RESPONSE_EXPECTED;
            if (reply.getCode() != SMTPCodes.OK) {
                this.deliveryFailed = true;
                sessionState.setReply(reply);
            }
            break;
        case RCPT_RESPONSE_EXPECTED:
            String recipient = this.recipients.removeFirst();

            if (reply.getCode() != SMTPCodes.OK) {
                sessionState.getFailures().add(new RcptResult(reply, recipient));
            }
            if (this.recipients.isEmpty()) {
                this.codecState = CodecState.DATA_RESPONSE_EXPECTED;
            }
            break;
        case DATA_RESPONSE_EXPECTED:
            this.codecState = CodecState.COMPLETED;
            if (reply.getCode() != SMTPCodes.START_MAIL_INPUT) {
                this.deliveryFailed = true;
            }
            sessionState.setReply(reply);
            break;
        default:
            if (reply.getCode() == SMTPCodes.ERR_TRANS_SERVICE_NOT_AVAILABLE) {
                sessionState.setReply(reply);
                this.codecState = CodecState.COMPLETED;
            } else {
                throw new SMTPProtocolException("Unexpected reply: " + reply);
            }
        }
    }
}