18
18
19
19
import java .io .IOException ;
20
20
import java .nio .charset .Charset ;
21
- import java .util .ArrayList ;
22
21
import java .util .Arrays ;
22
+ import java .util .Collection ;
23
23
import java .util .Collections ;
24
24
import java .util .Date ;
25
25
import java .util .HashSet ;
26
+ import java .util .LinkedHashSet ;
26
27
import java .util .List ;
27
28
import java .util .Random ;
29
+ import java .util .Set ;
28
30
import java .util .concurrent .TimeUnit ;
29
31
import javax .servlet .http .HttpServletRequest ;
30
32
56
58
* path resolution and handling of static SockJS requests (e.g. "/info", "/iframe.html",
57
59
* etc). Sub-classes must handle session URLs (i.e. transport-specific requests).
58
60
*
59
- * By default, only same origin requests are allowed. Use {@link #setAllowedOrigins(List) }
61
+ * By default, only same origin requests are allowed. Use {@link #setAllowedOrigins}
60
62
* to specify a list of allowed origins (a list containing "*" will allow all origins).
61
63
*
62
64
* @author Rossen Stoyanchev
@@ -94,10 +96,10 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig
94
96
95
97
private boolean webSocketEnabled = true ;
96
98
97
- private final List <String > allowedOrigins = new ArrayList <String >();
98
-
99
99
private boolean suppressCors = false ;
100
100
101
+ protected final Set <String > allowedOrigins = new LinkedHashSet <String >();
102
+
101
103
102
104
public AbstractSockJsService (TaskScheduler scheduler ) {
103
105
Assert .notNull (scheduler , "TaskScheduler must not be null" );
@@ -274,6 +276,24 @@ public boolean isWebSocketEnabled() {
274
276
return this .webSocketEnabled ;
275
277
}
276
278
279
+ /**
280
+ * This option can be used to disable automatic addition of CORS headers for
281
+ * SockJS requests.
282
+ * <p>The default value is "false".
283
+ * @since 4.1.2
284
+ */
285
+ public void setSuppressCors (boolean suppressCors ) {
286
+ this .suppressCors = suppressCors ;
287
+ }
288
+
289
+ /**
290
+ * @since 4.1.2
291
+ * @see #setSuppressCors(boolean)
292
+ */
293
+ public boolean shouldSuppressCors () {
294
+ return this .suppressCors ;
295
+ }
296
+
277
297
/**
278
298
* Configure allowed {@code Origin} header values. This check is mostly
279
299
* designed for browsers. There is nothing preventing other types of client
@@ -289,36 +309,18 @@ public boolean isWebSocketEnabled() {
289
309
* @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
290
310
* @see <a href="https://github.com/sockjs/sockjs-client#supported-transports-by-browser-html-served-from-http-or-https">SockJS supported transports by browser</a>
291
311
*/
292
- public void setAllowedOrigins (List <String > allowedOrigins ) {
293
- Assert .notNull (allowedOrigins , "Allowed origin List must not be null" );
312
+ public void setAllowedOrigins (Collection <String > allowedOrigins ) {
313
+ Assert .notNull (allowedOrigins , "Allowed origins Collection must not be null" );
294
314
this .allowedOrigins .clear ();
295
315
this .allowedOrigins .addAll (allowedOrigins );
296
316
}
297
317
298
318
/**
299
319
* @since 4.1.2
300
- * @see #setAllowedOrigins(List)
301
- */
302
- public List <String > getAllowedOrigins () {
303
- return Collections .unmodifiableList (this .allowedOrigins );
304
- }
305
-
306
- /**
307
- * This option can be used to disable automatic addition of CORS headers for
308
- * SockJS requests.
309
- * <p>The default value is "false".
310
- * @since 4.1.2
311
- */
312
- public void setSuppressCors (boolean suppressCors ) {
313
- this .suppressCors = suppressCors ;
314
- }
315
-
316
- /**
317
- * @since 4.1.2
318
- * @see #setSuppressCors(boolean)
320
+ * @see #setAllowedOrigins
319
321
*/
320
- public boolean shouldSuppressCors () {
321
- return this .suppressCors ;
322
+ public Collection < String > getAllowedOrigins () {
323
+ return Collections . unmodifiableSet ( this .allowedOrigins ) ;
322
324
}
323
325
324
326
@@ -465,24 +467,11 @@ private boolean validatePath(ServerHttpRequest request) {
465
467
String path = request .getURI ().getPath ();
466
468
int index = path .lastIndexOf ('/' ) + 1 ;
467
469
String filename = path .substring (index );
468
- return filename .indexOf (';' ) == -1 ;
470
+ return ( filename .indexOf (';' ) == -1 ) ;
469
471
}
470
472
471
- /**
472
- * Handle request for raw WebSocket communication, i.e. without any SockJS message framing.
473
- */
474
- protected abstract void handleRawWebSocketRequest (ServerHttpRequest request ,
475
- ServerHttpResponse response , WebSocketHandler webSocketHandler ) throws IOException ;
476
-
477
- /**
478
- * Handle a SockJS session URL (i.e. transport-specific request).
479
- */
480
- protected abstract void handleTransportRequest (ServerHttpRequest request , ServerHttpResponse response ,
481
- WebSocketHandler webSocketHandler , String sessionId , String transport ) throws SockJsException ;
482
-
483
-
484
- protected boolean checkOrigin (ServerHttpRequest request , ServerHttpResponse response ,
485
- HttpMethod ... httpMethods ) throws IOException {
473
+ protected boolean checkOrigin (ServerHttpRequest request , ServerHttpResponse response , HttpMethod ... httpMethods )
474
+ throws IOException {
486
475
487
476
if (WebUtils .isSameOrigin (request )) {
488
477
return true ;
@@ -529,6 +518,19 @@ protected void sendMethodNotAllowed(ServerHttpResponse response, HttpMethod... h
529
518
}
530
519
531
520
521
+ /**
522
+ * Handle request for raw WebSocket communication, i.e. without any SockJS message framing.
523
+ */
524
+ protected abstract void handleRawWebSocketRequest (ServerHttpRequest request ,
525
+ ServerHttpResponse response , WebSocketHandler webSocketHandler ) throws IOException ;
526
+
527
+ /**
528
+ * Handle a SockJS session URL (i.e. transport-specific request).
529
+ */
530
+ protected abstract void handleTransportRequest (ServerHttpRequest request , ServerHttpResponse response ,
531
+ WebSocketHandler webSocketHandler , String sessionId , String transport ) throws SockJsException ;
532
+
533
+
532
534
private interface SockJsRequestHandler {
533
535
534
536
void handle (ServerHttpRequest request , ServerHttpResponse response ) throws IOException ;
@@ -546,8 +548,8 @@ public void handle(ServerHttpRequest request, ServerHttpResponse response) throw
546
548
addNoCacheHeaders (response );
547
549
if (checkOrigin (request , response )) {
548
550
response .getHeaders ().setContentType (new MediaType ("application" , "json" , UTF8_CHARSET ));
549
- String content = String .format (INFO_CONTENT , random . nextInt (),
550
- isSessionCookieNeeded (), isWebSocketEnabled ());
551
+ String content = String .format (
552
+ INFO_CONTENT , random . nextInt (), isSessionCookieNeeded (), isWebSocketEnabled ());
551
553
response .getBody ().write (content .getBytes ());
552
554
}
553
555
0 commit comments