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

dev.fitko.fitconnect.client.sender.model.Attachment 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.sender.model;

import dev.fitko.fitconnect.api.exceptions.client.FitConnectSenderException;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;

public class Attachment {
    private final byte[] data;
    private final String fileName;
    private final String description;
    private final String mimeType;

    /**
     * Create an attachment and read the content from a given path.
     *
     * @param filePath path of the attachment file
     * @param mimeType mime-type of the attachment
     */
    public static Attachment fromPath(final Path filePath, final String mimeType) throws FitConnectSenderException {
        return fromPath(filePath, mimeType, null, null);
    }

    /**
     * Create an attachment and read the content from a given path.
     *
     * @param filePath    path of the attachment file
     * @param mimeType    mime-type of the attachment
     * @param fileName    name of the attachment file
     * @param description description of the attachment file
     * @throws FitConnectSenderException if the file path could not be read
     */
    public static Attachment fromPath(final Path filePath, final String mimeType, final String fileName, final String description) throws FitConnectSenderException {
        return new Attachment(readBytesFromPath(filePath), mimeType, fileName, description);
    }

    /**
     * Create an attachment and read the content from an input stream.
     *
     * @param inputStream stream of the attachment data
     * @param mimeType    mime type of the provided attachment data
     * @throws FitConnectSenderException if the input-stream could not be read
     */
    public static Attachment fromInputStream(final InputStream inputStream, final String mimeType) throws FitConnectSenderException {
        return fromInputStream(inputStream, mimeType, null, null);
    }

    /**
     * Create an attachment and read the content from an input stream.
     *
     * @param inputStream stream of the attachment data
     * @param mimeType    mime type of the provided attachment data
     * @param fileName    name of the attachment file
     * @param description description of the attachment file
     * @throws FitConnectSenderException if the input-stream could not be read
     */
    public static Attachment fromInputStream(final InputStream inputStream, final String mimeType, final String fileName, final String description) throws FitConnectSenderException {
        return new Attachment(readBytesFromInputStream(inputStream), mimeType, fileName, description);
    }

    /**
     * Create an attachment and read the content from a byte array.
     *
     * @param content  data of the attachment as byte[]
     * @param mimeType mime type of the provided attachment data
     */
    public static Attachment fromByteArray(final byte[] content, final String mimeType) {
        return fromByteArray(content, mimeType, null, null);
    }

    /**
     * Create an attachment and read the content from a byte array.
     *
     * @param content     data of the attachment as byte[]
     * @param fileName    name of the attachment file
     * @param mimeType    mime type of the provided attachment data
     * @param description description of the attachment file
     */
    public static Attachment fromByteArray(final byte[] content, final String mimeType, final String fileName, final String description) {
        return new Attachment(content, mimeType, fileName, description);
    }

    /**
     * Create an attachment and read the content from a given string. The content will be read with UTF-8 encoding.
     * 

Note: If you don't use an SDK to retrieve the submission you may have to decode the string with UTF-8

* * @param content data of the attachment as byte[] * @param mimeType mime type of the provided attachment data */ public static Attachment fromString(final String content, final String mimeType) { return fromString(content, mimeType, null, null); } /** * Create an attachment and read the content from a given string. The content will be read with UTF-8 encoding. *

Note: If you don't use an SDK to retrieve the submission you may have to decode the string with UTF-8

* * @param content data of the attachment as string * @param fileName name of the attachment file * @param mimeType mime type of the provided attachment data * @param description description of the attachment file */ public static Attachment fromString(final String content, final String mimeType, final String fileName, final String description) { return new Attachment(content.getBytes(StandardCharsets.UTF_8), mimeType, fileName, description); } /** * Get the attachment content as byte[]. * * @return byte array of the attachments content */ public byte[] getDataAsBytes() { return data; } /** * Get the attachment content as string, e.g. in case the mime-type is json or xml. * * @param encoding charset the string should be decoded with * @return string of the attachments content. */ public String getDataAString(final Charset encoding) { return new String(data, encoding); } /** * Get the attachment content as string with a default UTF-8 encoding, e.g. in case the mime-type is json or xml. * * @return utf-8 encoded string of the attachments content. */ public String getDataAString() { return getDataAString(StandardCharsets.UTF_8); } /** * Filename of the attachment. This filed is optional so it might be null. * @return filename as string, null if not present */ public String getFileName() { return fileName; } /** * Description of the attachment. This filed is optional so it might be null. * @return description as string, null if not present */ public String getDescription() { return description; } /** * Mimetype of the attachments content. * @return mimetype as string. */ public String getMimeType() { return mimeType; } private Attachment(final byte[] data, final String mimeType, final String fileName, final String description) { this.data = data; this.fileName = fileName != null ? getBaseNameFromPath(fileName) : null; // prevent maliciously injected filePaths this.mimeType = mimeType; this.description = description; } private static byte[] readBytesFromInputStream(final InputStream inputStream) { try (final BufferedInputStream bis = new BufferedInputStream(inputStream)) { return bis.readAllBytes(); } catch (final IOException e) { throw new FitConnectSenderException("Attachment could not be read from input-stream ", e); } } private static byte[] readBytesFromPath(final Path path) { try { return Files.readAllBytes(path); } catch (final IOException e) { throw new FitConnectSenderException("Reading attachment from path '" + path + "' failed ", e); } } private static String getBaseNameFromPath(final String fileName) { try { return Path.of(fileName).getFileName().toString(); } catch (final InvalidPathException e) { throw new FitConnectSenderException("Reading filename '" + fileName + "' failed ", e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy