|
1 | 1 | package com.pusher.client.channel.impl; |
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonSyntaxException; |
4 | 5 | import com.google.gson.annotations.SerializedName; |
| 6 | +import com.pusher.client.AuthorizationFailureException; |
5 | 7 | import com.pusher.client.Authorizer; |
6 | 8 | import com.pusher.client.channel.ChannelEventListener; |
7 | 9 | import com.pusher.client.channel.PresenceChannel; |
@@ -66,7 +68,7 @@ else if (event.equals(MEMBER_REMOVED_EVENT)) { |
66 | 68 | @Override |
67 | 69 | public String toSubscribeMessage() { |
68 | 70 | String msg = super.toSubscribeMessage(); |
69 | | - storeMyUserId(channelData); |
| 71 | + myUserID = extractUserIdFromChannelData((String)channelData); |
70 | 72 | return msg; |
71 | 73 | } |
72 | 74 |
|
@@ -161,9 +163,24 @@ private static PresenceData extractPresenceDataFrom(final String message) { |
161 | 163 | } |
162 | 164 |
|
163 | 165 | @SuppressWarnings("rawtypes") |
164 | | - private void storeMyUserId(final Object channelData) { |
165 | | - final Map channelDataMap = GSON.fromJson((String)channelData, Map.class); |
166 | | - myUserID = String.valueOf(channelDataMap.get("user_id")); |
| 166 | + private String extractUserIdFromChannelData(final String channelData) { |
| 167 | + final Map channelDataMap; |
| 168 | + try { |
| 169 | + channelDataMap = GSON.fromJson((String)channelData, Map.class); |
| 170 | + } catch (final JsonSyntaxException e) { |
| 171 | + throw new AuthorizationFailureException("Invalid response from Authorizer: unable to parse channel_data object: " + channelData, e); |
| 172 | + } |
| 173 | + Object maybeUserId; |
| 174 | + try { |
| 175 | + maybeUserId = channelDataMap.get("user_id"); |
| 176 | + } catch (final NullPointerException e) { |
| 177 | + throw new AuthorizationFailureException("Invalid response from Authorizer: no user_id key in channel_data object: " + channelData); |
| 178 | + } |
| 179 | + if (maybeUserId == null) { |
| 180 | + throw new AuthorizationFailureException("Invalid response from Authorizer: no user_id key in channel_data object: " + channelData); |
| 181 | + } |
| 182 | + // user_id can be a string or an integer in the Channels websocket protocol |
| 183 | + return String.valueOf(maybeUserId); |
167 | 184 | } |
168 | 185 |
|
169 | 186 | private class MemberData { |
|
0 commit comments