2626import java .util .concurrent .atomic .AtomicInteger ;
2727import java .util .zip .GZIPOutputStream ;
2828
29+ import com .sun .net .httpserver .Authenticator ;
30+ import com .sun .net .httpserver .HttpContext ;
2931import com .sun .net .httpserver .HttpExchange ;
3032import com .sun .net .httpserver .HttpHandler ;
3133import com .sun .net .httpserver .HttpServer ;
@@ -195,6 +197,7 @@ public static class Builder {
195197 private boolean daemon = false ;
196198 private Predicate <String > sampleNameFilter ;
197199 private Supplier <Predicate <String >> sampleNameFilterSupplier ;
200+ private Authenticator authenticator ;
198201
199202 /**
200203 * Port to bind to. Must not be called together with {@link #withInetSocketAddress(InetSocketAddress)}
@@ -286,6 +289,18 @@ public Builder withRegistry(CollectorRegistry registry) {
286289 return this ;
287290 }
288291
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+ */
289304 public HTTPServer build () throws IOException {
290305 if (sampleNameFilter != null ) {
291306 assertNull (sampleNameFilterSupplier , "cannot configure 'sampleNameFilter' and 'sampleNameFilterSupplier' at the same time" );
@@ -296,7 +311,7 @@ public HTTPServer build() throws IOException {
296311 assertNull (hostname , "cannot configure 'httpServer' and 'hostname' at the same time" );
297312 assertNull (inetAddress , "cannot configure 'httpServer' and 'inetAddress' at the same time" );
298313 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 );
300315 } else if (inetSocketAddress != null ) {
301316 assertZero (port , "cannot configure 'inetSocketAddress' and 'port' at the same time" );
302317 assertNull (hostname , "cannot configure 'inetSocketAddress' and 'hostname' at the same time" );
@@ -309,7 +324,7 @@ public HTTPServer build() throws IOException {
309324 } else {
310325 inetSocketAddress = new InetSocketAddress (port );
311326 }
312- return new HTTPServer (HttpServer .create (inetSocketAddress , 3 ), registry , daemon , sampleNameFilterSupplier );
327+ return new HTTPServer (HttpServer .create (inetSocketAddress , 3 ), registry , daemon , sampleNameFilterSupplier , authenticator );
313328 }
314329
315330 private void assertNull (Object o , String msg ) {
@@ -330,7 +345,7 @@ private void assertZero(int i, String msg) {
330345 * The {@code httpServer} is expected to already be bound to an address
331346 */
332347 public HTTPServer (HttpServer httpServer , CollectorRegistry registry , boolean daemon ) throws IOException {
333- this (httpServer , registry , daemon , null );
348+ this (httpServer , registry , daemon , null , null );
334349 }
335350
336351 /**
@@ -375,15 +390,24 @@ public HTTPServer(String host, int port) throws IOException {
375390 this (new InetSocketAddress (host , port ), CollectorRegistry .defaultRegistry , false );
376391 }
377392
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 ) {
379394 if (httpServer .getAddress () == null )
380395 throw new IllegalArgumentException ("HttpServer hasn't been bound to an address" );
381396
382397 server = httpServer ;
383398 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+ }
387411 executorService = Executors .newFixedThreadPool (5 , NamedDaemonThreadFactory .defaultThreadFactory (daemon ));
388412 server .setExecutor (executorService );
389413 start (daemon );
0 commit comments