appengine-java11/gaeinfo/src/main/java/com/example/appengine/standard/GaeInfoServlet.java [46:117]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class GaeInfoServlet extends HttpServlet {

  private final String[] metaPath = {
    "/computeMetadata/v1/project/numeric-project-id",
    "/computeMetadata/v1/project/project-id",
    "/computeMetadata/v1/instance/zone",
    "/computeMetadata/v1/instance/service-accounts/default/aliases",
    "/computeMetadata/v1/instance/service-accounts/default/",
    "/computeMetadata/v1/instance/service-accounts/default/scopes",
    // Tokens work - but are a security risk to display
    // "/computeMetadata/v1/instance/service-accounts/default/token"
  };

  final String[] metaServiceAcct = {
    "/computeMetadata/v1/instance/service-accounts/{account}/aliases",
    "/computeMetadata/v1/instance/service-accounts/{account}/email",
    "/computeMetadata/v1/instance/service-accounts/{account}/scopes",
    // Tokens work - but are a security risk to display
    // "/computeMetadata/v1/instance/service-accounts/{account}/token"
  };

  private final String metadata = "http://metadata.google.internal";

  private TemplateEngine templateEngine;
  private JavaxServletWebApplication application;

  // Use OkHttp from Square as it's quite easy to use for simple fetches.
  private final OkHttpClient ok =
      new OkHttpClient.Builder()
          .readTimeout(500, TimeUnit.MILLISECONDS) // Don't dawdle
          .writeTimeout(500, TimeUnit.MILLISECONDS)
          .build();

  // Setup to pretty print returned json
  private final Gson gson = new GsonBuilder().setPrettyPrinting().create();

  // Fetch Metadata
  String fetchMetadata(String key) throws IOException {
    Request request =
        new Request.Builder()
            .url(metadata + key)
            .addHeader("Metadata-Flavor", "Google")
            .get()
            .build();

    Response response = ok.newCall(request).execute();
    return response.body().string();
  }

  String fetchJsonMetadata(String prefix) throws IOException {
    Request request =
        new Request.Builder()
            .url(metadata + prefix)
            .addHeader("Metadata-Flavor", "Google")
            .get()
            .build();

    Response response = ok.newCall(request).execute();

    // Convert json to pretty json
    return gson.toJson(JsonParser.parseString(response.body().string()));
  }

  @Override
  public void init() {
    // Setup ThymeLeaf
    application = JavaxServletWebApplication.buildApplication(this.getServletContext());
    WebApplicationTemplateResolver templateResolver =
        new WebApplicationTemplateResolver(application);

    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



appengine-java8/gaeinfo/src/main/java/com/example/appengine/standard/GaeInfoServlet.java [53:124]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class GaeInfoServlet extends HttpServlet {

  private final String[] metaPath = {
      "/computeMetadata/v1/project/numeric-project-id", //  (pending)
      "/computeMetadata/v1/project/project-id",
      "/computeMetadata/v1/instance/zone",
      "/computeMetadata/v1/instance/service-accounts/default/aliases",
      "/computeMetadata/v1/instance/service-accounts/default/",
      "/computeMetadata/v1/instance/service-accounts/default/scopes",
      // Tokens work - but are a security risk to display
      //      "/computeMetadata/v1/instance/service-accounts/default/token"
  };

  final String[] metaServiceAcct = {
      "/computeMetadata/v1/instance/service-accounts/{account}/aliases",
      "/computeMetadata/v1/instance/service-accounts/{account}/email",
      "/computeMetadata/v1/instance/service-accounts/{account}/scopes",
      // Tokens work - but are a security risk to display
      //     "/computeMetadata/v1/instance/service-accounts/{account}/token"
  };

  private final String metadata = "http://metadata.google.internal";

  private TemplateEngine templateEngine;
  private JavaxServletWebApplication application;

  // Use OkHttp from Square as it's quite easy to use for simple fetches.
  private final OkHttpClient ok =
      new OkHttpClient.Builder()
          .readTimeout(500, TimeUnit.MILLISECONDS) // Don't dawdle
          .writeTimeout(500, TimeUnit.MILLISECONDS)
          .build();

  // Setup to pretty print returned json
  private final Gson gson = new GsonBuilder().setPrettyPrinting().create();

  // Fetch Metadata
  String fetchMetadata(String key) throws IOException {
    Request request =
        new Request.Builder()
            .url(metadata + key)
            .addHeader("Metadata-Flavor", "Google")
            .get()
            .build();

    Response response = ok.newCall(request).execute();
    return response.body().string();
  }

  String fetchJsonMetadata(String prefix) throws IOException {
    Request request =
        new Request.Builder()
            .url(metadata + prefix)
            .addHeader("Metadata-Flavor", "Google")
            .get()
            .build();

    Response response = ok.newCall(request).execute();

    // Convert json to prety json
    return gson.toJson(JsonParser.parseString(response.body().string()));
  }

  @Override
  public void init() {
    // Setup ThymeLeaf
    application = JavaxServletWebApplication.buildApplication(this.getServletContext());
    WebApplicationTemplateResolver templateResolver =
        new WebApplicationTemplateResolver(application);

    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



