Browse Source

Refactor toString method in SockJsFrame

See gh-35510

Signed-off-by: KNU-K <knukang334@gmail.com>
pull/35551/head
KNU-K 3 months ago committed by rstoyanchev
parent
commit
e4a431e536
  1. 39
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.java

39
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.java

@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* Represents a SockJS frame. Provides factory methods to create SockJS frames. * Represents a SockJS frame. Provides factory methods to create SockJS frames.
@ -46,6 +45,11 @@ public class SockJsFrame {
private static final SockJsFrame CLOSE_ANOTHER_CONNECTION_OPEN_FRAME = private static final SockJsFrame CLOSE_ANOTHER_CONNECTION_OPEN_FRAME =
closeFrame(2010, "Another connection still open"); closeFrame(2010, "Another connection still open");
private static final String TRUNCATED_SUFFIX = "...(truncated)";
private static final String FRAME_PREFIX = "SockJsFrame content='";
private static final int MAX_CONTENT_PREVIEW_LENGTH = 80;
private final SockJsFrameType type; private final SockJsFrameType type;
@ -83,7 +87,6 @@ public class SockJsFrame {
} }
} }
/** /**
* Return the SockJS frame type. * Return the SockJS frame type.
*/ */
@ -119,7 +122,6 @@ public class SockJsFrame {
} }
} }
@Override @Override
public boolean equals(@Nullable Object other) { public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof SockJsFrame that && return (this == other || (other instanceof SockJsFrame that &&
@ -133,13 +135,32 @@ public class SockJsFrame {
@Override @Override
public String toString() { public String toString() {
String result = this.content; int contentLength = this.content.length();
if (result.length() > 80) { int truncatedLength = Math.min(contentLength, MAX_CONTENT_PREVIEW_LENGTH);
result = result.substring(0, 80) + "...(truncated)"; boolean isTruncated = contentLength > MAX_CONTENT_PREVIEW_LENGTH;
int suffixLength = isTruncated ? TRUNCATED_SUFFIX.length() : 0;
int initialCapacity = FRAME_PREFIX.length() + truncatedLength + suffixLength + 1;
StringBuilder sb = new StringBuilder(initialCapacity);
sb.append(FRAME_PREFIX);
for (int i = 0; i < truncatedLength; i++) {
char c = this.content.charAt(i);
switch (c) {
case '\n' -> sb.append("\\n");
case '\r' -> sb.append("\\r");
default -> sb.append(c);
}
} }
result = StringUtils.replace(result, "\n", "\\n");
result = StringUtils.replace(result, "\r", "\\r"); if (isTruncated) {
return "SockJsFrame content='" + result + "'"; sb.append(TRUNCATED_SUFFIX);
}
sb.append('\'');
return sb.toString();
} }

Loading…
Cancel
Save