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

dev.fitko.fitconnect.client.util.SubmissionUtil 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.client.util;

import com.nimbusds.jose.JWEObject;
import dev.fitko.fitconnect.api.domain.model.event.authtags.AuthenticationTags;
import dev.fitko.fitconnect.api.domain.model.submission.CreateSubmission;
import dev.fitko.fitconnect.api.domain.model.submission.SentSubmission;
import dev.fitko.fitconnect.api.domain.model.submission.ServiceType;
import dev.fitko.fitconnect.api.domain.model.submission.Submission;
import dev.fitko.fitconnect.api.domain.model.submission.SubmitSubmission;
import dev.fitko.fitconnect.client.sender.model.AttachmentPayload;
import dev.fitko.fitconnect.client.sender.model.SendableEncryptedSubmission;
import dev.fitko.fitconnect.client.sender.model.SendableSubmission;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Helper to create types for announced, sent and submit submissions.
 */
public final class SubmissionUtil {

    private SubmissionUtil() {
    }

    public static CreateSubmission buildCreateSubmission(final SendableSubmission sendableSubmission, final List attachments) {
        return CreateSubmission.builder()
                .destinationId(sendableSubmission.getDestinationId())
                .announcedAttachments(attachments.stream().map(AttachmentPayload::getAttachmentId).collect(Collectors.toList()))
                .serviceType(getServiceTypeFromSendableSubmission(sendableSubmission.getServiceName(), sendableSubmission.getServiceIdentifier()))
                .build();
    }

    public static CreateSubmission buildCreateSubmission(final SendableEncryptedSubmission sendableSubmission, final Map attachments) {
        return CreateSubmission.builder()
                .destinationId(sendableSubmission.getDestinationId())
                .announcedAttachments(new ArrayList<>(attachments.keySet()))
                .serviceType(getServiceTypeFromSendableSubmission(sendableSubmission.getServiceName(), sendableSubmission.getServiceIdentifier()))
                .build();
    }

    private static ServiceType getServiceTypeFromSendableSubmission(final String serviceName, final String serviceIdentifier) {
        return ServiceType.builder()
                .name(serviceName)
                .identifier(serviceIdentifier)
                .build();
    }

    public static SubmitSubmission buildSubmitSubmission(final UUID submissionId, final String encryptedData, final String encryptedMetadata) {
        final SubmitSubmission submission = new SubmitSubmission();
        submission.setSubmissionId(submissionId);
        submission.setEncryptedData(encryptedData);
        submission.setEncryptedMetadata(encryptedMetadata);
        return submission;
    }

    public static SentSubmission buildSentSubmission(final AuthenticationTags authenticationTags, final Submission submission) {
        return SentSubmission.builder()
                .submissionId(submission.getSubmissionId())
                .caseId(submission.getCaseId())
                .destinationId(submission.getDestinationId())
                .authenticationTags(authenticationTags)
                .build();
    }

    public static AuthenticationTags buildAuthenticationTags(final List encryptedAttachments, final String encryptedData, final String encryptedMetadata) {
        final AuthenticationTags authenticationTags = new AuthenticationTags();
        authenticationTags.setData(getAuthTag(encryptedData));
        authenticationTags.setMetadata(getAuthTag(encryptedMetadata));
        authenticationTags.setAttachments(encryptedAttachments.stream().collect(Collectors.toMap(AttachmentPayload::getAttachmentId, SubmissionUtil::getAuthTag)));
        return authenticationTags;
    }

    public static AuthenticationTags buildAuthenticationTagsForEncryptedSubmission(final Map encryptedAttachments, final String encryptedData, final String encryptedMetadata) {
        final AuthenticationTags authenticationTags = new AuthenticationTags();
        authenticationTags.setData(getAuthTag(encryptedData));
        authenticationTags.setMetadata(getAuthTag(encryptedMetadata));
        authenticationTags.setAttachments(encryptedAttachments.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (entry) -> getAuthTag(entry.getValue()))));
        return authenticationTags;
    }

    private static String getAuthTag(final AttachmentPayload attachmentPayload) {
        return getAuthTag(attachmentPayload.getEncryptedData());
    }

    private static String getAuthTag(final String encryptedJWTData) {
        try {
            return JWEObject.parse(encryptedJWTData).getAuthTag().toString();
        } catch (final ParseException | NullPointerException e) {
            return "";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy