1717package org .springframework .messaging .simp .stomp ;
1818
1919import java .nio .charset .Charset ;
20+ import java .security .Principal ;
2021import java .util .Arrays ;
2122import java .util .Collections ;
2223import java .util .List ;
@@ -163,11 +164,13 @@ else if (StompCommand.CONNECT.equals(command)) {
163164 }
164165
165166 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 );
168170 }
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 ());
171174 }
172175 trySetStompHeaderForSubscriptionId ();
173176 }
@@ -185,21 +188,24 @@ Map<String, List<String>> getNativeHeaders() {
185188 }
186189
187190 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 );
190194 }
191- if (getCommand () == null ) {
195+ StompCommand command = getCommand ();
196+ if (command == null ) {
192197 setHeader (COMMAND_HEADER , StompCommand .SEND );
193198 }
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 );
196201 }
197- return getCommand () ;
202+ return command ;
198203 }
199204
200205 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 );
203209 }
204210 StompCommand command = getCommand ();
205211 if ((command == null ) || StompCommand .SEND .equals (command )) {
@@ -273,7 +279,8 @@ public void setSubscriptionId(String subscriptionId) {
273279 private void trySetStompHeaderForSubscriptionId () {
274280 String subscriptionId = getSubscriptionId ();
275281 if (subscriptionId != null ) {
276- if (getCommand () != null && StompCommand .MESSAGE .equals (getCommand ())) {
282+ StompCommand command = getCommand ();
283+ if (command != null && StompCommand .MESSAGE .equals (command )) {
277284 setNativeHeader (STOMP_SUBSCRIPTION_HEADER , subscriptionId );
278285 }
279286 else {
@@ -286,10 +293,8 @@ private void trySetStompHeaderForSubscriptionId() {
286293 }
287294
288295 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 );
293298 }
294299
295300 public void setContentLength (int contentLength ) {
@@ -390,23 +395,26 @@ public void setVersion(String version) {
390395
391396 @ Override
392397 public String getShortLogMessage (Object payload ) {
393- if (StompCommand .SUBSCRIBE .equals (getCommand ())) {
398+ StompCommand command = getCommand ();
399+ if (StompCommand .SUBSCRIBE .equals (command )) {
394400 return "SUBSCRIBE " + getDestination () + " id=" + getSubscriptionId () + appendSession ();
395401 }
396- else if (StompCommand .UNSUBSCRIBE .equals (getCommand () )) {
402+ else if (StompCommand .UNSUBSCRIBE .equals (command )) {
397403 return "UNSUBSCRIBE id=" + getSubscriptionId () + appendSession ();
398404 }
399- else if (StompCommand .SEND .equals (getCommand () )) {
405+ else if (StompCommand .SEND .equals (command )) {
400406 return "SEND " + getDestination () + appendSession () + appendPayload (payload );
401407 }
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 ();
404411 }
405- else if (StompCommand .CONNECTED .equals (getCommand () )) {
412+ else if (StompCommand .CONNECTED .equals (command )) {
406413 return "CONNECTED heart-beat=" + Arrays .toString (getHeartbeat ()) + appendSession ();
407414 }
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 ();
410418 }
411419 else {
412420 return getDetailedLogMessage (payload );
@@ -444,11 +452,12 @@ private String appendPayload(Object payload) {
444452 "Expected byte array payload but got: " + ClassUtils .getQualifiedName (payload .getClass ()));
445453 }
446454 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 ()) {
449458 return contentType ;
450459 }
451- Charset charset = getContentType () .getCharset ();
460+ Charset charset = mimeType .getCharset ();
452461 charset = (charset != null ? charset : StompDecoder .UTF8_CHARSET );
453462 return (bytes .length < 80 ) ?
454463 contentType + " payload=" + new String (bytes , charset ) :
0 commit comments