diff options
author | Ola Aunrønning <ola.aunroe@gmail.com> | 2018-09-10 15:58:51 +0200 |
---|---|---|
committer | Ola Aunrønning <ola.aunroe@gmail.com> | 2018-09-10 15:58:51 +0200 |
commit | dd4f6af83ca7374d6458b4e31d2706a8684c43c8 (patch) | |
tree | 7b8c97014dcbe7661d7d6fe74a6d5e38b51146a4 /container-core | |
parent | 44053e445e8f893514667325be40613830056e6e (diff) |
Check if container is running on logserver
Diffstat (limited to 'container-core')
3 files changed, 22 insertions, 28 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java b/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java index 474d7d7321a..4183b642af1 100644 --- a/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java +++ b/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java @@ -1,12 +1,15 @@ package com.yahoo.container.handler; -import com.fasterxml.jackson.core.JsonFactory; import com.google.inject.Inject; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; +import org.json.JSONException; +import org.json.JSONObject; + import java.io.IOException; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.util.concurrent.Executor; public class LogHandler extends ThreadedHttpRequestHandler { @@ -20,11 +23,22 @@ public class LogHandler extends ThreadedHttpRequestHandler { @Override public HttpResponse handle(HttpRequest request) { + JSONObject logJson; + try { + logJson = LogReader.readLogs(LOG_DIRECTORY); + } catch (IOException | JSONException e) { + return new HttpResponse(404) { + @Override + public void render(OutputStream outputStream) {} + }; + } return new HttpResponse(200) { @Override public void render(OutputStream outputStream) throws IOException { - LogReader.writeToOutputStream(LOG_DIRECTORY, outputStream); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); + outputStreamWriter.write(logJson.toString()); + outputStreamWriter.close(); } }; } diff --git a/container-core/src/main/java/com/yahoo/container/handler/LogReader.java b/container-core/src/main/java/com/yahoo/container/handler/LogReader.java index e35ea748c0e..eb00446dd0e 100644 --- a/container-core/src/main/java/com/yahoo/container/handler/LogReader.java +++ b/container-core/src/main/java/com/yahoo/container/handler/LogReader.java @@ -1,32 +1,20 @@ package com.yahoo.container.handler; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonGenerator; -import org.apache.commons.codec.binary.Base64; import org.json.JSONException; import org.json.JSONObject; import javax.xml.bind.DatatypeConverter; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.nio.file.Files; public class LogReader { - protected static void writeToOutputStream(String logDirectory, OutputStream outputStream) throws IOException { - OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); + protected static JSONObject readLogs(String logDirectory) throws IOException, JSONException { JSONObject json = new JSONObject(); File root = new File(logDirectory); - try { - traverse_folder(root, json); - } catch (JSONException e) { - outputStreamWriter.write("Failed to create log JSON"); - } - outputStreamWriter.write(json.toString()); - outputStreamWriter.close(); + traverse_folder(root, json); + return json; } private static void traverse_folder(File root, JSONObject json) throws IOException, JSONException { diff --git a/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java b/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java index ff6ea74a411..e5302ee43ee 100644 --- a/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java +++ b/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java @@ -1,5 +1,6 @@ package com.yahoo.container.handler; +import org.json.JSONObject; import org.junit.Before; import org.junit.Test; @@ -19,18 +20,9 @@ public class LogReaderTest { @Test public void testThatFilesAreWrittenCorrectlyToOutputStream() throws Exception{ String logDirectory = "src/test/resources/logfolder/"; - LogReader.writeToOutputStream(logDirectory, outputStream); + JSONObject json = LogReader.readLogs(logDirectory); String expected = "{\"subfolder\":{\"log2.log\":\"VGhpcyBpcyBhbm90aGVyIGxvZyBmaWxl\"},\"log1.log\":\"VGhpcyBpcyBvbmUgbG9nIGZpbGU=\"}"; - String actual = new String(outputStream.toByteArray()); - assertEquals(expected, actual); - } - - @Test - public void testNothingISWrittenToOutputStreamWithEmptyLogFolder() throws Exception { - String logDirectory = "src/test/resources/emptylogfolder/"; - LogReader.writeToOutputStream(logDirectory, outputStream); - String expected = "{}"; - String actual = new String(outputStream.toByteArray()); + String actual = json.toString(); assertEquals(expected, actual); } }
\ No newline at end of file |