-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request body extraction for Spring MVC integration (#1595)
Fixes #1334
- Loading branch information
1 parent
5ab8e8d
commit ab5a278
Showing
19 changed files
with
639 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
sentry-samples/sentry-samples-spring-boot/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
sentry-spring/src/main/java/io/sentry/spring/CachedBodyHttpServletRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.sentry.spring; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import javax.servlet.ServletInputStream; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletRequestWrapper; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.util.StreamUtils; | ||
|
||
final class CachedBodyHttpServletRequest extends HttpServletRequestWrapper { | ||
|
||
private final @NotNull byte[] cachedBody; | ||
|
||
public CachedBodyHttpServletRequest(final @NotNull HttpServletRequest request) | ||
throws IOException { | ||
super(request); | ||
this.cachedBody = StreamUtils.copyToByteArray(request.getInputStream()); | ||
} | ||
|
||
@Override | ||
public @NotNull ServletInputStream getInputStream() { | ||
return new CachedBodyServletInputStream(this.cachedBody); | ||
} | ||
|
||
@Override | ||
public @NotNull BufferedReader getReader() { | ||
return new BufferedReader( | ||
new InputStreamReader(new ByteArrayInputStream(this.cachedBody), StandardCharsets.UTF_8)); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
sentry-spring/src/main/java/io/sentry/spring/CachedBodyServletInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.sentry.spring; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import javax.servlet.ReadListener; | ||
import javax.servlet.ServletInputStream; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
final class CachedBodyServletInputStream extends ServletInputStream { | ||
|
||
private final @NotNull InputStream cachedBodyInputStream; | ||
|
||
public CachedBodyServletInputStream(final @NotNull byte[] cachedBody) { | ||
this.cachedBodyInputStream = new ByteArrayInputStream(cachedBody); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("EmptyCatch") | ||
public boolean isFinished() { | ||
try { | ||
return cachedBodyInputStream.available() == 0; | ||
} catch (IOException e) { | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void setReadListener(final @Nullable ReadListener readListener) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
return cachedBodyInputStream.read(); | ||
} | ||
|
||
@Override | ||
public int read(@NotNull byte[] b, int off, int len) throws IOException { | ||
return cachedBodyInputStream.read(b, off, len); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
sentry-spring/src/main/java/io/sentry/spring/RequestPayloadExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.sentry.spring; | ||
|
||
import io.sentry.SentryLevel; | ||
import io.sentry.SentryOptions; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.springframework.util.StreamUtils; | ||
|
||
final class RequestPayloadExtractor { | ||
|
||
@Nullable | ||
String extract(final @NotNull HttpServletRequest request, final @NotNull SentryOptions options) { | ||
// request body can be read only once from the stream | ||
// original request can be replaced with CachedBodyHttpServletRequest in SentrySpringFilter | ||
if (request instanceof CachedBodyHttpServletRequest) { | ||
try { | ||
final byte[] body = StreamUtils.copyToByteArray(request.getInputStream()); | ||
return new String(body, StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
options.getLogger().log(SentryLevel.ERROR, "Failed to set request body", e); | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.