160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/* 260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Copyright 2007 the original author or authors. 360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * you may not use this file except in compliance with the License. 660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * You may obtain a copy of the License at 760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * http://www.apache.org/licenses/LICENSE-2.0 960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Unless required by applicable law or agreed to in writing, software 1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * distributed under the License is distributed on an "AS IS" BASIS, 1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * See the License for the specific language governing permissions and 1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * limitations under the License. 1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpackage org.mockftpserver.core.command; 1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.session.Session; 1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/** 2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * The abstract superclass for CommandHandler classes that default to sending 2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * back a configured reply code and text. You can customize the returned reply 2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * code by setting the required <code>replyCode</code> property. If only the 2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <code>replyCode</code> property is set, then the default reply text corresponding to that 2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * reply code is used in the response. You can optionally configure the reply text by setting 2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * the <code>replyMessageKey</code> or <code>replyText</code> property. 2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <p> 2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Subclasses can optionally override the reply code and/or text for the reply by calling 2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * {@link #setReplyCode(int)}, {@link #setReplyMessageKey(String)} and {@link #setReplyText(String)}. 3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$ 3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair 3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpublic abstract class AbstractStaticReplyCommandHandler extends AbstractTrackingCommandHandler { 3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Defaults to zero; must be set to non-zero 3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair protected int replyCode = 0; 3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Defaults to null; if set to non-null, this value will override the default reply text associated with 4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // the replyCode. 4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair protected String replyText = null; 4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // The message key for the reply text. Defaults to null. If null, use the default message associated 4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // with the reply code 4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair protected String replyMessageKey = null; 4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Set the reply code. 5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @param replyCode - the replyCode 5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @throws org.mockftpserver.core.util.AssertFailedException - if the replyCode is not valid 5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair public void setReplyCode(int replyCode) { 5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertValidReplyCode(replyCode); 5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair this.replyCode = replyCode; 5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Set the reply text. If null, then use the (default) message key for the replyCode. 6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @param replyText - the replyText 6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair public void setReplyText(String replyText) { 6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair this.replyText = replyText; 6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Set the message key for the reply text. If null, then use the default message key. 7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @param replyMessageKey - the replyMessageKey to set 7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair public void setReplyMessageKey(String replyMessageKey) { 7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair this.replyMessageKey = replyMessageKey; 7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // ------------------------------------------------------------------------- 7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Utility methods for subclasses 8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // ------------------------------------------------------------------------- 8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Send the reply using the replyCode and message key/text configured for this command handler. 8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @param session - the Session 8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @throws org.mockftpserver.core.util.AssertFailedException if the replyCode is not valid 8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair protected void sendReply(Session session) { 8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair sendReply(session, null); 9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Send the reply using the replyCode and message key/text configured for this command handler. 9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @param session - the Session 9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @param messageParameter - message parameter; may be null 9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @throws org.mockftpserver.core.util.AssertFailedException if the replyCode is not valid 9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair protected void sendReply(Session session, Object messageParameter) { 10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair Object[] parameters = (messageParameter == null) ? null : new Object[] { messageParameter }; 10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair sendReply(session, replyCode, replyMessageKey, replyText, parameters); 10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 10360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 10460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}