Package mireka.pop.command

Source Code of mireka.pop.command.UidlCommand

package mireka.pop.command;

import java.io.IOException;
import java.util.List;

import mireka.pop.Command;
import mireka.pop.CommandParser;
import mireka.pop.IllegalSessionStateException;
import mireka.pop.Pop3Exception;
import mireka.pop.Session;
import mireka.pop.SessionState;
import mireka.pop.store.MaildropPopException;
import mireka.pop.store.UidListing;

public class UidlCommand implements Command {
    private final Session session;

    public UidlCommand(Session session) {
        this.session = session;
    }

    @Override
    public void execute(CommandParser commandParser) throws IOException,
            Pop3Exception {
        if (session.getSessionState() != SessionState.TRANSACTION)
            throw new IllegalSessionStateException();

        List<String> args = commandParser.parseArguments();
        if (args.isEmpty())
            displayList();
        else
            displaySingleListing(commandParser.parseSingleNumericArgument());
    }

    private void displayList() throws IOException {
        List<UidListing> listings = session.getMaildrop().getUidListings();
        session.getThread().sendResponse("+OK");
        ResultListWriter writer =
                new ResultListWriter(session.getThread().getOutputStream());
        for (UidListing listing : listings) {
            writer.writeLine(listing.toString());
        }
        writer.endList();

    }

    private void displaySingleListing(int messageNumber) throws IOException,
            MaildropPopException {
        UidListing listing;
        try {
            listing = session.getMaildrop().getUidListing(messageNumber);
        } catch (IllegalArgumentException e) {
            session.getThread().sendResponse("-ERR " + e.getMessage());
            return;
        }
        session.getThread().sendResponse("+OK " + listing.toString());
    }

}
TOP

Related Classes of mireka.pop.command.UidlCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.