You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rd...@apache.org on 2007/03/03 09:44:24 UTC
svn commit: r514102 [2/2] - in /james/server/sandbox/seda-imap/src:
java/org/apache/james/imapserver/ java/org/apache/james/imapserver/commands/
test/org/apache/james/imapserver/commands/
Added: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StandardImapDecoder.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StandardImapDecoder.java?view=auto&rev=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StandardImapDecoder.java (added)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StandardImapDecoder.java Sat Mar 3 00:44:22 2007
@@ -0,0 +1,88 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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.apache.james.imapserver.commands;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.james.imapserver.ImapDecoder;
+import org.apache.james.imapserver.ImapRequestLineReader;
+import org.apache.james.imapserver.ProtocolException;
+
+public class StandardImapDecoder extends AbstractLogEnabled implements ImapDecoder {
+
+ private static final String INVALID_COMMAND = "Invalid command.";
+ // TODO: inject dependency
+ private final ImapCommandParserFactory imapCommands = new ImapCommandParserFactory();
+ private static final String REQUEST_SYNTAX = "Protocol Error: Was expecting <tag SPACE command [arguments]>";
+
+ /**
+ * @see org.apache.avalon.framework.logger.AbstractLogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
+ */
+ public void enableLogging(Logger logger) {
+ super.enableLogging(logger);
+ setupLogger(imapCommands);
+ }
+
+ public ImapCommandMessage decode(ImapRequestLineReader request) {
+ ImapCommandMessage message;
+ final Logger logger = getLogger();
+
+ try {
+ final String tag = AbstractImapCommandParser.tag( request );
+ message = decodeCommandTagged(request, logger, tag);
+ }
+ catch ( ProtocolException e ) {
+ logger.debug("error parsing request", e);
+ message = new BadResponseMessage( REQUEST_SYNTAX );
+ }
+ return message;
+ }
+
+ private ImapCommandMessage decodeCommandTagged(final ImapRequestLineReader request, final Logger logger, final String tag) {
+ ImapCommandMessage message;
+ if (logger.isDebugEnabled()) {
+ logger.debug( "Got <tag>: " + tag );
+ }
+ try {
+ final String commandName = AbstractImapCommandParser.atom( request );
+ message = decodeCommandNamed(request, tag, commandName, logger);
+ }
+ catch ( ProtocolException e ) {
+ logger.debug("Error during initial request parsing", e);
+ message = new ErrorResponseMessage(REQUEST_SYNTAX , tag);
+ }
+ return message;
+ }
+
+ private ImapCommandMessage decodeCommandNamed(final ImapRequestLineReader request,
+ final String tag, String commandName, final Logger logger) {
+ ImapCommandMessage message;
+ if (logger.isDebugEnabled()) {
+ logger.debug( "Got <command>: " + commandName);
+ }
+ final ImapCommandParser command = imapCommands.getParser( commandName );
+ if ( command == null ) {
+ logger.info("Missing command implementation.");
+ message = new ErrorResponseMessage(INVALID_COMMAND, tag);
+ } else {
+ message = command.parse( request, tag );
+ }
+ return message;
+ }
+}
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StatusCommand.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StatusCommand.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StatusCommand.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StatusCommand.java Sat Mar 3 00:44:22 2007
@@ -19,8 +19,6 @@
package org.apache.james.imapserver.commands;
-import org.apache.james.imapserver.ImapRequestLineReader;
-import org.apache.james.imapserver.ProtocolException;
/**
* Handles processeing for the STATUS imap command.
@@ -38,8 +36,6 @@
static final String UIDVALIDITY = "UIDVALIDITY";
static final String UNSEEN = "UNSEEN";
- private StatusCommandParser parser = new StatusCommandParser(this);
-
/** @see ImapCommand#getName */
public String getName()
{
@@ -50,11 +46,6 @@
public String getArgSyntax()
{
return ARGS;
- }
-
- protected AbstractImapCommandMessage decode(ImapRequestLineReader request, String tag) throws ProtocolException {
- final AbstractImapCommandMessage result = parser.decode(request, tag);
- return result;
}
}
/*
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StatusCommandParser.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StatusCommandParser.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StatusCommandParser.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StatusCommandParser.java Sat Mar 3 00:44:22 2007
@@ -20,13 +20,11 @@
import org.apache.james.imapserver.ImapRequestLineReader;
import org.apache.james.imapserver.ProtocolException;
-import org.apache.james.imapserver.commands.CommandParser.CharacterValidator;
-import org.apache.james.imapserver.commands.CommandParser.NoopCharValidator;
-class StatusCommandParser extends CommandParser
+class StatusCommandParser extends AbstractImapCommandParser
{
- public StatusCommandParser(ImapCommand command) {
- super(command);
+ public StatusCommandParser() {
+ super(new StatusCommand());
}
StatusDataItems statusDataItems( ImapRequestLineReader request )
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StoreCommand.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StoreCommand.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StoreCommand.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StoreCommand.java Sat Mar 3 00:44:22 2007
@@ -20,20 +20,16 @@
package org.apache.james.imapserver.commands;
-import org.apache.james.imapserver.ImapRequestLineReader;
-import org.apache.james.imapserver.ProtocolException;
/**
* Handles processeing for the STORE imap command.
*
* @version $Revision: 109034 $
*/
-class StoreCommand extends SelectedStateCommand implements UidEnabledCommand
+class StoreCommand extends SelectedStateCommand
{
public static final String NAME = "STORE";
public static final String ARGS = "<Message-set> ['+'|'-']FLAG[.SILENT] <flag-list>";
-
- private StoreCommandParser parser = new StoreCommandParser(this);
/** @see ImapCommand#getName */
public String getName()
@@ -45,15 +41,6 @@
public String getArgSyntax()
{
return ARGS;
- }
-
- protected AbstractImapCommandMessage decode(ImapRequestLineReader request, String tag) throws ProtocolException {
- return decode(request, false, tag);
- }
-
- public AbstractImapCommandMessage decode(ImapRequestLineReader request, boolean useUids, String tag) throws ProtocolException {
- final AbstractImapCommandMessage result = parser.decode(this, request, tag, useUids);
- return result;
}
}
/*
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StoreCommandParser.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StoreCommandParser.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StoreCommandParser.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/StoreCommandParser.java Sat Mar 3 00:44:22 2007
@@ -22,12 +22,11 @@
import org.apache.james.imapserver.ImapRequestLineReader;
import org.apache.james.imapserver.ProtocolException;
-import org.apache.james.imapserver.commands.CommandParser.NoopCharValidator;
class StoreCommandParser extends AbstractUidCommandParser
{
- public StoreCommandParser(ImapCommand command) {
- super(command);
+ public StoreCommandParser() {
+ super(new StoreCommand());
}
StoreDirective storeDirective( ImapRequestLineReader request ) throws ProtocolException
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/SubscribeCommand.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/SubscribeCommand.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/SubscribeCommand.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/SubscribeCommand.java Sat Mar 3 00:44:22 2007
@@ -19,8 +19,6 @@
package org.apache.james.imapserver.commands;
-import org.apache.james.imapserver.ImapRequestLineReader;
-import org.apache.james.imapserver.ProtocolException;
/**
* Handles processeing for the SUBSCRIBE imap command.
@@ -30,8 +28,6 @@
class SubscribeCommand extends AuthenticatedStateCommand {
public static final String NAME = "SUBSCRIBE";
public static final String ARGS = "<mailbox>";
-
- private final SubscribeCommandParser parser = new SubscribeCommandParser(this);
/** @see ImapCommand#getName */
public String getName() {
@@ -41,10 +37,5 @@
/** @see CommandTemplate#getArgSyntax */
public String getArgSyntax() {
return ARGS;
- }
-
- protected AbstractImapCommandMessage decode(ImapRequestLineReader request, String tag) throws ProtocolException {
- final AbstractImapCommandMessage result = parser.decode(request, tag);
- return result;
}
}
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/SubscribeCommandParser.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/SubscribeCommandParser.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/SubscribeCommandParser.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/SubscribeCommandParser.java Sat Mar 3 00:44:22 2007
@@ -21,10 +21,10 @@
import org.apache.james.imapserver.ImapRequestLineReader;
import org.apache.james.imapserver.ProtocolException;
-class SubscribeCommandParser extends CommandParser {
+class SubscribeCommandParser extends AbstractImapCommandParser {
- public SubscribeCommandParser(ImapCommand command) {
- super(command);
+ public SubscribeCommandParser() {
+ super(new SubscribeCommand());
}
protected AbstractImapCommandMessage decode(ImapCommand command, ImapRequestLineReader request, String tag) throws ProtocolException {
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UidCommand.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UidCommand.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UidCommand.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UidCommand.java Sat Mar 3 00:44:22 2007
@@ -19,8 +19,6 @@
package org.apache.james.imapserver.commands;
-import org.apache.james.imapserver.ImapRequestLineReader;
-import org.apache.james.imapserver.ProtocolException;
/**
* Handles processeing for the UID imap command.
@@ -32,8 +30,6 @@
public static final String NAME = "UID";
public static final String ARGS = "<fetch-command>|<store-command>|<copy-command>|<search-command>";
- private ImapCommandFactory commandFactory;
-
/** @see ImapCommand#getName */
public String getName()
{
@@ -44,16 +40,5 @@
public String getArgSyntax()
{
return ARGS;
- }
-
- public void setCommandFactory( ImapCommandFactory imapCommandFactory )
- {
- this.commandFactory = imapCommandFactory;
- }
-
- protected AbstractImapCommandMessage decode(ImapRequestLineReader request, String tag) throws ProtocolException {
- UidCommandParser parser = new UidCommandParser(this, commandFactory);
- final AbstractImapCommandMessage result = parser.decode(request, tag);
- return result;
}
}
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UidCommandParser.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UidCommandParser.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UidCommandParser.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UidCommandParser.java Sat Mar 3 00:44:22 2007
@@ -21,12 +21,16 @@
import org.apache.james.imapserver.ImapRequestLineReader;
import org.apache.james.imapserver.ProtocolException;
-class UidCommandParser extends CommandParser {
- private final ImapCommandFactory commandFactory;
+class UidCommandParser extends AbstractImapCommandParser {
+ private ImapCommandParserFactory commandFactory;
- public UidCommandParser(ImapCommand command, final ImapCommandFactory commandFactory) {
- super(command);
- this.commandFactory = commandFactory;
+ public UidCommandParser() {
+ super(new UidCommand());
+ }
+
+ public void setCommandFactory( ImapCommandParserFactory imapCommandFactory )
+ {
+ this.commandFactory = imapCommandFactory;
}
protected AbstractImapCommandMessage decode(ImapCommand command, ImapRequestLineReader request, String tag) throws ProtocolException {
@@ -36,13 +40,14 @@
// TODO: this will be easier to fix a little later
// TODO: also not sure whether the old implementation shares this flaw
String commandName = atom( request );
- ImapCommand helperCommand = commandFactory.getCommand( commandName );
+ ImapCommandParser helperCommand = commandFactory.getParser( commandName );
+ // TODO: replace abstract class with interface
if ( helperCommand == null ||
- ! (helperCommand instanceof UidEnabledCommand ) ) {
+ ! (helperCommand instanceof AbstractUidCommandParser ) ) {
throw new ProtocolException("Invalid UID command: '" + commandName + "'" );
}
- final UidEnabledCommand uidEnabled = (UidEnabledCommand) helperCommand;
- final AbstractImapCommandMessage result = uidEnabled.decode( request, true, tag );
+ final AbstractUidCommandParser uidEnabled = (AbstractUidCommandParser) helperCommand;
+ final AbstractImapCommandMessage result = uidEnabled.decode( request, tag, true );
return result;
}
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UnsubscribeCommand.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UnsubscribeCommand.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UnsubscribeCommand.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UnsubscribeCommand.java Sat Mar 3 00:44:22 2007
@@ -19,8 +19,6 @@
package org.apache.james.imapserver.commands;
-import org.apache.james.imapserver.ImapRequestLineReader;
-import org.apache.james.imapserver.ProtocolException;
/**
* Handles processeing for the UNSUBSCRIBE imap command.
@@ -32,8 +30,6 @@
public static final String NAME = "UNSUBSCRIBE";
public static final String ARGS = "<mailbox>";
- private final UnsubscribeCommandParser parser = new UnsubscribeCommandParser(this);
-
/** @see ImapCommand#getName */
public String getName() {
return NAME;
@@ -42,10 +38,5 @@
/** @see CommandTemplate#getArgSyntax */
public String getArgSyntax() {
return ARGS;
- }
-
- protected AbstractImapCommandMessage decode(ImapRequestLineReader request, String tag) throws ProtocolException {
- final AbstractImapCommandMessage result = parser.decode(request, tag);
- return result;
}
}
Modified: james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UnsubscribeCommandParser.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UnsubscribeCommandParser.java?view=diff&rev=514102&r1=514101&r2=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UnsubscribeCommandParser.java (original)
+++ james/server/sandbox/seda-imap/src/java/org/apache/james/imapserver/commands/UnsubscribeCommandParser.java Sat Mar 3 00:44:22 2007
@@ -21,10 +21,10 @@
import org.apache.james.imapserver.ImapRequestLineReader;
import org.apache.james.imapserver.ProtocolException;
-class UnsubscribeCommandParser extends CommandParser {
+class UnsubscribeCommandParser extends AbstractImapCommandParser {
- public UnsubscribeCommandParser(ImapCommand command) {
- super(command);
+ public UnsubscribeCommandParser() {
+ super(new UnsubscribeCommand());
}
protected AbstractImapCommandMessage decode(ImapCommand command, ImapRequestLineReader request, String tag) throws ProtocolException {
Added: james/server/sandbox/seda-imap/src/test/org/apache/james/imapserver/commands/BadResponseMessageTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/seda-imap/src/test/org/apache/james/imapserver/commands/BadResponseMessageTest.java?view=auto&rev=514102
==============================================================================
--- james/server/sandbox/seda-imap/src/test/org/apache/james/imapserver/commands/BadResponseMessageTest.java (added)
+++ james/server/sandbox/seda-imap/src/test/org/apache/james/imapserver/commands/BadResponseMessageTest.java Sat Mar 3 00:44:22 2007
@@ -0,0 +1,64 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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.apache.james.imapserver.commands;
+
+import org.apache.james.imapserver.ImapConstants;
+import org.apache.james.imapserver.ImapResponse;
+import org.apache.james.imapserver.ImapSession;
+import org.apache.james.imapserver.MockImapResponseWriter;
+import org.apache.james.imapserver.MockImapSession;
+
+import junit.framework.TestCase;
+
+public class BadResponseMessageTest extends TestCase {
+
+ private static final String MESSAGE = "A Message";
+
+ MockImapResponseWriter writer;
+ ImapResponse response;
+ BadResponseMessage message;
+ ImapSession session;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ writer = new MockImapResponseWriter();
+ response = new ImapResponse(writer);
+ message = new BadResponseMessage(MESSAGE);
+ session = new MockImapSession();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testProcessAndEncode() throws Exception {
+ ImapResponseMessage response = message.process(session);
+ assertNotNull("Response is required", response);
+ response.encode(this.response, session);
+ assertEquals(4, this.writer.operations.size());
+ assertEquals(new MockImapResponseWriter.UntaggedOperation(), writer.operations.get(0));
+ assertEquals(new MockImapResponseWriter.TextMessageOperation(ImapConstants.BAD),
+ writer.operations.get(1));
+ assertEquals(new MockImapResponseWriter.TextMessageOperation(MESSAGE),
+ writer.operations.get(2));
+ assertEquals(new MockImapResponseWriter.EndOperation(),
+ writer.operations.get(3));
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org