content: package org.jboss.aerogear.keycloak.metrics;
  
  import jakarta.ws.rs.GET;
  import jakarta.ws.rs.Produces;
  import jakarta.ws.rs.core.Context;
  import jakarta.ws.rs.core.HttpHeaders;
  import jakarta.ws.rs.core.MediaType;
  import jakarta.ws.rs.core.Response;
  import jakarta.ws.rs.core.Response.Status;
  import jakarta.ws.rs.core.StreamingOutput;
  import org.keycloak.services.resource.RealmResourceProvider;
  
  public class MetricsEndpoint implements RealmResourceProvider {
  
      // The ID of the provider is also used as the name of the endpoint
      public final static String ID = "metrics";
  
      private static final boolean DISABLE_EXTERNAL_ACCESS = Boolean.parseBoolean(System.getenv("DISABLE_EXTERNAL_ACCESS"));
  
      @Override
      public Object getResource() {
          return this;
      }
  
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public Response get(@Context HttpHeaders headers) {
          if (DISABLE_EXTERNAL_ACCESS) {
              if (!headers.getRequestHeader("x-forwarded-host").isEmpty()) {
                  // Request is being forwarded by HA Proxy on Openshift
                  return Response.status(Status.FORBIDDEN).build(); //(stream).build();
              }
          }
  
          final StreamingOutput stream = output -> PrometheusExporter.instance().export(output);
          return Response.ok(stream).build();
      }
  
      @Override
      public void close() {
          // Nothing to do, no resources to close
      }
  }
  