Skip to content

Commit 24cdefb

Browse files
committed
Workaround for WFLY-3474 NullPointerException
Prior to this commit, the ServletResponseHttpHeaders.get method would throw an NPE when used under Wildfly 8.0.0.Final and 8.1.0.Final. This can be traced to WFLY-3474, which throws an NPE when calling HttpServletResponse.getHeaders("foo") and that header has not been defined prior to that. This would cause NPE being thrown by AbstractSockJsService when checking for CORS HTTP headers in the server HTTP response. This commit surrounds that method call in AbstractSockJsService and guards against this issue. Issue: SPR-11919
1 parent 8221c9a commit 24cdefb

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,15 @@ protected void addCorsHeaders(ServerHttpRequest request, ServerHttpResponse resp
367367
HttpHeaders requestHeaders = request.getHeaders();
368368
HttpHeaders responseHeaders = response.getHeaders();
369369

370-
// Perhaps a CORS Filter has already added this?
371-
if (!CollectionUtils.isEmpty(responseHeaders.get("Access-Control-Allow-Origin"))) {
372-
logger.debug("Skip adding CORS headers, response already contains \"Access-Control-Allow-Origin\"");
373-
return;
370+
try {
371+
// Perhaps a CORS Filter has already added this?
372+
if (!CollectionUtils.isEmpty(responseHeaders.get("Access-Control-Allow-Origin"))) {
373+
logger.debug("Skip adding CORS headers, response already contains \"Access-Control-Allow-Origin\"");
374+
return;
375+
}
376+
}
377+
catch (NullPointerException npe) {
378+
// See SPR-11919 and https://issues.jboss.org/browse/WFLY-3474
374379
}
375380

376381
String origin = requestHeaders.getFirst("origin");

spring-websocket/src/test/java/org/springframework/web/socket/sockjs/support/SockJsServiceTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323
import org.springframework.http.HttpStatus;
2424
import org.springframework.http.server.ServerHttpRequest;
2525
import org.springframework.http.server.ServerHttpResponse;
26+
import org.springframework.http.server.ServletServerHttpResponse;
2627
import org.springframework.scheduling.TaskScheduler;
2728
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
2829
import org.springframework.web.socket.AbstractHttpRequestTests;
2930
import org.springframework.web.socket.WebSocketHandler;
3031
import org.springframework.web.socket.sockjs.SockJsException;
3132

3233
import static org.junit.Assert.*;
34+
import static org.mockito.Mockito.*;
35+
36+
import javax.servlet.ServletOutputStream;
37+
import javax.servlet.http.HttpServletResponse;
3338

3439
/**
3540
* Test fixture for {@link AbstractSockJsService}.
@@ -106,6 +111,21 @@ public void handleInfoGetCorsFilter() throws Exception {
106111
assertEquals("foobar:123", this.servletResponse.getHeader("Access-Control-Allow-Origin"));
107112
}
108113

114+
// SPR-11919
115+
116+
@Test
117+
public void handleInfoGetWildflyNPE() throws Exception {
118+
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
119+
ServletOutputStream ous = mock(ServletOutputStream.class);
120+
when(mockResponse.getHeaders("Access-Control-Allow-Origin")).thenThrow(NullPointerException.class);
121+
when(mockResponse.getOutputStream()).thenReturn(ous);
122+
this.response = new ServletServerHttpResponse(mockResponse);
123+
124+
handleRequest("GET", "/echo/info", HttpStatus.OK);
125+
126+
verify(mockResponse.getOutputStream(), times(1));
127+
}
128+
109129
@Test
110130
public void handleInfoOptions() throws Exception {
111131

0 commit comments

Comments
 (0)