[Java] Play! retrofit2 client content-type handling fixes (#6440)

* proper content-type handling (npe fixed when content-type is absent, improved content-length calculation)

* samples updated

* fixed missing imports, samples updated
This commit is contained in:
lukoyanov
2017-09-06 10:43:19 +03:00
committed by wing328
parent c7d145a4ba
commit f623aa1f5b
19 changed files with 47 additions and 16 deletions

View File

@@ -16,6 +16,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Creates {@link Call} instances that invoke underlying {@link WSClient}
@@ -163,20 +164,23 @@ public class Play24CallFactory implements okhttp3.Call.Factory {
@Override
public MediaType contentType() {
return MediaType.parse(r.getHeader("Content-Type"));
return Optional.ofNullable(r.getHeader("Content-Type"))
.map(MediaType::parse)
.orElse(null);
}
@Override
public long contentLength() {
return r.getBody().getBytes().length;
return r.asByteArray().length;
}
@Override
public BufferedSource source() {
return new Buffer().write(r.getBody().getBytes());
return new Buffer().write(r.asByteArray());
}
});
for (Map.Entry<String, List<String>> entry : r.getAllHeaders().entrySet()) {
for (String value : entry.getValue()) {
builder.addHeader(entry.getKey(), value);

View File

@@ -15,6 +15,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletionStage;
/**
@@ -160,20 +161,23 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
@Override
public MediaType contentType() {
return MediaType.parse(r.getHeader("Content-Type"));
return Optional.ofNullable(r.getHeader("Content-Type"))
.map(MediaType::parse)
.orElse(null);
}
@Override
public long contentLength() {
return r.getBody().getBytes().length;
return r.asByteArray().length;
}
@Override
public BufferedSource source() {
return new Buffer().write(r.getBody().getBytes());
return new Buffer().write(r.asByteArray());
}
});
for (Map.Entry<String, List<String>> entry : r.getAllHeaders().entrySet()) {
for (String value : entry.getValue()) {
builder.addHeader(entry.getKey(), value);