<commit_before>/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.devtools.cdbg.debuglets.java;

import static com.google.devtools.cdbg.debuglets.java.AgentLogger.severefmt;
import static com.google.devtools.cdbg.debuglets.java.AgentLogger.warnfmt;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * Reads project and auth token from GCE metadata service.
 * 
 * <p>The metadata service is explained
 * <a href="https://developers.google.com/compute/docs/metadata">here</a>.
 */
public final class GceMetadataQuery implements MetadataQuery {
  /**
   * Metadata service token. 
   */
  private static final class Token {
    @SerializedName("access_token")
    private String accessToken;
    
    @SerializedName("expires_in")
    private int expiresIn;
    
    @SerializedName("token_type")
    private String tokenType;
    
    public String getAccessToken() {
      return accessToken;
    }
    
    public int getExpiresIn() {
      return expiresIn;
    }
  }
  
  /**
   * Minimal remaining expiration time of access token before we query a new one.
   */
  static final int CACHE_BUFFER_SEC = 30;
  
  /**
   * Buffer size for reading input streams.
   */
  static final int BUFFER_SIZE = 1024;

  /**
   * Timeout for reading data from the metadata server.
   */
  static final int READ_TIMEOUT_MILLIS = 10000;

  /**
   * Base URL for metadata service. Specific attributes are appended to this URL.
   */
  static final String DEFAULT_LOCAL_METADATA_SERVICE_BASE =
      "http://metadata.google.internal/computeMetadata/v1/";
  
  /**
   * JSON deserializer to read access token.
   */
  private static final Gson gson = new Gson();
  
  /**
   * Base URL for metadata service. Specific attributes are appended to this URL. Unit test
   * may override it to inject a mock HTTP client.
   */
  private final URL localMetadataServiceBaseUrl;
<commit_msg>Switch to use TimeUtil for converting from seconds to millis.<commit_after>/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.devtools.cdbg.debuglets.java;

import static com.google.devtools.cdbg.debuglets.java.AgentLogger.severefmt;
import static com.google.devtools.cdbg.debuglets.java.AgentLogger.warnfmt;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Reads project and auth token from GCE metadata service.
 * 
 * <p>The metadata service is explained
 * <a href="https://developers.google.com/compute/docs/metadata">here</a>.
 */
public final class GceMetadataQuery implements MetadataQuery {
  /**
   * Metadata service token. 
   */
  private static final class Token {
    @SerializedName("access_token")
    private String accessToken;
    
    @SerializedName("expires_in")
    private int expiresIn;
    
    @SerializedName("token_type")
    private String tokenType;
    
    public String getAccessToken() {
      return accessToken;
    }
    
    public int getExpiresIn() {
      return expiresIn;
    }
  }
  
  /**
   * Minimal remaining expiration time of access token before we query a new one.
   */
  static final int CACHE_BUFFER_SEC = 30;
  
  /**
   * Buffer size for reading input streams.
   */
  static final int BUFFER_SIZE = 1024;

  /**
   * Timeout for reading data from the metadata server.
   */
  private static final int READ_TIMEOUT_MILLIS =
      (int) TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS);

  /**
   * Base URL for metadata service. Specific attributes are appended to this URL.
   */
  static final String DEFAULT_LOCAL_METADATA_SERVICE_BASE =
      "http://metadata.google.internal/computeMetadata/v1/";
  
  /**
   * JSON deserializer to read access token.
   */
  private static final Gson gson = new Gson();
  
  /**
   * Base URL for metadata service. Specific attributes are appended to this URL. Unit test
   * may override it to inject a mock HTTP client.
   */
  private final URL localMetadataServiceBaseUrl;

{'fraction_non_alphanumeric': 0.08733401430030643, 'fraction_numerical': 0.006639427987742594, 'mean_word_length': 3.533179012345679, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '988214', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1657, 'n_words': 558}