Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercept Input Streams #1615

Open
SamHoque opened this issue Apr 18, 2024 · 2 comments
Open

Intercept Input Streams #1615

SamHoque opened this issue Apr 18, 2024 · 2 comments
Assignees
Labels
Milestone

Comments

@SamHoque
Copy link

SamHoque commented Apr 18, 2024

I am trying to intercept an input stream and then return an unconsumed input stream so the original method can still read from the input stream. but for some reason the inputStream is being returned empty.

heres my advice:

        @Advice.OnMethodExit
        static void interceptGetInputStream(@Advice.Return(readOnly = false) InputStream inputStream) {
            System.out.println("exit...");
            try {
                if (inputStream != null) {
                    // Read the entire content of the input stream into a buffer
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                    byte[] data = new byte[4096];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
                        buffer.write(data, 0, bytesRead);
                    }
                    buffer.flush();
                    byte[] responseData = buffer.toByteArray();

                    // Print the response data
                    String responseDataString = new String(responseData, StandardCharsets.UTF_8);
                    System.out.println("Response content:\n" + responseDataString);

                    // Reset the input stream by creating a new ByteArrayInputStream
                    inputStream = new ByteArrayInputStream(responseData);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        new AgentBuilder.Default()
                .disableClassFormatChanges()
                .with(RETRANSFORMATION)
                // Make sure we see helpful logs
                .with(AgentBuilder.RedefinitionStrategy.Listener.StreamWriting.toSystemError())
                .with(AgentBuilder.Listener.StreamWriting.toSystemError().withTransformationsOnly())
                .with(AgentBuilder.InstallationListener.StreamWriting.toSystemError())
                .ignore(none())
                // Ignore Byte Buddy and JDK classes we are not interested in
                .ignore(
                        nameStartsWith("net.bytebuddy.")
                                .or(nameStartsWith("jdk.internal.reflect."))
                                .or(nameStartsWith("java.lang.invoke."))
                                .or(nameStartsWith("com.sun.proxy."))
                )
                .disableClassFormatChanges()
                .with(RETRANSFORMATION)
                .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
                .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
                .type(nameContains("HttpURLConnection"))
                .transform((builder, type, classLoader, transformer, module) -> builder.visit(Advice.to(HttpUrlConnectionInstrumentation.GetResponseCodeAdvice.class).on(named("getInputStream"))))
                .installOnByteBuddyAgent();
@raphw raphw added the question label Apr 19, 2024
@raphw raphw added this to the 1.14.14 milestone Apr 19, 2024
@raphw raphw self-assigned this Apr 19, 2024
@raphw
Copy link
Owner

raphw commented Apr 19, 2024

Are you sure your advice is applied? Did you dump the class file snd look st the generated class?

@SamHoque
Copy link
Author

Hey, I was able to fix this by only reading the input stream once and for future exits, I return a file output stream from my saved response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants