Skip to content

Commit b9fe6f9

Browse files
committed
Remove Client, Request, Response, TypedInput, and TypedOutput abstractions.
1 parent 909c8f8 commit b9fe6f9

File tree

55 files changed

+1651
-3305
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1651
-3305
lines changed

retrofit-converters/jackson/src/main/java/retrofit/converter/JacksonConverter.java

+12-17
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.JavaType;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.squareup.okhttp.MediaType;
7+
import com.squareup.okhttp.RequestBody;
8+
import com.squareup.okhttp.ResponseBody;
69
import java.io.IOException;
710
import java.io.InputStream;
8-
import java.io.UnsupportedEncodingException;
911
import java.lang.reflect.Type;
10-
import retrofit.mime.TypedByteArray;
11-
import retrofit.mime.TypedInput;
12-
import retrofit.mime.TypedOutput;
1312

1413
/**
1514
* A {@link Converter} which uses Jackson for reading and writing entities.
1615
*
1716
* @author Kai Waldron ([email protected])
1817
*/
1918
public class JacksonConverter implements Converter {
20-
private static final String MIME_TYPE = "application/json; charset=UTF-8";
19+
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
2120

2221
private final ObjectMapper objectMapper;
2322

@@ -26,34 +25,30 @@ public JacksonConverter() {
2625
}
2726

2827
public JacksonConverter(ObjectMapper objectMapper) {
28+
if (objectMapper == null) throw new NullPointerException("objectMapper == null");
2929
this.objectMapper = objectMapper;
3030
}
3131

32-
@Override public Object fromBody(TypedInput body, Type type) throws IOException {
33-
InputStream in = null;
32+
@Override public Object fromBody(ResponseBody body, Type type) throws IOException {
33+
InputStream is = body.byteStream();
3434
try {
3535
JavaType javaType = objectMapper.getTypeFactory().constructType(type);
36-
in = body.in();
37-
return objectMapper.readValue(in, javaType);
36+
return objectMapper.readValue(is, javaType);
3837
} finally {
3938
try {
40-
if (in != null) {
41-
in.close();
42-
}
39+
is.close();
4340
} catch (IOException ignored) {
4441
}
4542
}
4643
}
4744

48-
@Override public TypedOutput toBody(Object object, Type type) {
45+
@Override public RequestBody toBody(Object object, Type type) {
4946
try {
5047
JavaType javaType = objectMapper.getTypeFactory().constructType(type);
5148
String json = objectMapper.writerWithType(javaType).writeValueAsString(object);
52-
return new TypedByteArray(MIME_TYPE, json.getBytes("UTF-8"));
49+
return RequestBody.create(MEDIA_TYPE, json);
5350
} catch (JsonProcessingException e) {
54-
throw new AssertionError(e);
55-
} catch (UnsupportedEncodingException e) {
56-
throw new AssertionError(e);
51+
throw new RuntimeException(e);
5752
}
5853
}
5954
}

retrofit-converters/jackson/src/test/java/retrofit/converter/JacksonConverterTest.java

+20-18
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,55 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.fasterxml.jackson.databind.JsonMappingException;
55
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
6-
import java.io.ByteArrayOutputStream;
6+
import com.squareup.okhttp.MediaType;
7+
import com.squareup.okhttp.RequestBody;
8+
import com.squareup.okhttp.ResponseBody;
9+
import java.io.IOException;
10+
import okio.Buffer;
11+
import org.assertj.core.api.AbstractCharSequenceAssert;
712
import org.junit.Test;
8-
import retrofit.mime.TypedByteArray;
9-
import retrofit.mime.TypedInput;
10-
import retrofit.mime.TypedOutput;
1113

1214
import static org.assertj.core.api.Assertions.assertThat;
1315

1416
public class JacksonConverterTest {
15-
private static final String MIME_TYPE = "application/json; charset=UTF-8";
17+
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
1618
private static final MyObject OBJECT = new MyObject("hello world", 10);
1719
private final String JSON = "{\"message\":\"hello world\",\"count\":10}";
1820

1921
private final JacksonConverter converter = new JacksonConverter();
2022

2123
@Test public void serialize() throws Exception {
22-
TypedOutput typedOutput = converter.toBody(OBJECT, MyObject.class);
23-
assertThat(typedOutput.mimeType()).isEqualTo(MIME_TYPE);
24-
assertThat(asString(typedOutput)).isEqualTo(JSON);
24+
RequestBody body = converter.toBody(OBJECT, MyObject.class);
25+
assertThat(body.contentType()).isEqualTo(MEDIA_TYPE);
26+
assertBody(body).isEqualTo(JSON);
2527
}
2628

2729
@Test public void deserialize() throws Exception {
28-
TypedInput input = new TypedByteArray(MIME_TYPE, JSON.getBytes());
29-
MyObject result = (MyObject) converter.fromBody(input, MyObject.class);
30+
ResponseBody body = ResponseBody.create(MEDIA_TYPE, JSON);
31+
MyObject result = (MyObject) converter.fromBody(body, MyObject.class);
3032
assertThat(result).isEqualTo(OBJECT);
3133
}
3234

3335
@Test public void deserializeWrongValue() throws Exception {
34-
TypedInput input = new TypedByteArray(MIME_TYPE, "{\"foo\":\"bar\"}".getBytes());
36+
ResponseBody body = ResponseBody.create(MEDIA_TYPE, "{\"foo\":\"bar\"}");
3537
try {
36-
converter.fromBody(input, MyObject.class);
38+
converter.fromBody(body, MyObject.class);
3739
} catch (UnrecognizedPropertyException ignored) {
3840
}
3941
}
4042

4143
@Test public void deserializeWrongClass() throws Exception {
42-
TypedInput input = new TypedByteArray(MIME_TYPE, JSON.getBytes());
44+
ResponseBody body = ResponseBody.create(MEDIA_TYPE, JSON);
4345
try {
44-
converter.fromBody(input, String.class);
46+
converter.fromBody(body, String.class);
4547
} catch (JsonMappingException ignored) {
4648
}
4749
}
4850

49-
private String asString(TypedOutput typedOutput) throws Exception {
50-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
51-
typedOutput.writeTo(bytes);
52-
return new String(bytes.toByteArray());
51+
private static AbstractCharSequenceAssert<?, String> assertBody(RequestBody body) throws IOException {
52+
Buffer buffer = new Buffer();
53+
body.writeTo(buffer);
54+
return assertThat(buffer.readUtf8());
5355
}
5456

5557
static class MyObject {

retrofit-converters/protobuf/src/main/java/retrofit/converter/ProtoConverter.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
package retrofit.converter;
33

44
import com.google.protobuf.AbstractMessageLite;
5+
import com.squareup.okhttp.MediaType;
6+
import com.squareup.okhttp.RequestBody;
7+
import com.squareup.okhttp.ResponseBody;
58
import java.io.IOException;
69
import java.io.InputStream;
710
import java.lang.reflect.InvocationTargetException;
811
import java.lang.reflect.Method;
912
import java.lang.reflect.Type;
10-
import retrofit.mime.TypedByteArray;
11-
import retrofit.mime.TypedInput;
12-
import retrofit.mime.TypedOutput;
1313

1414
/** A {@link Converter} that reads and writes protocol buffers. */
1515
public class ProtoConverter implements Converter {
16-
private static final String MIME_TYPE = "application/x-protobuf";
16+
private static final MediaType MEDIA_TYPE = MediaType.parse("application/x-protobuf");
1717

18-
@Override public Object fromBody(TypedInput body, Type type) throws IOException {
18+
@Override public Object fromBody(ResponseBody body, Type type) throws IOException {
1919
if (!(type instanceof Class<?>)) {
2020
throw new IllegalArgumentException("Expected a raw Class<?> but was " + type);
2121
}
@@ -24,30 +24,31 @@ public class ProtoConverter implements Converter {
2424
throw new IllegalArgumentException("Expected a protobuf message but was " + c.getName());
2525
}
2626

27-
String mimeType = body.mimeType();
28-
if (!MIME_TYPE.equals(mimeType)) {
29-
throw new RuntimeException("Response content type was not a proto: " + mimeType);
30-
}
31-
27+
InputStream is = body.byteStream();
3228
try {
3329
Method parseFrom = c.getMethod("parseFrom", InputStream.class);
34-
return parseFrom.invoke(null, body.in());
30+
return parseFrom.invoke(null, is);
3531
} catch (InvocationTargetException e) {
3632
throw new RuntimeException(c.getName() + ".parseFrom() failed", e.getCause());
3733
} catch (NoSuchMethodException e) {
3834
throw new IllegalArgumentException("Expected a protobuf message but was " + c.getName());
3935
} catch (IllegalAccessException e) {
4036
throw new AssertionError();
37+
} finally {
38+
try {
39+
is.close();
40+
} catch (IOException ignored) {
41+
}
4142
}
4243
}
4344

44-
@Override public TypedOutput toBody(Object object, Type type) {
45+
@Override public RequestBody toBody(Object object, Type type) {
4546
if (!(object instanceof AbstractMessageLite)) {
4647
throw new IllegalArgumentException(
4748
"Expected a protobuf message but was " + (object != null ? object.getClass().getName()
4849
: "null"));
4950
}
5051
byte[] bytes = ((AbstractMessageLite) object).toByteArray();
51-
return new TypedByteArray(MIME_TYPE, bytes);
52+
return RequestBody.create(MEDIA_TYPE, bytes);
5253
}
5354
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright 2013 Square, Inc.
22
package retrofit.converter;
33

4-
import com.google.common.io.BaseEncoding;
54
import com.google.protobuf.InvalidProtocolBufferException;
6-
import java.io.ByteArrayOutputStream;
5+
import com.squareup.okhttp.MediaType;
6+
import com.squareup.okhttp.RequestBody;
7+
import com.squareup.okhttp.ResponseBody;
78
import java.io.IOException;
8-
import java.io.UnsupportedEncodingException;
99
import java.util.ArrayList;
10+
import okio.Buffer;
11+
import okio.ByteString;
12+
import org.assertj.core.api.AbstractCharSequenceAssert;
1013
import org.junit.Test;
11-
import retrofit.mime.TypedByteArray;
12-
import retrofit.mime.TypedOutput;
1314

1415
import static org.assertj.core.api.Assertions.assertThat;
1516
import static org.junit.Assert.fail;
@@ -19,22 +20,22 @@ public final class ProtoConverterTest {
1920
private static final Phone PROTO = Phone.newBuilder().setNumber("(519) 867-5309").build();
2021
private static final String ENCODED_PROTO = "Cg4oNTE5KSA4NjctNTMwOQ==";
2122

22-
private final ProtoConverter protoConverter = new ProtoConverter();
23+
private final ProtoConverter converter = new ProtoConverter();
2324

2425
@Test public void serialize() throws Exception {
25-
TypedOutput protoBytes = protoConverter.toBody(PROTO, Phone.class);
26-
assertThat(protoBytes.mimeType()).isEqualTo("application/x-protobuf");
27-
assertThat(bytesOf(protoBytes)).isEqualTo(bytesOf(decodeBase64(ENCODED_PROTO)));
26+
RequestBody body = converter.toBody(PROTO, Phone.class);
27+
assertThat(body.contentType().toString()).isEqualTo("application/x-protobuf");
28+
assertBody(body).isEqualTo(ENCODED_PROTO);
2829
}
2930

3031
@Test public void deserialize() throws Exception {
31-
Object proto = protoConverter.fromBody(decodeBase64(ENCODED_PROTO), Phone.class);
32+
Object proto = converter.fromBody(protoResponse(ENCODED_PROTO), Phone.class);
3233
assertThat(proto).isEqualTo(PROTO);
3334
}
3435

3536
@Test public void deserializeWrongClass() throws Exception {
3637
try {
37-
protoConverter.fromBody(decodeBase64(ENCODED_PROTO), String.class);
38+
converter.fromBody(protoResponse(ENCODED_PROTO), String.class);
3839
fail();
3940
} catch (IllegalArgumentException e) {
4041
assertThat(e).hasMessage("Expected a protobuf message but was java.lang.String");
@@ -43,7 +44,7 @@ public final class ProtoConverterTest {
4344

4445
@Test public void deserializeWrongType() throws Exception {
4546
try {
46-
protoConverter.fromBody(decodeBase64(ENCODED_PROTO), ArrayList.class.getGenericSuperclass());
47+
converter.fromBody(protoResponse(ENCODED_PROTO), ArrayList.class.getGenericSuperclass());
4748
fail();
4849
} catch (IllegalArgumentException e) {
4950
assertThat(e).hasMessage("Expected a raw Class<?> but was java.util.AbstractList<E>");
@@ -52,33 +53,21 @@ public final class ProtoConverterTest {
5253

5354
@Test public void deserializeWrongValue() throws Exception {
5455
try {
55-
protoConverter.fromBody(decodeBase64("////"), Phone.class);
56+
converter.fromBody(protoResponse("////"), Phone.class);
5657
fail();
5758
} catch (RuntimeException expected) {
5859
assertThat(expected.getCause() instanceof InvalidProtocolBufferException);
5960
}
6061
}
6162

62-
@Test public void deserializeWrongMime() throws Exception {
63-
try {
64-
protoConverter.fromBody(decodeBase64("////", "yummy/bytes"), Phone.class);
65-
fail();
66-
} catch (RuntimeException e) {
67-
assertThat(e).hasMessage("Response content type was not a proto: yummy/bytes");
68-
}
69-
}
70-
71-
private static byte[] bytesOf(TypedOutput protoBytes) throws IOException {
72-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
73-
protoBytes.writeTo(bytes);
74-
return bytes.toByteArray();
75-
}
76-
77-
private static TypedByteArray decodeBase64(String base64) throws UnsupportedEncodingException {
78-
return decodeBase64(base64, "application/x-protobuf");
63+
private static ResponseBody protoResponse(String encodedProto) {
64+
return ResponseBody.create(MediaType.parse("application/x-protobuf"), ByteString.decodeBase64(
65+
encodedProto).toByteArray());
7966
}
8067

81-
private static TypedByteArray decodeBase64(String base64, String mime) throws UnsupportedEncodingException {
82-
return new TypedByteArray(mime, BaseEncoding.base64().decode(base64));
68+
private static AbstractCharSequenceAssert<?, String> assertBody(RequestBody body) throws IOException {
69+
Buffer buffer = new Buffer();
70+
body.writeTo(buffer);
71+
return assertThat(buffer.readByteString().base64());
8372
}
8473
}

retrofit-converters/simplexml/src/main/java/retrofit/converter/SimpleXMLConverter.java

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package retrofit.converter;
22

3-
import java.io.ByteArrayOutputStream;
3+
import com.squareup.okhttp.MediaType;
4+
import com.squareup.okhttp.RequestBody;
5+
import com.squareup.okhttp.ResponseBody;
46
import java.io.IOException;
7+
import java.io.InputStream;
58
import java.io.OutputStreamWriter;
69
import java.lang.reflect.Type;
7-
8-
import org.simpleframework.xml.core.Persister;
10+
import okio.Buffer;
911
import org.simpleframework.xml.Serializer;
10-
11-
import retrofit.mime.TypedByteArray;
12-
import retrofit.mime.TypedInput;
13-
import retrofit.mime.TypedOutput;
12+
import org.simpleframework.xml.core.Persister;
1413

1514
/**
1615
* A {@link Converter} which uses SimpleXML for reading and writing entities.
@@ -20,7 +19,8 @@
2019
public class SimpleXMLConverter implements Converter {
2120
private static final boolean DEFAULT_STRICT = true;
2221
private static final String CHARSET = "UTF-8";
23-
private static final String MIME_TYPE = "application/xml; charset=" + CHARSET;
22+
private static final MediaType MEDIA_TYPE =
23+
MediaType.parse("application/xml; charset=" + CHARSET);
2424

2525
private final Serializer serializer;
2626

@@ -43,35 +43,34 @@ public SimpleXMLConverter(Serializer serializer, boolean strict) {
4343
this.strict = strict;
4444
}
4545

46-
@Override public Object fromBody(TypedInput body, Type type) throws IOException {
46+
@Override public Object fromBody(ResponseBody body, Type type) throws IOException {
47+
InputStream is = body.byteStream();
4748
try {
48-
return serializer.read((Class<?>) type, body.in(), strict);
49+
return serializer.read((Class<?>) type, is, strict);
4950
} catch (IOException e) {
5051
throw e;
5152
} catch (Exception e) {
5253
throw new RuntimeException(e);
54+
} finally {
55+
try {
56+
is.close();
57+
} catch (IOException ignored) {
58+
}
5359
}
5460
}
5561

56-
@Override public TypedOutput toBody(Object source, Type type) {
57-
OutputStreamWriter osw = null;
58-
62+
@Override public RequestBody toBody(Object source, Type type) {
63+
byte[] bytes;
5964
try {
60-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
61-
osw = new OutputStreamWriter(bos, CHARSET);
65+
Buffer buffer = new Buffer();
66+
OutputStreamWriter osw = new OutputStreamWriter(buffer.outputStream(), CHARSET);
6267
serializer.write(source, osw);
6368
osw.flush();
64-
return new TypedByteArray(MIME_TYPE, bos.toByteArray());
69+
bytes = buffer.readByteArray();
6570
} catch (Exception e) {
6671
throw new AssertionError(e);
67-
} finally {
68-
try {
69-
if (osw != null) {
70-
osw.close();
71-
}
72-
} catch (IOException ignored) {
73-
}
7472
}
73+
return RequestBody.create(MEDIA_TYPE, bytes);
7574
}
7675

7776
public boolean isStrict() {

0 commit comments

Comments
 (0)