Commit Session on Include Dispatch

The servlet spec disallows any writing of headers after an include has been issued.

This commit intercepts the include and commits the session, then
allowing the include to proceed.

See: #1250
This commit is contained in:
Josh Cummings
2018-11-02 21:18:53 +01:00
committed by Vedran Pavic
parent 3940a22d5e
commit 695f2f1509
2 changed files with 42 additions and 0 deletions

View File

@@ -22,8 +22,11 @@ import java.util.HashMap;
import java.util.Map;
import javax.servlet.FilterChain;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
@@ -397,6 +400,12 @@ public class SessionRepositoryFilter<S extends ExpiringSession>
.getRequestedSessionId(this);
}
@Override
public RequestDispatcher getRequestDispatcher(String path) {
RequestDispatcher requestDispatcher = super.getRequestDispatcher(path);
return new SessionCommittingRequestDispatcher(requestDispatcher);
}
/**
* Allows creating an HttpSession from a Session instance.
*
@@ -417,6 +426,24 @@ public class SessionRepositoryFilter<S extends ExpiringSession>
SessionRepositoryFilter.this.sessionRepository.delete(getId());
}
}
private final class SessionCommittingRequestDispatcher implements RequestDispatcher {
private final RequestDispatcher delegate;
SessionCommittingRequestDispatcher(RequestDispatcher delegate) {
this.delegate = delegate;
}
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
this.delegate.forward(request, response);
}
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
SessionRepositoryRequestWrapper.this.commitSession();
this.delegate.include(request, response);
}
}
}
/**

View File

@@ -1175,6 +1175,21 @@ public class SessionRepositoryFilterTests {
});
}
@Test
public void doFilterInclude() throws Exception {
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest,
HttpServletResponse wrappedResponse) throws IOException, ServletException {
String id = wrappedRequest.getSession().getId();
wrappedRequest.getRequestDispatcher("/").include(wrappedRequest, wrappedResponse);
assertThat(
SessionRepositoryFilterTests.this.sessionRepository.getSession(id))
.isNotNull();
}
});
}
// --- MultiHttpSessionStrategyAdapter
@Test