Correctly parse RedPhone JSON.

Fixes #4182
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-10-02 11:42:24 -07:00
parent c91315c0f1
commit c041495834
3 changed files with 9 additions and 5 deletions

View File

@ -1,7 +1,5 @@
package org.thoughtcrime.redphone.signaling;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
@ -9,6 +7,7 @@ import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.thoughtcrime.securesms.util.Base64;
import org.thoughtcrime.securesms.util.JsonUtils;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.push.TrustStore;
@ -52,7 +51,7 @@ public class RedPhoneAccountManager {
builder.header("Authorization", "Basic " + Base64.encodeBytes((login + ":" + password).getBytes()));
if (gcmId.isPresent()) {
String body = new ObjectMapper().writeValueAsString(new RedPhoneGcmId(gcmId.get()));
String body = JsonUtils.toJson(new RedPhoneGcmId(gcmId.get()));
builder.put(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body));
} else {
builder.delete();
@ -70,7 +69,7 @@ public class RedPhoneAccountManager {
}
public void createAccount(String verificationToken, RedPhoneAccountAttributes attributes) throws IOException {
String body = new ObjectMapper().writeValueAsString(attributes);
String body = JsonUtils.toJson(attributes);
Request request = new Request.Builder()
.url(baseUrl + "/api/v1/accounts/token/" + verificationToken)

View File

@ -30,6 +30,7 @@ import org.thoughtcrime.redphone.signaling.signals.RingingSignal;
import org.thoughtcrime.redphone.signaling.signals.ServerSignal;
import org.thoughtcrime.redphone.signaling.signals.Signal;
import org.thoughtcrime.redphone.util.LineReader;
import org.thoughtcrime.securesms.util.JsonUtils;
import org.whispersystems.textsecure.api.push.TrustStore;
import java.io.IOException;
@ -182,7 +183,7 @@ public class SignalingSocket {
case 404: throw new NoSuchUserException("No such redphone user.");
case 402: throw new ServerMessageException(new String(response.getBody()));
case 401: throw new LoginFailedException("Initiate threw 401");
case 200: return new ObjectMapper().readValue(response.getBody(), SessionDescriptor.class);
case 200: return JsonUtils.fromJson(response.getBody(), SessionDescriptor.class);
default: throw new SignalingException("Unknown response: " + response.getStatusCode());
}
} catch (IOException e) {

View File

@ -15,6 +15,10 @@ public class JsonUtils {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static <T> T fromJson(byte[] serialized, Class<T> clazz) throws IOException {
return fromJson(new String(serialized), clazz);
}
public static <T> T fromJson(String serialized, Class<T> clazz) throws IOException {
return objectMapper.readValue(serialized, clazz);
}