Skip to content

Commit 4008c11

Browse files
committed
use an explicitly typed object for attachment references
1 parent f1ac2ec commit 4008c11

6 files changed

Lines changed: 23 additions & 10 deletions

File tree

src/main/java/it/aboutbits/springboot/emailservice/lib/AttachmentDataSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.io.InputStream;
66

77
public interface AttachmentDataSource {
8-
InputStream getAttachmentPayload(String reference) throws AttachmentException;
8+
InputStream getAttachmentPayload(AttachmentReference reference) throws AttachmentException;
99

10-
void releaseAttachment(String reference) throws AttachmentException;
10+
void releaseAttachment(AttachmentReference reference) throws AttachmentException;
1111
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package it.aboutbits.springboot.emailservice.lib;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import lombok.NonNull;
5+
6+
public record AttachmentReference(
7+
@NonNull
8+
@NotBlank
9+
String value
10+
) {
11+
}

src/main/java/it/aboutbits/springboot/emailservice/lib/EmailAttachmentDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public record EmailAttachmentDto(
1111

1212
String contentType,
1313

14-
String reference
14+
AttachmentReference reference
1515
) {
1616
}

src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailParameter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package it.aboutbits.springboot.emailservice.lib.application;
22

3+
import it.aboutbits.springboot.emailservice.lib.AttachmentReference;
34
import jakarta.validation.constraints.NotBlank;
45
import jakarta.validation.constraints.NotEmpty;
56
import lombok.Builder;
@@ -48,8 +49,7 @@ public record Email(
4849
@Builder
4950
public record Attachment(
5051
@NonNull
51-
@NotBlank
52-
String reference,
52+
AttachmentReference reference,
5353
@NonNull
5454
@NotBlank
5555
String fileName,

src/main/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmail.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource;
5+
import it.aboutbits.springboot.emailservice.lib.AttachmentReference;
56
import it.aboutbits.springboot.emailservice.lib.EmailDto;
67
import it.aboutbits.springboot.emailservice.lib.EmailState;
78
import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException;
@@ -61,7 +62,7 @@ public EmailDto schedule(@NonNull @Valid EmailParameter parameter) {
6162
.map(a -> {
6263
var attachment = new EmailAttachment();
6364
attachment.setEmail(email);
64-
attachment.setReference(a.reference());
65+
attachment.setReference(a.reference().value());
6566
attachment.setContentType(a.contentType());
6667
attachment.setFileName(a.fileName());
6768
return attachment;
@@ -105,7 +106,7 @@ Email send(Email email) {
105106

106107
private void cleanupAttachments(final Email email) throws AttachmentException {
107108
for (var attachment : email.getAttachments()) {
108-
attachmentDataSource.releaseAttachment(attachment.getReference());
109+
attachmentDataSource.releaseAttachment(new AttachmentReference(attachment.getReference()));
109110
}
110111
}
111112

@@ -138,7 +139,7 @@ private void sendMail(String fromAddress, String fromName, List<String> recipien
138139

139140
if (!attachments.isEmpty()) {
140141
for (var attachment : attachments) {
141-
var payload = attachmentDataSource.getAttachmentPayload(attachment.getReference());
142+
var payload = attachmentDataSource.getAttachmentPayload(new AttachmentReference(attachment.getReference()));
142143
helper.addAttachment(attachment.getFileName(), new ByteArrayResource(payload.readAllBytes()));
143144
payload.close();
144145
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package it.aboutbits.springboot.emailservice.lib.application;
22

33
import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource;
4+
import it.aboutbits.springboot.emailservice.lib.AttachmentReference;
45
import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException;
56

67
import java.io.InputStream;
78

89
public final class UnavailableAttachmentDataSource implements AttachmentDataSource {
910
@Override
10-
public InputStream getAttachmentPayload(final String reference) throws AttachmentException {
11+
public InputStream getAttachmentPayload(AttachmentReference reference) throws AttachmentException {
1112
throw new AttachmentException("attachments not available");
1213
}
1314

1415
@Override
15-
public void releaseAttachment(final String reference) throws AttachmentException {
16+
public void releaseAttachment(AttachmentReference reference) throws AttachmentException {
1617
throw new AttachmentException("attachments not available");
1718
}
1819
}

0 commit comments

Comments
 (0)