Skip to content

Commit

Permalink
remove attribute after use
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Feb 14, 2024
1 parent af4cbec commit 325bee1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ public abstract class ServletApiAdvice {
private static final DetachedThreadLocal<Object> servletPathTL = GlobalVariables.get(ServletApiAdvice.class, "servletPath", WeakConcurrent.buildThreadLocal());
private static final DetachedThreadLocal<Object> pathInfoTL = GlobalVariables.get(ServletApiAdvice.class, "pathInfo", WeakConcurrent.buildThreadLocal());

private static final List<String> requestExceptionAttributes = Arrays.asList("javax.servlet.error.exception", "jakarta.servlet.error.exception", "exception", "org.springframework.web.servlet.DispatcherServlet.EXCEPTION", "co.elastic.apm.exception");
private static final String ELASTIC_EXCEPTION = "co.elastic.apm.exception";
private static final String JAVAX_ERROR_EXCEPTION = "javax.servlet.error.exception";
private static final String JAKARTA_ERROR_EXCEPTION = "jakarta.servlet.error.exception";
private static final List<String> requestExceptionAttributes = Arrays.asList(
JAVAX_ERROR_EXCEPTION,
JAKARTA_ERROR_EXCEPTION,
"exception",
"org.springframework.web.servlet.DispatcherServlet.EXCEPTION",
ELASTIC_EXCEPTION);

@Nullable
public static <HttpServletRequest, HttpServletResponse, ServletContext, ServletContextEvent, FilterConfig, ServletConfig> Object onServletEnter(
Expand Down Expand Up @@ -233,7 +241,13 @@ public static <HttpServletRequest, HttpServletResponse, ServletContext, ServletC
Object throwable = adapter.getAttribute(httpServletRequest, attributeName);
if (throwable instanceof Throwable) {
t2 = (Throwable) throwable;
if (!attributeName.equals("javax.servlet.error.exception") && !attributeName.equals("jakarta.servlet.error.exception")) {

// elastic exception can be removed as it's not needed after transaction end
if (attributeName.equals(ELASTIC_EXCEPTION)) {
adapter.removeAttribute(httpServletRequest, attributeName);
}

if (!attributeName.equals(JAVAX_ERROR_EXCEPTION) && !attributeName.equals(JAKARTA_ERROR_EXCEPTION)) {
overrideStatusCodeOnThrowable = false;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public void setAttribute(HttpServletRequest servletRequest, String attributeName
servletRequest.setAttribute(attributeName, value);
}

@Override
public void removeAttribute(HttpServletRequest httpServletRequest, String attributeName) {
httpServletRequest.removeAttribute(attributeName);
}

@Override
public Collection<String> getHeaderNames(HttpServletResponse httpServletResponse) {
return httpServletResponse.getHeaderNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ public void setAttribute(HttpServletRequest servletRequest, String attributeName
servletRequest.setAttribute(attributeName, value);
}

@Override
public void removeAttribute(HttpServletRequest servletRequest, String attributeName) {
servletRequest.removeAttribute(attributeName);
}

@Override
public Collection<String> getHeaderNames(HttpServletResponse httpServletResponse) {
return httpServletResponse.getHeaderNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public interface ServletRequestAdapter<HttpServletRequest, ServletContext> {

void setAttribute(HttpServletRequest request, String attributeName, Object value);

void removeAttribute(HttpServletRequest request, String attributeName);

Map<String, String[]> getParameterMap(HttpServletRequest httpServletRequest);

TextHeaderGetter<HttpServletRequest> getRequestHeaderGetter();
Expand Down

0 comments on commit 325bee1

Please sign in to comment.