diff --git a/client/src/main/java/de/fitko/fitconnect/client/SenderClient.java b/client/src/main/java/de/fitko/fitconnect/client/SenderClient.java index 418842be8e74158a19487e4603cc9897fc6b681b..1e225a0a5d66a813f5d20987775a1e0c1ccbe355 100644 --- a/client/src/main/java/de/fitko/fitconnect/client/SenderClient.java +++ b/client/src/main/java/de/fitko/fitconnect/client/SenderClient.java @@ -5,28 +5,30 @@ import de.fitko.fitconnect.api.domain.auth.OAuthToken; import de.fitko.fitconnect.api.domain.model.metadata.Metadata; import de.fitko.fitconnect.api.domain.model.metadata.attachment.Attachment; import de.fitko.fitconnect.api.domain.model.metadata.data.Data; -import de.fitko.fitconnect.api.domain.model.submission.SubmissionRequest; -import de.fitko.fitconnect.api.domain.model.submission.CreatedSubmission; +import de.fitko.fitconnect.api.domain.model.submission.SubmissionForPickup; +import de.fitko.fitconnect.api.domain.model.submission.SubmissionSubmit; import de.fitko.fitconnect.api.domain.validation.ValidationResult; -import de.fitko.fitconnect.api.exceptions.client.ClientNotAuthenticated; +import de.fitko.fitconnect.api.exceptions.AttachmentUploadException; +import de.fitko.fitconnect.api.exceptions.ClientNotAuthenticatedException; +import de.fitko.fitconnect.api.exceptions.EncryptionException; +import de.fitko.fitconnect.api.exceptions.InvalidPublicKeyException; import de.fitko.fitconnect.api.services.Sender; import java.io.File; import java.nio.charset.StandardCharsets; import java.util.List; -import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; /** - * A fluent client for announcing and handing in a {@link SubmissionRequest} + * A fluent client for announcing and handing in a {@link SubmissionSubmit} */ public class SenderClient { private SenderClient() { } - public static WithDestination build(Sender sender,String clientId, String secret ) { + public static WithDestination build(final Sender sender, final String clientId, final String secret) { return new ClientBuilder(sender, clientId, secret); } @@ -40,11 +42,11 @@ public class SenderClient { */ WithAttachments withDestination(UUID destinationId); - CreatedSubmission send(UUID destinationId, List<File> attachments, String data); + SubmissionForPickup send(UUID destinationId, List<File> attachments, String data); - CreatedSubmission send(UUID destinationId, List<File> attachments, byte[] data); + SubmissionForPickup send(UUID destinationId, List<File> attachments, byte[] data); - CreatedSubmission send(UUID destinationId, List<File> attachments); + SubmissionForPickup send(UUID destinationId, List<File> attachments); } public interface WithAttachments { @@ -64,16 +66,16 @@ public class SenderClient { Submit withData(byte[] data); - CreatedSubmission submit(); + SubmissionForPickup submit(); } public interface Submit { - CreatedSubmission submit(); + SubmissionForPickup submit(); } public static class ClientBuilder implements WithDestination, WithAttachments, WithData, Submit { - private Sender sender; + private final Sender sender; private final String clientId; private final String secret; @@ -83,7 +85,7 @@ public class SenderClient { private RSAKey encryptionKey; private UUID submissionId; - public ClientBuilder(Sender sender, String clientId, String secret) { + public ClientBuilder(final Sender sender, final String clientId, final String secret) { this.sender = sender; this.clientId = clientId; this.secret = secret; @@ -91,18 +93,18 @@ public class SenderClient { } @Override - public WithAttachments withDestination(UUID destinationId) { + public WithAttachments withDestination(final UUID destinationId) { final RSAKey encryptionKey = sender.getEncryptionKeyForDestination(destinationId); final ValidationResult validationResult = sender.validatePublicKey(encryptionKey); - if(validationResult.hasErrors()){ - throw new RuntimeException("Public encryption key is not valid"); + if (validationResult.hasError()) { + throw new InvalidPublicKeyException("Public encryption key is not valid", validationResult.getError()); } this.encryptionKey = encryptionKey; return this; } @Override - public CreatedSubmission send(UUID destinationId, List<File> attachments, String data) { + public SubmissionForPickup send(final UUID destinationId, final List<File> attachments, final String data) { return this.withDestination(destinationId) .withAttachments(attachments) .withData(data) @@ -110,7 +112,7 @@ public class SenderClient { } @Override - public CreatedSubmission send(UUID destinationId, List<File> attachments, byte[] data) { + public SubmissionForPickup send(final UUID destinationId, final List<File> attachments, final byte[] data) { return this.withDestination(destinationId) .withAttachments(attachments) .withData(data) @@ -118,32 +120,29 @@ public class SenderClient { } @Override - public CreatedSubmission send(UUID destinationId, List<File> attachments) { + public SubmissionForPickup send(final UUID destinationId, final List<File> attachments) { return this.withDestination(destinationId) .withAttachments(attachments) .submit(); } @Override - public Submit withData(String data) { + public Submit withData(final String data) { return withData(data.getBytes(StandardCharsets.UTF_8)); } @Override - public Submit withData(byte[] data) { - var unencryptedData = Data.builder().build(); - final Optional<Data> encryptedData = sender.encryptSubmissionData(encryptionKey, unencryptedData); - if(encryptedData.isEmpty()){ - throw new RuntimeException("Data could not be encrypted"); - } - this.data = encryptedData.get(); + public Submit withData(final byte[] data) throws EncryptionException { + final var unencryptedData = Data.builder().build(); + final Data encryptedData = sender.encryptSubmissionData(encryptionKey, unencryptedData); + this.data = encryptedData; return this; } @Override - public WithData withAttachments(List<File> attachmentFiles) { + public WithData withAttachments(final List<File> attachmentFiles) throws AttachmentUploadException { final List<Attachment> attachments = readFilesToAttachments(attachmentFiles); - final CreatedSubmission submission = createSubmission(destinationId, attachments); + final SubmissionForPickup submission = createSubmission(destinationId, attachments); this.submissionId = submission.getSubmissionId(); final List<Attachment> encryptedAttachments = encryptAttachments(encryptionKey, attachments); sender.uploadAttachments(submissionId, encryptedAttachments); @@ -151,66 +150,53 @@ public class SenderClient { } @Override - public CreatedSubmission submit() { - final Optional<Metadata> metadata = getMetadata(); - if(metadata.isEmpty()){ - throw new RuntimeException("Metadata could not be created"); - } - return sender.sendSubmission(submissionId, metadata.get()).orElseThrow(); + public SubmissionForPickup submit() { + return sender.sendSubmission(submissionId, getMetadata()); } - private Optional<Metadata> getMetadata() { - return this.data == null ? sender.createMetadata(attachments) : sender.createMetadata(data, attachments); + private Metadata getMetadata() { + if (this.data == null) { + return sender.createMetadata(attachments); + } + return sender.createMetadata(data, attachments); } - private List<Attachment> encryptAttachments(RSAKey encryptionKey, List<Attachment> attachments) { + private List<Attachment> encryptAttachments(final RSAKey encryptionKey, final List<Attachment> attachments) { return attachments.stream().map(attachment -> encryptAttachment(encryptionKey, attachment)).collect(Collectors.toList()); } - private Attachment encryptAttachment(RSAKey encryptionKey, Attachment attachment) { - final Optional<Attachment> encryptedAttachment = sender.encryptAttachment(encryptionKey, attachment); - if (encryptedAttachment.isEmpty()) { - throw new RuntimeException("Attachment could not be encrypted"); - } - return encryptedAttachment.get(); + private Attachment encryptAttachment(final RSAKey encryptionKey, final Attachment attachment) { + return sender.encryptAttachment(encryptionKey, attachment); } - private CreatedSubmission createSubmission(UUID destinationId, List<Attachment> attachments) { - final SubmissionRequest request = SubmissionRequest.builder() + private SubmissionForPickup createSubmission(final UUID destinationId, final List<Attachment> attachments) { + final SubmissionSubmit request = SubmissionSubmit.builder() .destinationId(destinationId) .announcedAttachments(asListOfAttachmentsIds(attachments)) .build(); - final Optional<CreatedSubmission> response = sender.createSubmission(request); - if (response.isEmpty()) { - throw new RuntimeException("Could not create submission"); - } - return response.get(); + return sender.createSubmission(request); } - private List<UUID> asListOfAttachmentsIds(List<Attachment> attachments) { + private List<UUID> asListOfAttachmentsIds(final List<Attachment> attachments) { return attachments.stream().map(attachment -> attachment.getAttachmentId()).collect(Collectors.toList()); } private void authenticate() { - final Optional<OAuthToken> oAuthToken = sender.retrieveOAuthToken(clientId, secret); - if (oAuthToken.isEmpty()) { - throw new ClientNotAuthenticated("Client is not authenticated, please authenticate first"); + final OAuthToken oAuthToken = sender.retrieveOAuthToken(clientId, secret); + if (oAuthToken == null) { + throw new ClientNotAuthenticatedException("Client is not authenticated, please authenticate first"); } } - private List<Attachment> readFilesToAttachments(List<File> attachmentFiles) { + private List<Attachment> readFilesToAttachments(final List<File> attachmentFiles) { return attachmentFiles.stream() .map(this::readFileToAttachment) .collect(Collectors.toList()); } - private Attachment readFileToAttachment(File file) { - final Optional<Attachment> attachment = sender.createAttachment(file); - if(attachment.isEmpty()){ - throw new RuntimeException("Attachment could not be created"); - } - return attachment.get(); + private Attachment readFileToAttachment(final File file) { + return sender.createAttachment(file); } } } \ No newline at end of file diff --git a/client/src/main/java/de/fitko/fitconnect/client/di/FluentSenderClient.java b/client/src/main/java/de/fitko/fitconnect/client/di/FluentSenderClient.java index 90a95664afcdf12dfd93503bdf45e2c0881375cf..2767679f9b478bd3f372236d6cf75926a7f4baea 100644 --- a/client/src/main/java/de/fitko/fitconnect/client/di/FluentSenderClient.java +++ b/client/src/main/java/de/fitko/fitconnect/client/di/FluentSenderClient.java @@ -5,30 +5,30 @@ import de.fitko.fitconnect.api.domain.auth.OAuthToken; import de.fitko.fitconnect.api.domain.model.metadata.Metadata; import de.fitko.fitconnect.api.domain.model.metadata.attachment.Attachment; import de.fitko.fitconnect.api.domain.model.metadata.data.Data; -import de.fitko.fitconnect.api.domain.model.submission.SubmissionRequest; -import de.fitko.fitconnect.api.domain.model.submission.CreatedSubmission; +import de.fitko.fitconnect.api.domain.model.submission.SubmissionForPickup; +import de.fitko.fitconnect.api.domain.model.submission.SubmissionSubmit; import de.fitko.fitconnect.api.domain.validation.ValidationResult; -import de.fitko.fitconnect.api.exceptions.client.ClientNotAuthenticated; +import de.fitko.fitconnect.api.exceptions.ClientNotAuthenticatedException; import de.fitko.fitconnect.api.services.Sender; import java.io.File; import java.nio.charset.StandardCharsets; import java.util.List; -import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; /** - * A fluent client for announcing and handing in a {@link SubmissionRequest} + * A fluent client for announcing and handing in a {@link SubmissionSubmit} */ public class FluentSenderClient extends Client { - private FluentSenderClient() {} + private FluentSenderClient() { + } public static WithDestination build() { final Sender sender = getService(Sender.class); - final String clientId = getProperty("clientId"); - final String secret = getProperty("clientSecret"); + final String clientId = getProperty("clientId"); + final String secret = getProperty("clientSecret"); return new ClientBuilder(sender, clientId, secret); } @@ -42,11 +42,11 @@ public class FluentSenderClient extends Client { */ WithAttachments withDestination(UUID destinationId); - CreatedSubmission send(UUID destinationId, List<File> attachments, String data); + SubmissionForPickup send(UUID destinationId, List<File> attachments, String data); - CreatedSubmission send(UUID destinationId, List<File> attachments, byte[] data); + SubmissionForPickup send(UUID destinationId, List<File> attachments, byte[] data); - CreatedSubmission send(UUID destinationId, List<File> attachments); + SubmissionForPickup send(UUID destinationId, List<File> attachments); } public interface WithAttachments { @@ -66,16 +66,16 @@ public class FluentSenderClient extends Client { Submit withData(byte[] data); - CreatedSubmission submit(); + SubmissionForPickup submit(); } public interface Submit { - CreatedSubmission submit(); + SubmissionForPickup submit(); } public static class ClientBuilder implements WithDestination, WithAttachments, WithData, Submit { - private Sender sender; + private final Sender sender; private final String clientId; private final String secret; @@ -84,8 +84,9 @@ public class FluentSenderClient extends Client { private List<Attachment> attachments; private RSAKey encryptionKey; private UUID submissionId; + private OAuthToken token; - public ClientBuilder(Sender sender, String clientId, String secret) { + public ClientBuilder(final Sender sender, final String clientId, final String secret) { this.sender = sender; this.clientId = clientId; this.secret = secret; @@ -93,18 +94,18 @@ public class FluentSenderClient extends Client { } @Override - public WithAttachments withDestination(UUID destinationId) { + public WithAttachments withDestination(final UUID destinationId) { final RSAKey encryptionKey = sender.getEncryptionKeyForDestination(destinationId); final ValidationResult validationResult = sender.validatePublicKey(encryptionKey); - if(validationResult.hasErrors()){ - throw new RuntimeException("Public encryption key is not valid"); + if (validationResult.hasError()) { + throw new RuntimeException("Public encryption key is not valid", validationResult.getError()); } this.encryptionKey = encryptionKey; return this; } @Override - public CreatedSubmission send(UUID destinationId, List<File> attachments, String data) { + public SubmissionForPickup send(final UUID destinationId, final List<File> attachments, final String data) { return this.withDestination(destinationId) .withAttachments(attachments) .withData(data) @@ -112,7 +113,7 @@ public class FluentSenderClient extends Client { } @Override - public CreatedSubmission send(UUID destinationId, List<File> attachments, byte[] data) { + public SubmissionForPickup send(final UUID destinationId, final List<File> attachments, final byte[] data) { return this.withDestination(destinationId) .withAttachments(attachments) .withData(data) @@ -120,32 +121,29 @@ public class FluentSenderClient extends Client { } @Override - public CreatedSubmission send(UUID destinationId, List<File> attachments) { + public SubmissionForPickup send(final UUID destinationId, final List<File> attachments) { return this.withDestination(destinationId) .withAttachments(attachments) .submit(); } @Override - public Submit withData(String data) { + public Submit withData(final String data) { return withData(data.getBytes(StandardCharsets.UTF_8)); } @Override - public Submit withData(byte[] data) { - var unencryptedData = Data.builder().build(); - final Optional<Data> encryptedData = sender.encryptSubmissionData(encryptionKey, unencryptedData); - if(encryptedData.isEmpty()){ - throw new RuntimeException("Data could not be encrypted"); - } - this.data = encryptedData.get(); + public Submit withData(final byte[] data) { + final var unencryptedData = Data.builder().build(); + final Data encryptedData = sender.encryptSubmissionData(encryptionKey, unencryptedData); + this.data = encryptedData; return this; } @Override - public WithData withAttachments(List<File> attachmentFiles) { + public WithData withAttachments(final List<File> attachmentFiles) { final List<Attachment> attachments = readFilesToAttachments(attachmentFiles); - final CreatedSubmission submission = createSubmission(destinationId, attachments); + final SubmissionForPickup submission = createSubmission(destinationId, attachments); this.submissionId = submission.getSubmissionId(); final List<Attachment> encryptedAttachments = encryptAttachments(encryptionKey, attachments); sender.uploadAttachments(submissionId, encryptedAttachments); @@ -153,66 +151,50 @@ public class FluentSenderClient extends Client { } @Override - public CreatedSubmission submit() { - final Optional<Metadata> metadata = getMetadata(); - if(metadata.isEmpty()){ - throw new RuntimeException("Metadata could not be created"); - } - return sender.sendSubmission(submissionId, metadata.get()).orElseThrow(); + public SubmissionForPickup submit() { + return sender.sendSubmission(submissionId, getMetadata()); } - private Optional<Metadata> getMetadata() { + private Metadata getMetadata() { return this.data == null ? sender.createMetadata(attachments) : sender.createMetadata(data, attachments); } - private List<Attachment> encryptAttachments(RSAKey encryptionKey, List<Attachment> attachments) { + private List<Attachment> encryptAttachments(final RSAKey encryptionKey, final List<Attachment> attachments) { return attachments.stream().map(attachment -> encryptAttachment(encryptionKey, attachment)).collect(Collectors.toList()); } - private Attachment encryptAttachment(RSAKey encryptionKey, Attachment attachment) { - final Optional<Attachment> encryptedAttachment = sender.encryptAttachment(encryptionKey, attachment); - if (encryptedAttachment.isEmpty()) { - throw new RuntimeException("Attachment could not be encrypted"); - } - return encryptedAttachment.get(); + private Attachment encryptAttachment(final RSAKey encryptionKey, final Attachment attachment) { + return sender.encryptAttachment(encryptionKey, attachment); } - private CreatedSubmission createSubmission(UUID destinationId, List<Attachment> attachments) { - final SubmissionRequest request = SubmissionRequest.builder() + private SubmissionForPickup createSubmission(final UUID destinationId, final List<Attachment> attachments) { + final SubmissionSubmit request = SubmissionSubmit.builder() .destinationId(destinationId) .announcedAttachments(asListOfAttachmentsIds(attachments)) .build(); - final Optional<CreatedSubmission> response = sender.createSubmission(request); - if (response.isEmpty()) { - throw new RuntimeException("Could not create submission"); - } - return response.get(); + return sender.createSubmission(request); } - private List<UUID> asListOfAttachmentsIds(List<Attachment> attachments) { + private List<UUID> asListOfAttachmentsIds(final List<Attachment> attachments) { return attachments.stream().map(attachment -> attachment.getAttachmentId()).collect(Collectors.toList()); } private void authenticate() { - final Optional<OAuthToken> oAuthToken = sender.retrieveOAuthToken(clientId, secret); - if (oAuthToken.isEmpty()) { - throw new ClientNotAuthenticated("Client is not authenticated, please authenticate first"); + final OAuthToken oAuthToken = sender.retrieveOAuthToken(clientId, secret); + if (oAuthToken == null) { + throw new ClientNotAuthenticatedException("Could not authenticate client, please authenticate first"); } } - private List<Attachment> readFilesToAttachments(List<File> attachmentFiles) { + private List<Attachment> readFilesToAttachments(final List<File> attachmentFiles) { return attachmentFiles.stream() .map(this::readFileToAttachment) .collect(Collectors.toList()); } - private Attachment readFileToAttachment(File file) { - final Optional<Attachment> attachment = sender.createAttachment(file); - if(attachment.isEmpty()){ - throw new RuntimeException("Attachment could not be created"); - } - return attachment.get(); + private Attachment readFileToAttachment(final File file) { + return sender.createAttachment(file); } } } \ No newline at end of file