From 73f32239388a4582d1d7c72d37a97b7954fb57ee Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Sat, 20 Jul 2024 08:12:33 -0500 Subject: [PATCH] Fixes #311 - Add ability to get a specific log using a REST API --- .../manorrock/sphynx/rest/LogResource.java | 54 ++++++++++++++++++- rest/src/test/java/it/LogIT.java | 19 +++++-- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/rest/src/main/java/com/manorrock/sphynx/rest/LogResource.java b/rest/src/main/java/com/manorrock/sphynx/rest/LogResource.java index d6c1d88..09a3b29 100644 --- a/rest/src/main/java/com/manorrock/sphynx/rest/LogResource.java +++ b/rest/src/main/java/com/manorrock/sphynx/rest/LogResource.java @@ -35,7 +35,11 @@ import jakarta.ws.rs.WebApplicationException; import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; +import static jakarta.ws.rs.core.Response.Status.NOT_FOUND; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import static java.lang.System.Logger.Level.ERROR; import java.util.ArrayList; import java.util.Arrays; @@ -46,7 +50,7 @@ * * @author Manfred Riem (mriem@manorrock.com) */ -@Path("job/{name}") +@Path("job/{jobName}/log") @RequestScoped public class LogResource { @@ -60,6 +64,52 @@ public class LogResource { */ protected File baseDirectory = new File(System.getProperty("user.home") + "/.manorrock/sphynx"); + /** + * Get the specific log. + * + * @param jobName the job name. + * @param logName the log name. + * @return the log. + */ + @GET + @Path("{logName}") + public String get( + @PathParam("jobName") String jobName, + @PathParam("logName") String logName) { + + StringBuilder result = new StringBuilder(); + + File logFile = new File(baseDirectory, + "jobs" + + File.separator + + jobName + + File.separator + + "logs" + + File.separator + + logName + + ".log"); + if (!logFile.exists()) { + LOGGER.log(ERROR, "Log file does not exist"); + throw new WebApplicationException(NOT_FOUND); + } + + try (BufferedInputStream input = new BufferedInputStream( + new FileInputStream(logFile))) { + while (input.available() > 0) { + int character = input.read(); + if (character == -1) { + break; + } + result.append((char) character); + } + } catch (IOException ex) { + LOGGER.log(ERROR, "I/O error occured", ex); + throw new WebApplicationException(INTERNAL_SERVER_ERROR); + } + + return result.toString(); + } + /** * List the logs. * @@ -68,7 +118,7 @@ public class LogResource { */ @GET @Produces(APPLICATION_JSON) - public List list(@PathParam("name") String jobName) { + public List list(@PathParam("jobName") String jobName) { File logsDirectory = new File(baseDirectory, "jobs" + File.separator diff --git a/rest/src/test/java/it/LogIT.java b/rest/src/test/java/it/LogIT.java index 5ae3ce2..234b693 100644 --- a/rest/src/test/java/it/LogIT.java +++ b/rest/src/test/java/it/LogIT.java @@ -1,5 +1,3 @@ -package it; - /* * Copyright (c) 2002-2024, Manorrock.com. All Rights Reserved. * @@ -29,26 +27,39 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ +package it; + import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; /** - * The integration tests for Jobs. + * The integration tests for Logs. * * @author Manfred Riem (mriem@manorrock.com) */ class LogIT { + @Test + void testGet() throws Exception { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest + .newBuilder(new URI("http://localhost:8080/rest/job/test/log/1234")) + .build(); + HttpResponse response = client.send(request, BodyHandlers.ofString()); + assertEquals(404, response.statusCode()); + } + @Test void testList() throws Exception { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest - .newBuilder(new URI("http://localhost:8080/rest/job/test")) + .newBuilder(new URI("http://localhost:8080/rest/job/test/log")) .build(); HttpResponse response = client.send(request, BodyHandlers.ofString()); System.out.println(response.body());