JAMES-2134 Parse Final-Recipient field and construct appropriate object
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/cd8afdd4 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/cd8afdd4 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/cd8afdd4 Branch: refs/heads/master Commit: cd8afdd45300a0c5cdbe104ab2e64ff25b7b141d Parents: bda0634 Author: Raphael Ouazana <[email protected]> Authored: Wed Apr 4 10:47:06 2018 +0200 Committer: Raphael Ouazana <[email protected]> Committed: Thu Apr 5 14:48:42 2018 +0200 ---------------------------------------------------------------------- .../org/apache/james/mdn/MDNReportParser.java | 37 ++++++++++++++++---- .../apache/james/mdn/MDNReportParserTest.java | 11 ++++++ 2 files changed, 42 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/cd8afdd4/mdn/src/main/java/org/apache/james/mdn/MDNReportParser.java ---------------------------------------------------------------------- diff --git a/mdn/src/main/java/org/apache/james/mdn/MDNReportParser.java b/mdn/src/main/java/org/apache/james/mdn/MDNReportParser.java index f8a922a..a9f8eeb 100644 --- a/mdn/src/main/java/org/apache/james/mdn/MDNReportParser.java +++ b/mdn/src/main/java/org/apache/james/mdn/MDNReportParser.java @@ -20,6 +20,7 @@ package org.apache.james.mdn; import org.apache.james.mdn.fields.AddressType; +import org.apache.james.mdn.fields.FinalRecipient; import org.apache.james.mdn.fields.Gateway; import org.apache.james.mdn.fields.OriginalRecipient; import org.apache.james.mdn.fields.ReportingUserAgent; @@ -442,21 +443,21 @@ public class MDNReportParser { push(OriginalRecipient.builder()), "Original-Recipient", ":", ows(), - addressType(), ACTION(setAddressType()), + addressType(), ACTION(setOriginalAddressType()), ows(), ";", ows(), - genericAddress(), ACTION(setGenericAddress()), + genericAddress(), ACTION(setOriginalGenericAddress()), ows(), ACTION(buildOriginalRecipient())); } - boolean setAddressType() { + boolean setOriginalAddressType() { this.<OriginalRecipient.Builder>peekT().addressType(new AddressType(match())); return true; } - boolean setGenericAddress() { + boolean setOriginalGenericAddress() { this.<OriginalRecipient.Builder>peekT().originalRecipient(Text.fromRawText(match())); return true; } @@ -480,8 +481,32 @@ public class MDNReportParser { "Final-Recipient" ":" OWS address-type OWS ";" OWS generic-address OWS */ Rule finalRecipientField() { - return Sequence("Final-Recipient", ":", ows(), addressType(), ows(), - ";", ows(), genericAddress(), ows()); + return Sequence( + push(FinalRecipient.builder()), + "Final-Recipient", ":", + ows(), + addressType(), ACTION(setFinalAddressType()), + ows(), + ";", + ows(), + genericAddress(), ACTION(setFinalGenericAddress()), + ows(), + ACTION(buildFinalRecipient())); + } + + boolean setFinalAddressType() { + this.<FinalRecipient.Builder>peekT().addressType(new AddressType(match())); + return true; + } + + boolean setFinalGenericAddress() { + this.<FinalRecipient.Builder>peekT().finalRecipient(Text.fromRawText(match())); + return true; + } + + boolean buildFinalRecipient() { + push(this.<FinalRecipient.Builder>popT().build()); + return true; } // original-message-id-field = "Original-Message-ID" ":" msg-id http://git-wip-us.apache.org/repos/asf/james-project/blob/cd8afdd4/mdn/src/test/java/org/apache/james/mdn/MDNReportParserTest.java ---------------------------------------------------------------------- diff --git a/mdn/src/test/java/org/apache/james/mdn/MDNReportParserTest.java b/mdn/src/test/java/org/apache/james/mdn/MDNReportParserTest.java index a2ba033..c27f280 100644 --- a/mdn/src/test/java/org/apache/james/mdn/MDNReportParserTest.java +++ b/mdn/src/test/java/org/apache/james/mdn/MDNReportParserTest.java @@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat; import org.apache.james.mdn.MDNReportParser.Parser; import org.apache.james.mdn.fields.AddressType; +import org.apache.james.mdn.fields.FinalRecipient; import org.apache.james.mdn.fields.Gateway; import org.apache.james.mdn.fields.OriginalRecipient; import org.apache.james.mdn.fields.ReportingUserAgent; @@ -117,4 +118,14 @@ public class MDNReportParserTest { assertThat(result.resultValue).isInstanceOf(OriginalRecipient.class); assertThat((OriginalRecipient)result.resultValue).isEqualTo(OriginalRecipient.builder().addressType(new AddressType("rfc822")).originalRecipient(Text.fromRawText("originalRecipient")).build()); } + + @Test + public void finalRecipientFieldShouldParse() { + String finalRecipient = "Final-Recipient: rfc822; final_recipient"; + Parser parser = Parboiled.createParser(MDNReportParser.Parser.class); + ParsingResult<Object> result = new ReportingParseRunner<>(parser.finalRecipientField()).run(finalRecipient); + assertThat(result.matched).isTrue(); + assertThat(result.resultValue).isInstanceOf(FinalRecipient.class); + assertThat((FinalRecipient)result.resultValue).isEqualTo(FinalRecipient.builder().addressType(new AddressType("rfc822")).finalRecipient(Text.fromRawText("final_recipient")).build()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
