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

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

Introduction

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

Prototype

void setEvent(int op);

Source Link

Document

Declares interest in a particular I/O event type by updating the event mask associated with the session.

Usage

From source file:com.ok2c.lightmtp.impl.protocol.SimpleSendEnvelopCodec.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();
    DeliveryRequest request = sessionState.getRequest();

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

    if (reply != null) {
        switch (this.codecState) {
        case MAIL_RESPONSE_EXPECTED:
            if (reply.getCode() == SMTPCodes.OK) {
                this.codecState = CodecState.RCPT_REQUEST_READY;
                this.recipients.clear();
                this.recipients.addAll(request.getRecipients());
                iosession.setEvent(SelectionKey.OP_WRITE);
            } else {
                this.deliveryFailed = true;
                this.codecState = CodecState.COMPLETED;
                sessionState.setReply(reply);
            }//from  w  ww. j a va2  s .  c  om
            break;
        case RCPT_RESPONSE_EXPECTED:
            if (this.recipients.isEmpty()) {
                throw new IllegalStateException("Unexpected state: " + this.codecState);
            }
            String recipient = this.recipients.removeFirst();

            if (reply.getCode() != SMTPCodes.OK) {
                sessionState.getFailures().add(new RcptResult(reply, recipient));
            }

            if (this.recipients.isEmpty()) {
                List<String> requested = request.getRecipients();
                List<RcptResult> failured = sessionState.getFailures();
                if (requested.size() > failured.size()) {
                    this.codecState = CodecState.DATA_REQUEST_READY;
                } else {
                    this.deliveryFailed = true;
                    this.codecState = CodecState.COMPLETED;
                    sessionState.setReply(reply);
                }
            } else {
                this.codecState = CodecState.RCPT_REQUEST_READY;
            }
            iosession.setEvent(SelectionKey.OP_WRITE);
            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);
            }
        }
    }

    if (bytesRead == -1 && !sessionState.isTerminated()) {
        throw new UnexpectedEndOfStreamException();
    }
}

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

@Override
public void consumeData(final IOSession iosession, final ClientState state)
        throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(state, "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 AUTH_RESPONSE_READY:
            AuthMode mode = (AuthMode) iosession.getAttribute(AUTH_TYPE);

            if (reply.getCode() == SMTPCodes.START_AUTH_INPUT) {
                if (mode == AuthMode.PLAIN) {
                    this.codecState = CodecState.AUTH_PLAIN_INPUT_READY;
                } else if (mode == AuthMode.LOGIN) {
                    this.codecState = CodecState.AUTH_LOGIN_USERNAME_INPUT_READY;
                }/*from  w w  w. j av  a2s.  co  m*/
                state.setReply(reply);
                iosession.setEvent(SelectionKey.OP_WRITE);
            } else {
                // TODO: should we set the failure here ?
                //       At the moment we just process as maybe its possible to send
                //       the mail even without auth
                this.codecState = CodecState.COMPLETED;
                state.setReply(reply);
            }
            break;

        case AUTH_PLAIN_INPUT_RESPONSE_EXPECTED:

            if (reply.getCode() == SMTPCodes.AUTH_OK) {
                this.codecState = CodecState.COMPLETED;
                state.setReply(reply);
                iosession.setEvent(SelectionKey.OP_WRITE);

            } else {
                // TODO: should we set the failure here ?
                //       At the moment we just process as maybe its possible to send
                //       the mail even without auth
                this.codecState = CodecState.COMPLETED;
                state.setReply(reply);
            }
            break;

        case AUTH_LOGIN_USERNAME_INPUT_RESPONSE_EXPECTED:
            if (reply.getCode() == SMTPCodes.START_AUTH_INPUT) {
                this.codecState = CodecState.AUTH_LOGIN_PASSWORD_INPUT_READY;
                state.setReply(reply);
                iosession.setEvent(SelectionKey.OP_WRITE);
            } else {
                throw new SMTPProtocolException("Unexpected reply:" + reply);
            }

            break;

        case AUTH_LOGIN_PASSWORD_INPUT_RESPONSE_EXPECTED:
            if (reply.getCode() == SMTPCodes.AUTH_OK) {
                this.codecState = CodecState.COMPLETED;
                state.setReply(reply);
                iosession.setEvent(SelectionKey.OP_WRITE);
            } else {
                // TODO: should we set the failure here ?
                //       At the moment we just process as maybe its possible to send
                //       the mail even without auth
                this.codecState = CodecState.COMPLETED;
                state.setReply(reply);

            }
            break;

        default:
            if (reply.getCode() == SMTPCodes.ERR_TRANS_SERVICE_NOT_AVAILABLE) {
                state.setReply(reply);
                this.codecState = CodecState.COMPLETED;
            } else {
                throw new SMTPProtocolException("Unexpected reply:" + reply);
            }
        }
    } else {
        if (bytesRead == -1 && !state.isTerminated()) {
            throw new UnexpectedEndOfStreamException();
        }
    }
}

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 av a2  s. co m*/
                }
                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);
        }
    }
}