Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.proofpoint.http.client;

public interface BodySource
public sealed interface BodySource
permits DynamicBodySource, InputStreamBodySource, StaticBodyGenerator
{
/**
* @return the content length, if known, or -1 if the content length is unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import java.io.OutputStream;

public interface DynamicBodySource extends BodySource
public non-sealed interface DynamicBodySource
extends BodySource
{
/**
* Start writing the request body.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import static com.google.common.base.Preconditions.checkState;

public class InputStreamBodySource
public non-sealed class InputStreamBodySource
implements BodySource, LimitedRetryable
{
private final InputStream inputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.nio.charset.Charset;

public class StaticBodyGenerator implements BodySource
public non-sealed class StaticBodyGenerator implements BodySource
{
public static StaticBodyGenerator createStaticBodyGenerator(String body, Charset charset)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ListMultimap;
import org.testng.annotations.Test;

import java.io.OutputStream;
import java.net.URI;

import static com.proofpoint.http.client.Request.Builder.prepareGet;
Expand Down Expand Up @@ -115,8 +116,13 @@ private static ListMultimap<String, String> createHeadersB()

public static BodySource createBodySource()
{
return new BodySource()
return new DynamicBodySource()
{
@Override
public Writer start(OutputStream out) throws Exception
{
throw new UnsupportedOperationException();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableListMultimap;
import org.testng.annotations.Test;

import java.io.OutputStream;
import java.net.URI;

import static com.proofpoint.http.client.Request.Builder.fromRequest;
Expand All @@ -30,8 +31,8 @@

public class TestRequestBuilder
{
public static final BodySource NULL_BODY_SOURCE = new BodySource()
{
public static final BodySource NULL_BODY_SOURCE = (DynamicBodySource) out -> {
throw new UnsupportedOperationException();
};

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.proofpoint.http.client.balancing;

import com.proofpoint.http.client.BodySource;
import com.proofpoint.http.client.DynamicBodySource;
import com.proofpoint.http.client.HttpClient;
import com.proofpoint.http.client.LimitedRetryable;
import com.proofpoint.http.client.Request;
Expand Down Expand Up @@ -108,7 +109,7 @@ protected void setUp()
.setMinBackoff(new Duration(1, TimeUnit.MILLISECONDS))
.setMaxBackoff(new Duration(2, TimeUnit.MILLISECONDS));
balancingHttpClient = createBalancingHttpClient();
bodySource = mock(BodySource.class);
bodySource = mock(DynamicBodySource.class);
request = preparePut().setUri(URI.create("v1/service")).setBodySource(bodySource).build();
requestArgumentCaptor = ArgumentCaptor.forClass(Request.class);
response = mock(Response.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class TestingHttpClient
private String method;
private List<URI> uris = new ArrayList<>();
private List<Object> responses = new ArrayList<>();
private boolean skipBodyGenerator = false;
private boolean skipBodySource = false;

TestingHttpClient(String method)
{
Expand Down Expand Up @@ -130,7 +130,7 @@ private TestingHttpClient expectCall(URI uri, Object response)
@Override
public TestingClient firstCallNoBodyGenerator()
{
skipBodyGenerator = true;
skipBodySource = true;
return this;
}

Expand All @@ -155,8 +155,8 @@ public <T, E extends Exception> T execute(Request request, ResponseHandler<T, E>
assertEquals(request.getUri(), uris.remove(0), "request uri");
assertEquals(request.getBodySource(), bodySource, "request body generator");

if (skipBodyGenerator) {
skipBodyGenerator = false;
if (skipBodySource) {
skipBodySource = false;
}
else if (bodySource instanceof LimitedRetryable) {
try {
Expand Down