How to add okhttp custom interceptor at runtime using bytebuddy?

116 views
Skip to first unread message

Gourav Kumar

unread,
Sep 7, 2022, 1:09:41 AM9/7/22
to Byte Buddy
I want to add my interceptor at runtime in okhttpclient, I am making Agent using bytebuddy in premain class. And i am trying to intercept the constructor of *"okhttp3.OkHttpClient.Builder"* to add my interceptor in the interceptors list. But the problem is that the type() method is unable to find my class, I've tried different ways for eg: ElementMatchers .is(), .nameStartsWith(), .nameContains(), named() etc. but couldn't find the class.

I know that okhttp uses Kotlin & java adds *$* in the namespace to invoke some methods, so for my case i changed it to *"okhttp3.OkHttpClient$Builder"*, but again it was not finding the matching class.

I also want to know my approach that am i doing the right way to add the custom interceptor.

Agent:

```
    public static void premain(String arg, Instrumentation instrumentation) {
   
        new AgentBuilder.Default()          
              .type(ElementMatchers.named("okhttp3.OkHttpClient.Builder"))
              .transform((builder, typeDescription, classLoader, javaModule, protectionDomain) -> {
                        System.out.println("Transformer: adding custom okhttpinterceptor using agent: " + typeDescription.getName());
                        return builder.constructor(ElementMatchers.takesArgument(1, OkHttpClient.class))
                                .intercept(MethodDelegation.to(ConstructorInterceptor.class));
                    })
              .installOn(instrumentation);
        }
```
ConstructorInterceptor:

```
     public class ConstructorInterceptor {
   
             public static void doProceed(
                @SuperCall
                Callable<?> client,
                @Argument(0)
                OkHttpClient okHttpClient, @Origin String method) throws Exception {
   
            System.out.println("Constructor Interceptor was called for: " + method);
            client.call();
            okHttpClient.interceptors().add(new HttpInterceptor());
        }
    }
```

HttpInterceptor:

```
    public class HttpInterceptor implements Interceptor{
   
          @NotNull
          @Override
          public Response intercept(@NotNull Interceptor.Chain chain) throws IOException {
            Request request = chain.request();
   
            String reqBody = getRequestBody(request);
   
            System.out.println("Inside HttpInterceptor before next()");
   
            Response response = chain.proceed(request);
   
            System.out.println("Inside HttpInterceptor after next()");
   
            return response;
        }
     }
```

Rafael Winterhalter

unread,
Sep 7, 2022, 3:04:20 AM9/7/22
to Gourav Kumar, Byte Buddy
Hello,
you already reached out via GitHub and StackOverflow where I answered your question. Inner classes are separated by a dollar sign, you chose the wrong binary name.
Best regards, Rafael

Gourav Kumar <gourav...@gmail.com> schrieb am Mi., 7. Sept. 2022, 06:54:

I want to add my interceptor at runtime in okhttpclient, I am making Agent using bytebuddy in premain class. And i am trying to intercept the constructor of "okhttp3.OkHttpClient.Builder" to add my interceptor in the interceptors list. But the problem is that the type() method is unable to find my class, I've tried different ways for eg: ElementMatchers .is(), .nameStartsWith(), .nameContains(), named() etc. but couldn't find the class.

I know that okhttp uses Kotlin & java adds $ in the namespace to invoke some methods, so for my case i changed it to "okhttp3.OkHttpClient$Builder", but again it was not finding the matching class.

I also want to know my approach that am i doing the right way to add the custom interceptor.

Agent:

```java 

 

```


--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/byte-buddy/dbabd997-5681-421f-b9ac-1f325379bca4n%40googlegroups.com.

Gourav Kumar

unread,
Sep 7, 2022, 4:30:17 AM9/7/22
to Byte Buddy
Ok, Thanks alot!! i will continue my issue on github only henceforth.
Reply all
Reply to author
Forward
0 new messages