Skip to content

Commit 486d161

Browse files
committed
More cleanup
1 parent f20b0df commit 486d161

8 files changed

Lines changed: 26 additions & 151 deletions

File tree

conjure-java-jaxrs-client/src/main/java/com/palantir/conjure/java/client/jaxrs/DialogueFeignClient.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,6 @@ public Integer length() {
221221
.orElse(null);
222222
}
223223

224-
@Override
225-
public boolean isRepeatable() {
226-
return false;
227-
}
228-
229224
@Override
230225
public InputStream asInputStream() {
231226
return response.body();
@@ -279,7 +274,6 @@ enum FeignResponseDeserializer implements Deserializer<com.palantir.conjure.java
279274
public com.palantir.conjure.java.client.jaxrs.Response deserialize(Response response) {
280275
return com.palantir.conjure.java.client.jaxrs.Response.create(
281276
response.code(),
282-
null,
283277
Multimaps.asMap((Multimap<String, String>) response.headers()),
284278
new DialogueResponseBody(response));
285279
}

conjure-java-jaxrs-client/src/main/java/com/palantir/conjure/java/client/jaxrs/Response.java

Lines changed: 6 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.InputStream;
2222
import java.io.InputStreamReader;
2323
import java.io.Reader;
24-
import java.nio.charset.Charset;
2524
import java.nio.charset.StandardCharsets;
2625
import java.util.ArrayList;
2726
import java.util.Collection;
@@ -36,38 +35,22 @@
3635
final class Response implements Closeable {
3736

3837
private final int status;
39-
private final String reason;
4038
private final Map<String, Collection<String>> headers;
4139
private final Body body;
4240

43-
private Response(int status, String reason, Map<String, Collection<String>> headers, Body body) {
41+
private Response(int status, Map<String, Collection<String>> headers, Body body) {
4442
Util.checkState(status >= 200, "Invalid status code: %s", status);
4543
this.status = status;
46-
this.reason = reason; // nullable
4744
this.headers = Collections.unmodifiableMap(caseInsensitiveCopyOf(headers));
48-
this.body = body; // nullable
45+
this.body = body;
4946
}
5047

51-
static Response create(
52-
int status,
53-
String reason,
54-
Map<String, Collection<String>> headers,
55-
InputStream inputStream,
56-
Integer length) {
57-
return new Response(status, reason, headers, InputStreamBody.orNull(inputStream, length));
48+
static Response create(int status, Map<String, Collection<String>> headers, byte[] data) {
49+
return new Response(status, headers, new ByteArrayBody(data));
5850
}
5951

60-
static Response create(int status, String reason, Map<String, Collection<String>> headers, byte[] data) {
61-
return new Response(status, reason, headers, ByteArrayBody.orNull(data));
62-
}
63-
64-
static Response create(
65-
int status, String reason, Map<String, Collection<String>> headers, String text, Charset charset) {
66-
return new Response(status, reason, headers, ByteArrayBody.orNull(text, charset));
67-
}
68-
69-
static Response create(int status, String reason, Map<String, Collection<String>> headers, Body body) {
70-
return new Response(status, reason, headers, body);
52+
static Response create(int status, Map<String, Collection<String>> headers, Body body) {
53+
return new Response(status, headers, body);
7154
}
7255

7356
/**
@@ -79,15 +62,6 @@ int status() {
7962
return status;
8063
}
8164

82-
/**
83-
* Nullable and not set when using http/2
84-
*
85-
* See https://github.com/http2/http2-spec/issues/202
86-
*/
87-
String reason() {
88-
return reason;
89-
}
90-
9165
/**
9266
* Returns a case-insensitive mapping of header names to their values.
9367
*/
@@ -105,9 +79,6 @@ Body body() {
10579
@Override
10680
public String toString() {
10781
StringBuilder builder = new StringBuilder("HTTP/1.1 ").append(status);
108-
if (reason != null) {
109-
builder.append(' ').append(reason);
110-
}
11182
builder.append('\n');
11283
for (String field : headers.keySet()) {
11384
for (String value : Util.valuesOrEmpty(headers, field)) {
@@ -135,11 +106,6 @@ interface Body extends Closeable {
135106
*/
136107
Integer length();
137108

138-
/**
139-
* True if {@link #asInputStream()} and {@link #asReader()} can be called more than once.
140-
*/
141-
boolean isRepeatable();
142-
143109
/**
144110
* It is the responsibility of the caller to close the stream.
145111
*/
@@ -151,49 +117,6 @@ interface Body extends Closeable {
151117
Reader asReader() throws IOException;
152118
}
153119

154-
private static final class InputStreamBody implements Body {
155-
156-
private final InputStream inputStream;
157-
private final Integer length;
158-
159-
private InputStreamBody(InputStream inputStream, Integer length) {
160-
this.inputStream = inputStream;
161-
this.length = length;
162-
}
163-
164-
private static Body orNull(InputStream inputStream, Integer length) {
165-
if (inputStream == null) {
166-
return null;
167-
}
168-
return new InputStreamBody(inputStream, length);
169-
}
170-
171-
@Override
172-
public Integer length() {
173-
return length;
174-
}
175-
176-
@Override
177-
public boolean isRepeatable() {
178-
return false;
179-
}
180-
181-
@Override
182-
public InputStream asInputStream() throws IOException {
183-
return inputStream;
184-
}
185-
186-
@Override
187-
public Reader asReader() throws IOException {
188-
return new InputStreamReader(inputStream, StandardCharsets.UTF_8);
189-
}
190-
191-
@Override
192-
public void close() throws IOException {
193-
inputStream.close();
194-
}
195-
}
196-
197120
private static final class ByteArrayBody implements Body {
198121

199122
private final byte[] data;
@@ -202,31 +125,11 @@ private static final class ByteArrayBody implements Body {
202125
this.data = data;
203126
}
204127

205-
private static Body orNull(byte[] data) {
206-
if (data == null) {
207-
return null;
208-
}
209-
return new ByteArrayBody(data);
210-
}
211-
212-
private static Body orNull(String text, Charset charset) {
213-
if (text == null) {
214-
return null;
215-
}
216-
Util.checkNotNull(charset, "charset");
217-
return new ByteArrayBody(text.getBytes(charset));
218-
}
219-
220128
@Override
221129
public Integer length() {
222130
return data.length;
223131
}
224132

225-
@Override
226-
public boolean isRepeatable() {
227-
return true;
228-
}
229-
230133
@Override
231134
public InputStream asInputStream() throws IOException {
232135
return new ByteArrayInputStream(data);

conjure-java-jaxrs-client/src/main/java/com/palantir/conjure/java/client/jaxrs/SynchronousMethodHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Object executeAndDecode(RequestTemplate template) throws Exception {
6464
}
6565
// Ensure the response body is disconnected
6666
byte[] bodyData = Util.toByteArray(response.body().asInputStream());
67-
return Response.create(response.status(), response.reason(), response.headers(), bodyData);
67+
return Response.create(response.status(), response.headers(), bodyData);
6868
}
6969
if (response.status() >= 200 && response.status() < 300) {
7070
if (void.class == metadata.returnType()) {

conjure-java-jaxrs-client/src/test/java/com/palantir/conjure/java/client/jaxrs/EmptyContainerDecoderTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
public class EmptyContainerDecoderTest {
5050

5151
private static final JsonMapper mapper = ObjectMappers.newClientJsonMapper();
52-
private static final Response HTTP_204 = Response.create(204, "No Content", Collections.emptyMap(), new byte[] {});
52+
private static final Response HTTP_204 = Response.create(204, Collections.emptyMap(), new byte[] {});
5353
private final Decoder delegate = mock(Decoder.class);
5454
private final EmptyContainerDecoder emptyContainerDecoder = new EmptyContainerDecoder(mapper, delegate);
5555

@@ -58,10 +58,8 @@ public void http_200_uses_delegate_decoder() throws IOException {
5858
when(delegate.decode(any(), eq(String.class))).thenReturn("text response");
5959
Response http200 = Response.create(
6060
200,
61-
"OK",
6261
ImmutableMap.of(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN)),
63-
"text response",
64-
StandardCharsets.UTF_8);
62+
"text response".getBytes(StandardCharsets.UTF_8));
6563

6664
emptyContainerDecoder.decode(http200, String.class);
6765
verify(delegate, times(1)).decode(any(), any());

conjure-java-jaxrs-client/src/test/java/com/palantir/conjure/java/client/jaxrs/InputStreamDelegateDecoderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void before() {
5454
public void testDecodesAsInputStream() throws Exception {
5555
String data = "data";
5656

57-
Response response = Response.create(200, "OK", ImmutableMap.of(), data, StandardCharsets.UTF_8);
57+
Response response = Response.create(200, ImmutableMap.of(), data.getBytes(StandardCharsets.UTF_8));
5858

5959
InputStream decoded = (InputStream) inputStreamDelegateDecoder.decode(response, InputStream.class);
6060

@@ -66,15 +66,15 @@ public void testUsesDelegateWhenReturnTypeNotInputStream() throws Exception {
6666
String returned = "string";
6767

6868
when(delegate.decode(any(), any())).thenReturn(returned);
69-
Response response = Response.create(200, "OK", ImmutableMap.of(), returned, StandardCharsets.UTF_8);
69+
Response response = Response.create(200, ImmutableMap.of(), returned.getBytes(StandardCharsets.UTF_8));
7070
String decodedObject = (String) inputStreamDelegateDecoder.decode(response, String.class);
7171
assertThat(decodedObject).isEqualTo(returned);
7272
}
7373

7474
@Test
7575
public void testSupportsNullBody() throws Exception {
7676
String data = "";
77-
Response response = Response.create(200, "OK", ImmutableMap.of(), (Response.Body) null);
77+
Response response = Response.create(200, ImmutableMap.of(), (Response.Body) null);
7878

7979
InputStream decoded = (InputStream) inputStreamDelegateDecoder.decode(response, InputStream.class);
8080

conjure-java-jaxrs-client/src/test/java/com/palantir/conjure/java/client/jaxrs/NeverReturnNullDecoderTest.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,9 @@ public final class NeverReturnNullDecoderTest extends TestBase {
3535
private final Decoder textDelegateDecoder =
3636
new NeverReturnNullDecoder(new JacksonDecoder(ObjectMappers.newClientJsonMapper()));
3737

38-
@Test
39-
public void throws_nullpointerexception_when_body_is_null() {
40-
Response response = Response.create(200, "OK", headers, null, StandardCharsets.UTF_8);
41-
42-
Assertions.assertThatLoggableExceptionThrownBy(() -> textDelegateDecoder.decode(response, List.class))
43-
.isInstanceOf(NullPointerException.class)
44-
.hasLogMessage("Unexpected null body")
45-
.containsArgs(SafeArg.of("status", 200));
46-
}
47-
4838
@Test
4939
public void throws_nullpointerexception_when_body_is_string_null() {
50-
Response response = Response.create(200, "OK", headers, "null", StandardCharsets.UTF_8);
40+
Response response = Response.create(200, headers, "null".getBytes(StandardCharsets.UTF_8));
5141

5242
Assertions.assertThatLoggableExceptionThrownBy(() -> textDelegateDecoder.decode(response, List.class))
5343
.isInstanceOf(NullPointerException.class)
@@ -57,7 +47,7 @@ public void throws_nullpointerexception_when_body_is_string_null() {
5747

5848
@Test
5949
public void throws_nullpointerexception_when_body_is_empty_string() {
60-
Response response = Response.create(200, "OK", headers, "", StandardCharsets.UTF_8);
50+
Response response = Response.create(200, headers, "".getBytes(StandardCharsets.UTF_8));
6151

6252
Assertions.assertThatLoggableExceptionThrownBy(() -> textDelegateDecoder.decode(response, List.class))
6353
.isInstanceOf(NullPointerException.class)
@@ -67,7 +57,7 @@ public void throws_nullpointerexception_when_body_is_empty_string() {
6757

6858
@Test
6959
public void works_fine_when_body_is_not_null() throws Exception {
70-
Response response = Response.create(200, "OK", headers, "[1, 2, 3]", StandardCharsets.UTF_8);
60+
Response response = Response.create(200, headers, "[1, 2, 3]".getBytes(StandardCharsets.UTF_8));
7161
Object decodedObject = textDelegateDecoder.decode(response, List.class);
7262
assertThat(decodedObject).isEqualTo(ImmutableList.of(1, 2, 3));
7363
}

conjure-java-jaxrs-client/src/test/java/com/palantir/conjure/java/client/jaxrs/QosErrorDecoderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static QosErrorDecoder decoder() {
4646
public void http_429_throw_qos_throttle() {
4747
QosReason expected = QosReason.builder().reason("client-qos-response").build();
4848
Map<String, Collection<String>> headers = headersFor(expected);
49-
Response response = Response.create(429, "too many requests", headers, new byte[0]);
49+
Response response = Response.create(429, headers, new byte[0]);
5050
assertThat(decoder().decode(methodKey, response))
5151
.isInstanceOfSatisfying(QosException.Throttle.class, throttle -> {
5252
assertThat(throttle.getRetryAfter()).isEmpty();
@@ -62,7 +62,7 @@ public void http_429_throw_qos_throttle_with_metadata() {
6262
.retryHint(RetryHint.DO_NOT_RETRY)
6363
.build();
6464
Map<String, Collection<String>> headers = headersFor(expected);
65-
Response response = Response.create(429, "too many requests", headers, new byte[0]);
65+
Response response = Response.create(429, headers, new byte[0]);
6666
assertThat(decoder().decode(methodKey, response))
6767
.isInstanceOfSatisfying(QosException.Throttle.class, throttle -> {
6868
assertThat(throttle.getRetryAfter()).isEmpty();
@@ -73,7 +73,7 @@ public void http_429_throw_qos_throttle_with_metadata() {
7373
@Test
7474
public void http_429_throw_qos_throttle_with_retry_after() {
7575
Map<String, Collection<String>> headers = ImmutableMap.of(HttpHeaders.RETRY_AFTER, ImmutableList.of("5"));
76-
Response response = Response.create(429, "too many requests", headers, new byte[0]);
76+
Response response = Response.create(429, headers, new byte[0]);
7777
assertThat(decoder().decode(methodKey, response))
7878
.isInstanceOfSatisfying(
7979
QosException.Throttle.class,
@@ -84,7 +84,7 @@ public void http_429_throw_qos_throttle_with_retry_after() {
8484
public void http_503_throw_qos_unavailable() {
8585
QosReason expected = QosReason.builder().reason("client-qos-response").build();
8686
Map<String, Collection<String>> headers = headersFor(expected);
87-
Response response = Response.create(503, "unavailable", headers, new byte[0]);
87+
Response response = Response.create(503, headers, new byte[0]);
8888
assertThat(decoder().decode(methodKey, response))
8989
.isInstanceOfSatisfying(
9090
QosException.Unavailable.class,
@@ -99,7 +99,7 @@ public void http_503_throw_qos_unavailable_with_metadata() {
9999
.retryHint(RetryHint.DO_NOT_RETRY)
100100
.build();
101101
Map<String, Collection<String>> headers = headersFor(expected);
102-
Response response = Response.create(503, "unavailable", headers, new byte[0]);
102+
Response response = Response.create(503, headers, new byte[0]);
103103
assertThat(decoder().decode(methodKey, response))
104104
.isInstanceOfSatisfying(
105105
QosException.Unavailable.class,

conjure-java-jaxrs-client/src/test/java/com/palantir/conjure/java/client/jaxrs/TextDelegateDecoderTest.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void before() {
6262
@Test
6363
public void testUsesStringDecoderWithTextPlain() throws Exception {
6464
headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN));
65-
Response response = Response.create(200, "OK", headers, "text response", StandardCharsets.UTF_8);
65+
Response response = Response.create(200, headers, "text response".getBytes(StandardCharsets.UTF_8));
6666
Object decodedObject = textDelegateDecoder.decode(response, String.class);
6767

6868
assertThat(decodedObject).isEqualTo("text response");
@@ -81,7 +81,7 @@ public void testCannotReturnStringWithMediaTypeJson() {
8181
@Test
8282
public void testUsesStringDecoderWithTextPlainAndCharset() throws Exception {
8383
headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN + "; charset=utf-8"));
84-
Response response = Response.create(200, "OK", headers, "text response", StandardCharsets.UTF_8);
84+
Response response = Response.create(200, headers, "text response".getBytes(StandardCharsets.UTF_8));
8585

8686
Object decodedObject = textDelegateDecoder.decode(response, String.class);
8787

@@ -92,27 +92,17 @@ public void testUsesStringDecoderWithTextPlainAndCharset() throws Exception {
9292
@Test
9393
public void testUsesStringDecoderWithTextPlainWithWeirdHeaderCapitalization() throws Exception {
9494
headers.put("content-TYPE", ImmutableSet.of(MediaType.TEXT_PLAIN));
95-
Response response = Response.create(200, "OK", headers, "text response", StandardCharsets.UTF_8);
95+
Response response = Response.create(200, headers, "text response".getBytes(StandardCharsets.UTF_8));
9696
Object decodedObject = textDelegateDecoder.decode(response, String.class);
9797

9898
assertThat(decodedObject).isEqualTo("text response");
9999
verifyNoMoreInteractions(delegate);
100100
}
101101

102-
@Test
103-
public void testReturnsEmptyStringForNullResponseBodyWithTextPlain() throws Exception {
104-
headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN));
105-
Response response = Response.create(200, "OK", headers, null, StandardCharsets.UTF_8);
106-
Object decodedObject = textDelegateDecoder.decode(response, String.class);
107-
108-
assertThat(decodedObject).isEqualTo("");
109-
verifyNoMoreInteractions(delegate);
110-
}
111-
112102
@Test
113103
public void testUsesDelegateWithNoHeader() throws Exception {
114104
when(delegate.decode(any(), any())).thenReturn(DELEGATE_RESPONSE);
115-
Response response = Response.create(200, "OK", headers, new byte[0]);
105+
Response response = Response.create(200, headers, new byte[0]);
116106
Object decodedObject = textDelegateDecoder.decode(response, String.class);
117107

118108
assertThat(decodedObject).isEqualTo(DELEGATE_RESPONSE);
@@ -122,7 +112,7 @@ public void testUsesDelegateWithNoHeader() throws Exception {
122112
public void testUsesDelegateWithComplexHeader() throws Exception {
123113
headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON));
124114
when(delegate.decode(any(), any())).thenReturn(DELEGATE_RESPONSE);
125-
Response response = Response.create(200, "OK", headers, new byte[0]);
115+
Response response = Response.create(200, headers, new byte[0]);
126116
Object decodedObject = textDelegateDecoder.decode(response, String.class);
127117

128118
assertThat(decodedObject).isEqualTo(DELEGATE_RESPONSE);
@@ -132,7 +122,7 @@ public void testUsesDelegateWithComplexHeader() throws Exception {
132122
public void testUsesDelegateWithNonTextContentType() throws Exception {
133123
headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.APPLICATION_JSON));
134124
when(delegate.decode(any(), any())).thenReturn(DELEGATE_RESPONSE);
135-
Response response = Response.create(200, "OK", headers, new byte[0]);
125+
Response response = Response.create(200, headers, new byte[0]);
136126
Object decodedObject = textDelegateDecoder.decode(response, String.class);
137127

138128
assertThat(decodedObject).isEqualTo(DELEGATE_RESPONSE);

0 commit comments

Comments
 (0)