Fixed encoding errors!

This commit is contained in:
Norbi Peti 2016-08-11 10:28:19 +02:00
parent cc6fdf38ce
commit 9ed20a0087
3 changed files with 6 additions and 11 deletions

View file

@ -31,9 +31,9 @@ import io.github.norbipeti.chat.server.page.Page;
public class IOHelper {
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 (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];
int count;
while ((count = bis.read(buffer)) != -1) {
@ -62,8 +62,7 @@ public class IOHelper {
}
public static String ReadFile(File file) throws FileNotFoundException, IOException {
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
LogManager.getLogger().debug(content); // TODO: FIx UTF-8 file reading
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8); // TODO: FIx UTF-8 file reading
return content;
}
@ -71,7 +70,7 @@ public class IOHelper {
try {
if (exchange.getRequestBody().available() == 0)
return "";
String content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1);
String content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.UTF_8);
return content;
} catch (Exception e) {
e.printStackTrace();

View file

@ -24,9 +24,6 @@ public class IndexPage extends Page {
@Override
public void handlePage(HttpExchange exchange) throws IOException {
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);
if (user == null)
IOHelper.SendModifiedPage(200, this, (doc) -> {

View file

@ -32,9 +32,7 @@ public class SendMessageAjaxPage extends Page {
}
JsonObject obj = IOHelper.GetPOSTJSON(exchange);
if (obj == null) {
IOHelper.SendResponse(400,
"<h1>400 Bad request</h1><p>Not a JSON string!</p><p>" + IOHelper.GetPOST(exchange) + "</p>",
exchange);
IOHelper.SendResponse(400, "JSONERROR: " + IOHelper.GetPOST(exchange), exchange);
// IOHelper.SendResponse(400, "JSONERROR", exchange);
return;
}
@ -70,6 +68,7 @@ public class SendMessageAjaxPage extends Page {
chunk.setConversation(conv);
Message msg = new Message();
msg.setSender(user);
LogManager.getLogger().debug(message);
msg.setMessage(message);
msg.setTime(new Date());
msg.setMessageChunk(chunk); // TODO: Store relations at one side or both