Skip to content

Commit

Permalink
HTTP body publisher improved
Browse files Browse the repository at this point in the history
  • Loading branch information
elomagic committed Sep 24, 2024
1 parent d0b76a5 commit cef9fa5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
29 changes: 0 additions & 29 deletions src/main/java/de/elomagic/rb/backend/restclient/BodyPublisher.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cef9fa5

Please sign in to comment.