diff --git a/src/main/java/de/elomagic/rb/backend/restclient/AbstractRestClient.java b/src/main/java/de/elomagic/rb/backend/restclient/AbstractRestClient.java index 356abc0..9798724 100644 --- a/src/main/java/de/elomagic/rb/backend/restclient/AbstractRestClient.java +++ b/src/main/java/de/elomagic/rb/backend/restclient/AbstractRestClient.java @@ -59,47 +59,32 @@ protected HttpRequest createDefaultDELETE(@Nonnull URI uri, @Nonnull String... h } @Nonnull - protected HttpRequest createDefaultPUT(@Nonnull URI uri, @Nonnull HttpRequest.BodyPublisher publisher, @Nonnull String... headers) { + protected HttpRequest createDefaultPUT(@Nonnull URI uri, @Nonnull BodyPublisherWrap publisher, @Nonnull String... headers) { HttpRequest.Builder builder = createDefaultRequest(uri) - .header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) - .header(HttpHeaders.ACCEPT,APPLICATION_JSON); + .header(HttpHeaders.CONTENT_TYPE, publisher.getContentType()) + .header(HttpHeaders.ACCEPT, APPLICATION_JSON); if (headers.length != 0) { builder = builder.headers(headers); } return builder - .PUT(publisher) + .PUT(publisher.getBodyPublisher()) .build(); } @Nonnull - protected HttpRequest createDefaultPOST(@Nonnull URI uri, @Nonnull HttpRequest.BodyPublisher publisher, @Nonnull String... headers) { + protected HttpRequest createDefaultPOST(@Nonnull URI uri, @Nonnull BodyPublisherWrap publisher, @Nonnull String... headers) { HttpRequest.Builder builder = createDefaultRequest(uri) - .header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) - .header(HttpHeaders.ACCEPT,APPLICATION_JSON); + .header(HttpHeaders.CONTENT_TYPE, publisher.getContentType()) + .header(HttpHeaders.ACCEPT, APPLICATION_JSON); if (headers.length != 0) { builder = builder.headers(headers); } return builder - .POST(publisher) - .build(); - } - - @Nonnull - protected HttpRequest createDefaultFormPOST(@Nonnull URI uri, @Nonnull HttpRequest.BodyPublisher publisher, @Nonnull String... headers) { - HttpRequest.Builder builder = createDefaultRequest(uri) - .header(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded") - .header(HttpHeaders.ACCEPT,APPLICATION_JSON); - - if (headers.length != 0) { - builder = builder.headers(headers); - } - - return builder - .POST(publisher) + .POST(publisher.getBodyPublisher()) .build(); } diff --git a/src/main/java/de/elomagic/rb/backend/restclient/BodyPublisher.java b/src/main/java/de/elomagic/rb/backend/restclient/BodyPublisher.java deleted file mode 100644 index 5d06b76..0000000 --- a/src/main/java/de/elomagic/rb/backend/restclient/BodyPublisher.java +++ /dev/null @@ -1,29 +0,0 @@ -package de.elomagic.rb.backend.restclient; - -import java.net.URLEncoder; -import java.net.http.HttpRequest; -import java.nio.charset.StandardCharsets; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * Body publisher for urlencoded forms aka as content type "application/x-www-form-urlencoded". - */ -public final class BodyPublisher { - - private BodyPublisher() {} - - public static HttpRequest.BodyPublisher of(final Map<String, String> data) { - - String s = data - .entrySet() - .stream() - .map(e -> "%s=%s".formatted( - URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8), - URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))) - .collect(Collectors.joining("&")); - - return HttpRequest.BodyPublishers.ofString(s, StandardCharsets.UTF_8); - - } -} diff --git a/src/main/java/de/elomagic/rb/backend/restclient/BodyPublisherWrap.java b/src/main/java/de/elomagic/rb/backend/restclient/BodyPublisherWrap.java new file mode 100644 index 0000000..d85e24d --- /dev/null +++ b/src/main/java/de/elomagic/rb/backend/restclient/BodyPublisherWrap.java @@ -0,0 +1,85 @@ +package de.elomagic.rb.backend.restclient; + +import com.fasterxml.jackson.core.JsonProcessingException; +import jakarta.annotation.Nonnull; + +import de.elomagic.rb.backend.utils.Json5MapperFactory; + +import java.net.URLEncoder; +import java.net.http.HttpRequest; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Improved body publisher. + */ +public final class BodyPublisherWrap { + + private BodyPublisherWrap() {} + + private String contentType; + private HttpRequest.BodyPublisher bodyPublisher; + + /** + * Serialize and provide object as a JSON string with content type "application/json". + * + * @param o Object to serialize + * @return Returns the wrapper + * @throws JsonProcessingException Thrown when unable to serialize object + */ + public static BodyPublisherWrap ofObject(final Object o) throws JsonProcessingException { + String json = Json5MapperFactory.create().writeValueAsString(o); + + return wrapHttpBodyPublisher( + HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8), + "application/json" + ); + } + + /** + * Serialize and provide form data with content type "application/x-www-form-urlencoded". + * + * @param data Form data as a map + * @return Returns the wrapper + */ + public static BodyPublisherWrap of(final Map<String, String> data) { + + String s = data + .entrySet() + .stream() + .map(e -> "%s=%s".formatted( + URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8), + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))) + .collect(Collectors.joining("&")); + + return wrapHttpBodyPublisher( + HttpRequest.BodyPublishers.ofString(s, StandardCharsets.UTF_8), + "application/x-www-form-urlencoded" + ); + + } + + public static BodyPublisherWrap wrapHttpBodyPublisher( + @Nonnull HttpRequest.BodyPublisher bodyPublisher, + + @Nonnull String contentType + ) { + BodyPublisherWrap bp = new BodyPublisherWrap(); + bp.bodyPublisher = bodyPublisher; + bp.contentType = contentType; + + return bp; + } + + @Nonnull + public String getContentType() { + return contentType; + } + + @Nonnull + public HttpRequest.BodyPublisher getBodyPublisher() { + return bodyPublisher; + } + +} diff --git a/src/main/java/de/elomagic/rb/backend/restclient/OAuth2RestClient.java b/src/main/java/de/elomagic/rb/backend/restclient/OAuth2RestClient.java index 80dca88..e811671 100644 --- a/src/main/java/de/elomagic/rb/backend/restclient/OAuth2RestClient.java +++ b/src/main/java/de/elomagic/rb/backend/restclient/OAuth2RestClient.java @@ -45,7 +45,7 @@ private void createAccessToken() { formData.put("client_secret", clientSecret); formData.put("scope", scopeUrl); - HttpRequest request = createDefaultFormPOST(uri, BodyPublisher.of(formData)); + HttpRequest request = createDefaultPOST(uri, BodyPublisherWrap.of(formData)); TokenResponse tokenResponse = executeRequest(request, TokenResponse.class); bearerToken = tokenResponse.getAccessToken();