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

dev.fitko.fitconnect.api.config.chunking.AttachmentChunkingConfig 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.config.chunking;

import dev.fitko.fitconnect.api.domain.model.attachment.Attachment;

import java.nio.file.Path;

public class AttachmentChunkingConfig {

    public static final ChunkSize MAX_CHUNK_SIZE_IN_MB = ChunkSize.ofMB(500);
    public static final ChunkSize DEFAULT_CHUNK_SIZE_IN_MB = ChunkSize.ofMB(50);
    public static final String DEFAULT_ATTACHMENT_FOLDER_NAME = "fit-connect-attachments";
    public static final String TEMP_BUFFERED_FILE_PREFIX = "temp_fit_connect_attachment_";

    private final boolean chunkAllAttachments;
    private final ChunkSize chunkSizeInMB;
    private final String attachmentStoragePath;

    public AttachmentChunkingConfig(final boolean chunkAllAttachments, final ChunkSize chunkSizeInMB, final String attachmentStoragePath) {
        this.chunkAllAttachments = chunkAllAttachments;
        this.chunkSizeInMB = getValidatedChunkSize(chunkSizeInMB);
        this.attachmentStoragePath = attachmentStoragePath;
    }


    private AttachmentChunkingConfig(final boolean chunkAllAttachments, final ChunkSize chunkSizeInMB, final Path attachmentStoragePath) {
        this(chunkAllAttachments, chunkSizeInMB, attachmentStoragePath == null ? null : attachmentStoragePath.toString());
    }

    public AttachmentChunkingConfig() {
        this(false, DEFAULT_CHUNK_SIZE_IN_MB, "");
    }

    private static ChunkSize getValidatedChunkSize(ChunkSize chunkSize) {
        if (chunkSize == null || chunkSize.getSizeInMB() == 0) {
            return DEFAULT_CHUNK_SIZE_IN_MB;
        }
        if (chunkSize.getSizeInMB() > MAX_CHUNK_SIZE_IN_MB.getSizeInMB()) {
            throw new IllegalArgumentException("Chunk size of " + chunkSize.getSizeInMB() + " MB is larger than allowed max. size of " + MAX_CHUNK_SIZE_IN_MB.getSizeInMB() + " MB");
        }
        return chunkSize;
    }

    public static AttachmentConfigBuilder builder() {
        return new AttachmentConfigBuilder();
    }

    /**
     * Checks if attachment chunking for all attachments, including in-memory attachments, is active.
     * Per default large attachments will be chunked automatically.
     *
     * 
    *
  • If true all attachments will be chunked
  • *
  • If false only large attachments attachments will be chunked
  • *
* * @return true or false */ public boolean isChunkAllAttachments() { return chunkAllAttachments; } /** * Size of the chunks the attachment will be split into. * * @return chunk size in bytes */ public int getChunkSizeInBytes() { return chunkSizeInMB.getSizeInBytes(); } /** * Size of the chunks the attachment will be split into. * * @return chunk size in MB (^10) */ public int getChunkSizeInMB() { return chunkSizeInMB.getSizeInMB(); } /** * Path where the sdk stores attachment chunks for up- and download. * * @return storage path, null if not set */ public Path getAttachmentStoragePath() { if (attachmentStoragePath == null || attachmentStoragePath.isEmpty()) { return null; } return Path.of(attachmentStoragePath); } public static class AttachmentConfigBuilder { private boolean chunkAllAttachments; private ChunkSize chunkSize; private Path attachmentStoragePath; AttachmentConfigBuilder() { } /** * Activate attachment chunking for all attachments including in-memory attachments. * Per default chunking is only active for large attachments that do not fit into memory. * * @param chunkAllAttachments chunking for all attachment types on == true or off == false * @return the builder * @see Attachment#fromLargeAttachment */ public AttachmentConfigBuilder chunkAllAttachments(final boolean chunkAllAttachments) { this.chunkAllAttachments = chunkAllAttachments; return this; } /** * Set the chunk size in MB. Default is 50 MB, allowed max. size is 500 MB. * * @param sizeInMB size of a file chunk in MegaByte (^10) * @return the builder * @see #DEFAULT_CHUNK_SIZE_IN_MB * @see #MAX_CHUNK_SIZE_IN_MB */ public AttachmentConfigBuilder chunkSizeInMB(final int sizeInMB) { this.chunkSize = ChunkSize.ofMB(sizeInMB); return this; } /** * Path where the sdk stores attachment chunks for up- and download. If no path is set a folder in "java.io.tmpdir" is used. * * @param attachmentStoragePath path were attachment chunks and files are stored * @return the builder * * @see AttachmentChunkingConfig#DEFAULT_ATTACHMENT_FOLDER_NAME */ public AttachmentConfigBuilder attachmentStoragePath(final Path attachmentStoragePath) { this.attachmentStoragePath = attachmentStoragePath; return this; } /** * Build a new config object. * * @return AttachmentConfig * @throws IllegalArgumentException if the chunk size > max. size of 500 MB */ public AttachmentChunkingConfig build() { return new AttachmentChunkingConfig(chunkAllAttachments, chunkSize, attachmentStoragePath); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy