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

dev.fitko.fitconnect.core.zbp.ZBPApiService 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.core.zbp;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nimbusds.jose.jwk.RSAKey;
import dev.fitko.fitconnect.api.domain.zbp.ZBPEnvelope;
import dev.fitko.fitconnect.api.domain.zbp.attachment.ZBPApiAttachment;
import dev.fitko.fitconnect.api.domain.zbp.message.CreateMessageResponse;
import dev.fitko.fitconnect.api.exceptions.internal.RestApiException;
import dev.fitko.fitconnect.api.services.http.HttpClient;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import java.util.List;
import java.util.Map;

public class ZBPApiService {

    public static final String MAILBOX_MESSAGE_PATH = "/v6/mailbox/messages";
    public static final String CREATE_STATE_PATH = "/v6/mailbox/applications/states";
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    private final HttpClient httpClient;
    private final String baseUrl;

    public ZBPApiService(final HttpClient httpClient, final String baseUrl) {
        this.httpClient = httpClient;
        this.baseUrl = baseUrl;
    }

    /**
     * Insert a new message with optional attachments to the user mailbox.
     *
     * @param signingKey      key to sign the access token
     * @param envelope the signed envelope that contains the message payload
     * @param attachments     the attachments for the message, if empty no attachments will be sent
     * @return {@link CreateMessageResponse}
     * @throws RestApiException if there was a technical error sending the message
     */
    public CreateMessageResponse sendMessageToMailbox(final RSAKey signingKey, final ZBPEnvelope envelope, final List attachments) throws RestApiException {
        final String url = String.format(baseUrl + MAILBOX_MESSAGE_PATH);
        try {
            final Map headers = buildHeaders(signingKey, "multipart/form-data");
            final MultipartBody multipartBody = buildMultipartBody(envelope, attachments);
            return httpClient.put(url, headers, multipartBody, CreateMessageResponse.class).getBody();
        } catch (final Exception e) {
            throw new RestApiException("Could not send message to zbp mailbox", e);
        }
    }

    /**
     * Create a new state for a given
     *
     * @param signingKey          key to sign the access token
     * @param createStateEnvelope the signed envelope that contains the signed state payload
     * @throws RestApiException if there was a technical error creating the state
     */
    public void createNewState(final RSAKey signingKey, final ZBPEnvelope createStateEnvelope) throws RestApiException {
        final String url = String.format(baseUrl + CREATE_STATE_PATH);
        try {
            final Map headers = buildHeaders(signingKey, "application/json");
            httpClient.post(url, headers, createStateEnvelope, Void.class);
        } catch (final Exception e) {
            throw new RestApiException("Could not create application state", e);
        }
    }

    private MultipartBody buildMultipartBody(final ZBPEnvelope messageEnvelope, final List attachments) {
        var multipartBodyBuilder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("json", writeToString(messageEnvelope));

        for (ZBPApiAttachment attachment : attachments) {
            final RequestBody requestBody = RequestBody.create(MediaType.parse(attachment.getMimeType()), attachment.getData());
            multipartBodyBuilder.addFormDataPart("files", attachment.getFilename(), requestBody);
        }

        return multipartBodyBuilder.build();
    }

    private Map buildHeaders(RSAKey privateSigningKey, String contentType) {
        return Map.of(
                "Authorization", "Bearer " + TokenGenerator.buildToken(privateSigningKey),
                "Content-Type", contentType);
    }

    private String writeToString(ZBPEnvelope envelope) {
        try {
            return OBJECT_MAPPER.writeValueAsString(envelope);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy