Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of custom imap handlers #235

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

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.

None yet

2 participants