package com.jscape.inet.ftp;

import com.jscape.util.Assert;

/**
 * Abstract server request.
 *
 * @author Alex
 * @version 1.0
 * @see <a href="AbstractRequest.java.html">Source code</a>
 */
public abstract class AbstractRequest {

    /**
     * Checks if request is valid.
     *
     * @return <code>true</code> if request is vald;
     *         <code>false</code> otherwise
     */
    public abstract boolean isValid();

    /**
     * Executes the request.
     *
     * @param interpreter protocol interpreter
     *
     * @throws FtpException if an error occurs
     */
    public abstract void execute(ClientProtocolInterpreter interpreter)
            throws FtpException;

    /**
     * Assert that reply is positive.
     *
     * @param reply reply to check
     *
     * @throws FtpException if reply is negative
     */
    protected void assertIsPositive(String reply)
            throws FtpException {
        if (reply.startsWith("4") || reply.startsWith("5")) {
            throw new FtpException(reply);
        }
    }

    /**
     * Asserts that request is in valid state.
     */
    protected void assertIsValid() {
        Assert.isValidState(isValid(), "invalid request");
    }

}
