Home | History | Annotate | Download | only in command
      1 /*
      2  * Copyright 2007 the original author or authors.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.mockftpserver.stub.command;
     17 
     18 import org.mockftpserver.core.command.AbstractCommandHandler;
     19 import org.mockftpserver.core.session.Session;
     20 import org.mockftpserver.core.util.AssertFailedException;
     21 import org.mockftpserver.stub.StubFtpServer;
     22 
     23 /**
     24  * The abstract superclass for CommandHandler classes for the {@link StubFtpServer}.
     25  * <p>
     26  * Subclasses can optionally override the reply code and/or text for the reply by calling
     27  * {@link #setReplyCode(int)}, {@link #setReplyMessageKey(String)} and {@link #setReplyText(String)}.
     28  *
     29  * @version $Revision$ - $Date$
     30  *
     31  * @author Chris Mair
     32  */
     33 public abstract class AbstractStubCommandHandler extends AbstractCommandHandler {
     34 
     35     // Defaults to zero; must be set to non-zero
     36     protected int replyCode = 0;
     37 
     38     // Defaults to null; if set to non-null, this value will override the default reply text associated with
     39     // the replyCode.
     40     protected String replyText = null;
     41 
     42     // The message key for the reply text. Defaults to null. If null, use the default message associated
     43     // with the reply code
     44     protected String replyMessageKey = null;
     45 
     46     /**
     47      * Set the reply code.
     48      *
     49      * @param replyCode - the replyCode
     50      *
     51      * @throws AssertFailedException - if the replyCode is not valid
     52      */
     53     public void setReplyCode(int replyCode) {
     54         assertValidReplyCode(replyCode);
     55         this.replyCode = replyCode;
     56     }
     57 
     58     /**
     59      * Set the reply text. If null, then use the (default) message key for the replyCode.
     60      *
     61      * @param replyText - the replyText
     62      */
     63     public void setReplyText(String replyText) {
     64         this.replyText = replyText;
     65     }
     66 
     67     /**
     68      * Set the message key for the reply text. If null, then use the default message key.
     69      *
     70      * @param replyMessageKey - the replyMessageKey to set
     71      */
     72     public void setReplyMessageKey(String replyMessageKey) {
     73         this.replyMessageKey = replyMessageKey;
     74     }
     75 
     76     // -------------------------------------------------------------------------
     77     // Utility methods for subclasses
     78     // -------------------------------------------------------------------------
     79 
     80     /**
     81      * Send the reply using the replyCode and message key/text configured for this command handler.
     82      * @param session - the Session
     83      *
     84      * @throws AssertFailedException if the replyCode is not valid
     85      */
     86     protected void sendReply(Session session) {
     87         sendReply(session, null);
     88     }
     89 
     90     /**
     91      * Send the reply using the replyCode and message key/text configured for this command handler.
     92      * @param session - the Session
     93      * @param messageParameter - message parameter; may be null
     94      *
     95      * @throws AssertFailedException if the replyCode is not valid
     96      */
     97     protected void sendReply(Session session, Object messageParameter) {
     98         Object[] parameters = (messageParameter == null) ? null : new Object[] { messageParameter };
     99         sendReply(session, replyCode, replyMessageKey, replyText, parameters);
    100     }
    101 
    102 }
    103