From c9be8b6d76584bc426d2949f212e10eb3cb1ea1d Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Thu, 11 Apr 2024 13:14:11 +0200 Subject: [PATCH] Importing json should tolerate a BOM --- .../main/kotlin/org/jeudego/pairgoth/web/ImportServlet.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/web/ImportServlet.kt b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/web/ImportServlet.kt index 95dcd74..093b230 100644 --- a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/web/ImportServlet.kt +++ b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/web/ImportServlet.kt @@ -1,9 +1,12 @@ package org.jeudego.pairgoth.web import com.republicate.kson.Json +import org.apache.commons.io.input.BOMInputStream import org.jeudego.pairgoth.util.Upload import org.jeudego.pairgoth.view.ApiTool import org.jeudego.pairgoth.view.PairgothTool.Companion.EXAMPLES_DIRECTORY +import java.io.ByteArrayInputStream +import java.io.InputStreamReader import java.nio.charset.StandardCharsets import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest @@ -23,7 +26,8 @@ class ImportServlet: HttpServlet() { val bytes = uploads.first().second var apiResp: Json? = null if (name.endsWith(".tour")) { - val json = Json.parse(bytes.toString(StandardCharsets.UTF_8)) + val content = InputStreamReader(BOMInputStream(ByteArrayInputStream(bytes)), StandardCharsets.UTF_8).readText() + val json = Json.parse(content) if (json == null || !json.isObject) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST) } else {