-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ([email protected]) | ||
*/ | ||
@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<String> list(@PathParam("name") String jobName) { | ||
public List<String> list(@PathParam("jobName") String jobName) { | ||
File logsDirectory = new File(baseDirectory, | ||
"jobs" | ||
+ File.separator | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
|
@@ -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()); | ||
|