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

dev.fitko.fitconnect.api.domain.subscriber.SendableReply Maven / Gradle / Ivy

Go to download

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

There is a newer version: 2.3.5
Show newest version
package dev.fitko.fitconnect.api.domain.subscriber;

import dev.fitko.fitconnect.api.config.ApplicationConfig;
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.subscriber.steps.BuildReplyStep;
import dev.fitko.fitconnect.api.domain.subscriber.steps.OptionalReplyPropertiesStep;
import dev.fitko.fitconnect.api.domain.subscriber.steps.ReplyDataStep;
import lombok.Getter;

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

@Getter
public final class SendableReply {

    private final UUID destinationId;
    private final UUID caseId;
    private final UUID originalSubmissionId;
    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 SendableReply(final Builder builder) {
        destinationId = builder.getDestinationId();
        caseId = builder.getCaseId();
        originalSubmissionId = builder.getOriginalSubmissionId();
        data = builder.getData();
        submissionSchema = new SubmissionSchema(builder.getSchemaUri(), builder.getDataMimeType());
        attachments = Collections.unmodifiableList(builder.getAttachments());
        serviceName = builder.getServiceName();
        serviceIdentifier = builder.getServiceIdentifier();
        authenticationInformation = builder.getAuthenticationInformation();
        additionalReferenceInfo = builder.getAdditionalReferenceInfo();
        replyChannel = builder.getReplyChannel();
        paymentInformation = builder.getPaymentInformation();
        callback = builder.getCallback();
    }

    public static ReplyDataStep from(final ReceivedSubmission receivedSubmission) {
        return new Builder(receivedSubmission);
    }

    @Getter
    private static final class Builder implements ReplyDataStep, OptionalReplyPropertiesStep, BuildReplyStep {

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

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

        private Builder(final ReceivedSubmission receivedSubmission) {
            caseId = receivedSubmission.getCaseId();
            originalSubmissionId = receivedSubmission.getSubmissionId();
            destinationId = receivedSubmission.getDestinationId();
            serviceIdentifier = receivedSubmission.getServiceType().getIdentifier();
            serviceName = receivedSubmission.getServiceType().getName();
            callback = receivedSubmission.getCallback();
            replyChannel = receivedSubmission.getMetadata().getReplyChannel();
            authenticationInformation = receivedSubmission.getMetadata().getAuthenticationInformation();
            paymentInformation = receivedSubmission.getMetadata().getPaymentInformation();
            additionalReferenceInfo = receivedSubmission.getMetadata().getAdditionalReferenceInfo();
        }

        public OptionalReplyPropertiesStep setJsonData(final String replyData, final URI replyDataSchemaUri) {
            setJsonData(replyData.getBytes(StandardCharsets.UTF_8), replyDataSchemaUri);
            return this;
        }

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

        public OptionalReplyPropertiesStep setXmlData(final String replyData, final URI replyDataSchemaUri) {
            setXmlData(replyData.getBytes(StandardCharsets.UTF_8), replyDataSchemaUri);
            return this;
        }

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

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

        @Override
        public OptionalReplyPropertiesStep addAttachment(final Attachment attachment) {
            this.attachments.add(attachment);
            return this;
        }

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

        /**
         * Sets the reply 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