17
17
package org .springframework .messaging .simp .stomp ;
18
18
19
19
import java .nio .charset .Charset ;
20
+ import java .security .Principal ;
20
21
import java .util .Arrays ;
21
22
import java .util .Collections ;
22
23
import java .util .List ;
@@ -163,11 +164,13 @@ else if (StompCommand.CONNECT.equals(command)) {
163
164
}
164
165
165
166
void updateStompHeadersFromSimpMessageHeaders () {
166
- if (getDestination () != null ) {
167
- setNativeHeader (STOMP_DESTINATION_HEADER , getDestination ());
167
+ String destination = getDestination ();
168
+ if (destination != null ) {
169
+ setNativeHeader (STOMP_DESTINATION_HEADER , destination );
168
170
}
169
- if (getContentType () != null ) {
170
- setNativeHeader (STOMP_CONTENT_TYPE_HEADER , getContentType ().toString ());
171
+ MimeType contentType = getContentType ();
172
+ if (contentType != null ) {
173
+ setNativeHeader (STOMP_CONTENT_TYPE_HEADER , contentType .toString ());
171
174
}
172
175
trySetStompHeaderForSubscriptionId ();
173
176
}
@@ -185,21 +188,24 @@ Map<String, List<String>> getNativeHeaders() {
185
188
}
186
189
187
190
public StompCommand updateStompCommandAsClientMessage () {
188
- if (getMessageType () != SimpMessageType .MESSAGE ) {
189
- throw new IllegalStateException ("Unexpected message type " + getMessageType ());
191
+ SimpMessageType messageType = getMessageType ();
192
+ if (messageType != SimpMessageType .MESSAGE ) {
193
+ throw new IllegalStateException ("Unexpected message type " + messageType );
190
194
}
191
- if (getCommand () == null ) {
195
+ StompCommand command = getCommand ();
196
+ if (command == null ) {
192
197
setHeader (COMMAND_HEADER , StompCommand .SEND );
193
198
}
194
- else if (!getCommand () .equals (StompCommand .SEND )) {
195
- throw new IllegalStateException ("Unexpected STOMP command " + getCommand () );
199
+ else if (!command .equals (StompCommand .SEND )) {
200
+ throw new IllegalStateException ("Unexpected STOMP command " + command );
196
201
}
197
- return getCommand () ;
202
+ return command ;
198
203
}
199
204
200
205
public void updateStompCommandAsServerMessage () {
201
- if (getMessageType () != SimpMessageType .MESSAGE ) {
202
- throw new IllegalStateException ("Unexpected message type " + getMessageType ());
206
+ SimpMessageType messageType = getMessageType ();
207
+ if (messageType != SimpMessageType .MESSAGE ) {
208
+ throw new IllegalStateException ("Unexpected message type " + messageType );
203
209
}
204
210
StompCommand command = getCommand ();
205
211
if ((command == null ) || StompCommand .SEND .equals (command )) {
@@ -273,7 +279,8 @@ public void setSubscriptionId(String subscriptionId) {
273
279
private void trySetStompHeaderForSubscriptionId () {
274
280
String subscriptionId = getSubscriptionId ();
275
281
if (subscriptionId != null ) {
276
- if (getCommand () != null && StompCommand .MESSAGE .equals (getCommand ())) {
282
+ StompCommand command = getCommand ();
283
+ if (command != null && StompCommand .MESSAGE .equals (command )) {
277
284
setNativeHeader (STOMP_SUBSCRIPTION_HEADER , subscriptionId );
278
285
}
279
286
else {
@@ -286,10 +293,8 @@ private void trySetStompHeaderForSubscriptionId() {
286
293
}
287
294
288
295
public Integer getContentLength () {
289
- if (containsNativeHeader (STOMP_CONTENT_LENGTH_HEADER )) {
290
- return Integer .valueOf (getFirstNativeHeader (STOMP_CONTENT_LENGTH_HEADER ));
291
- }
292
- return null ;
296
+ String header = getFirstNativeHeader (STOMP_CONTENT_LENGTH_HEADER );
297
+ return (header != null ? Integer .valueOf (header ) : null );
293
298
}
294
299
295
300
public void setContentLength (int contentLength ) {
@@ -390,23 +395,26 @@ public void setVersion(String version) {
390
395
391
396
@ Override
392
397
public String getShortLogMessage (Object payload ) {
393
- if (StompCommand .SUBSCRIBE .equals (getCommand ())) {
398
+ StompCommand command = getCommand ();
399
+ if (StompCommand .SUBSCRIBE .equals (command )) {
394
400
return "SUBSCRIBE " + getDestination () + " id=" + getSubscriptionId () + appendSession ();
395
401
}
396
- else if (StompCommand .UNSUBSCRIBE .equals (getCommand () )) {
402
+ else if (StompCommand .UNSUBSCRIBE .equals (command )) {
397
403
return "UNSUBSCRIBE id=" + getSubscriptionId () + appendSession ();
398
404
}
399
- else if (StompCommand .SEND .equals (getCommand () )) {
405
+ else if (StompCommand .SEND .equals (command )) {
400
406
return "SEND " + getDestination () + appendSession () + appendPayload (payload );
401
407
}
402
- else if (StompCommand .CONNECT .equals (getCommand ())) {
403
- return "CONNECT" + (getUser () != null ? " user=" + getUser ().getName () : "" ) + appendSession ();
408
+ else if (StompCommand .CONNECT .equals (command )) {
409
+ Principal user = getUser ();
410
+ return "CONNECT" + (user != null ? " user=" + user .getName () : "" ) + appendSession ();
404
411
}
405
- else if (StompCommand .CONNECTED .equals (getCommand () )) {
412
+ else if (StompCommand .CONNECTED .equals (command )) {
406
413
return "CONNECTED heart-beat=" + Arrays .toString (getHeartbeat ()) + appendSession ();
407
414
}
408
- else if (StompCommand .DISCONNECT .equals (getCommand ())) {
409
- return "DISCONNECT" + (getReceipt () != null ? " receipt=" + getReceipt () : "" ) + appendSession ();
415
+ else if (StompCommand .DISCONNECT .equals (command )) {
416
+ String receipt = getReceipt ();
417
+ return "DISCONNECT" + (receipt != null ? " receipt=" + receipt : "" ) + appendSession ();
410
418
}
411
419
else {
412
420
return getDetailedLogMessage (payload );
@@ -444,11 +452,12 @@ private String appendPayload(Object payload) {
444
452
"Expected byte array payload but got: " + ClassUtils .getQualifiedName (payload .getClass ()));
445
453
}
446
454
byte [] bytes = (byte []) payload ;
447
- String contentType = (getContentType () != null ? " " + getContentType ().toString () : "" );
448
- if (bytes .length == 0 || getContentType () == null || !isReadableContentType ()) {
455
+ MimeType mimeType = getContentType ();
456
+ String contentType = (mimeType != null ? " " + mimeType .toString () : "" );
457
+ if (bytes .length == 0 || mimeType == null || !isReadableContentType ()) {
449
458
return contentType ;
450
459
}
451
- Charset charset = getContentType () .getCharset ();
460
+ Charset charset = mimeType .getCharset ();
452
461
charset = (charset != null ? charset : StompDecoder .UTF8_CHARSET );
453
462
return (bytes .length < 80 ) ?
454
463
contentType + " payload=" + new String (bytes , charset ) :
0 commit comments