Skip to content

Commit 854a14d

Browse files
committed
replace with embedded http server
1 parent 4b70941 commit 854a14d

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

allure-commandline/build.gradle.kts

-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ dependencies {
131131
implementation("com.fasterxml.jackson.core:jackson-databind")
132132
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
133133
implementation("commons-io:commons-io")
134-
// implementation("org.eclipse.jetty:jetty-server")
135-
implementation("io.undertow:undertow-core:2.3.18.Final")
136134
implementation(project(":allure-generator"))
137135
testImplementation("io.qameta.allure:allure-junit-platform")
138136
testImplementation("org.apache.commons:commons-lang3")

allure-commandline/src/main/java/io/qameta/allure/Commands.java

+43-26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package io.qameta.allure;
1717

18+
import com.sun.net.httpserver.HttpExchange;
19+
import com.sun.net.httpserver.HttpServer;
1820
import io.qameta.allure.config.ConfigLoader;
1921
import io.qameta.allure.core.Configuration;
2022
import io.qameta.allure.core.Plugin;
@@ -23,18 +25,17 @@
2325
import io.qameta.allure.option.ReportNameOptions;
2426
import io.qameta.allure.plugin.DefaultPluginLoader;
2527
import io.qameta.allure.util.DeleteVisitor;
26-
import io.undertow.Handlers;
27-
import io.undertow.Undertow;
28-
import io.undertow.server.handlers.resource.PathResourceManager;
2928
import org.apache.commons.io.FileUtils;
3029
import org.slf4j.Logger;
3130
import org.slf4j.LoggerFactory;
3231

3332
import java.awt.AWTError;
3433
import java.awt.Desktop;
3534
import java.io.IOException;
35+
import java.io.OutputStream;
3636
import java.net.InetSocketAddress;
3737
import java.net.URI;
38+
import java.nio.charset.StandardCharsets;
3839
import java.nio.file.DirectoryStream;
3940
import java.nio.file.Files;
4041
import java.nio.file.Path;
@@ -44,6 +45,7 @@
4445
import java.util.Optional;
4546
import java.util.stream.Collectors;
4647

48+
import static io.qameta.allure.DefaultResultsVisitor.probeContentType;
4749
import static java.lang.String.format;
4850

4951
/**
@@ -235,7 +237,7 @@ public ExitCode serve(final List<Path> resultsDirectories,
235237
*/
236238
public ExitCode open(final Path reportDirectory, final String host, final int port) {
237239
LOGGER.info("Starting web server...");
238-
final Undertow server;
240+
final HttpServer server;
239241
try {
240242
server = setUpServer(host, port, reportDirectory);
241243
server.start();
@@ -244,19 +246,11 @@ public ExitCode open(final Path reportDirectory, final String host, final int po
244246
return ExitCode.GENERIC_ERROR;
245247
}
246248

247-
final List<Undertow.ListenerInfo> listenerInfos = server.getListenerInfo();
248-
if (listenerInfos.isEmpty()) {
249-
LOGGER.error("Can't find any listener info");
250-
return ExitCode.GENERIC_ERROR;
251-
}
252-
final Undertow.ListenerInfo listenerInfo = listenerInfos.get(0);
253-
final InetSocketAddress socketAddress = (InetSocketAddress) listenerInfo.getAddress();
254-
final URI uri = URI.create(
255-
listenerInfo.getProtcol()
256-
+ "://"
257-
+ socketAddress.getHostString()
258-
+ ":"
259-
+ socketAddress.getPort()
249+
final InetSocketAddress socketAddress = server.getAddress();
250+
final URI uri = URI.create("http://"
251+
+ socketAddress.getHostString()
252+
+ ":"
253+
+ socketAddress.getPort()
260254
);
261255

262256
try {
@@ -320,22 +314,45 @@ protected Configuration createReportConfiguration(
320314
}
321315

322316
/**
323-
* Set up Undertow server to serve Allure Report.
317+
* Set up HttpServer to serve Allure Report.
324318
*
325319
* @param host the host
326320
* @param port the port
327321
* @param reportDirectory the report directory
328322
* @return self for method chaining
329323
* @throws IOException the io exception
330324
*/
331-
protected Undertow setUpServer(final String host, final int port, final Path reportDirectory) throws IOException {
332-
return Undertow.builder()
333-
.addHttpListener(port, Objects.isNull(host) ? "localhost" : host)
334-
.setHandler(Handlers
335-
.resource(new PathResourceManager(reportDirectory.toRealPath()))
336-
.setDirectoryListingEnabled(true)
337-
)
338-
.build();
325+
protected HttpServer setUpServer(final String host, final int port, final Path reportDirectory) throws IOException {
326+
final HttpServer server = HttpServer
327+
.create(new InetSocketAddress(Objects.isNull(host) ? "localhost" : host, port), 0);
328+
329+
server.createContext("/", exchange -> {
330+
final Path resolve = reportDirectory.resolve("." + exchange.getRequestURI().getPath());
331+
if (Files.isDirectory(resolve)) {
332+
serveFile(exchange, resolve.resolve("index.html"));
333+
} else {
334+
serveFile(exchange, resolve);
335+
}
336+
});
337+
338+
return server;
339+
}
340+
341+
private static void serveFile(final HttpExchange exchange, final Path resolve) throws IOException {
342+
if (Files.isRegularFile(resolve)) {
343+
final String contentType = probeContentType(resolve);
344+
exchange.sendResponseHeaders(200, Files.size(resolve));
345+
exchange.getResponseHeaders().add("Content-Type", contentType);
346+
try (OutputStream os = exchange.getResponseBody()) {
347+
Files.copy(resolve, os);
348+
}
349+
} else {
350+
final String response = "404 Not Found";
351+
exchange.sendResponseHeaders(404, response.length());
352+
try (OutputStream os = exchange.getResponseBody()) {
353+
os.write(response.getBytes(StandardCharsets.UTF_8));
354+
}
355+
}
339356
}
340357

341358
/**

allure-generator/src/main/java/io/qameta/allure/DefaultResultsVisitor.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class DefaultResultsVisitor implements ResultsVisitor {
5050

5151
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultResultsVisitor.class);
5252

53-
public static final String WILDCARD = "*/*";
53+
public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
5454

5555
private final Configuration configuration;
5656

@@ -130,7 +130,7 @@ public static String probeContentType(final Path path) {
130130
return probeContentType(stream, Objects.toString(path.getFileName()));
131131
} catch (IOException e) {
132132
LOGGER.warn("Couldn't detect the media type of attachment {}", path, e);
133-
return WILDCARD;
133+
return APPLICATION_OCTET_STREAM;
134134
}
135135
}
136136

@@ -141,7 +141,7 @@ public static String probeContentType(final InputStream is, final String name) {
141141
return getDefaultMimeTypes().detect(stream, metadata).toString();
142142
} catch (IOException e) {
143143
LOGGER.warn("Couldn't detect the media type of attachment {}", name, e);
144-
return WILDCARD;
144+
return APPLICATION_OCTET_STREAM;
145145
}
146146
}
147147

0 commit comments

Comments
 (0)