Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal Report Viewer: host and use the the report viewer locally #1176

Merged
merged 55 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
fd89367
Testing local report viewer
dfuchss Jul 5, 2023
739fa34
Update to Java JDK 20
dfuchss Jul 16, 2023
6807daf
Merge branch 'develop' into feature/internal-web-viewer
dfuchss Jul 16, 2023
a52b059
Merge branch 'develop' into feature/internal-web-viewer
tsaglam Sep 29, 2023
5d2c8df
Implemented routing for internal report viewer.
TwoOfTwelve Oct 12, 2023
7e2d1bd
Implemented server side for local mode.
TwoOfTwelve Oct 12, 2023
e27f632
Fixed 404 response codes for local mode.
TwoOfTwelve Oct 12, 2023
3085de0
frontend logic for local mode
Kr0nox Oct 12, 2023
0defe37
Removed accidental changes from package.json
TwoOfTwelve Oct 12, 2023
5a27848
remove unneeded dependencies
Kr0nox Oct 16, 2023
3a5b0be
Merge remote-tracking branch 'origin/develop' into feature/internal-w…
Kr0nox Oct 16, 2023
853ff7f
Changed local report viewer to return index html for every path, that…
TwoOfTwelve Oct 18, 2023
c794eff
Merge remote-tracking branch 'origin/feature/internal-web-viewer' int…
TwoOfTwelve Oct 18, 2023
4c8ea19
Fixed small error for local mode.
TwoOfTwelve Oct 18, 2023
30ac721
fix new tabs on comparison
Kr0nox Oct 21, 2023
288716f
Moved compiling the report-viewer into a profile.
TwoOfTwelve Oct 31, 2023
d65afab
Merge remote-tracking branch 'origin/feature/internal-web-viewer' int…
TwoOfTwelve Oct 31, 2023
e9f902d
fix tests
Kr0nox Nov 8, 2023
e36eb01
Merge remote-tracking branch 'origin/develop' into feature/internal-w…
Kr0nox Nov 30, 2023
4f055d0
fix report viewer build
Kr0nox Nov 30, 2023
daede42
fix new tabs
Kr0nox Nov 30, 2023
71d094c
Added javadoc to internal report viewer and moved the resource declar…
TwoOfTwelve Dec 1, 2023
a207694
Added logging for not existing resource url.
TwoOfTwelve Dec 5, 2023
dc58ad6
Merge branch 'develop' into feature/internal-web-viewer
dfuchss Dec 6, 2023
31b818b
Changed port to 1996.
TwoOfTwelve Dec 11, 2023
aadeae3
Merge remote-tracking branch 'origin/feature/internal-web-viewer' int…
TwoOfTwelve Dec 11, 2023
f9d32d2
Merge remote-tracking branch 'origin/develop' into feature/internal-w…
Kr0nox Dec 11, 2023
10a9453
Added port option for internal report viewer
TwoOfTwelve Dec 12, 2023
3c84512
Merge remote-tracking branch 'origin/feature/internal-web-viewer' int…
TwoOfTwelve Dec 12, 2023
e4fa97f
Merge remote-tracking branch 'origin/develop' into feature/internal-w…
dfuchss Dec 14, 2023
86f0716
Build report viewer only if needed.
dfuchss Dec 14, 2023
b69cd36
Merge branch 'develop' into feature/internal-web-viewer
TwoOfTwelve Jan 23, 2024
52bf30b
fix prettier
Kr0nox Jan 23, 2024
1dcc4b5
fix loading index html file
Kr0nox Jan 23, 2024
4b9c925
file name in tab
Kr0nox Jan 23, 2024
d4920db
fix anonymous propagation
Kr0nox Jan 23, 2024
7956543
Fixed some minor code style issues in the internal report viewer.
TwoOfTwelve Feb 6, 2024
ab6ae74
Merge branch 'develop' into feature/internal-web-viewer
TwoOfTwelve Feb 6, 2024
34c177b
Merge remote-tracking branch 'origin/feature/internal-web-viewer' int…
TwoOfTwelve Feb 6, 2024
ccaddce
add example zip name again
Kr0nox Feb 6, 2024
7b724bb
add node js link to docs
Kr0nox Feb 6, 2024
70a449f
Written tests for internal server.
TwoOfTwelve Feb 9, 2024
24fba7e
Merge remote-tracking branch 'origin/feature/internal-web-viewer' int…
TwoOfTwelve Feb 9, 2024
889cc05
Improved clarity for HttpRequestMethod
TwoOfTwelve Feb 9, 2024
0464d4f
Merge remote-tracking branch 'origin/develop' into feature/internal-w…
Kr0nox Feb 13, 2024
93b6655
Added port lookup.
TwoOfTwelve Feb 13, 2024
1f27130
spotless
TwoOfTwelve Feb 13, 2024
6fe98ad
Merge remote-tracking branch 'origin/develop' into feature/internal-w…
Kr0nox Feb 14, 2024
13d40b6
Clean up logging for internal server and remove unnecessary index.html
TwoOfTwelve Feb 14, 2024
c198274
Fixed error due to renaming of enum constant.
TwoOfTwelve Feb 14, 2024
c3fcf62
Added more tests for RoutingTree
TwoOfTwelve Feb 16, 2024
00d7d8c
Fixed log message if port is not available.
TwoOfTwelve Feb 16, 2024
cec3c6d
Sonarcloud issues
TwoOfTwelve Feb 20, 2024
54d8474
Merge remote-tracking branch 'origin/develop' into feature/internal-w…
TwoOfTwelve Feb 20, 2024
d4303bc
Code style and documentation
TwoOfTwelve Feb 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: 17
java-version: 20
distribution: 'temurin'

