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.core.command;
     17 
     18 import org.mockftpserver.core.session.Session;
     19 
     20 /**
     21  * The abstract superclass for CommandHandler classes that default to sending
     22  * back a configured reply code and text. You can customize the returned reply
     23  * code by setting the required <code>replyCode</code> property. If only the
     24  * <code>replyCode</code> property is set, then the default reply text corresponding to that
     25  * reply code is used in the response. You can optionally configure the reply text by setting
     26  * the <code>replyMessageKey</code> or <code>replyText</code> property.
     27  * <p>
     28  * Subclasses can optionally override the reply code and/or text for the reply by calling
     29  * {@link #setReplyCode(int)}, {@link #setReplyMessageKey(String)} and {@link #setReplyText(String)}.
     30  *
     31  * @version $Revision$ - $Date$
     32  *
     33  * @author Chris Mair
     34  */
     35 public abstract class AbstractStaticReplyCommandHandler extends AbstractTrackingCommandHandler {
     36 
     37     // Defaults to zero; must be set to non-zero
     38     protected int replyCode = 0;
     39 
     40     // Defaults to null; if set to non-null, this value will override the default reply text associated with
     41     // the replyCode.
     42     protected String replyText = null;
     43 
     44     // The message key for the reply text. Defaults to null. If null, use the default message associated
     45     // with the reply code
     46     protected String replyMessageKey = null;
     47 
     48     /**
     49      * Set the reply code.
     50      *
     51      * @param replyCode - the replyCode
     52      *
     53      * @throws org.mockftpserver.core.util.AssertFailedException - if the replyCode is not valid
     54      */
     55     public void setReplyCode(int replyCode) {
     56         assertValidReplyCode(replyCode);
     57         this.replyCode = replyCode;
     58     }
     59 
     60     /**
     61      * Set the reply text. If null, then use the (default) message key for the replyCode.
     62      *
     63      * @param replyText - the replyText
     64      */
     65     public void setReplyText(String replyText) {
     66         this.replyText = replyText;
     67     }
     68 
     69     /**
     70      * Set the message key for the reply text. If null, then use the default message key.
     71      *
     72      * @param replyMessageKey - the replyMessageKey to set
     73      */
     74     public void setReplyMessageKey(String replyMessageKey) {
     75         this.replyMessageKey = replyMessageKey;
     76     }
     77 
     78     // -------------------------------------------------------------------------
     79     // Utility methods for subclasses
     80     // -------------------------------------------------------------------------
     81 
     82     /**
     83      * Send the reply using the replyCode and message key/text configured for this command handler.
     84      * @param session - the Session
     85      *
     86      * @throws org.mockftpserver.core.util.AssertFailedException if the replyCode is not valid
     87      */
     88     protected void sendReply(Session session) {
     89         sendReply(session, null);
     90     }
     91 
     92     /**
     93      * Send the reply using the replyCode and message key/text configured for this command handler.
     94      * @param session - the Session
     95      * @param messageParameter - message parameter; may be null
     96      *
     97      * @throws org.mockftpserver.core.util.AssertFailedException if the replyCode is not valid
     98      */
     99     protected void sendReply(Session session, Object messageParameter) {
    100         Object[] parameters = (messageParameter == null) ? null : new Object[] { messageParameter };
    101         sendReply(session, replyCode, replyMessageKey, replyText, parameters);
    102     }
    103 
    104 }