diff options
author | chrismair <chrismair@531de8e6-9941-0410-b38b-9a92acbe0330> | 2008-03-26 01:48:22 +0000 |
---|---|---|
committer | chrismair <chrismair@531de8e6-9941-0410-b38b-9a92acbe0330> | 2008-03-26 01:48:22 +0000 |
commit | 78795c9e5e3c55fc294f011bcc129b7aab0f49b7 (patch) | |
tree | 267f65de7f7539e39df7b87fc479bdb76a185c3e /MockFtpServer/src/main/groovy/org | |
parent | abd32a990e145862f22d15316feedced5ad246aa (diff) | |
download | mockftpserver-78795c9e5e3c55fc294f011bcc129b7aab0f49b7.tar.gz |
Initial fake command-handler for USER command
git-svn-id: svn://svn.code.sf.net/p/mockftpserver/code@30 531de8e6-9941-0410-b38b-9a92acbe0330
Diffstat (limited to 'MockFtpServer/src/main/groovy/org')
5 files changed, 232 insertions, 0 deletions
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy new file mode 100644 index 0000000..9aec814 --- /dev/null +++ b/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy @@ -0,0 +1,28 @@ +/* + * Copyright 2008 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.session + +/** + * Constants for names of properties (attributes) stored in the session. + */ + class SessionKeys { + + static final USERNAME = "username" + static final USER_ACCOUNT = "userAccount" + static final CURRENT_DIRECTORY = "currentDirectory" + static final RENAME_FROM = "renameFrom" + +}
\ No newline at end of file diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfiguration.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfiguration.groovy new file mode 100644 index 0000000..56f89c0 --- /dev/null +++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfiguration.groovy @@ -0,0 +1,36 @@ +/* + * Copyright 2008 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.fake + +import org.mockftpserver.fake.filesystem.FileSystem +import org.mockftpserver.fake.user.UserAccount + +/** + * Interface for objects that provide access to server-specific information. + */ +interface ServerConfiguration { + + FileSystem getFileSystem() + void setFileSystem(FileSystem fileSystem) + + ResourceBundle getReplyTextBundle() + void setReplyTextBundle(ResourceBundle resourceBundle) + + UserAccount getUserAccount(String username) + + String getTextForReplyCode(int replyCode) + +}
\ No newline at end of file diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfigurationAware.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfigurationAware.groovy new file mode 100644 index 0000000..b977d8a --- /dev/null +++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/ServerConfigurationAware.groovy @@ -0,0 +1,24 @@ +/* + * Copyright 2008 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.fake + +/** + * Interface for classes that provide setter and getter to access a ServerConfiguration instance. + */ +interface ServerConfigurationAware { + ServerConfiguration getServerConfiguration(); + void setServerConfiguration(ServerConfiguration serverConfiguration); +}
\ No newline at end of file diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractFakeCommandHandler.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractFakeCommandHandler.groovy new file mode 100644 index 0000000..006ae0f --- /dev/null +++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractFakeCommandHandler.groovy @@ -0,0 +1,114 @@ +/* + * Copyright 2008 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.fake.command + +import org.mockftpserver.fake.ServerConfigurationAware +import org.mockftpserver.fake.ServerConfiguration +import org.mockftpserver.core.session.Session
import org.apache.log4j.Logger
import org.apache.log4j.Logger
import java.text.MessageFormat
import org.mockftpserver.core.command.Command + +/** + * Abstract superclass for CommandHandler classes for the "Fake" server. + */
+abstract class AbstractFakeCommandHandler implements ServerConfigurationAware { + + final Logger LOG = Logger.getLogger(this.class) + ServerConfiguration serverConfiguration + + /** + * Use template method to centralize and ensure common validation + */ + protected final void handleCommand(Command command, Session session) { + assert serverConfiguration != null + assert command != null + assert session != null + + handle(command, session) + } + + /** + * Subclasses must implement this + */ + protected abstract void handle(Command command, Session session) + + // ------------------------------------------------------------------------- + // Utility methods for subclasses + // ------------------------------------------------------------------------- + + /** + * Send a reply for this command on the control connection. + * + * The reply code is designated by the <code>replyCode</code> property, and the reply text + * is retrieved from the <code>replyText</code> ResourceBundle, using the reply code as the key. + * + * @param session - the Session + * @param replyCode - the reply code + * + * @throws AssertionError - if session is null + * + * @see MessageFormat + */ + protected void sendReply(Session session, int replyCode) { + assert session + assertValidReplyCode(replyCode); + + //String key = (replyMessageKey != null) ? replyMessageKey : Integer.toString(replyCode); + String key = Integer.toString(replyCode); + //String text = getTextForReplyCode(replyCode, key, null); + String text = serverConfiguration.getTextForReplyCode(replyCode) + String replyTextToLog = (text == null) ? "" : " " + text; + LOG.debug("Sending reply [" + replyCode + replyTextToLog + "]"); + session.sendReply(replyCode, text); + } + + /** + * Return the text for the specified reply code, formatted using the message arguments, if + * supplied. Return the text mapped to the code from the replyText ResourceBundle. If the + * ResourceBundle contains no mapping, then return null. + * <p> + * 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 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, Object[] arguments) { + try { + String t = serverConfiguration.getTextForReplyCode(code) + 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; + } + } + + /** + * Assert that the specified number is a valid reply code + * @param replyCode - the reply code to check + * + * @throws AssertionError - if the replyCode is invalid + */ + protected void assertValidReplyCode(int replyCode) { + assert replyCode > 0, "The number [" + replyCode + "] is not a valid reply code" + } + + +}
\ No newline at end of file diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/UserCommandHandler.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/UserCommandHandler.groovy new file mode 100644 index 0000000..e125739 --- /dev/null +++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/UserCommandHandler.groovy @@ -0,0 +1,30 @@ +/* + * Copyright 2008 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.fake.command + +import org.mockftpserver.fake.command.AbstractFakeCommandHandler
import org.mockftpserver.core.command.Command
import org.mockftpserver.core.session.Session
import org.mockftpserver.core.session.SessionKeys +import org.mockftpserver.core.command.ReplyCodes
+ +class UserCommandHandler extends AbstractFakeCommandHandler { + + protected void handle(Command command, Session session) { + def username = command.getRequiredString(0) + assert username.size() > 0 + session.setAttribute(SessionKeys.USERNAME, username) + sendReply(session, ReplyCodes.USER_NEED_PASSWORD_OK) + } + +}
\ No newline at end of file |