1848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair/*
2848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * Copyright 2007 the original author or authors.
3848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair *
4848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * you may not use this file except in compliance with the License.
6848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * You may obtain a copy of the License at
7848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair *
8848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair *
10848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * Unless required by applicable law or agreed to in writing, software
11848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * See the License for the specific language governing permissions and
14848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * limitations under the License.
15848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair */
16848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismairpackage org.mockftpserver.core.command;
17848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
18848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismairimport org.mockftpserver.core.session.Session;
19848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
20848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair/**
21848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * The abstract superclass for CommandHandler classes that default to sending
22848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * back a configured reply code and text. You can customize the returned reply
23848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * code by setting the required <code>replyCode</code> property. If only the
24848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * <code>replyCode</code> property is set, then the default reply text corresponding to that
25848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * reply code is used in the response. You can optionally configure the reply text by setting
26848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * the <code>replyMessageKey</code> or <code>replyText</code> property.
27848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * <p>
28848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * Subclasses can optionally override the reply code and/or text for the reply by calling
29848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * {@link #setReplyCode(int)}, {@link #setReplyMessageKey(String)} and {@link #setReplyText(String)}.
30848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair *
31848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * @version $Revision$ - $Date$
32848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair *
33848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair * @author Chris Mair
34848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair */
35848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismairpublic abstract class AbstractStaticReplyCommandHandler extends AbstractTrackingCommandHandler {
36848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
37848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    // Defaults to zero; must be set to non-zero
38848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    protected int replyCode = 0;
39848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
40848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    // Defaults to null; if set to non-null, this value will override the default reply text associated with
41848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    // the replyCode.
42848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    protected String replyText = null;
43848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
44848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    // The message key for the reply text. Defaults to null. If null, use the default message associated
45848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    // with the reply code
46848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    protected String replyMessageKey = null;
47848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
48848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    /**
49848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * Set the reply code.
50848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     *
51848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @param replyCode - the replyCode
52848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     *
53848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @throws org.mockftpserver.core.util.AssertFailedException - if the replyCode is not valid
54848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     */
55848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    public void setReplyCode(int replyCode) {
56848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair        assertValidReplyCode(replyCode);
57848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair        this.replyCode = replyCode;
58848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    }
59848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
60848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    /**
61848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * Set the reply text. If null, then use the (default) message key for the replyCode.
62848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     *
63848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @param replyText - the replyText
64848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     */
65848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    public void setReplyText(String replyText) {
66848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair        this.replyText = replyText;
67848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    }
68848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
69848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    /**
70848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * Set the message key for the reply text. If null, then use the default message key.
71848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     *
72848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @param replyMessageKey - the replyMessageKey to set
73848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     */
74848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    public void setReplyMessageKey(String replyMessageKey) {
75848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair        this.replyMessageKey = replyMessageKey;
76848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    }
77848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
78848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    // -------------------------------------------------------------------------
79848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    // Utility methods for subclasses
80848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    // -------------------------------------------------------------------------
81848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
82848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    /**
83848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * Send the reply using the replyCode and message key/text configured for this command handler.
84848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @param session - the Session
85848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     *
86848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @throws org.mockftpserver.core.util.AssertFailedException if the replyCode is not valid
87848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     */
88848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    protected void sendReply(Session session) {
89848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair        sendReply(session, null);
90848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    }
91848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
92848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    /**
93848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * Send the reply using the replyCode and message key/text configured for this command handler.
94848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @param session - the Session
95848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @param messageParameter - message parameter; may be null
96848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     *
97848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     * @throws org.mockftpserver.core.util.AssertFailedException if the replyCode is not valid
98848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair     */
99848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    protected void sendReply(Session session, Object messageParameter) {
100848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair        Object[] parameters = (messageParameter == null) ? null : new Object[] { messageParameter };
101848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair        sendReply(session, replyCode, replyMessageKey, replyText, parameters);
102848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair    }
103848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair
104848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79chrismair}