Skip to content

Commit

Permalink
Add converters module and protobuf converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Jul 15, 2013
1 parent b54de2e commit ebf8c26
Show file tree
Hide file tree
Showing 9 changed files with 760 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

<modules>
<module>retrofit</module>
<module>retrofit-converters</module>
<module>retrofit-samples</module>
</modules>

Expand All @@ -48,8 +49,10 @@
<android.version>4.1.1.4</android.version>
<android.platform>16</android.platform>
<gson.version>2.2.4</gson.version>
<javax.inject.version>1</javax.inject.version>
<okhttp.version>1.0.2</okhttp.version>
<okhttp.version>1.1.1</okhttp.version>

<!-- Converter Dependencies -->
<protobuf.version>2.5.0</protobuf.version>

<!-- Test Dependencies -->
<junit.version>4.10</junit.version>
Expand Down Expand Up @@ -99,6 +102,12 @@
<version>${gson.version}</version>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
14 changes: 14 additions & 0 deletions retrofit-converters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Retrofit Converters
===================

Retrofit ships with a default converter for JSON that uses Gson but the library is content-format
agnostic. The child modules contained herein are additional converters for other popular formats.

To use, supply an instance of your desired converter when building your `RestAdapter` instance.

```java
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer("https://api.fake.google.com")
.setConverter(new ProtoConverter())
.build();
```
20 changes: 20 additions & 0 deletions retrofit-converters/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.squareup.retrofit</groupId>
<artifactId>parent</artifactId>
<version>1.1.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>retrofit-converters</artifactId>
<name>Converters</name>
<packaging>pom</packaging>

<modules>
<module>protobuf</module>
</modules>
</project>
43 changes: 43 additions & 0 deletions retrofit-converters/protobuf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit-converters</artifactId>
<version>1.1.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>converter-protobuf</artifactId>
<name>Converter: Protocol Buffers</name>

<dependencies>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2013 Square, Inc.
package retrofit.converter;

import com.google.protobuf.AbstractMessageLite;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;

/** A {@link Converter} that reads and writes protocol buffers. */
public class ProtoConverter implements Converter {
private static final String MIME_TYPE = "application/x-protobuf";

@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
if (!(type instanceof Class<?>)) {
throw new IllegalArgumentException("Expected a raw Class<?> but was " + type);
}
Class<?> c = (Class<?>) type;
if (!AbstractMessageLite.class.isAssignableFrom(c)) {
throw new IllegalArgumentException("Expected a protobuf message but was " + c.getName());
}

String mimeType = body.mimeType();
if (!MIME_TYPE.equals(mimeType)) {
throw new ConversionException("Response content type was not a proto: " + mimeType);
}

try {
Method parseFrom = c.getMethod("parseFrom", InputStream.class);
return parseFrom.invoke(null, body.in());
} catch (InvocationTargetException e) {
throw new ConversionException(c.getName() + ".parseFrom() failed", e.getCause());
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Expected a protobuf message but was " + c.getName());
} catch (IllegalAccessException e) {
throw new AssertionError();
} catch (IOException e) {
throw new ConversionException(e);
}
}

@Override public TypedOutput toBody(Object object) {
if (!(object instanceof AbstractMessageLite)) {
throw new IllegalArgumentException(
"Expected a protobuf message but was " + (object != null ? object.getClass().getName()
: "null"));
}
byte[] bytes = ((AbstractMessageLite) object).toByteArray();
return new TypedByteArray(MIME_TYPE, bytes);
}
}
4 changes: 4 additions & 0 deletions retrofit-converters/protobuf/src/test/gen-protos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

set -ex
protoc --java_out=java/ protos/phone.proto
Loading

0 comments on commit ebf8c26

Please sign in to comment.