Skip to content

Commit

Permalink
feat: replace FormBody with a BodyPublisher
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Oct 17, 2023
1 parent 1ebd081 commit bbf6980
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import org.bsc.confluence.ConfluenceService;
import org.bsc.confluence.rest.FormBody;
import org.bsc.confluence.rest.FormUrlEncodedBodyPublisher;
import org.bsc.confluence.rest.RESTConfluenceService;
import org.bsc.confluence.rest.scrollversions.model.ScrollVersions;
import org.bsc.ssl.SSLCertificateInfo;
Expand All @@ -15,7 +15,10 @@
import java.net.URL;
import java.net.http.HttpRequest;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -344,15 +347,14 @@ CompletableFuture<ScrollVersions.Model.NewPageResult> createVersionPage(String s
return CompletableFuture.failedFuture(e);
}

final var body = new FormBody.Builder()
.add("parentConfluenceId", masterPageId.toString() )
.add( "versionId", version.getId() )
.add( "pageTitle", title )
.build()
;
final var body = new FormUrlEncodedBodyPublisher( Map.of(
"parentConfluenceId", masterPageId.toString(),
"versionId", version.getId(),
"pageTitle", title
));

final var request = newRequestBuilder()
.header("Content-Type", FormBody.getContentType())
.header("Content-Type", body.getContentType())
.uri(url)
.POST(body)
;
Expand Down Expand Up @@ -403,16 +405,15 @@ CompletableFuture<ScrollVersions.Model.NewPageResult> manageVersionPage(Model.ID
return CompletableFuture.failedFuture(e);
}

final var body = new FormBody.Builder()
.add("masterPageId", masterPageId.toString() )
.add( "pageTitle", title)
.add( "versionId", version.getId() )
.add( "changeType", changeType.typeName )
.build()
final var body = new FormUrlEncodedBodyPublisher( Map.of(
"masterPageId", masterPageId.toString(),
"pageTitle", title,
"versionId", version.getId(),
"changeType", changeType.typeName))
;

final var request = newRequestBuilder()
.header("Content-Type", FormBody.getContentType())
.header("Content-Type", body.getContentType())
.uri(url)
.POST(body)
;
Expand Down

This file was deleted.

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();
}
}

0 comments on commit bbf6980

Please sign in to comment.