10
10
11
11
package platform .tooling .support .tests ;
12
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
13
import java .io .IOException ;
18
- import java .io .InputStream ;
19
14
import java .net .InetAddress ;
20
15
import java .net .InetSocketAddress ;
21
16
import java .net .URI ;
22
- import java .net .http .HttpClient ;
23
- import java .net .http .HttpRequest ;
24
- import java .net .http .HttpResponse ;
25
17
import java .util .List ;
26
18
27
- import com .sun .net .httpserver .HttpExchange ;
28
19
import com .sun .net .httpserver .HttpServer ;
29
20
30
21
public class MavenRepoProxy implements AutoCloseable {
31
22
32
23
// Forbid downloading JUnit artifacts since we want to use the local ones
33
24
private static final List <String > FORBIDDEN_PATHS = List .of ("/org/junit" );
34
25
35
- private static final List <String > RESTRICTED_HEADER_NAMES = List .of ("Connection" , "Host" );
36
-
37
26
private final HttpServer httpServer ;
38
- private final HttpClient httpClient ;
39
27
40
28
@ SuppressWarnings ("unused" )
41
29
public MavenRepoProxy () throws IOException {
@@ -48,7 +36,6 @@ public MavenRepoProxy(int port) throws IOException {
48
36
}
49
37
50
38
private MavenRepoProxy (String proxiedUrl , int port ) throws IOException {
51
- httpClient = HttpClient .newBuilder ().followRedirects (HttpClient .Redirect .ALWAYS ).build ();
52
39
httpServer = HttpServer .create (new InetSocketAddress (InetAddress .getLoopbackAddress (), port ), 0 );
53
40
httpServer .createContext ("/" , exchange -> {
54
41
try (exchange ) {
@@ -57,17 +44,12 @@ private MavenRepoProxy(String proxiedUrl, int port) throws IOException {
57
44
case "GET" :
58
45
if (FORBIDDEN_PATHS .stream ().anyMatch (
59
46
it -> exchange .getRequestURI ().getPath ().startsWith (it ))) {
60
- exchange .sendResponseHeaders (404 , 0 );
47
+ exchange .sendResponseHeaders (404 , - 1 );
61
48
break ;
62
49
}
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
- }
50
+ var redirectUrl = proxiedUrl + exchange .getRequestURI ().getPath ();
51
+ exchange .getResponseHeaders ().add ("Location" , redirectUrl );
52
+ exchange .sendResponseHeaders (302 , -1 );
71
53
break ;
72
54
default :
73
55
exchange .sendResponseHeaders (405 , -1 );
@@ -85,40 +67,9 @@ URI getBaseUri() {
85
67
return URI .create ("http://" + address .getAddress ().getHostName () + ":" + address .getPort ());
86
68
}
87
69
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
70
@ Override
108
71
public void close () {
109
- assertAll ( //
110
- () -> assertDoesNotThrow (() -> httpServer .stop (0 )), //
111
- () -> assertDoesNotThrow (httpClient ::close ) //
112
- );
72
+ httpServer .stop (0 );
113
73
}
114
74
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
75
}
0 commit comments