- name: Run Tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: '17'
java-version: '20'
distribution: 'temurin'
- name: Set maven settings.xml
uses: whelk-io/maven-settings-xml-action@v21
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
java-version: 20

- name: Cache SonarCloud packages
uses: actions/cache@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
java-version: 20

- name: Cache SonarCloud packages
uses: actions/cache@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/spotless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: 17
java-version: 20
distribution: 'temurin'

- name: Check with Spotless
Expand Down
62 changes: 62 additions & 0 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<version>${revision}</version>
</parent>
<artifactId>cli</artifactId>

<dependencies>
<!-- IMPORTANT: For Coverage testing, you have to add dependencies to the coverage-report project ! -->

Expand Down Expand Up @@ -123,8 +124,20 @@
<artifactId>picocli</artifactId>
<version>4.7.5</version>
</dependency>

<dependency>
dfuchss marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.23.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<targetPath>report-viewer</targetPath>
<directory>../report-viewer/dist</directory>
</resource>
</resources>
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
Expand Down Expand Up @@ -156,4 +169,53 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>build-report-viewer</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>npm install</id>
<goals>
<goal>exec</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<executable>npm</executable>
<workingDirectory>../report-viewer</workingDirectory>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>npm build</id>
<goals>
<goal>exec</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<executable>npm</executable>
<workingDirectory>../report-viewer</workingDirectory>
<arguments>
<argument>run</argument>
<argument>build</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
38 changes: 33 additions & 5 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import static picocli.CommandLine.Model.UsageMessageSpec.SECTION_KEY_OPTION_LIST;
import static picocli.CommandLine.Model.UsageMessageSpec.SECTION_KEY_SYNOPSIS;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -20,6 +25,7 @@
import de.jplag.JPlagResult;
import de.jplag.Language;
import de.jplag.cli.logger.CollectedLoggerFactory;
import de.jplag.cli.server.ReportViewer;
import de.jplag.clustering.ClusteringOptions;
import de.jplag.clustering.Preprocessing;
import de.jplag.exceptions.ExitException;
Expand Down Expand Up @@ -74,12 +80,13 @@ public static void main(String[] args) {
ParseResult parseResult = cli.parseOptions(args);

if (!parseResult.isUsageHelpRequested() && !(parseResult.subcommand() != null && parseResult.subcommand().isUsageHelpRequested())) {
JPlagOptions options = cli.buildOptionsFromArguments(parseResult);
JPlagResult result = JPlag.run(options);
ReportObjectFactory reportObjectFactory = new ReportObjectFactory();
reportObjectFactory.createAndSaveReport(result, cli.getResultFolder());
switch (cli.options.mode) {
case RUN -> cli.runJPlag(parseResult);
case VIEWER -> cli.runViewer(null);
case RUN_AND_VIEW -> cli.runViewer(cli.runJPlag(parseResult));
}
}
} catch (ExitException exception) {
} catch (ExitException | IOException exception) {
logger.error(exception.getMessage()); // do not pass exception here to keep log clean
finalizeLogger();
System.exit(1);
Expand Down Expand Up @@ -110,6 +117,27 @@ public CLI() {
this.commandLine.setAllowSubcommandsAsOptionParameters(true);
}

public File runJPlag(ParseResult parseResult) throws ExitException {
JPlagOptions options = buildOptionsFromArguments(parseResult);
JPlagResult result = JPlag.run(options);
ReportObjectFactory reportObjectFactory = new ReportObjectFactory();
return reportObjectFactory.createAndSaveReport(result, getResultFolder());
}

public void runViewer(File zipFile) throws IOException {
ReportViewer reportViewer = new ReportViewer(zipFile);
int port = reportViewer.start();
logger.info("ReportViewer started on port http://localhost:{}", port);
Desktop.getDesktop().browse(URI.create("http://localhost:" + port + "/"));

Terminal terminal = TerminalBuilder.terminal();
terminal.writer().print("Press any key to exit...");
terminal.writer().flush();
terminal.enterRawMode();
terminal.reader().read();
reportViewer.stop();
}

private List<CommandSpec> buildSubcommands() {
return LanguageLoader.getAllAvailableLanguages().values().stream().map(language -> {
CommandSpec command = CommandSpec.create().name(language.getIdentifier());
Expand Down
3 changes: 3 additions & 0 deletions cli/src/main/java/de/jplag/cli/CliOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class CliOptions implements Runnable {
"--result-directory"}, description = "Name of the directory in which the comparison results will be stored (default: result)%n")
public String resultFolder = "results";

@Option(names = {"--mode"}, description = "The mode to run jplag in")
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
public JPlagMode mode = JPlagMode.RUN_AND_VIEW;
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved

@ArgGroup(heading = "Advanced%n", exclusive = false)
public Advanced advanced = new Advanced();

Expand Down
7 changes: 7 additions & 0 deletions cli/src/main/java/de/jplag/cli/JPlagMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.jplag.cli;

public enum JPlagMode {
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
RUN,
VIEWER,
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
RUN_AND_VIEW
}
31 changes: 31 additions & 0 deletions cli/src/main/java/de/jplag/cli/server/ContentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.jplag.cli.server;

public enum ContentType {
HTML("text/html; charset=utf-8"),
JS("application/javascript; charset=utf-8"),
CSS("text/css; charset=utf-8"),
PNG("image/png"),
PLAIN("text/plain; charset=utf-8"),
ZIP("application/zip");

private final String value;

ContentType(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public static ContentType fromPath(String path) {
return switch (path.substring(path.lastIndexOf('.'))) {
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
case ".html" -> ContentType.HTML;
case ".js" -> ContentType.JS;
case ".css" -> ContentType.CSS;
case ".png" -> ContentType.PNG;
case ".zip" -> ContentType.ZIP;
default -> ContentType.PLAIN;
};
}
}
26 changes: 26 additions & 0 deletions cli/src/main/java/de/jplag/cli/server/HttpMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.jplag.cli.server;

public enum HttpMethod {
GET("GET"),
POST("POST");

private final String name;

HttpMethod(String name) {
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
this.name = name;
}

public String getName() {
return name;
}

public static HttpMethod fromName(String name) {
for (HttpMethod value : HttpMethod.values()) {
if (value.name.equals(name)) {
return value;
}
}

return null;
}
}
86 changes: 86 additions & 0 deletions cli/src/main/java/de/jplag/cli/server/ReportViewer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package de.jplag.cli.server;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class ReportViewer implements HttpHandler {
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
private static final Logger logger = LoggerFactory.getLogger(ReportViewer.class);
private static final int SUCCESS_RESPONSE = 200;
private static final int NOT_FOUND_RESPONSE = 404;

private final RoutingTree routingTree;

private HttpServer server;

public ReportViewer(File zipFile) throws IOException {
this.routingTree = new RoutingTree();

this.routingTree.insertRouting("", new RoutingResources("report-viewer").or(new RoutingAlias("index.html")));
this.routingTree.insertRouting("results.zip", new RoutingStaticFile(zipFile, ContentType.ZIP));
tsaglam marked this conversation as resolved.
Show resolved Hide resolved
}

public int start() throws IOException {
if (server != null) {
throw new IllegalStateException("Server already started");
}
server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
server.createContext("/", this);
server.setExecutor(null);
server.start();

return server.getAddress().getPort();
}

public void stop() {
server.stop(0);
}

public void handle(HttpExchange exchange) throws IOException {
RoutingPath path = new RoutingPath(exchange.getRequestURI().getPath());
Pair<RoutingPath, Routing> resolved = this.routingTree.resolveRouting(path);
HttpMethod method = HttpMethod.fromName(exchange.getRequestMethod());

if (resolved == null || !ArrayUtils.contains(resolved.getRight().allowedMethods(), method)) {
exchange.sendResponseHeaders(NOT_FOUND_RESPONSE, 0);
exchange.close();
return;
}

logger.debug("Serving {}", path);

ResponseData responseData = resolved.getRight().fetchData(resolved.getLeft(), exchange, this);
if (responseData == null) {
exchange.sendResponseHeaders(NOT_FOUND_RESPONSE, 0);
exchange.close();
return;
}

InputStream inputStream = responseData.stream();

if (responseData.contentType() != null) {
exchange.getResponseHeaders().set("Content-Type", responseData.contentType().getValue());
}
exchange.sendResponseHeaders(SUCCESS_RESPONSE, responseData.size());

inputStream.transferTo(exchange.getResponseBody());
exchange.getResponseBody().flush();
exchange.getResponseBody().close();
inputStream.close();
}

public RoutingTree getRoutingTree() {
return routingTree;
}
}
Loading
Loading