Обновить сервер и клиент для описания приложения

This commit is contained in:
AidarKC
2026-12-03 14:53:48 +03:00
parent 4427d1de97
commit ac66096e58
29 changed files with 543 additions and 185 deletions
+1
View File
@@ -0,0 +1 @@
OpenMindSoft LLC (Georgia)
@@ -33,6 +33,7 @@ public class Net_GetServerInfo_Handler implements JsonMessageHandler {
resp.setOrigin(CONFIG.getStringOrEmpty("server.info.origin"));
resp.setExtraInfo(CONFIG.getStringOrEmpty("server.info.extraInfo"));
resp.setSolanaCluster(CONFIG.getStringOrEmpty("solana.cluster"));
resp.setAboutText(ServerAboutText.readServerAbout(java.nio.file.Path.of(".")));
return resp;
}
@@ -0,0 +1,34 @@
package server.logic.ws_protocol.JSON.handlers.system;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
/** Публичное описание узла; читается заново, без перезапуска сервера. */
final class ServerAboutText {
static final int MAX_BYTES = 16 * 1024;
private ServerAboutText() {}
static String readServerAbout(Path directory) {
String text = read(directory.resolve("ABOUT.txt"));
return text.isBlank() ? "" : text;
}
static String read(Path file) {
try {
if (!Files.isRegularFile(file)) return "";
try (InputStream input = Files.newInputStream(file)) {
byte[] bytes = input.readNBytes(MAX_BYTES + 1);
if (bytes.length > MAX_BYTES) return "";
return StandardCharsets.UTF_8.newDecoder().decode(ByteBuffer.wrap(bytes)).toString();
}
} catch (IOException | SecurityException e) {
// Отсутствие публичного описания не ломает GetServerInfo.
return "";
}
}
}
@@ -11,4 +11,9 @@ import server.logic.ws_protocol.JSON.entyties.Net_Request;
* }
*/
public class Net_GetServerInfo_Request extends Net_Request {
// Совместимость с предыдущим клиентом: язык больше не влияет на описание узла.
private String language;
public String getLanguage() { return language; }
public void setLanguage(String language) { this.language = language; }
}
@@ -27,6 +27,10 @@ public class Net_GetServerInfo_Response extends Net_Response {
private String origin;
private String extraInfo;
private String solanaCluster;
private String aboutText;
public String getAboutText() { return aboutText; }
public void setAboutText(String aboutText) { this.aboutText = aboutText; }
public String getSolanaCluster() { return solanaCluster; }
public void setSolanaCluster(String cluster) { this.solanaCluster = cluster; }
@@ -0,0 +1,41 @@
package server.logic.ws_protocol.JSON.handlers.system;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ServerAboutTextTest {
@TempDir Path temp;
@Test void readsPublicTextAgainAfterEdit() throws Exception {
Path file = temp.resolve("ABOUT.txt");
assertEquals("", ServerAboutText.readServerAbout(temp));
Files.writeString(file, "OpenMindSoft LLC (Georgia)\nhttps://example.org/", StandardCharsets.UTF_8);
assertEquals("OpenMindSoft LLC (Georgia)\nhttps://example.org/", ServerAboutText.readServerAbout(temp));
Files.writeString(file, "Другой сервер", StandardCharsets.UTF_8);
assertEquals("Другой сервер", ServerAboutText.readServerAbout(temp));
}
@Test void rejectsOversizeInvalidUtf8AndDirectories() throws Exception {
Path file = temp.resolve("ABOUT.txt");
Files.write(file, new byte[ServerAboutText.MAX_BYTES + 1]);
assertEquals("", ServerAboutText.readServerAbout(temp));
Files.write(file, new byte[]{(byte) 0xC3, 0x28});
assertEquals("", ServerAboutText.readServerAbout(temp));
assertEquals("", ServerAboutText.read(temp));
}
@Test void readsOnlyAboutAndIgnoresLegacyReadmes() throws Exception {
Files.writeString(temp.resolve("README.en.txt"), "Old English");
Files.writeString(temp.resolve("README.ru.txt"), "Старый текст");
assertEquals("", ServerAboutText.readServerAbout(temp));
Files.writeString(temp.resolve("ABOUT.txt"), "OpenMindSoft LLC (Georgia)");
assertEquals("OpenMindSoft LLC (Georgia)", ServerAboutText.readServerAbout(temp));
Files.writeString(temp.resolve("ABOUT.txt"), " ");
assertEquals("", ServerAboutText.readServerAbout(temp));
}
}