1818
1919import java .io .IOException ;
2020import java .nio .charset .Charset ;
21- import java .util .ArrayList ;
2221import java .util .Arrays ;
22+ import java .util .Collection ;
2323import java .util .Collections ;
2424import java .util .Date ;
2525import java .util .HashSet ;
26+ import java .util .LinkedHashSet ;
2627import java .util .List ;
2728import java .util .Random ;
29+ import java .util .Set ;
2830import java .util .concurrent .TimeUnit ;
2931import javax .servlet .http .HttpServletRequest ;
3032
5658 * path resolution and handling of static SockJS requests (e.g. "/info", "/iframe.html",
5759 * etc). Sub-classes must handle session URLs (i.e. transport-specific requests).
5860 *
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}
6062 * to specify a list of allowed origins (a list containing "*" will allow all origins).
6163 *
6264 * @author Rossen Stoyanchev
@@ -94,10 +96,10 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig
9496
9597 private boolean webSocketEnabled = true ;
9698
97- private final List <String > allowedOrigins = new ArrayList <String >();
98-
9999 private boolean suppressCors = false ;
100100
101+ protected final Set <String > allowedOrigins = new LinkedHashSet <String >();
102+
101103
102104 public AbstractSockJsService (TaskScheduler scheduler ) {
103105 Assert .notNull (scheduler , "TaskScheduler must not be null" );
@@ -274,6 +276,24 @@ public boolean isWebSocketEnabled() {
274276 return this .webSocketEnabled ;
275277 }
276278
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+
277297 /**
278298 * Configure allowed {@code Origin} header values. This check is mostly
279299 * designed for browsers. There is nothing preventing other types of client
@@ -289,36 +309,18 @@ public boolean isWebSocketEnabled() {
289309 * @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
290310 * @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>
291311 */
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" );
294314 this .allowedOrigins .clear ();
295315 this .allowedOrigins .addAll (allowedOrigins );
296316 }
297317
298318 /**
299319 * @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
319321 */
320- public boolean shouldSuppressCors () {
321- return this .suppressCors ;
322+ public Collection < String > getAllowedOrigins () {
323+ return Collections . unmodifiableSet ( this .allowedOrigins ) ;
322324 }
323325
324326
@@ -465,24 +467,11 @@ private boolean validatePath(ServerHttpRequest request) {
465467 String path = request .getURI ().getPath ();
466468 int index = path .lastIndexOf ('/' ) + 1 ;
467469 String filename = path .substring (index );
468- return filename .indexOf (';' ) == -1 ;
470+ return ( filename .indexOf (';' ) == -1 ) ;
469471 }
470472
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 {
486475
487476 if (WebUtils .isSameOrigin (request )) {
488477 return true ;
@@ -529,6 +518,19 @@ protected void sendMethodNotAllowed(ServerHttpResponse response, HttpMethod... h
529518 }
530519
531520
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+
532534 private interface SockJsRequestHandler {
533535
534536 void handle (ServerHttpRequest request , ServerHttpResponse response ) throws IOException ;
@@ -546,8 +548,8 @@ public void handle(ServerHttpRequest request, ServerHttpResponse response) throw
546548 addNoCacheHeaders (response );
547549 if (checkOrigin (request , response )) {
548550 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 ());
551553 response .getBody ().write (content .getBytes ());
552554 }
553555
0 commit comments