33import io .mailtrap .CustomValidator ;
44import io .mailtrap .config .MailtrapConfig ;
55import io .mailtrap .exception .InvalidRequestBodyException ;
6+ import io .mailtrap .model .mailvalidation .ContentView ;
7+ import io .mailtrap .model .mailvalidation .MailContentView ;
8+ import io .mailtrap .model .mailvalidation .ResolvedMailContentView ;
9+ import io .mailtrap .model .mailvalidation .ResolvedMailView ;
10+ import io .mailtrap .model .request .emails .BatchEmailBase ;
611import io .mailtrap .model .request .emails .MailtrapBatchMail ;
712import io .mailtrap .model .request .emails .MailtrapMail ;
813import org .apache .commons .collections4 .MapUtils ;
@@ -19,74 +24,93 @@ protected SendApiResource(MailtrapConfig config, CustomValidator customValidator
1924 super (config , customValidator );
2025 }
2126
22- protected void assertBatchMailNotNull (MailtrapBatchMail batchMail ) {
23- if (batchMail == null ) {
24- throw new InvalidRequestBodyException ("BatchMail must not be null" );
25- }
26- if (batchMail .getRequests () == null || batchMail .getRequests ().isEmpty ()) {
27- throw new InvalidRequestBodyException ("BatchMail.requests must not be null or empty" );
28- }
29- if (batchMail .getRequests ().stream ().anyMatch (Objects ::isNull )) {
30- throw new InvalidRequestBodyException ("BatchMail.requests must not contain null items" );
31- }
32- }
33-
3427 /**
3528 * Validates the request body of an email message and throws an exception if it is invalid.
3629 *
3730 * @param mail The email message to be validated.
3831 * @throws InvalidRequestBodyException If the request body is invalid.
3932 */
40- protected void validateMailPayload (MailtrapMail mail ) throws InvalidRequestBodyException {
41- // Check if the mail object itself is null
33+ protected void validateMailPayload (MailtrapMail mail ) {
4234 if (mail == null ) {
4335 throw new InvalidRequestBodyException ("Mail must not be null" );
4436 }
4537
46- // Check if all three subject, text, and html are empty
47- boolean isSubjectTextHtmlEmpty = StringUtils .isBlank (mail .getSubject ())
48- && StringUtils .isBlank (mail .getText ())
49- && StringUtils .isBlank (mail .getHtml ());
50-
51- // Validate depending on whether the templateUuid is set
52- if (StringUtils .isEmpty (mail .getTemplateUuid ())) {
53- // Validation for the scenario where templateUuid is not provided
54- validateWithoutTemplate (mail , isSubjectTextHtmlEmpty );
55- } else {
56- // Validation for the scenario where templateUuid is provided
57- validateWithTemplate (mail );
58- }
59-
60- // Additional validation logic (assumed to be provided by the user)
38+ // Perform bean validation (NotNull, etc.)
6139 validateRequestBodyAndThrowException (mail );
40+
41+ // Validate subject/text/html/templateUuid
42+ validateContentRules (MailContentView .of (mail ));
6243 }
6344
64- private void validateWithoutTemplate (MailtrapMail mail , boolean isSubjectTextHtmlEmpty ) throws InvalidRequestBodyException {
65- // Ensure that at least subject, text, or html is provided if templateUuid is not set
66- if (isSubjectTextHtmlEmpty ) {
67- throw new InvalidRequestBodyException ("Mail must have subject and either text or html when templateUuid is not provided" );
68- }
45+ /**
46+ * Validates the request body of batch email and throws an exception if it is invalid.
47+ *
48+ * @param batch batch request to be validated.
49+ * @throws InvalidRequestBodyException If the request body is invalid.
50+ */
51+ protected void validateBatchPayload (MailtrapBatchMail batch ) {
52+ assertBatchMailNotNull (batch );
6953
70- // Ensure templateVariables are not used if templateUuid is not set
71- if (MapUtils .isNotEmpty (mail .getTemplateVariables ())) {
72- throw new InvalidRequestBodyException ("Mail templateVariables must only be used with templateUuid" );
73- }
54+ BatchEmailBase base = batch .getBase ();
55+
56+ for (int i = 0 ; i < batch .getRequests ().size (); i ++) {
57+ MailtrapMail mail = batch .getRequests ().get (i );
58+ ResolvedMailView mailView = new ResolvedMailView (base , mail );
59+
60+ try {
61+ // Perform bean validation (NotNull, etc.)
62+ validateRequestBodyAndThrowException (mailView );
63+ } catch (InvalidRequestBodyException e ) {
64+ throw new InvalidRequestBodyException ("requests[" + i + "]: " + e .getMessage (), e );
65+ }
66+
67+ validateContentRules (ResolvedMailContentView .of (mailView ));
7468
75- // Ensure the subject is not empty
76- if ( StringUtils . isBlank ( mail . getSubject ())) {
77- throw new InvalidRequestBodyException ( "Subject must not be null or empty" );
69+ if ( mailView . getFrom () == null ) {
70+ throw new InvalidRequestBodyException ( "requests[" + i + "]: from is required (either in mail or base)" );
71+ }
7872 }
73+ }
7974
80- // Ensure at least one of text or html is present
81- if (StringUtils . isBlank ( mail . getText ()) && StringUtils . isBlank ( mail . getHtml ()) ) {
82- throw new InvalidRequestBodyException ("Mail must have subject and either text or html when templateUuid is not provided " );
75+ private void assertBatchMailNotNull ( MailtrapBatchMail batchMail ) {
76+ if (batchMail == null ) {
77+ throw new InvalidRequestBodyException ("BatchMail must not be null " );
8378 }
79+ if (batchMail .getRequests () == null || batchMail .getRequests ().isEmpty ()) {
80+ throw new InvalidRequestBodyException ("BatchMail.requests must not be null or empty" );
81+ }
82+ if (batchMail .getRequests ().stream ().anyMatch (Objects ::isNull )) {
83+ throw new InvalidRequestBodyException ("BatchMail.requests must not contain null items" );
84+ }
85+
8486 }
8587
86- private void validateWithTemplate (MailtrapMail mail ) throws InvalidRequestBodyException {
87- // Ensure that subject, text, and html are not used when templateUuid is set
88- if (StringUtils .isNotEmpty (mail .getText ()) || StringUtils .isNotEmpty (mail .getHtml ()) || StringUtils .isNotEmpty (mail .getSubject ())) {
89- throw new InvalidRequestBodyException ("When templateUuid is used, subject, text, and html must not be used" );
88+ private void validateContentRules (ContentView v ) {
89+ boolean templateUuidBlank = StringUtils .isBlank (v .getTemplateUuid ());
90+
91+ boolean subjectTextHtmlEmpty = StringUtils .isBlank (v .getSubject ())
92+ && StringUtils .isBlank (v .getText ())
93+ && StringUtils .isBlank (v .getHtml ());
94+
95+ if (templateUuidBlank ) {
96+ if (subjectTextHtmlEmpty ) {
97+ throw new InvalidRequestBodyException ("Mail must have subject and either text or html when templateUuid is not provided" );
98+ }
99+ if (MapUtils .isNotEmpty (v .getTemplateVariables ())) {
100+ throw new InvalidRequestBodyException ("Mail templateVariables must only be used with templateUuid" );
101+ }
102+ if (StringUtils .isBlank (v .getSubject ())) {
103+ throw new InvalidRequestBodyException ("Subject must not be null or empty" );
104+ }
105+ if (StringUtils .isBlank (v .getText ()) && StringUtils .isBlank (v .getHtml ())) {
106+ throw new InvalidRequestBodyException ("Mail must have subject and either text or html when templateUuid is not provided" );
107+ }
108+ } else {
109+ if (StringUtils .isNotEmpty (v .getSubject ())
110+ || StringUtils .isNotEmpty (v .getText ())
111+ || StringUtils .isNotEmpty (v .getHtml ()))
112+ throw new InvalidRequestBodyException ("When templateUuid is used, subject, text, and html must not be used" );
90113 }
91114 }
115+
92116}
0 commit comments