/* * Copyright 2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mockftpserver.core.command; import org.mockftpserver.core.CommandSyntaxException; import org.mockftpserver.core.session.Session; import org.mockftpserver.core.util.Assert; import org.mockftpserver.core.util.AssertFailedException; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.MissingResourceException; /** * The abstract superclass for CommandHandler classes that manage the List of InvocationRecord * objects corresponding to each invocation of the command handler, and provide helper methods for subclasses. * * @author Chris Mair * @version $Revision$ - $Date$ */ public abstract class AbstractTrackingCommandHandler extends AbstractCommandHandler implements InvocationHistory { private List invocations = new ArrayList(); // ------------------------------------------------------------------------- // Template Method // ------------------------------------------------------------------------- /** * Handle the specified command for the session. This method is declared to throw Exception, * allowing CommandHandler implementations to avoid unnecessary exception-handling. All checked * exceptions are expected to be wrapped and handled by the caller. * * @param command - the Command to be handled * @param session - the session on which the Command was submitted * @throws Exception * @throws AssertFailedException - if the command or session is null * @see org.mockftpserver.core.command.CommandHandler#handleCommand(org.mockftpserver.core.command.Command, * org.mockftpserver.core.session.Session) */ public final void handleCommand(Command command, Session session) throws Exception { Assert.notNull(command, "command"); Assert.notNull(session, "session"); InvocationRecord invocationRecord = new InvocationRecord(command, session.getClientHost()); invocations.add(invocationRecord); try { handleCommand(command, session, invocationRecord); } catch (CommandSyntaxException e) { sendReply(session, ReplyCodes.COMMAND_SYNTAX_ERROR, null, null, null); } invocationRecord.lock(); } /** * Handle the specified command for the session. This method is declared to throw Exception, * allowing CommandHandler implementations to avoid unnecessary exception-handling. All checked * exceptions are expected to be wrapped and handled by the caller. * * @param command - the Command to be handled * @param session - the session on which the Command was submitted * @param invocationRecord - the InvocationRecord; CommandHandlers are expected to add * handler-specific data to the InvocationRecord, as appropriate * @throws Exception */ protected abstract void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception; // ------------------------------------------------------------------------- // Utility methods for subclasses // ------------------------------------------------------------------------- /** * Send a reply for this command on the control connection. *
* The reply code is designated by thereplyCode
property, and the reply text
* is determined by the following rules:
* replyText
property is non-null, then use that.replyMessageKey
is non-null, the use that to retrieve a
* localized message from the replyText
ResourceBundle.replyText
ResourceBundle,
* using the reply code as the key.numberOfInvocations()
method will return zero.
*
* @see org.mockftpserver.core.command.InvocationHistory#clearInvocations()
*/
public void clearInvocations() {
invocations.clear();
}
// -------------------------------------------------------------------------
// Internal Helper Methods
// -------------------------------------------------------------------------
/**
* Return the text for the specified reply code, formatted using the message arguments, if
* supplied. If overrideText is not null, then return that. Otherwise, return the text mapped to
* the code from the replyText ResourceBundle. If the ResourceBundle contains no mapping, then
* return null.
*
* If arguments is not null, then the returned reply text if formatted using the
* {@link MessageFormat} class.
*
* @param code - the reply code
* @param messageKey - the key used to retrieve the reply text from the replyTextBundle
* @param overrideText - if not null, this is used instead of the text from the replyTextBundle.
* @param arguments - the array of arguments to be formatted and substituted within the reply
* text; may be null
* @return the text for the reply code; may be null
*/
private String getTextForReplyCode(int code, String messageKey, String overrideText, Object[] arguments) {
try {
String t = (overrideText == null) ? getReplyTextBundle().getString(messageKey) : overrideText;
String formattedMessage = MessageFormat.format(t, arguments);
return (formattedMessage == null) ? null : formattedMessage.trim();
}
catch (MissingResourceException e) {
// No reply text is mapped for the specified key
LOG.warn("No reply text defined for reply code [" + code + "]");
return null;
}
}
}