dev.fitko.fitconnect.core.submission.SubmissionApiService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of client Show documentation
Show all versions of client Show documentation
Library that provides client access to the FIT-Connect api-endpoints for sending, subscribing and
routing
package dev.fitko.fitconnect.core.submission;
import dev.fitko.fitconnect.api.domain.model.destination.Destination;
import dev.fitko.fitconnect.api.domain.model.submission.AnnounceSubmission;
import dev.fitko.fitconnect.api.domain.model.submission.CreatedSubmission;
import dev.fitko.fitconnect.api.domain.model.submission.Submission;
import dev.fitko.fitconnect.api.domain.model.submission.SubmissionsForPickup;
import dev.fitko.fitconnect.api.domain.model.submission.SubmitSubmission;
import dev.fitko.fitconnect.api.exceptions.internal.RestApiException;
import dev.fitko.fitconnect.api.services.auth.OAuthService;
import dev.fitko.fitconnect.api.services.http.HttpClient;
import dev.fitko.fitconnect.api.services.submission.SubmissionService;
import dev.fitko.fitconnect.core.http.HttpHeaders;
import dev.fitko.fitconnect.core.http.MimeTypes;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class SubmissionApiService implements SubmissionService {
public static final String SUBMISSION_PATH = "/v1/submissions/%s";
public static final String SUBMISSIONS_PATH = "/v1/submissions";
public static final String SUBMISSION_ATTACHMENT_PATH = "/v1/submissions/%s/attachments/%s";
public static final String DESTINATIONS_PATH = "/v1/destinations/%s";
private final OAuthService authService;
private final HttpClient httpClient;
private final String baseUrl;
public SubmissionApiService(final OAuthService authService, final HttpClient httpClient, final String baseUrl) {
this.authService = authService;
this.httpClient = httpClient;
this.baseUrl = baseUrl;
}
@Override
public Destination getDestination(final UUID destinationID) throws RestApiException {
final String url = String.format(baseUrl + DESTINATIONS_PATH, destinationID);
try {
return httpClient.get(url, getHeaders(), Destination.class).getBody();
} catch (final RestApiException e) {
throw new RestApiException("Could not get destination " + destinationID, e);
}
}
@Override
public CreatedSubmission announceSubmission(final AnnounceSubmission submission) throws RestApiException {
try {
return httpClient.post(baseUrl + SUBMISSIONS_PATH, getHeaders(), submission, CreatedSubmission.class).getBody();
} catch (final RestApiException e) {
throw new RestApiException("Could not announce submission for destination " + submission.getDestinationId(), e);
}
}
@Override
public void uploadAttachment(final UUID submissionId, final UUID attachmentId, final String encryptedAttachment) throws RestApiException {
final String url = String.format(baseUrl + SUBMISSION_ATTACHMENT_PATH, submissionId, attachmentId);
try {
httpClient.put(url, getHeaders("application/jose"), encryptedAttachment, Void.class);
} catch (final RestApiException e) {
throw new RestApiException("Could not upload attachment", e);
}
}
@Override
public void uploadAttachmentStream(final UUID submissionId, final UUID attachmentId, final InputStream encryptedAttachment) throws RestApiException {
final String url = String.format(baseUrl + SUBMISSION_ATTACHMENT_PATH, submissionId, attachmentId);
try {
httpClient.put(url, getHeaders("application/jose"), encryptedAttachment, Void.class);
} catch (final RestApiException e) {
throw new RestApiException("Could not upload attachment", e);
}
}
@Override
public String getAttachment(final UUID submissionId, final UUID attachmentId) throws RestApiException {
final String url = String.format(baseUrl + SUBMISSION_ATTACHMENT_PATH, submissionId, attachmentId);
try {
return httpClient.get(url, getHeaders("application/jose"), String.class).getBody();
} catch (final RestApiException e) {
throw new RestApiException("Could not get attachment " + attachmentId, e);
}
}
@Override
public Submission sendSubmission(final SubmitSubmission submission) throws RestApiException {
final String url = String.format(baseUrl + SUBMISSION_PATH, submission.getSubmissionId());
try {
return httpClient.put(url, getHeaders(), submission, Submission.class).getBody();
} catch (final RestApiException e) {
throw new RestApiException("Could not send submission " + submission.getSubmissionId(), e);
}
}
@Override
public SubmissionsForPickup pollAvailableSubmissionsForDestination(final UUID destinationId, final int offset, final int limit) {
final String urlWithQueryParams = baseUrl + SUBMISSIONS_PATH + "?destinationId=" + destinationId + "&limit=" + limit + "&offset=" + offset;
try {
return httpClient.get(urlWithQueryParams, getHeaders(), SubmissionsForPickup.class).getBody();
} catch (final RestApiException e) {
throw new RestApiException("Could not poll for available submissions on destination " + destinationId, e);
}
}
@Override
public Submission getSubmission(final UUID submissionId) throws RestApiException {
try {
return httpClient.get(String.format(baseUrl + SUBMISSION_PATH, submissionId), getHeaders(), Submission.class).getBody();
} catch (final RestApiException e) {
throw new RestApiException("Submission with id " + submissionId + " does not exist", e, e.getStatusCode());
}
}
private Map getHeaders() {
return getHeaders(MimeTypes.APPLICATION_JSON);
}
private Map getHeaders(final String contentType) {
return new HashMap<>(Map.of(
HttpHeaders.AUTHORIZATION, "Bearer " + authService.getCurrentToken().getAccessToken(),
HttpHeaders.CONTENT_TYPE, contentType,
HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.toString()
));
}
}