26
26
import java .util .concurrent .atomic .AtomicInteger ;
27
27
import java .util .zip .GZIPOutputStream ;
28
28
29
+ import com .sun .net .httpserver .Authenticator ;
30
+ import com .sun .net .httpserver .HttpContext ;
29
31
import com .sun .net .httpserver .HttpExchange ;
30
32
import com .sun .net .httpserver .HttpHandler ;
31
33
import com .sun .net .httpserver .HttpServer ;
@@ -195,6 +197,7 @@ public static class Builder {
195
197
private boolean daemon = false ;
196
198
private Predicate <String > sampleNameFilter ;
197
199
private Supplier <Predicate <String >> sampleNameFilterSupplier ;
200
+ private Authenticator authenticator ;
198
201
199
202
/**
200
203
* Port to bind to. Must not be called together with {@link #withInetSocketAddress(InetSocketAddress)}
@@ -286,6 +289,18 @@ public Builder withRegistry(CollectorRegistry registry) {
286
289
return this ;
287
290
}
288
291
292
+ /**
293
+ * Optional: {@link Authenticator} to use to support authentication.
294
+ */
295
+ public Builder withAuthenticator (Authenticator authenticator ) {
296
+ this .authenticator = authenticator ;
297
+ return this ;
298
+ }
299
+
300
+ /**
301
+ * Build the HTTPServer
302
+ * @throws IOException
303
+ */
289
304
public HTTPServer build () throws IOException {
290
305
if (sampleNameFilter != null ) {
291
306
assertNull (sampleNameFilterSupplier , "cannot configure 'sampleNameFilter' and 'sampleNameFilterSupplier' at the same time" );
@@ -296,7 +311,7 @@ public HTTPServer build() throws IOException {
296
311
assertNull (hostname , "cannot configure 'httpServer' and 'hostname' at the same time" );
297
312
assertNull (inetAddress , "cannot configure 'httpServer' and 'inetAddress' at the same time" );
298
313
assertNull (inetSocketAddress , "cannot configure 'httpServer' and 'inetSocketAddress' at the same time" );
299
- return new HTTPServer (httpServer , registry , daemon , sampleNameFilterSupplier );
314
+ return new HTTPServer (httpServer , registry , daemon , sampleNameFilterSupplier , authenticator );
300
315
} else if (inetSocketAddress != null ) {
301
316
assertZero (port , "cannot configure 'inetSocketAddress' and 'port' at the same time" );
302
317
assertNull (hostname , "cannot configure 'inetSocketAddress' and 'hostname' at the same time" );
@@ -309,7 +324,7 @@ public HTTPServer build() throws IOException {
309
324
} else {
310
325
inetSocketAddress = new InetSocketAddress (port );
311
326
}
312
- return new HTTPServer (HttpServer .create (inetSocketAddress , 3 ), registry , daemon , sampleNameFilterSupplier );
327
+ return new HTTPServer (HttpServer .create (inetSocketAddress , 3 ), registry , daemon , sampleNameFilterSupplier , authenticator );
313
328
}
314
329
315
330
private void assertNull (Object o , String msg ) {
@@ -330,7 +345,7 @@ private void assertZero(int i, String msg) {
330
345
* The {@code httpServer} is expected to already be bound to an address
331
346
*/
332
347
public HTTPServer (HttpServer httpServer , CollectorRegistry registry , boolean daemon ) throws IOException {
333
- this (httpServer , registry , daemon , null );
348
+ this (httpServer , registry , daemon , null , null );
334
349
}
335
350
336
351
/**
@@ -375,15 +390,24 @@ public HTTPServer(String host, int port) throws IOException {
375
390
this (new InetSocketAddress (host , port ), CollectorRegistry .defaultRegistry , false );
376
391
}
377
392
378
- private HTTPServer (HttpServer httpServer , CollectorRegistry registry , boolean daemon , Supplier <Predicate <String >> sampleNameFilterSupplier ) {
393
+ private HTTPServer (HttpServer httpServer , CollectorRegistry registry , boolean daemon , Supplier <Predicate <String >> sampleNameFilterSupplier , Authenticator authenticator ) {
379
394
if (httpServer .getAddress () == null )
380
395
throw new IllegalArgumentException ("HttpServer hasn't been bound to an address" );
381
396
382
397
server = httpServer ;
383
398
HttpHandler mHandler = new HTTPMetricHandler (registry , sampleNameFilterSupplier );
384
- server .createContext ("/" , mHandler );
385
- server .createContext ("/metrics" , mHandler );
386
- server .createContext ("/-/healthy" , mHandler );
399
+ HttpContext mContext = server .createContext ("/" , mHandler );
400
+ if (authenticator != null ) {
401
+ mContext .setAuthenticator (authenticator );
402
+ }
403
+ mContext = server .createContext ("/metrics" , mHandler );
404
+ if (authenticator != null ) {
405
+ mContext .setAuthenticator (authenticator );
406
+ }
407
+ mContext = server .createContext ("/-/healthy" , mHandler );
408
+ if (authenticator != null ) {
409
+ mContext .setAuthenticator (authenticator );
410
+ }
387
411
executorService = Executors .newFixedThreadPool (5 , NamedDaemonThreadFactory .defaultThreadFactory (daemon ));
388
412
server .setExecutor (executorService );
389
413
start (daemon );
0 commit comments