-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
src/main/java/de/elomagic/rb/backend/restclient/BodyPublisher.java
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
src/main/java/de/elomagic/rb/backend/restclient/BodyPublisherWrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters