Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • fit-connect/sdk-java
1 result
Show changes
Showing
with 559 additions and 180 deletions
package dev.fitko.fitconnect.client.sender.model;
import dev.fitko.fitconnect.api.exceptions.AttachmentCreationException;
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 AttachmentCreationException {
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 AttachmentCreationException 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 AttachmentCreationException {
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 AttachmentCreationException if the input-stream could not be read
*/
public static Attachment fromInputStream(final InputStream inputStream, final String mimeType) throws AttachmentCreationException {
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 AttachmentCreationException 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 AttachmentCreationException {
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.
* <p>Note: If you don't use an SDK to retrieve the submission you may have to decode the string with UTF-8</p>
*
* @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.
* <p>Note: If you don't use an SDK to retrieve the submission you may have to decode the string with UTF-8</p>
*
* @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);
}
/**
* 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 AttachmentCreationException("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 AttachmentCreationException("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 AttachmentCreationException("Reading filename '" + fileName + "' failed ", e);
}
}
}
......@@ -4,16 +4,16 @@ import lombok.Builder;
import lombok.Getter;
import lombok.With;
import java.io.File;
import java.util.UUID;
@With
@Getter
@Builder
public class AttachmentPayload {
private File file;
private UUID attachmentId;
private String hashedData;
private String encryptedData;
private String mimeType;
private UUID attachmentId;
private String fileName;
private String description;
}
package dev.fitko.fitconnect.client.sender.model;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import java.util.UUID;
@Getter
@EqualsAndHashCode
public class EncryptedAttachment {
private final UUID attachmentId;
private final String content;
/**
* Create a new encrypted attachment.
*
* @param attachmentId the attachmentId that matches with the announced attachmentId in the submissions metadata
* @param content encrypted content of the attachment
*/
public EncryptedAttachment(final UUID attachmentId, final String content) {
this.attachmentId = attachmentId;
this.content = content;
}
}
package dev.fitko.fitconnect.client.sender.model;
import dev.fitko.fitconnect.api.domain.model.submission.ServiceType;
import dev.fitko.fitconnect.client.sender.EncryptedSubmissionBuilder;
import lombok.Getter;
import java.util.Map;
import java.util.UUID;
@Getter
public class EncryptedSubmissionPayload {
private final UUID destinationId;
private final Map<UUID, String> encryptedAttachments;
private final String encryptedData;
private final String encryptedMetadata;
private final ServiceType serviceType;
public EncryptedSubmissionPayload(final EncryptedSubmissionBuilder builder) {
destinationId = builder.getDestinationId();
encryptedAttachments = builder.getEncryptedAttachments();
encryptedData = builder.getEncryptedData();
encryptedMetadata = builder.getEncryptedMetadata();
serviceType = builder.getServiceType();
}
}
package dev.fitko.fitconnect.client.sender.model;
import dev.fitko.fitconnect.client.sender.steps.encrypted.EncryptedAttachmentsStep;
import dev.fitko.fitconnect.client.sender.steps.encrypted.EncryptedBuildStep;
import dev.fitko.fitconnect.client.sender.steps.encrypted.EncryptedDataStep;
import dev.fitko.fitconnect.client.sender.steps.encrypted.EncryptedDestinationStep;
import dev.fitko.fitconnect.client.sender.steps.encrypted.EncryptedMetadataStep;
import dev.fitko.fitconnect.client.sender.steps.encrypted.EncryptedServiceTypeStep;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
@Getter
public class SendableEncryptedSubmission {
private final UUID destinationId;
private final List<EncryptedAttachment> attachments;
private final String data;
private final String metadata;
private final String serviceName;
private final String serviceIdentifier;
private SendableEncryptedSubmission(final Builder builder) {
destinationId = builder.getDestinationId();
attachments = Collections.unmodifiableList(builder.getEncryptedAttachments());
data = builder.getEncryptedData();
metadata = builder.getEncryptedMetadata();
serviceName = builder.getServiceName();
serviceIdentifier = builder.getServiceIdentifier();
}
public static EncryptedDestinationStep Builder() {
return new Builder();
}
@Getter
private static final class Builder implements EncryptedDestinationStep,
EncryptedServiceTypeStep,
EncryptedMetadataStep,
EncryptedDataStep,
EncryptedAttachmentsStep,
EncryptedBuildStep {
private static final Logger LOGGER = LoggerFactory.getLogger(Builder.class);
private UUID destinationId;
private String serviceName;
private String serviceIdentifier;
private String encryptedData;
private String encryptedMetadata;
private final List<EncryptedAttachment> encryptedAttachments = new ArrayList<>();
private Builder() {
}
@Override
public EncryptedServiceTypeStep setDestination(final UUID destinationId) {
this.destinationId = destinationId;
return this;
}
@Override
public EncryptedAttachmentsStep addEncryptedAttachments(final List<EncryptedAttachment> attachments) {
if (attachments != null && !attachments.isEmpty()) {
encryptedAttachments.addAll(attachments);
}
return this;
}
@Override
public EncryptedAttachmentsStep addEncryptedAttachment(final EncryptedAttachment attachment) {
if(attachment != null){
encryptedAttachments.add(attachment);
}
return this;
}
@Override
public EncryptedAttachmentsStep setEncryptedData(final String data) {
encryptedData = data;
return this;
}
@Override
public EncryptedDataStep setEncryptedMetadata(final String encryptedMetadata) {
this.encryptedMetadata = encryptedMetadata;
return this;
}
@Override
public EncryptedMetadataStep setServiceType(final String serviceIdentifier, final String serviceName) {
this.serviceIdentifier = serviceIdentifier;
this.serviceName = serviceName;
return this;
}
@Override
public SendableEncryptedSubmission build() {
return new SendableEncryptedSubmission(this);
}
}
}
package dev.fitko.fitconnect.client.sender.model;
import dev.fitko.fitconnect.api.domain.model.metadata.AuthenticationInformation;
import dev.fitko.fitconnect.api.domain.model.metadata.payment.PaymentInformation;
import dev.fitko.fitconnect.api.domain.model.replychannel.ReplyChannel;
import dev.fitko.fitconnect.client.sender.steps.unencrypted.BuildStep;
import dev.fitko.fitconnect.client.sender.steps.unencrypted.DataStep;
import dev.fitko.fitconnect.client.sender.steps.unencrypted.DestinationStep;
import dev.fitko.fitconnect.client.sender.steps.unencrypted.OptionalPropertiesStep;
import dev.fitko.fitconnect.client.sender.steps.unencrypted.ServiceTypeStep;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import static dev.fitko.fitconnect.api.domain.model.metadata.data.MimeType.APPLICATION_XML;
@Getter
public class SendableSubmission {
private final UUID destinationId;
private final String data;
private final String dataMimeType;
private final List<Attachment> attachments;
private final String serviceName;
private final String serviceIdentifier;
private final List<AuthenticationInformation> authenticationInformation;
private final PaymentInformation paymentInformation;
private final ReplyChannel replyChannel;
private SendableSubmission(final Builder builder) {
destinationId = builder.getDestinationId();
data = builder.getData();
dataMimeType = builder.getDataMimeType();
attachments = Collections.unmodifiableList(builder.getAttachments());
serviceName = builder.getServiceName();
serviceIdentifier = builder.getServiceIdentifier();
authenticationInformation = builder.getAuthenticationInformation();
replyChannel = builder.getReplyChannel();
paymentInformation = builder.getPaymentInformation();
}
public static DestinationStep Builder() {
return new Builder();
}
@Getter
private static final class Builder implements DestinationStep,
ServiceTypeStep,
DataStep,
OptionalPropertiesStep,
BuildStep {
private static final Logger LOGGER = LoggerFactory.getLogger(Builder.class);
private UUID destinationId;
private final List<Attachment> attachments = new ArrayList<>();
private String data;
private String dataMimeType;
private String serviceName;
private String serviceIdentifier;
private List<AuthenticationInformation> authenticationInformation;
private PaymentInformation paymentInformation;
private ReplyChannel replyChannel;
private Builder() {
}
@Override
public ServiceTypeStep setDestination(final UUID destinationId) {
this.destinationId = destinationId;
return this;
}
@Override
public OptionalPropertiesStep addAttachments(final List<Attachment> attachments) {
if (attachments != null && !attachments.isEmpty()) {
this.attachments.addAll(attachments);
}
return this;
}
@Override
public OptionalPropertiesStep addAttachment(final Attachment attachment) {
if (attachment != null) {
attachments.add(attachment);
}
return this;
}
@Override
public OptionalPropertiesStep setReplyChannel(final ReplyChannel replyChannel) {
this.replyChannel = replyChannel;
return this;
}
@Override
public OptionalPropertiesStep setJsonData(final String jsonData) {
data = jsonData;
dataMimeType = APPLICATION_JSON.value();
return this;
}
@Override
public OptionalPropertiesStep setXmlData(final String xmlData) {
data = xmlData;
dataMimeType = APPLICATION_XML.value();
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> authenticationInformation) {
this.authenticationInformation = authenticationInformation;
return this;
}
@Override
public OptionalPropertiesStep setPaymentInformation(final PaymentInformation paymentInformation) {
this.paymentInformation = paymentInformation;
return this;
}
@Override
public SendableSubmission build() {
return new SendableSubmission(this);
}
}
}
package dev.fitko.fitconnect.client.sender.model;
import dev.fitko.fitconnect.api.domain.model.metadata.data.MimeType;
import dev.fitko.fitconnect.api.domain.model.submission.ServiceType;
import dev.fitko.fitconnect.client.sender.SubmissionBuilder;
import lombok.Getter;
import java.io.File;
import java.util.List;
import java.util.UUID;
@Getter
public class SubmissionPayload {
private final UUID destinationId;
private final String data;
private final MimeType dataMimeType;
private final List<File> attachments;
private final ServiceType serviceType;
public SubmissionPayload(final SubmissionBuilder builder) {
destinationId = builder.getDestinationId();
data = builder.getData();
dataMimeType = builder.getDataMimeType();
attachments = builder.getAttachments();
serviceType = builder.getServiceType();
}
}
package dev.fitko.fitconnect.client.sender.steps;
import dev.fitko.fitconnect.client.sender.model.SubmissionPayload;
public interface BuildStep {
SubmissionPayload build();
}
package dev.fitko.fitconnect.client.sender.steps;
import java.io.File;
import java.util.List;
public interface BuilderStartStep {
/**
* Sends the submission with a list of attachments
*
* @param attachments that are sent with the submission
* @return the next step where additional data can be added to the submission
*/
DataStep withAttachments(List<File> attachments);
/**
* Sends the submission with an attachments
*
* @param attachment that is sent with the submission
* @return the next step where additional data can be added to the submission
*/
DataStep withAttachment(File attachment);
/**
* JSON data as string.
*
* @param data json string
* @return next step to submit the data
*/
DestinationStep withJsonData(String data);
/**
* XML data as string.
*
* @param data xml string
* @return next step to submit the data
*/
DestinationStep withXmlData(String data);
}
package dev.fitko.fitconnect.client.sender.steps;
import dev.fitko.fitconnect.client.sender.model.EncryptedSubmissionPayload;
public interface EncryptedBuildStep {
EncryptedSubmissionPayload build();
}
package dev.fitko.fitconnect.client.sender.steps;
import java.util.Map;
import java.util.UUID;
public interface EncryptedBuilderStartStep {
/**
* Sends the submission with a list of already encrypted attachments
*
* @param encryptedAttachments map of encrypted attachments on attachment UUIDs
*
* @return the next step where additional encrypted data can be added to the submission
*/
EncryptedDataStep withEncryptedAttachments(Map<UUID, String> encryptedAttachments);
/**
* Sends the submission with an attachments
*
* @param attachmentId unique identifier attachment that is uploaded
* @param attachment encrypted attachment that is sent with the submission
* @return the next step where additional data can be added to the submission
*/
EncryptedDataStep withEncryptedAttachment(UUID attachmentId, String attachment);
/**
* Data as encrypted JWE string.
*
* @param encryptedData encrypted data as string
* @return next step to submit the data
*/
EncryptedMetadataStep withEncryptedData(String encryptedData);
}
package dev.fitko.fitconnect.client.sender.steps;
import dev.fitko.fitconnect.api.domain.model.submission.ServiceType;
public interface ServiceTypeStep {
/**
* Sets a {@link ServiceType} for a submission
*
* @param name a name of the service
* @param leikaKey unique identifier of the service in the form <b>urn:de:fim:leika:leistung:</b>
* @return next step to add attachments
* @see <a href="https://fimportal.de/">Search For Service Types</a>
*/
BuildStep withServiceType(String name, String leikaKey);
/**
* Sets a {@link ServiceType} for a submission
*
* @param name a name of the service
* @param description description of the service
* @param leikaKey unique identifier of the service in the form <b>urn:de:fim:leika:leistung:</b>
* @return next step to add attachments
* @see <a href="https://fimportal.de/">Search For Service Types</a>
*/
BuildStep withServiceType(String name, String description, String leikaKey);
}
package dev.fitko.fitconnect.client.sender.steps.encrypted;
import dev.fitko.fitconnect.client.sender.model.EncryptedAttachment;
import dev.fitko.fitconnect.client.sender.model.SendableEncryptedSubmission;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public interface EncryptedAttachmentsStep {
/**
* Add a list of encrypted attachments to the submission to send.
*
* @param attachments list of encrypted attachments
*
* @return the next step where additional encrypted data can be added to the submission
*/
EncryptedAttachmentsStep addEncryptedAttachments(List<EncryptedAttachment> attachments);
/**
* Add an encrypted attachment to the submission to send.
*
* @param attachment encrypted attachment to add
* @return the next step where additional data can be added to the submission
*/
EncryptedAttachmentsStep addEncryptedAttachment(EncryptedAttachment attachment);
SendableEncryptedSubmission build();
}
package dev.fitko.fitconnect.client.sender.steps.encrypted;
import dev.fitko.fitconnect.client.sender.model.SendableEncryptedSubmission;
public interface EncryptedBuildStep {
SendableEncryptedSubmission build();
}
package dev.fitko.fitconnect.client.sender.steps;
package dev.fitko.fitconnect.client.sender.steps.encrypted;
public interface EncryptedDataStep {
/**
* Data as encrypted JWE string.
*
* @param encryptedData encrypted data as string
* @return next step to add encrypted metadata
* @param data encrypted data as string
* @return next step to add encrypted attachments
*/
EncryptedMetadataStep withEncryptedData(String encryptedData);
EncryptedAttachmentsStep setEncryptedData(String data);
}
package dev.fitko.fitconnect.client.sender.steps;
package dev.fitko.fitconnect.client.sender.steps.encrypted;
import java.util.UUID;
......@@ -10,5 +10,5 @@ public interface EncryptedDestinationStep {
* @param destinationId unique identifier of the clients destination
* @return the upload step for attachments
*/
EncryptedServiceTypeStep withDestination(UUID destinationId);
EncryptedServiceTypeStep setDestination(UUID destinationId);
}
package dev.fitko.fitconnect.client.sender.steps;
package dev.fitko.fitconnect.client.sender.steps.encrypted;
public interface EncryptedMetadataStep {
/**
* JSON data as encrypted JWE string.
*
* @param encryptedMetadata encrypted JWE string of the metadata
* @return next step to add a destination
* @param metadata encrypted JWE string of the metadata
* @return next step to add encrypted data
*/
EncryptedDestinationStep withEncryptedMetadata(String encryptedMetadata);
EncryptedDataStep setEncryptedMetadata(String metadata);
}
package dev.fitko.fitconnect.client.sender.steps;
package dev.fitko.fitconnect.client.sender.steps.encrypted;
import dev.fitko.fitconnect.api.domain.model.submission.ServiceType;
......@@ -7,21 +7,10 @@ public interface EncryptedServiceTypeStep {
/**
* Sets a {@link ServiceType} for a submission
*
* @param name a name of the service
* @param leikaKey unique identifier of the service in the form <b>urn:de:fim:leika:leistung:</b>
* @return next step to add attachments
* @see <a href="https://fimportal.de/">Search For Service Types</a>
*/
EncryptedBuildStep withServiceType(String name, String leikaKey);
/**
* Sets a {@link ServiceType} for a submission
*
* @param name a name of the service
* @param description description of the service
* @param leikaKey unique identifier of the service in the form <b>urn:de:fim:leika:leistung:</b>
* @return next step to add attachments
* @param name a name of the service
* @return next step to add encrypted metadata
* @see <a href="https://fimportal.de/">Search For Service Types</a>
*/
EncryptedBuildStep withServiceType(String name, String description, String leikaKey);
EncryptedMetadataStep setServiceType(String leikaKey, String name);
}
package dev.fitko.fitconnect.client.sender.steps.unencrypted;
import dev.fitko.fitconnect.client.sender.model.SendableSubmission;
public interface BuildStep {
SendableSubmission build();
}
package dev.fitko.fitconnect.client.sender.steps;
package dev.fitko.fitconnect.client.sender.steps.unencrypted;
public interface DataStep {
......@@ -8,7 +8,7 @@ public interface DataStep {
* @param data json string
* @return next step to submit the data
*/
DestinationStep withJsonData(String data);
OptionalPropertiesStep setJsonData(String data);
/**
* XML data as string.
......@@ -16,5 +16,5 @@ public interface DataStep {
* @param data xml string
* @return next step to submit the data
*/
DestinationStep withXmlData(String data);
OptionalPropertiesStep setXmlData(String data);
}