1010import java .net .InetSocketAddress ;
1111import java .net .URLDecoder ;
1212import java .nio .charset .Charset ;
13+ import java .util .HashSet ;
1314import java .util .List ;
1415import java .util .Set ;
15- import java .util .HashSet ;
16- import java .util .concurrent .ExecutorService ;
1716import java .util .concurrent .ExecutionException ;
17+ import java .util .concurrent .ExecutorService ;
1818import java .util .concurrent .Executors ;
1919import java .util .concurrent .FutureTask ;
2020import java .util .concurrent .ThreadFactory ;
2121import java .util .concurrent .atomic .AtomicInteger ;
2222import java .util .zip .GZIPOutputStream ;
2323
24+ import com .sun .net .httpserver .HttpExchange ;
2425import com .sun .net .httpserver .HttpHandler ;
2526import com .sun .net .httpserver .HttpServer ;
26- import com .sun .net .httpserver .HttpExchange ;
2727
2828/**
2929 * Expose Prometheus metrics using a plain Java HttpServer.
3737 * */
3838public class HTTPServer {
3939 private static class LocalByteArray extends ThreadLocal <ByteArrayOutputStream > {
40+ @ Override
4041 protected ByteArrayOutputStream initialValue ()
4142 {
4243 return new ByteArrayOutputStream (1 << 20 );
@@ -47,15 +48,15 @@ protected ByteArrayOutputStream initialValue()
4748 * Handles Metrics collections from the given registry.
4849 */
4950 static class HTTPMetricHandler implements HttpHandler {
50- private CollectorRegistry registry ;
51+ private final CollectorRegistry registry ;
5152 private final LocalByteArray response = new LocalByteArray ();
5253 private final static String HEALTHY_RESPONSE = "Exporter is Healthy." ;
5354
5455 HTTPMetricHandler (CollectorRegistry registry ) {
5556 this .registry = registry ;
5657 }
5758
58-
59+ @ Override
5960 public void handle (HttpExchange t ) throws IOException {
6061 String query = t .getRequestURI ().getRawQuery ();
6162
@@ -70,18 +71,18 @@ public void handle(HttpExchange t) throws IOException {
7071 registry .filteredMetricFamilySamples (parseQuery (query )));
7172 }
7273
73- osw .flush ();
7474 osw .close ();
75- response .flush ();
76- response .close ();
7775 t .getResponseHeaders ().set ("Content-Type" ,
7876 TextFormat .CONTENT_TYPE_004 );
7977 if (shouldUseCompression (t )) {
8078 t .getResponseHeaders ().set ("Content-Encoding" , "gzip" );
8179 t .sendResponseHeaders (HttpURLConnection .HTTP_OK , 0 );
8280 final GZIPOutputStream os = new GZIPOutputStream (t .getResponseBody ());
83- response .writeTo (os );
84- os .close ();
81+ try {
82+ response .writeTo (os );
83+ } finally {
84+ os .close ();
85+ }
8586 } else {
8687 t .getResponseHeaders ().set ("Content-Length" ,
8788 String .valueOf (response .size ()));
@@ -100,7 +101,7 @@ protected static boolean shouldUseCompression(HttpExchange exchange) {
100101 for (String encodingHeader : encodingHeaders ) {
101102 String [] encodings = encodingHeader .split ("," );
102103 for (String encoding : encodings ) {
103- if (encoding .trim ().toLowerCase (). equals ("gzip" )) {
104+ if (encoding .trim ().equalsIgnoreCase ("gzip" )) {
104105 return true ;
105106 }
106107 }
0 commit comments