Skip to content

Commit

Permalink
Fixes #311 - Add ability to get a specific log using a REST API (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jul 20, 2024
1 parent 628283b commit 4a7e03a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
54 changes: 52 additions & 2 deletions rest/src/main/java/com/manorrock/sphynx/rest/LogResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,7 +50,7 @@
*
* @author Manfred Riem ([email protected])
*/
@Path("job/{name}")
@Path("job/{jobName}/log")
@RequestScoped
public class LogResource {

Expand All @@ -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.
*
Expand All @@ -68,7 +118,7 @@ public class LogResource {
*/
@GET
@Produces(APPLICATION_JSON)
public List<String> list(@PathParam("name") String jobName) {
public List<String> list(@PathParam("jobName") String jobName) {
File logsDirectory = new File(baseDirectory,
"jobs"
+ File.separator
Expand Down
19 changes: 15 additions & 4 deletions rest/src/test/java/it/LogIT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package it;

/*
* Copyright (c) 2002-2024, Manorrock.com. All Rights Reserved.
*
Expand Down Expand Up @@ -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 ([email protected])
*/
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<String> 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<String> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.body());
Expand Down

0 comments on commit 4a7e03a

Please sign in to comment.