Fixed encoding errors!
This commit is contained in:
parent
cc6fdf38ce
commit
9ed20a0087
3 changed files with 6 additions and 11 deletions
|
@ -31,9 +31,9 @@ import io.github.norbipeti.chat.server.page.Page;
|
||||||
|
|
||||||
public class IOHelper {
|
public class IOHelper {
|
||||||
public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException {
|
public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException {
|
||||||
exchange.sendResponseHeaders(code, content.length());
|
|
||||||
try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) {
|
try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) {
|
||||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes())) {
|
try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
|
||||||
|
exchange.sendResponseHeaders(code, bis.available());
|
||||||
byte[] buffer = new byte[512];
|
byte[] buffer = new byte[512];
|
||||||
int count;
|
int count;
|
||||||
while ((count = bis.read(buffer)) != -1) {
|
while ((count = bis.read(buffer)) != -1) {
|
||||||
|
@ -62,8 +62,7 @@ public class IOHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String ReadFile(File file) throws FileNotFoundException, IOException {
|
public static String ReadFile(File file) throws FileNotFoundException, IOException {
|
||||||
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
|
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8); // TODO: FIx UTF-8 file reading
|
||||||
LogManager.getLogger().debug(content); // TODO: FIx UTF-8 file reading
|
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +70,7 @@ public class IOHelper {
|
||||||
try {
|
try {
|
||||||
if (exchange.getRequestBody().available() == 0)
|
if (exchange.getRequestBody().available() == 0)
|
||||||
return "";
|
return "";
|
||||||
String content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1);
|
String content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.UTF_8);
|
||||||
return content;
|
return content;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -24,9 +24,6 @@ public class IndexPage extends Page {
|
||||||
@Override
|
@Override
|
||||||
public void handlePage(HttpExchange exchange) throws IOException {
|
public void handlePage(HttpExchange exchange) throws IOException {
|
||||||
final User user = IOHelper.GetLoggedInUser(exchange);
|
final User user = IOHelper.GetLoggedInUser(exchange);
|
||||||
/*
|
|
||||||
* final User user = new User(); user.setEmail("test@test.com"); user.setName("Norbi"); user.setId(3L);
|
|
||||||
*/
|
|
||||||
LogManager.getLogger().debug("Logged in user: " + user);
|
LogManager.getLogger().debug("Logged in user: " + user);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
IOHelper.SendModifiedPage(200, this, (doc) -> {
|
IOHelper.SendModifiedPage(200, this, (doc) -> {
|
||||||
|
|
|
@ -32,9 +32,7 @@ public class SendMessageAjaxPage extends Page {
|
||||||
}
|
}
|
||||||
JsonObject obj = IOHelper.GetPOSTJSON(exchange);
|
JsonObject obj = IOHelper.GetPOSTJSON(exchange);
|
||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
IOHelper.SendResponse(400,
|
IOHelper.SendResponse(400, "JSONERROR: " + IOHelper.GetPOST(exchange), exchange);
|
||||||
"<h1>400 Bad request</h1><p>Not a JSON string!</p><p>" + IOHelper.GetPOST(exchange) + "</p>",
|
|
||||||
exchange);
|
|
||||||
// IOHelper.SendResponse(400, "JSONERROR", exchange);
|
// IOHelper.SendResponse(400, "JSONERROR", exchange);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -70,6 +68,7 @@ public class SendMessageAjaxPage extends Page {
|
||||||
chunk.setConversation(conv);
|
chunk.setConversation(conv);
|
||||||
Message msg = new Message();
|
Message msg = new Message();
|
||||||
msg.setSender(user);
|
msg.setSender(user);
|
||||||
|
LogManager.getLogger().debug(message);
|
||||||
msg.setMessage(message);
|
msg.setMessage(message);
|
||||||
msg.setTime(new Date());
|
msg.setTime(new Date());
|
||||||
msg.setMessageChunk(chunk); // TODO: Store relations at one side or both
|
msg.setMessageChunk(chunk); // TODO: Store relations at one side or both
|
||||||
|
|
Loading…
Reference in a new issue