All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.fitko.fitconnect.api.domain.sender.SendableSubmission Maven / Gradle / Ivy

Go to download

Library that provides client access to the FIT-Connect api-endpoints for sending, subscribing and routing

The newest version!
package dev.fitko.fitconnect.api.domain.sender;

import dev.fitko.fitconnect.api.config.ApplicationConfig;
import dev.fitko.fitconnect.api.config.defaults.SchemaConfig;
import dev.fitko.fitconnect.api.domain.model.attachment.Attachment;
import dev.fitko.fitconnect.api.domain.model.callback.Callback;
import dev.fitko.fitconnect.api.domain.model.metadata.AdditionalReferenceInfo;
import dev.fitko.fitconnect.api.domain.model.metadata.AuthenticationInformation;
import dev.fitko.fitconnect.api.domain.model.metadata.ContentStructure;
import dev.fitko.fitconnect.api.domain.model.metadata.attachment.Purpose;
import dev.fitko.fitconnect.api.domain.model.metadata.data.MimeType;
import dev.fitko.fitconnect.api.domain.model.metadata.data.SubmissionSchema;
import dev.fitko.fitconnect.api.domain.model.metadata.payment.PaymentInformation;
import dev.fitko.fitconnect.api.domain.model.reply.replychannel.ReplyChannel;
import dev.fitko.fitconnect.api.domain.sender.steps.unencrypted.BuildStep;
import dev.fitko.fitconnect.api.domain.sender.steps.unencrypted.DataStep;
import dev.fitko.fitconnect.api.domain.sender.steps.unencrypted.DestinationStep;
import dev.fitko.fitconnect.api.domain.sender.steps.unencrypted.OptionalPropertiesStep;
import dev.fitko.fitconnect.api.domain.sender.steps.unencrypted.ServiceTypeStep;
import dev.fitko.fitconnect.api.domain.zbp.AuthorKeyPair;
import dev.fitko.fitconnect.api.domain.zbp.message.CreateMessage;
import dev.fitko.fitconnect.api.domain.zbp.state.CreateState;
import dev.fitko.fitconnect.client.zbp.ZBPEnvelopeBuilder;
import lombok.Getter;

import java.io.ByteArrayInputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static dev.fitko.fitconnect.api.domain.model.metadata.data.MimeType.APPLICATION_JSON;

@Getter
public final class SendableSubmission {

    private final UUID destinationId;
    private final UUID caseId;
    private final byte[] data;
    private final SubmissionSchema submissionSchema;
    private final List attachments;
    private final String serviceName;
    private final String serviceIdentifier;
    private final List authenticationInformation;
    private final AdditionalReferenceInfo additionalReferenceInfo;
    private final PaymentInformation paymentInformation;
    private final ReplyChannel replyChannel;
    private final Callback callback;

    private SendableSubmission(final Builder builder) {
        destinationId = builder.getDestinationId();
        caseId = builder.getCaseId();
        data = builder.getData();
        submissionSchema = new SubmissionSchema(builder.getSchemaUri(), builder.getDataMimeType());
        attachments = Collections.unmodifiableList(builder.getAttachments());
        serviceName = builder.getServiceName();
        serviceIdentifier = builder.getServiceIdentifier();
        authenticationInformation = builder.getAuthenticationInformation();
        replyChannel = builder.getReplyChannel();
        paymentInformation = builder.getPaymentInformation();
        callback = builder.getCallback();
        additionalReferenceInfo = builder.getAdditionalReferenceInfo();
    }

    public static DestinationStep Builder() {
        return new Builder();
    }

