use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class TomcatRequestUpgradeStrategy method upgrade.
// for old doUpgrade variant in Tomcat 9.0.55
@SuppressWarnings("deprecation")
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
HttpServletRequest servletRequest = ServerHttpRequestDecorator.getNativeRequest(request);
HttpServletResponse servletResponse = ServerHttpResponseDecorator.getNativeResponse(response);
HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
DataBufferFactory bufferFactory = response.bufferFactory();
// Trigger WebFlux preCommit actions and upgrade
return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
Endpoint endpoint = new StandardWebSocketHandlerAdapter(ContextWebSocketHandler.decorate(handler, contextView), session -> new TomcatWebSocketSession(session, handshakeInfo, bufferFactory));
String requestURI = servletRequest.getRequestURI();
DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
config.setSubprotocols(subProtocol != null ? Collections.singletonList(subProtocol) : Collections.emptyList());
WsServerContainer container = getContainer(servletRequest);
try {
container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
} catch (Exception ex) {
return Mono.error(ex);
}
return Mono.empty();
}));
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ControllerTests method servletWrappingController.
@Test
public void servletWrappingController() throws Exception {
HttpServletRequest request = new MockHttpServletRequest("GET", "/somePath");
HttpServletResponse response = new MockHttpServletResponse();
ServletWrappingController swc = new ServletWrappingController();
swc.setServletClass(TestServlet.class);
swc.setServletName("action");
Properties props = new Properties();
props.setProperty("config", "myValue");
swc.setInitParameters(props);
swc.afterPropertiesSet();
assertThat(TestServlet.config).isNotNull();
assertThat(TestServlet.config.getServletName()).isEqualTo("action");
assertThat(TestServlet.config.getInitParameter("config")).isEqualTo("myValue");
assertThat(TestServlet.request).isNull();
assertThat(TestServlet.destroyed).isFalse();
assertThat(swc.handleRequest(request, response)).isNull();
assertThat(TestServlet.request).isEqualTo(request);
assertThat(TestServlet.response).isEqualTo(response);
assertThat(TestServlet.destroyed).isFalse();
swc.destroy();
assertThat(TestServlet.destroyed).isTrue();
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilter method doFilterInternal.
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
HttpServletResponse responseToUse = response;
if (!isAsyncDispatch(request) && !(response instanceof ConditionalContentCachingResponseWrapper)) {
responseToUse = new ConditionalContentCachingResponseWrapper(response, request);
}
filterChain.doFilter(request, responseToUse);
if (!isAsyncStarted(request) && !isContentCachingDisabled(request)) {
updateResponse(request, responseToUse);
}
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ForwardedHeaderFilter method doFilterInternal.
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (this.removeOnly) {
ForwardedHeaderRemovingRequest wrappedRequest = new ForwardedHeaderRemovingRequest(request);
filterChain.doFilter(wrappedRequest, response);
} else {
HttpServletRequest wrappedRequest = new ForwardedHeaderExtractingRequest(request);
HttpServletResponse wrappedResponse = this.relativeRedirects ? RelativeRedirectResponseWrapper.wrapIfNecessary(response, HttpStatus.SEE_OTHER) : new ForwardedHeaderExtractingResponse(response, wrappedRequest);
filterChain.doFilter(wrappedRequest, wrappedResponse);
}
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class CharacterEncodingFilterTests method forceEncodingAlwaysSetsEncoding.
@Test
public void forceEncodingAlwaysSetsEncoding() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
request.setCharacterEncoding(ENCODING);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain filterChain = mock(FilterChain.class);
CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING, true);
filter.init(new MockFilterConfig(FILTER_NAME));
filter.doFilter(request, response, filterChain);
verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
verify(request).removeAttribute(filteredName(FILTER_NAME));
verify(response).setCharacterEncoding(ENCODING);
verify(filterChain).doFilter(request, response);
}
Aggregations