|
| 1 | +/* |
| 2 | + * Copyright 2015-2024 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * https://www.eclipse.org/legal/epl-v20.html |
| 9 | + */ |
| 10 | + |
| 11 | +package platform.tooling.support.tests; |
| 12 | + |
| 13 | +import static java.net.http.HttpRequest.BodyPublishers.noBody; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.net.InetAddress; |
| 20 | +import java.net.InetSocketAddress; |
| 21 | +import java.net.URI; |
| 22 | +import java.net.http.HttpClient; |
| 23 | +import java.net.http.HttpRequest; |
| 24 | +import java.net.http.HttpResponse; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import com.sun.net.httpserver.HttpExchange; |
| 28 | +import com.sun.net.httpserver.HttpServer; |
| 29 | + |
| 30 | +public class MavenRepoProxy implements AutoCloseable { |
| 31 | + |
| 32 | + // Forbid downloading JUnit artifacts since we want to use the local ones |
| 33 | + private static final List<String> FORBIDDEN_PATHS = List.of("/org/junit"); |
| 34 | + |
| 35 | + private static final List<String> RESTRICTED_HEADER_NAMES = List.of("Connection", "Host"); |
| 36 | + |
| 37 | + private final HttpServer httpServer; |
| 38 | + private final HttpClient httpClient; |
| 39 | + |
| 40 | + @SuppressWarnings("unused") |
| 41 | + public MavenRepoProxy() throws IOException { |
| 42 | + this(0); |
| 43 | + } |
| 44 | + |
| 45 | + @SuppressWarnings("unused") |
| 46 | + public MavenRepoProxy(int port) throws IOException { |
| 47 | + this("https://oss.sonatype.org/content/repositories/snapshots", port); |
| 48 | + } |
| 49 | + |
| 50 | + private MavenRepoProxy(String proxiedUrl, int port) throws IOException { |
| 51 | + httpClient = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build(); |
| 52 | + httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), port), 0); |
| 53 | + httpServer.createContext("/", exchange -> { |
| 54 | + try (exchange) { |
| 55 | + switch (exchange.getRequestMethod()) { |
| 56 | + case "HEAD": |
| 57 | + case "GET": |
| 58 | + if (FORBIDDEN_PATHS.stream().anyMatch( |
| 59 | + it -> exchange.getRequestURI().getPath().startsWith(it))) { |
| 60 | + exchange.sendResponseHeaders(404, 0); |
| 61 | + break; |
| 62 | + } |
| 63 | + var request = mapRequest(proxiedUrl, exchange); |
| 64 | + try { |
| 65 | + var response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); |
| 66 | + mapResponse(response, exchange); |
| 67 | + } |
| 68 | + catch (InterruptedException e) { |
| 69 | + Thread.currentThread().interrupt(); |
| 70 | + } |
| 71 | + break; |
| 72 | + default: |
| 73 | + exchange.sendResponseHeaders(405, -1); |
| 74 | + } |
| 75 | + } |
| 76 | + catch (Exception e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + }); |
| 80 | + httpServer.start(); |
| 81 | + } |
| 82 | + |
| 83 | + URI getBaseUri() { |
| 84 | + var address = httpServer.getAddress(); |
| 85 | + return URI.create("http://" + address.getAddress().getHostName() + ":" + address.getPort()); |
| 86 | + } |
| 87 | + |
| 88 | + private static void mapResponse(HttpResponse<InputStream> response, HttpExchange exchange) throws IOException { |
| 89 | + exchange.sendResponseHeaders(response.statusCode(), |
| 90 | + response.headers().firstValueAsLong("Content-Length").orElse(0)); |
| 91 | + response.headers().map().forEach((key, values) -> exchange.getResponseHeaders().put(key, values)); |
| 92 | + try (InputStream body = response.body()) { |
| 93 | + body.transferTo(exchange.getResponseBody()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static HttpRequest mapRequest(String proxiedUrl, HttpExchange exchange) { |
| 98 | + var request = HttpRequest.newBuilder().method(exchange.getRequestMethod(), noBody()) // |
| 99 | + .uri(URI.create(proxiedUrl + exchange.getRequestURI().getPath())); |
| 100 | + exchange.getRequestHeaders().entrySet().stream() // |
| 101 | + .filter(entry -> RESTRICTED_HEADER_NAMES.stream().noneMatch(it -> it.equalsIgnoreCase(entry.getKey()))) // |
| 102 | + .forEach(entry -> entry.getValue() // |
| 103 | + .forEach(value -> request.header(entry.getKey(), value))); |
| 104 | + return request.build(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void close() { |
| 109 | + assertAll( // |
| 110 | + () -> assertDoesNotThrow(() -> httpServer.stop(0)), // |
| 111 | + () -> assertDoesNotThrow(httpClient::close) // |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + @SuppressWarnings("unused") |
| 116 | + public static void main(String[] args) throws Exception { |
| 117 | + try (var proxy = new MavenRepoProxy(12345)) { |
| 118 | + System.out.println("Started proxy: " + proxy.getBaseUri()); |
| 119 | + while (!Thread.interrupted()) { |
| 120 | + Thread.onSpinWait(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments