Skip to content

Commit 9ff8a01

Browse files
committed
Polishing
1 parent 18d2ace commit 9ff8a01

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
*/
3737
public class PathResourceResolver extends AbstractResourceResolver {
3838

39-
4039
@Override
4140
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
4241
List<? extends Resource> locations, ResourceResolverChain chain) {
@@ -55,21 +54,21 @@ private Resource getResource(String resourcePath, List<? extends Resource> locat
5554
for (Resource location : locations) {
5655
try {
5756
if (logger.isTraceEnabled()) {
58-
logger.trace("Checking location=[" + location + "]");
57+
logger.trace("Checking location: " + location);
5958
}
6059
Resource resource = getResource(resourcePath, location);
6160
if (resource != null) {
6261
if (logger.isTraceEnabled()) {
63-
logger.trace("Found match");
62+
logger.trace("Found match: " + resource);
6463
}
6564
return resource;
6665
}
6766
else if (logger.isTraceEnabled()) {
68-
logger.trace("No match");
67+
logger.trace("No match for location: " + location);
6968
}
7069
}
7170
catch (IOException ex) {
72-
logger.trace("Failure checking for relative resource. Trying next location.", ex);
71+
logger.trace("Failure checking for relative resource - trying next location", ex);
7372
}
7473
}
7574
return null;
@@ -81,7 +80,7 @@ else if (logger.isTraceEnabled()) {
8180
* {@code Resource} for the given path relative to the location.
8281
* @param resourcePath the path to the resource
8382
* @param location the location to check
84-
* @return the resource or {@code null}
83+
* @return the resource, or {@code null} if none found
8584
*/
8685
protected Resource getResource(String resourcePath, Resource location) throws IOException {
8786
Resource resource = location.createRelative(resourcePath);

spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public interface WebSocketSession extends Closeable {
6161

6262
/**
6363
* Return a {@link java.security.Principal} instance containing the name of the
64-
* authenticated user. If the user has not been authenticated, the method returns
65-
* <code>null</code>.
64+
* authenticated user.
65+
* <p>If the user has not been authenticated, the method returns <code>null</code>.
6666
*/
6767
Principal getPrincipal();
6868

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.java

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,10 @@
1919
import java.net.URI;
2020
import java.security.Principal;
2121
import java.util.ArrayList;
22+
import java.util.HashSet;
2223
import java.util.List;
2324
import java.util.Map;
25+
import java.util.Set;
2426
import java.util.concurrent.ConcurrentHashMap;
2527

2628
import org.apache.commons.logging.Log;
@@ -31,6 +33,7 @@
3133
import org.springframework.scheduling.TaskScheduler;
3234
import org.springframework.util.Assert;
3335
import org.springframework.util.ClassUtils;
36+
import org.springframework.util.CollectionUtils;
3437
import org.springframework.util.concurrent.ListenableFuture;
3538
import org.springframework.util.concurrent.SettableListenableFuture;
3639
import org.springframework.web.socket.WebSocketHandler;
@@ -53,7 +56,6 @@
5356
*
5457
* @author Rossen Stoyanchev
5558
* @since 4.1
56-
*
5759
* @see <a href="http://sockjs.org">http://sockjs.org</a>
5860
* @see org.springframework.web.socket.sockjs.client.Transport
5961
*/
@@ -64,34 +66,41 @@ public class SockJsClient implements WebSocketClient, Lifecycle {
6466

6567
private static final Log logger = LogFactory.getLog(SockJsClient.class);
6668

69+
private static final Set<String> supportedProtocols = new HashSet<String>(4);
70+
71+
static {
72+
supportedProtocols.add("ws");
73+
supportedProtocols.add("wss");
74+
supportedProtocols.add("http");
75+
supportedProtocols.add("https");
76+
}
6777

68-
private InfoReceiver infoReceiver;
6978

7079
private final List<Transport> transports;
7180

81+
private InfoReceiver infoReceiver;
82+
7283
private SockJsMessageCodec messageCodec;
7384

7485
private TaskScheduler connectTimeoutScheduler;
7586

76-
private final Map<URI, ServerInfo> serverInfoCache = new ConcurrentHashMap<URI, ServerInfo>();
77-
7887
private volatile boolean running = false;
7988

89+
private final Map<URI, ServerInfo> serverInfoCache = new ConcurrentHashMap<URI, ServerInfo>();
90+
8091

8192
/**
8293
* Create a {@code SockJsClient} with the given transports.
83-
*
8494
* <p>If the list includes an {@link XhrTransport} (or more specifically an
8595
* implementation of {@link InfoReceiver}) the instance is used to initialize
8696
* the {@link #setInfoReceiver(InfoReceiver) infoReceiver} property, or
8797
* otherwise is defaulted to {@link RestTemplateXhrTransport}.
88-
*
8998
* @param transports the (non-empty) list of transports to use
9099
*/
91100
public SockJsClient(List<Transport> transports) {
92101
Assert.notEmpty(transports, "No transports provided");
93-
this.infoReceiver = initInfoReceiver(transports);
94102
this.transports = new ArrayList<Transport>(transports);
103+
this.infoReceiver = initInfoReceiver(transports);
95104
if (jackson2Present) {
96105
this.messageCodec = new Jackson2SockJsMessageCodec();
97106
}
@@ -110,39 +119,37 @@ private static InfoReceiver initInfoReceiver(List<Transport> transports) {
110119
/**
111120
* Configure the {@code InfoReceiver} to use to perform the SockJS "Info"
112121
* request before the SockJS session starts.
113-
*
114122
* <p>If the list of transports provided to the constructor contained an
115123
* {@link XhrTransport} or an implementation of {@link InfoReceiver} that
116124
* instance would have been used to initialize this property, or otherwise
117125
* it defaults to {@link RestTemplateXhrTransport}.
118-
*
119126
* @param infoReceiver the transport to use for the SockJS "Info" request
120127
*/
121128
public void setInfoReceiver(InfoReceiver infoReceiver) {
122-
Assert.notNull(infoReceiver, "'infoReceiver' is required");
129+
Assert.notNull(infoReceiver, "InfoReceiver is required");
123130
this.infoReceiver = infoReceiver;
124131
}
125132

126133
/**
127-
* Return the configured {@code InfoReceiver}, never {@code null}.
134+
* Return the configured {@code InfoReceiver} (never {@code null}).
128135
*/
129136
public InfoReceiver getInfoReceiver() {
130137
return this.infoReceiver;
131138
}
132139

133140
/**
134141
* Set the SockJsMessageCodec to use.
135-
*
136142
* <p>By default {@link org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec
137143
* Jackson2SockJsMessageCodec} is used if Jackson is on the classpath.
138-
*
139-
* @param messageCodec the message messageCodec to use
140144
*/
141145
public void setMessageCodec(SockJsMessageCodec messageCodec) {
142146
Assert.notNull(messageCodec, "'messageCodec' is required");
143147
this.messageCodec = messageCodec;
144148
}
145149

150+
/**
151+
* Return the SockJsMessageCodec to use.
152+
*/
146153
public SockJsMessageCodec getMessageCodec() {
147154
return this.messageCodec;
148155
}
@@ -159,6 +166,7 @@ public void setConnectTimeoutScheduler(TaskScheduler connectTimeoutScheduler) {
159166
this.connectTimeoutScheduler = connectTimeoutScheduler;
160167
}
161168

169+
162170
@Override
163171
public void start() {
164172
if (!isRunning()) {
@@ -192,35 +200,27 @@ public boolean isRunning() {
192200
return this.running;
193201
}
194202

195-
/**
196-
* By default the result of a SockJS "Info" request, including whether the
197-
* server has WebSocket disabled and how long the request took (used for
198-
* calculating transport timeout time) is cached. This method can be used to
199-
* clear that cache hence causing it to re-populate.
200-
*/
201-
public void clearServerInfoCache() {
202-
this.serverInfoCache.clear();
203-
}
204203

205204
@Override
206-
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler,
207-
String uriTemplate, Object... uriVars) {
205+
public ListenableFuture<WebSocketSession> doHandshake(
206+
WebSocketHandler handler, String uriTemplate, Object... uriVars) {
208207

209208
Assert.notNull(uriTemplate, "uriTemplate must not be null");
210209
URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri();
211210
return doHandshake(handler, null, uri);
212211
}
213212

214213
@Override
215-
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler,
216-
WebSocketHttpHeaders headers, URI url) {
214+
public final ListenableFuture<WebSocketSession> doHandshake(
215+
WebSocketHandler handler, WebSocketHttpHeaders headers, URI url) {
217216

218-
Assert.notNull(handler, "'webSocketHandler' is required");
219-
Assert.notNull(url, "'url' is required");
217+
Assert.notNull(handler, "WebSocketHandler is required");
218+
Assert.notNull(url, "URL is required");
220219

221220
String scheme = url.getScheme();
222-
Assert.isTrue(scheme != null && ("ws".equals(scheme) || "wss".equals(scheme) ||
223-
"http".equals(scheme) || "https".equals(scheme)), "Invalid scheme: " + scheme);
221+
if (!supportedProtocols.contains(scheme)) {
222+
throw new IllegalArgumentException("Invalid scheme: '" + scheme + "'");
223+
}
224224

225225
SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<WebSocketSession>();
226226
try {
@@ -259,7 +259,10 @@ private DefaultTransportRequest createRequest(SockJsUrlInfo urlInfo, HttpHeaders
259259
}
260260
}
261261
}
262-
Assert.notEmpty(requests, "No transports, " + urlInfo + ", wsEnabled=" + serverInfo.isWebSocketEnabled());
262+
if (CollectionUtils.isEmpty(requests)) {
263+
throw new IllegalStateException(
264+
"No transports: " + urlInfo + ", webSocketEnabled=" + serverInfo.isWebSocketEnabled());
265+
}
263266
for (int i = 0; i < requests.size() - 1; i++) {
264267
DefaultTransportRequest request = requests.get(i);
265268
request.setUser(getUser());
@@ -274,15 +277,24 @@ private DefaultTransportRequest createRequest(SockJsUrlInfo urlInfo, HttpHeaders
274277

275278
/**
276279
* Return the user to associate with the SockJS session and make available via
277-
* {@link org.springframework.web.socket.WebSocketSession#getPrincipal()
278-
* WebSocketSession#getPrincipal()}.
280+
* {@link org.springframework.web.socket.WebSocketSession#getPrincipal()}.
279281
* <p>By default this method returns {@code null}.
280-
* @return the user to associate with the session, possibly {@code null}
282+
* @return the user to associate with the session (possibly {@code null})
281283
*/
282284
protected Principal getUser() {
283285
return null;
284286
}
285287

288+
/**
289+
* By default the result of a SockJS "Info" request, including whether the
290+
* server has WebSocket disabled and how long the request took (used for
291+
* calculating transport timeout time) is cached. This method can be used to
292+
* clear that cache hence causing it to re-populate.
293+
*/
294+
public void clearServerInfoCache() {
295+
this.serverInfoCache.clear();
296+
}
297+
286298

287299
/**
288300
* A simple value object holding the result from a SockJS "Info" request.
@@ -293,8 +305,7 @@ private static class ServerInfo {
293305

294306
private final long responseTime;
295307

296-
297-
private ServerInfo(String response, long responseTime) {
308+
public ServerInfo(String response, long responseTime) {
298309
this.responseTime = responseTime;
299310
this.webSocketEnabled = !response.matches(".*[\"']websocket[\"']\\s*:\\s*false.*");
300311
}
@@ -308,4 +319,4 @@ public long getRetransmissionTimeout() {
308319
}
309320
}
310321

311-
}
322+
}

0 commit comments

Comments
 (0)