Skip to content

Commit 787c890

Browse files
committed
Adding Deflate support
1 parent c8e08da commit 787c890

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

core/src/main/java/feign/Client.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
import java.util.LinkedHashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.zip.DeflaterInputStream;
3738
import java.util.zip.DeflaterOutputStream;
3839
import java.util.zip.GZIPInputStream;
3940
import java.util.zip.GZIPOutputStream;
41+
import java.util.zip.InflaterInputStream;
4042
import javax.net.ssl.HostnameVerifier;
4143
import javax.net.ssl.HttpsURLConnection;
4244
import javax.net.ssl.SSLSocketFactory;
@@ -132,6 +134,8 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
132134
} else {
133135
if (this.isGzip(connection)) {
134136
stream = new GZIPInputStream(connection.getInputStream());
137+
} else if (this.isDeflate(connection)) {
138+
stream = new InflaterInputStream(connection.getInputStream());
135139
} else {
136140
stream = connection.getInputStream();
137141
}
@@ -223,9 +227,17 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
223227
}
224228

225229
private boolean isGzip(HttpURLConnection connection) {
226-
String contentEncoding = connection.getHeaderField("Content-Encoding");
230+
String contentEncoding = connection.getHeaderField(CONTENT_ENCODING);
227231
if (Util.isNotBlank(contentEncoding)) {
228-
return contentEncoding.equalsIgnoreCase("gzip");
232+
return contentEncoding.equalsIgnoreCase(ENCODING_GZIP);
233+
}
234+
return false;
235+
}
236+
237+
private boolean isDeflate(HttpURLConnection connection) {
238+
String contentEncoding = connection.getHeaderField(CONTENT_ENCODING);
239+
if (Util.isNotBlank(contentEncoding)) {
240+
return contentEncoding.equalsIgnoreCase(ENCODING_DEFLATE);
229241
}
230242
return false;
231243
}

core/src/main/java/feign/Response.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,6 @@ public void close() throws IOException {
284284
inputStream.close();
285285
}
286286

287-
@Override
288-
public String toString() {
289-
try {
290-
return new String(toByteArray(inputStream), UTF_8);
291-
} catch (Exception e) {
292-
return super.toString();
293-
}
294-
}
295287
}
296288

297289
private static final class ByteArrayBody implements Response.Body {
@@ -347,10 +339,6 @@ public Reader asReader(Charset charset) throws IOException {
347339
@Override
348340
public void close() throws IOException {}
349341

350-
@Override
351-
public String toString() {
352-
return decodeOrDefault(data, UTF_8, "Binary data");
353-
}
354342
}
355343

356344
private static Map<String, Collection<String>> caseInsensitiveCopyOf(Map<String, Collection<String>> headers) {

core/src/test/java/feign/client/DefaultClientTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.SocketAddress;
2828
import java.net.URL;
2929
import java.nio.charset.StandardCharsets;
30+
import java.util.zip.DeflaterOutputStream;
3031
import java.util.zip.GZIPOutputStream;
3132
import javax.net.ssl.HostnameVerifier;
3233
import javax.net.ssl.SSLSession;
@@ -169,13 +170,40 @@ public void canSupportGzip() throws Exception {
169170

170171
}
171172

173+
@Test
174+
public void canSupportDeflate() throws Exception {
175+
/* enqueue a zipped response */
176+
final String responseData = "Compressed Data";
177+
server.enqueue(new MockResponse()
178+
.addHeader("Content-Encoding", "deflate")
179+
.setBody(new Buffer().write(deflate(responseData))));
180+
181+
TestInterface api = newBuilder()
182+
.target(TestInterface.class, "http://localhost:" + server.getPort());
183+
184+
String result = api.get();
185+
186+
/* verify that the response is unzipped */
187+
assertThat(result).isNotNull()
188+
.isEqualToIgnoringCase(responseData);
189+
190+
}
191+
172192
private byte[] compress(String data) throws Exception {
173193
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) {
174194
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bos);
175195
gzipOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length());
176196
gzipOutputStream.close();
177197
return bos.toByteArray();
178198
}
199+
}
179200

201+
private byte[] deflate(String data) throws Exception {
202+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length())) {
203+
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(bos);
204+
deflaterOutputStream.write(data.getBytes(StandardCharsets.UTF_8), 0, data.length());
205+
deflaterOutputStream.close();
206+
return bos.toByteArray();
207+
}
180208
}
181209
}

0 commit comments

Comments
 (0)