Skip to content

Commit

Permalink
Basic text search support
Browse files Browse the repository at this point in the history
- only on root level
- does not search archives
  • Loading branch information
lukashinsch committed Mar 1, 2015
1 parent 49a12e5 commit a845f1c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
11 changes: 0 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ buildscript {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
// apply plugin: 'jacoco'
// apply plugin: 'com.github.kt3k.coveralls'


jar {
Expand Down Expand Up @@ -46,12 +44,3 @@ idea {
outputDir = file("$buildDir/classes/main/")
}
}

/*
jacocoTestReport {
reports {
xml.enabled = true // coveralls plugin depends on xml format report
html.enabled = true
}
}
*/
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.hinsch.spring.boot.actuator.logview;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
Expand All @@ -12,6 +13,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Path;
Expand Down Expand Up @@ -114,6 +116,36 @@ public void view(@PathVariable String filename, @RequestParam(required = false)
getFileProvider(path).streamContent(path, filename, response.getOutputStream());
}

@RequestMapping("/search")
public void search(@RequestParam String term, HttpServletResponse response) throws IOException {
Path folder = loggingPath(null);
List<FileEntry> files = getFileProvider(folder).getFileEntries(folder);
List<FileEntry> sortedFiles = sortFiles(files, SortBy.MODIFIED, false);

ServletOutputStream outputStream = response.getOutputStream();

sortedFiles.stream()
.filter(file -> file.getFileType().equals(FileType.FILE))
.forEach(file -> searchAndStreamFile(file, term, outputStream));
}

private void searchAndStreamFile(FileEntry fileEntry, String term, OutputStream outputStream) {
Path folder = loggingPath(null);
try {
List<String> lines = IOUtils.readLines(new FileInputStream(new File(folder.toFile().toString(), fileEntry.getFilename())))
.stream()
.filter(line -> line.contains(term))
.map(line -> "[" + fileEntry.getFilename() + "] " + line)
.collect(toList());
for (String line : lines) {
outputStream.write(line.getBytes());
outputStream.write(System.lineSeparator().getBytes());
}
} catch (IOException e) {
throw new RuntimeException("error reading file", e);
}
}

private void securityCheck(String filename) {
Assert.doesNotContain(filename, "..");
}
Expand Down
23 changes: 21 additions & 2 deletions lib/src/main/resources/templates/logview.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
  <head>
<title>Logfiles</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"/>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"/>
<style>
.form-group {
margin-right: 10px;
}
</style>
</head>
<body>
<div class="container">
Expand All @@ -11,7 +16,21 @@
</div>
<div class="panel panel-default">
<div class="panel-heading">
Current location ${currentFolder}
<div class="form-inline">
<form action="search" method="get">
<div class="form-group">
<label>Current location</label>
<p class="form-control-static">${currentFolder}</p>
</div>
<#if base == "">
<div class="form-group">
<label for="term">Search</label>
<input class="form-control" id="term" name="term" type="text"/>
</div>
<button class="btn btn-default">Search</button>
</#if>
</form>
</div>
</div>
<table class="table table-striped table-hover">
<thead>
Expand Down

0 comments on commit a845f1c

Please sign in to comment.