Skip to content

Conversation

@pmajkutewicz
Copy link

Not sure if that kind of change are "welcome", but I'll try.

I've needed mail server for testing, but with ability to once a while return errors. So here is my small change which allows me to inject custom imap request handler and reimplement imap commands - finally return error when some conditions are meet.

Sample use case:

I need own ImapHandler to inject my ImapRequestHandler:

    public class MyImapHandler extends ImapHandlerImpl {
		MyImapHandler() {
			setRequestHandler(new MyImapRequestHandler());
		}
	}

Custom ImapRequestHandler to inject ImapCommandFactory

	public class MyImapRequestHandler extends ImapRequestHandler {
		MyImapRequestHandler() {
			setImapCommands(new MyImapCommandFactory());
		}
	}

And finally, I'm able to replace eg. Append command with my own implementation:

	public class MyImapCommandFactory extends ImapCommandFactory {
		public MyImapCommandFactory() {
			Map<String, Class<? extends ImapCommand>> commands = getImapCommands();
			commands.remove(AppendCommand.NAME);
			commands.put(AppendCommand.NAME, MyAppendCommand.class);
		}
	}

My implementation of AppendCommand:

	public static class MyAppendCommand extends AppendCommand {
		private AppendCommandParser appendCommandParser = new AppendCommandParser();
		
		@Override
		public void doProcess(ImapRequestLineReader request, ImapResponse response, ImapSession session) throws ProtocolException, FolderException {
			if (counter.getAndIncrement() > 1) {
				appendCommandParser.mailbox(request);
				appendCommandParser.optionalAppendFlags(request);
				appendCommandParser.optionalDateTime(request);
				appendCommandParser.mimeMessage(request);
				appendCommandParser.endLine(request);
				throw new FolderException("Server exception");
			} else {
				super.doProcess(request, response, session);
			}
		}
	}

And doProcess does all magic and for subsequent requests throws error.

@marcelmay
Copy link
Member

@pmajkutewicz, we have such a feature request in #40 .

Regarding the PR: It is not concise and pretty massive (58 files, backward compatibility, ...).
Still, thx alot for sharing as we might use it for 'harvesting' from your implementation.

@marcelmay marcelmay self-assigned this Feb 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants