Skip to content

Commit 2d7ca90

Browse files
committed
add TokenInjectionException
1 parent 6f5fd48 commit 2d7ca90

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

src/main/java/io/getstream/chat/java/services/framework/UserTokenCallRewriter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.getstream.chat.java.services.framework;
22

3+
import io.getstream.chat.java.services.framework.internal.TokenInjectionException;
34
import okhttp3.Call;
45
import okhttp3.Request;
56
import org.jetbrains.annotations.NotNull;
@@ -36,10 +37,13 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
3637
return injectTokenIntoCall((retrofit2.Call<?>) result);
3738
}
3839

39-
return result;
40+
// All service methods must return Call<?> for token injection
41+
throw new TokenInjectionException(
42+
"Method " + method.getName() + " on " + delegate.getClass().getName() +
43+
" did not return retrofit2.Call<?>. User token injection requires all service methods to return Call<?>.");
4044
}
4145

42-
private retrofit2.Call<?> injectTokenIntoCall(retrofit2.Call<?> originalCall) {
46+
private retrofit2.Call<?> injectTokenIntoCall(retrofit2.Call<?> originalCall) throws TokenInjectionException {
4347
retrofit2.Call<?> clonedCall = originalCall.clone();
4448

4549
try {
@@ -67,11 +71,11 @@ private retrofit2.Call<?> injectTokenIntoCall(retrofit2.Call<?> originalCall) {
6771
return clonedCall;
6872
} catch (NoSuchFieldException e) {
6973
// If Retrofit's internal structure changes, provide clear error message
70-
throw new RuntimeException(
74+
throw new TokenInjectionException(
7175
"Retrofit internal structure changed. Field 'rawCall' not found in " +
7276
clonedCall.getClass().getName() + ". Update client implementation.", e);
7377
} catch (IllegalAccessException e) {
74-
throw new RuntimeException("Failed to inject token into call", e);
78+
throw new TokenInjectionException("Failed to inject token into call", e);
7579
}
7680
}
7781
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.getstream.chat.java.services.framework.internal;
2+
3+
/**
4+
* Thrown when user token injection into a request fails.
5+
* This can happen if:
6+
* - A service method doesn't return retrofit2.Call<?>
7+
* - Retrofit's internal structure changes and reflection fails
8+
*/
9+
public class TokenInjectionException extends Exception {
10+
public TokenInjectionException(String message) {
11+
super(message);
12+
}
13+
14+
public TokenInjectionException(String message, Throwable cause) {
15+
super(message, cause);
16+
}
17+
}
18+

src/test/java/io/getstream/chat/java/CustomTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package io.getstream.chat.java;
22

3+
import io.getstream.chat.java.exceptions.StreamException;
4+
import io.getstream.chat.java.models.Thread;
35
import io.getstream.chat.java.models.User;
46
import io.getstream.chat.java.services.UserService;
57
import io.getstream.chat.java.services.framework.DefaultClient;
68
import org.junit.jupiter.api.Test;
79
import java.lang.management.ManagementFactory;
10+
import java.util.ArrayList;
11+
import java.util.List;
812
import java.util.concurrent.TimeUnit;
13+
import java.util.function.Consumer;
914

1015
public class CustomTest {
1116

@@ -20,10 +25,17 @@ void customTest() throws Exception {
2025

2126
@Test
2227
void userReqTest() throws Exception {
23-
var userId = "admin";
24-
var userToken = User.createToken(userId, null, null);
25-
var response = User.list().filterCondition("id", userId).withUserToken(userToken).request();
26-
System.out.println("\n!.!.! " + response + "\n");
28+
var userIds = List.of( "admin", "MWRYXIHURH", "SRLOTCPYQS", "IOEXOFYTRH", "RIOPOIGMDQ", "XUXJMHTNOI", "QQASWMJEQI");
29+
30+
for (var userId : userIds) {
31+
var userToken = User.createToken(userId, null, null);
32+
User.list().filterCondition("id", userId).withUserToken(userToken).requestAsync(
33+
userListResponse -> System.out.println("\n!.!.! " + userListResponse + "\n"),
34+
e -> {}
35+
);
36+
}
37+
38+
java.lang.Thread.sleep(10000);
2739
}
2840

2941
@Test

0 commit comments

Comments
 (0)