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; @@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets;
import org.jspecify.annotations.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Represents a SockJS frame. Provides factory methods to create SockJS frames.
@ -46,6 +45,11 @@ public class SockJsFrame { @@ -46,6 +45,11 @@ public class SockJsFrame {
private static final SockJsFrame CLOSE_ANOTHER_CONNECTION_OPEN_FRAME =
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;
@ -83,7 +87,6 @@ public class SockJsFrame { @@ -83,7 +87,6 @@ public class SockJsFrame {
}
}
/**
* Return the SockJS frame type.
*/
@ -119,7 +122,6 @@ public class SockJsFrame { @@ -119,7 +122,6 @@ public class SockJsFrame {
}
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof SockJsFrame that &&
@ -133,13 +135,32 @@ public class SockJsFrame { @@ -133,13 +135,32 @@ public class SockJsFrame {
@Override
public String toString() {
String result = this.content;
if (result.length() > 80) {
result = result.substring(0, 80) + "...(truncated)";
int contentLength = this.content.length();
int truncatedLength = Math.min(contentLength, MAX_CONTENT_PREVIEW_LENGTH);
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);
}
}
if (isTruncated) {
sb.append(TRUNCATED_SUFFIX);
}
result = StringUtils.replace(result, "\n", "\\n");
result = StringUtils.replace(result, "\r", "\\r");
return "SockJsFrame content='" + result + "'";
sb.append('\'');
return sb.toString();
}

Loading…
Cancel
Save