1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -120,16 +120,17 @@ public void setPathMatcher(PathMatcher pathMatcher) {
120
120
121
121
@ Override
122
122
public UserDestinationResult resolveDestination (Message <?> message ) {
123
- String sourceDestination = SimpMessageHeaderAccessor .getDestination (message .getHeaders ());
124
123
ParseResult parseResult = parse (message );
125
124
if (parseResult == null ) {
126
125
return null ;
127
126
}
128
127
String user = parseResult .getUser ();
128
+ String sourceDestination = parseResult .getSourceDestination ();
129
129
Set <String > targetSet = new HashSet <String >();
130
130
for (String sessionId : parseResult .getSessionIds ()) {
131
131
String actualDestination = parseResult .getActualDestination ();
132
- String targetDestination = getTargetDestination (sourceDestination , actualDestination , sessionId , user );
132
+ String targetDestination = getTargetDestination (
133
+ sourceDestination , actualDestination , sessionId , user );
133
134
if (targetDestination != null ) {
134
135
targetSet .add (targetDestination );
135
136
}
@@ -140,65 +141,84 @@ public UserDestinationResult resolveDestination(Message<?> message) {
140
141
141
142
private ParseResult parse (Message <?> message ) {
142
143
MessageHeaders headers = message .getHeaders ();
143
- String destination = SimpMessageHeaderAccessor .getDestination (headers );
144
- if (destination == null || !checkDestination (destination , this .prefix )) {
144
+ String sourceDestination = SimpMessageHeaderAccessor .getDestination (headers );
145
+ if (sourceDestination == null || !checkDestination (sourceDestination , this .prefix )) {
145
146
return null ;
146
147
}
147
148
SimpMessageType messageType = SimpMessageHeaderAccessor .getMessageType (headers );
149
+ switch (messageType ) {
150
+ case SUBSCRIBE :
151
+ case UNSUBSCRIBE :
152
+ return parseSubscriptionMessage (message , sourceDestination );
153
+ case MESSAGE :
154
+ return parseMessage (headers , sourceDestination );
155
+ default :
156
+ return null ;
157
+ }
158
+ }
159
+
160
+ private ParseResult parseSubscriptionMessage (Message <?> message , String sourceDestination ) {
161
+ MessageHeaders headers = message .getHeaders ();
162
+ String sessionId = SimpMessageHeaderAccessor .getSessionId (headers );
163
+ if (sessionId == null ) {
164
+ logger .error ("No session id. Ignoring " + message );
165
+ return null ;
166
+ }
167
+ int prefixEnd = this .prefix .length () - 1 ;
168
+ String actualDestination = sourceDestination .substring (prefixEnd );
169
+ if (!this .keepLeadingSlash ) {
170
+ actualDestination = actualDestination .substring (1 );
171
+ }
148
172
Principal principal = SimpMessageHeaderAccessor .getUser (headers );
173
+ String user = (principal != null ? principal .getName () : null );
174
+ Set <String > sessionIds = Collections .singleton (sessionId );
175
+ return new ParseResult (sourceDestination , actualDestination , sourceDestination ,
176
+ sessionIds , user );
177
+ }
178
+
179
+ private ParseResult parseMessage (MessageHeaders headers , String sourceDestination ) {
180
+ int prefixEnd = this .prefix .length ();
181
+ int userEnd = sourceDestination .indexOf ('/' , prefixEnd );
182
+ Assert .isTrue (userEnd > 0 , "Expected destination pattern \" /user/{userId}/**\" " );
183
+ String actualDestination = sourceDestination .substring (userEnd );
184
+ String subscribeDestination = this .prefix .substring (0 , prefixEnd - 1 ) + actualDestination ;
185
+ String userName = sourceDestination .substring (prefixEnd , userEnd );
186
+ userName = StringUtils .replace (userName , "%2F" , "/" );
149
187
String sessionId = SimpMessageHeaderAccessor .getSessionId (headers );
150
- if (SimpMessageType .SUBSCRIBE .equals (messageType ) || SimpMessageType .UNSUBSCRIBE .equals (messageType )) {
151
- if (sessionId == null ) {
152
- logger .error ("No session id. Ignoring " + message );
153
- return null ;
154
- }
155
- int prefixEnd = this .prefix .length () - 1 ;
156
- String actualDestination = destination .substring (prefixEnd );
157
- if (!this .keepLeadingSlash ) {
158
- actualDestination = actualDestination .substring (1 );
159
- }
160
- String user = (principal != null ? principal .getName () : null );
161
- return new ParseResult (actualDestination , destination , Collections .singleton (sessionId ), user );
188
+ Set <String > sessionIds ;
189
+ if (userName .equals (sessionId )) {
190
+ userName = null ;
191
+ sessionIds = Collections .singleton (sessionId );
192
+ }
193
+ else {
194
+ sessionIds = getSessionIdsByUser (userName , sessionId );
195
+ }
196
+ if (!this .keepLeadingSlash ) {
197
+ actualDestination = actualDestination .substring (1 );
162
198
}
163
- else if (SimpMessageType .MESSAGE .equals (messageType )) {
164
- int prefixEnd = this .prefix .length ();
165
- int userEnd = destination .indexOf ('/' , prefixEnd );
166
- Assert .isTrue (userEnd > 0 , "Expected destination pattern \" /user/{userId}/**\" " );
167
- String actualDestination = destination .substring (userEnd );
168
- String subscribeDestination = this .prefix .substring (0 , prefixEnd - 1 ) + actualDestination ;
169
- String userName = destination .substring (prefixEnd , userEnd );
170
- userName = StringUtils .replace (userName , "%2F" , "/" );
171
- Set <String > sessionIds ;
172
- if (userName .equals (sessionId )) {
173
- userName = null ;
199
+ return new ParseResult (sourceDestination , actualDestination , subscribeDestination ,
200
+ sessionIds , userName );
201
+ }
202
+
203
+ private Set <String > getSessionIdsByUser (String userName , String sessionId ) {
204
+ Set <String > sessionIds ;
205
+ SimpUser user = this .userRegistry .getUser (userName );
206
+ if (user != null ) {
207
+ if (user .getSession (sessionId ) != null ) {
174
208
sessionIds = Collections .singleton (sessionId );
175
209
}
176
210
else {
177
- SimpUser user = this .userRegistry .getUser (userName );
178
- if (user != null ) {
179
- if (user .getSession (sessionId ) != null ) {
180
- sessionIds = Collections .singleton (sessionId );
181
- }
182
- else {
183
- Set <SimpSession > sessions = user .getSessions ();
184
- sessionIds = new HashSet <String >(sessions .size ());
185
- for (SimpSession session : sessions ) {
186
- sessionIds .add (session .getId ());
187
- }
188
- }
189
- }
190
- else {
191
- sessionIds = Collections .<String >emptySet ();
211
+ Set <SimpSession > sessions = user .getSessions ();
212
+ sessionIds = new HashSet <String >(sessions .size ());
213
+ for (SimpSession session : sessions ) {
214
+ sessionIds .add (session .getId ());
192
215
}
193
216
}
194
- if (!this .keepLeadingSlash ) {
195
- actualDestination = actualDestination .substring (1 );
196
- }
197
- return new ParseResult (actualDestination , subscribeDestination , sessionIds , userName );
198
217
}
199
218
else {
200
- return null ;
219
+ sessionIds = Collections . emptySet () ;
201
220
}
221
+ return sessionIds ;
202
222
}
203
223
204
224
protected boolean checkDestination (String destination , String requiredPrefix ) {
@@ -232,6 +252,8 @@ public String toString() {
232
252
*/
233
253
private static class ParseResult {
234
254
255
+ private final String sourceDestination ;
256
+
235
257
private final String actualDestination ;
236
258
237
259
private final String subscribeDestination ;
@@ -241,14 +263,21 @@ private static class ParseResult {
241
263
private final String user ;
242
264
243
265
244
- public ParseResult (String actualDest , String subscribeDest , Set <String > sessionIds , String user ) {
266
+ public ParseResult (String sourceDest , String actualDest , String subscribeDest ,
267
+ Set <String > sessionIds , String user ) {
268
+
269
+ this .sourceDestination = sourceDest ;
245
270
this .actualDestination = actualDest ;
246
271
this .subscribeDestination = subscribeDest ;
247
272
this .sessionIds = sessionIds ;
248
273
this .user = user ;
249
274
}
250
275
251
276
277
+ public String getSourceDestination () {
278
+ return this .sourceDestination ;
279
+ }
280
+
252
281
public String getActualDestination () {
253
282
return this .actualDestination ;
254
283
}
0 commit comments