-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace FormBody with a BodyPublisher
- Loading branch information
1 parent
1ebd081
commit bbf6980
Showing
3 changed files
with
58 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
37 changes: 0 additions & 37 deletions
37
service-rest-api/src/main/java/org/bsc/confluence/rest/FormBody.java
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
service-rest-api/src/main/java/org/bsc/confluence/rest/FormUrlEncodedBodyPublisher.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,41 @@ | ||
package org.bsc.confluence.rest; | ||
|
||
import java.net.http.HttpRequest; | ||
import java.net.URLEncoder; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.concurrent.Flow; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class FormUrlEncodedBodyPublisher implements HttpRequest.BodyPublisher { | ||
|
||
private final byte[] data; | ||
|
||
public final String getContentType() { | ||
return "application/x-www-form-urlencoded"; | ||
} | ||
public FormUrlEncodedBodyPublisher(Map<String, String> fields) { | ||
|
||
data = fields.entrySet().stream() | ||
.map(kv -> format("%s=%s", | ||
URLEncoder.encode(kv.getKey(), StandardCharsets.UTF_8), | ||
URLEncoder.encode(kv.getValue(), StandardCharsets.UTF_8))) | ||
.collect(Collectors.joining("&")) | ||
.getBytes(StandardCharsets.UTF_8);; | ||
|
||
} | ||
|
||
@Override | ||
public long contentLength() { | ||
return data.length; | ||
} | ||
|
||
@Override | ||
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { | ||
subscriber.onNext(ByteBuffer.wrap(data)); | ||
subscriber.onComplete(); | ||
} | ||
} |