use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ServletInvocableHandlerMethod method disableContentCachingIfNecessary.
private void disableContentCachingIfNecessary(ServletWebRequest webRequest) {
if (isRequestNotModified(webRequest)) {
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
Assert.notNull(response, "Expected HttpServletResponse");
if (StringUtils.hasText(response.getHeader(HttpHeaders.ETAG))) {
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
Assert.notNull(request, "Expected HttpServletRequest");
}
}
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class JettyRequestUpgradeStrategy method upgrade.
@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions, @Nullable Principal user, WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureException {
Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
ServletContext servletContext = servletRequest.getServletContext();
Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(handler, session);
JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
if (selectedProtocol != null) {
upgradeResponse.setAcceptedSubProtocol(selectedProtocol);
}
return handlerAdapter;
};
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
try {
container.upgrade(webSocketCreator, servletRequest, servletResponse);
} catch (UndeclaredThrowableException ex) {
throw new HandshakeFailureException("Failed to upgrade", ex.getUndeclaredThrowable());
} catch (Exception ex) {
throw new HandshakeFailureException("Failed to upgrade", ex);
}
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class UndertowRequestUpgradeStrategy method upgradeInternal.
@Override
protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException {
HttpServletRequest servletRequest = getHttpServletRequest(request);
HttpServletResponse servletResponse = getHttpServletResponse(response);
StringBuffer requestUrl = servletRequest.getRequestURL();
// shouldn't matter
String path = servletRequest.getRequestURI();
Map<String, String> pathParams = Collections.emptyMap();
ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
endpointConfig.setExtensions(selectedExtensions);
try {
getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
} catch (ServletException ex) {
throw new HandshakeFailureException("Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
} catch (IOException ex) {
throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + requestUrl, ex);
}
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class SockJsServiceTests method handleInfoGetWildflyNPE.
// SPR-11919
@Test
public void handleInfoGetWildflyNPE() throws IOException {
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
ServletOutputStream ous = mock(ServletOutputStream.class);
given(mockResponse.getHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).willThrow(NullPointerException.class);
given(mockResponse.getOutputStream()).willReturn(ous);
this.response = new ServletServerHttpResponse(mockResponse);
handleRequest("GET", "/echo/info", HttpStatus.OK);
verify(mockResponse, times(1)).getOutputStream();
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method handleReturnValueWithETagAndETagFilter.
// SPR-13423
@Test
public void handleReturnValueWithETagAndETagFilter() throws Exception {
String eTagValue = "\"deadb33f8badf00d\"";
String content = "body";
Method method = getClass().getDeclaredMethod("handle");
MethodParameter returnType = new MethodParameter(method, -1);
FilterChain chain = (req, res) -> {
ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(eTagValue).body(content);
try {
ServletWebRequest requestToUse = new ServletWebRequest((HttpServletRequest) req, (HttpServletResponse) res);
new HttpEntityMethodProcessor(Collections.singletonList(new StringHttpMessageConverter())).handleReturnValue(returnValue, returnType, mavContainer, requestToUse);
assertThat(this.servletResponse.getContentAsString()).as("Response body was cached? It should be written directly to the raw response").isEqualTo(content);
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
};
this.servletRequest.setMethod("GET");
new ShallowEtagHeaderFilter().doFilter(this.servletRequest, this.servletResponse, chain);
assertThat(this.servletResponse.getStatus()).isEqualTo(200);
assertThat(this.servletResponse.getHeader(HttpHeaders.ETAG)).isEqualTo(eTagValue);
assertThat(this.servletResponse.getContentAsString()).isEqualTo(content);
}
Aggregations