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 1 commit
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
42 changes: 42 additions & 0 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
</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 @@ -154,6 +160,42 @@
<finalName>jplag-${project.version}</finalName>
</configuration>
</plugin>
<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>
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
</plugins>
</build>
</project>
1 change: 1 addition & 0 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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
95 changes: 0 additions & 95 deletions cli/src/main/java/de/jplag/cli/ReportViewer.java

This file was deleted.

14 changes: 14 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,14 @@
package de.jplag.cli.server;

public enum ContentType {
;
private final String value;

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

public String getValue() {
return value;
}
}
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;
}
}
79 changes: 79 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,79 @@
package de.jplag.cli.server;

import java.io.IOException;
import java.io.InputStream;
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 METHOD_NOT_ALLOWED_RESPONSE = 405;
private static final int NOT_FOUND_RESPONSE = 404;

private final RoutingTree routingTree;

private HttpServer server;

public ReportViewer() {
this.routingTree = new RoutingTree();

this.routingTree.insertRouting(new RoutingPath(""), new RoutingResources("report-viewer"));
}

public int start() throws IOException {
if (server != null) {
throw new IllegalStateException("Server already started");
}
server = HttpServer.create(new InetSocketAddress(0), 0);
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 {
if (!exchange.getRequestMethod().equals("GET")) {
exchange.sendResponseHeaders(METHOD_NOT_ALLOWED_RESPONSE, 0);
exchange.getResponseBody().close();
return;
}

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);

Pair<InputStream, ContentType> data = resolved.getRight().fetchData(resolved.getLeft(), exchange, this);

if (data.getRight() != null) {
exchange.getResponseHeaders().set("Content-Type", data.getRight().getValue());
}
exchange.sendResponseHeaders(SUCCESS_RESPONSE, 0);

data.getLeft().transferTo(exchange.getResponseBody());
exchange.getResponseBody().flush();
exchange.getResponseBody().close();
data.getLeft().close();
}
}
15 changes: 15 additions & 0 deletions cli/src/main/java/de/jplag/cli/server/Routing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.jplag.cli.server;

import java.io.InputStream;

import org.apache.commons.lang3.tuple.Pair;

import com.sun.net.httpserver.HttpExchange;

public interface Routing {
default HttpMethod[] allowedMethods() {
return new HttpMethod[] {HttpMethod.GET};
}

Pair<InputStream, ContentType> fetchData(RoutingPath subPath, HttpExchange request, ReportViewer viewer);
}
49 changes: 49 additions & 0 deletions cli/src/main/java/de/jplag/cli/server/RoutingPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.jplag.cli.server;

public class RoutingPath {
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
private final String[] components;
private final int offset;

public RoutingPath(String path) {
this.components = path.split("/");
this.offset = 0;
}

private RoutingPath(String[] components, int offset) {
this.components = components;
this.offset = offset;
}

public String head() {
return components[offset];
}

public RoutingPath tail() {
if (!hasTail()) {
throw new IllegalStateException("Routing path is done.");
}

return new RoutingPath(this.components, this.offset + 1);
}

public boolean hasTail() {
return this.components.length > this.offset;
}

public boolean isEmpty() {
return this.offset == this.components.length;
}

public String asPath() {
StringBuilder builder = new StringBuilder();

for (int i = this.offset; i < this.components.length; i++) {
if (i > this.offset) {
builder.append("/");
}
builder.append(this.components[i]);
}

return builder.toString();
}
}
35 changes: 35 additions & 0 deletions cli/src/main/java/de/jplag/cli/server/RoutingResources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.jplag.cli.server;

import java.io.InputStream;

import org.apache.commons.lang3.tuple.Pair;

import com.sun.net.httpserver.HttpExchange;

public class RoutingResources implements Routing {
private String prefix;

public RoutingResources(String prefix) {
this.prefix = prefix;

if (!this.prefix.startsWith("/")) {
tsaglam marked this conversation as resolved.
Show resolved Hide resolved
this.prefix = "/" + this.prefix;
}

if (!this.prefix.endsWith("/")) {
this.prefix = this.prefix + "/";
}
}

@Override
public Pair<InputStream, ContentType> fetchData(RoutingPath subPath, HttpExchange request, ReportViewer viewer) {
String fullPath = this.prefix + subPath.asPath();
InputStream stream = this.getClass().getResourceAsStream(fullPath);

if (stream == null) {
return null;
}

return Pair.of(stream, null);
}
}
Loading
Loading