    @Getter
    private static final class Builder implements DestinationStep,
            ServiceTypeStep,
            DataStep,
            OptionalPropertiesStep,
            BuildStep {

        private UUID destinationId;
        private UUID caseId;
        private final List attachments = new ArrayList<>();
        private byte[] data;
        private URI schemaUri;
        private MimeType dataMimeType;
        private String serviceName;
        private String serviceIdentifier;
        private List authenticationInformation;
        private PaymentInformation paymentInformation;
        private ReplyChannel replyChannel;
        private AdditionalReferenceInfo additionalReferenceInfo = new AdditionalReferenceInfo();
        private Callback callback;

        private static final byte[] EMPTY_DATA = new byte[0];

        private Builder() {
        }

        @Override
        public ServiceTypeStep setDestination(final UUID destinationId) {
            this.destinationId = destinationId;
            return this;
        }

        @Override
        public OptionalPropertiesStep addAttachments(final List attachments) {
            if (attachments != null && !attachments.isEmpty()) {
                this.attachments.addAll(attachments);
            }
            return this;
        }

        @Override
        public OptionalPropertiesStep addAttachment(final Attachment attachment) {
            if (attachment != null) {
                this.attachments.add(attachment);
            }
            return this;
        }

        @Override
        public OptionalPropertiesStep setReplyChannel(final ReplyChannel replyChannel) {
            this.replyChannel = replyChannel;
            return this;
        }

        @Override
        public OptionalPropertiesStep setJsonData(final String jsonData, final URI schemaUri) {
            setJsonData(jsonData.getBytes(StandardCharsets.UTF_8), schemaUri);
            return this;
        }

        @Override
        public OptionalPropertiesStep setJsonData(byte[] data, URI schemaUri) {
            this.dataMimeType = APPLICATION_JSON;
            this.schemaUri = schemaUri;
            this.data = getValidatedData(data);
            return this;
        }

        @Override
        public OptionalPropertiesStep setXmlData(final String xmlData, final URI schemaUri) {
            setXmlData(xmlData.getBytes(StandardCharsets.UTF_8), schemaUri);
            return this;
        }

        @Override
        public OptionalPropertiesStep setXmlData(byte[] data, URI schemaUri) {
            this.dataMimeType = MimeType.APPLICATION_XML;
            this.schemaUri = schemaUri;
            this.data = getValidatedData(data);
            return this;
        }

        @Override
        public OptionalPropertiesStep setZBPMessage(CreateMessage message, AuthorKeyPair authorKeyPair) {
            data = ZBPEnvelopeBuilder.fromAuthorPayload(message, authorKeyPair);
            dataMimeType = APPLICATION_JSON;
            this.schemaUri = SchemaConfig.ZBP_ADAPTER_SCHEMA.getSchemaUri();
            return this;
        }

        @Override
        public OptionalPropertiesStep setZBPState(CreateState state, AuthorKeyPair authorKeyPair) {
            data = ZBPEnvelopeBuilder.fromAuthorPayload(state, authorKeyPair);
            dataMimeType = APPLICATION_JSON;
            this.schemaUri = SchemaConfig.ZBP_ADAPTER_SCHEMA.getSchemaUri();
            return this;
        }

        @Override
        public DataStep setServiceType(final String serviceIdentifier, final String serviceName) {
            this.serviceIdentifier = serviceIdentifier;
            this.serviceName = serviceName;
            return this;
        }

        @Override
        public OptionalPropertiesStep setAuthenticationInformation(final List authenticationInformation) {
            this.authenticationInformation = authenticationInformation;
            return this;
        }

        @Override
        public OptionalPropertiesStep setPaymentInformation(final PaymentInformation paymentInformation) {
            this.paymentInformation = paymentInformation;
            return this;
        }

        @Override
        public OptionalPropertiesStep setApplicationDate(final LocalDate applicationDate) {
            additionalReferenceInfo = additionalReferenceInfo.withApplicationDate(applicationDate.toString());
            return this;
        }

        @Override
        public OptionalPropertiesStep setIdBundDeApplicationId(UUID idBundDeApplicationId) {
            additionalReferenceInfo = additionalReferenceInfo.withIdBundDeApplicationId(idBundDeApplicationId);
            return this;
        }

        @Override
        public OptionalPropertiesStep setSenderReference(String senderReference) {
            additionalReferenceInfo = additionalReferenceInfo.withSenderReference(senderReference);
            return this;
        }

        @Override
        public OptionalPropertiesStep setCallback(final URI callbackUri, final String callbackSecret) {
            callback = new Callback(callbackUri, callbackSecret);
            return this;
        }

        @Override
        public OptionalPropertiesStep setCase(final UUID caseId) {
            this.caseId = caseId;
            return this;
        }

        @Override
        public SendableSubmission build() {
            return new SendableSubmission(this);
        }

        /**
         * Sets the submission data either as attachment or as byte[], depending on allowed size. 
* In case of an attachment, the data will be transferred with {@link Purpose#DATA} and the data within the {@link ContentStructure} will zero bytes. */ private byte[] getValidatedData(byte[] data) { if (data.length > ApplicationConfig.MAX_DATA_SIZE_IN_BYTE) { attachments.add(Attachment.fromSubmissionData(new ByteArrayInputStream(data), dataMimeType)); return EMPTY_DATA; } return data; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy