com.google.api.client.util.Beta Java Examples

The following examples show how to use com.google.api.client.util.Beta. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MockHttpClient.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Override
protected RequestDirector createClientRequestDirector(
    HttpRequestExecutor requestExec,
    ClientConnectionManager conman,
    ConnectionReuseStrategy reustrat,
    ConnectionKeepAliveStrategy kastrat,
    HttpRoutePlanner rouplan,
    HttpProcessor httpProcessor,
    HttpRequestRetryHandler retryHandler,
    RedirectHandler redirectHandler,
    AuthenticationHandler targetAuthHandler,
    AuthenticationHandler proxyAuthHandler,
    UserTokenHandler stateHandler,
    HttpParams params) {
  return new RequestDirector() {
    @Beta
    public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context)
        throws HttpException, IOException {
      return new BasicHttpResponse(HttpVersion.HTTP_1_1, responseCode, null);
    }
  };
}
 
Example #2
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * {@link Beta} <br/>
 * Return a credential defined by a Json file.
 *
 * @param credentialStream the stream with the credential definition.
 * @param transport the transport for Http calls.
 * @param jsonFactory the factory for Json parsing and formatting.
 * @return the credential defined by the credentialStream.
 * @throws IOException if the credential cannot be created from the stream.
 */
@Beta
public static GoogleCredential fromStream(InputStream credentialStream, HttpTransport transport,
    JsonFactory jsonFactory) throws IOException {
  Preconditions.checkNotNull(credentialStream);
  Preconditions.checkNotNull(transport);
  Preconditions.checkNotNull(jsonFactory);

  JsonObjectParser parser = new JsonObjectParser(jsonFactory);
  GenericJson fileContents = parser.parseAndClose(
      credentialStream, OAuth2Utils.UTF_8, GenericJson.class);
  String fileType = (String) fileContents.get("type");
  if (fileType == null) {
    throw new IOException("Error reading credentials from stream, 'type' field not specified.");
  }
  if (USER_FILE_TYPE.equals(fileType)) {
    return fromStreamUser(fileContents, transport, jsonFactory);
  }
  if (SERVICE_ACCOUNT_FILE_TYPE.equals(fileType)) {
    return fromStreamServiceAccount(fileContents, transport, jsonFactory);
  }
  throw new IOException(String.format(
      "Error reading credentials from stream, 'type' value '%s' not recognized."
          + " Expecting '%s' or '%s'.",
      fileType, USER_FILE_TYPE, SERVICE_ACCOUNT_FILE_TYPE));
}
 
Example #3
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * {@link Beta} <br/>
 * Create a builder from this credential.
 */
@Beta
public Builder toBuilder() {
  Builder builder = new GoogleCredential.Builder()
      .setServiceAccountPrivateKey(serviceAccountPrivateKey)
      .setServiceAccountPrivateKeyId(serviceAccountPrivateKeyId)
      .setServiceAccountId(serviceAccountId)
      .setServiceAccountProjectId(serviceAccountProjectId)
      .setServiceAccountUser(serviceAccountUser)
      .setServiceAccountScopes(serviceAccountScopes)
      .setTokenServerEncodedUrl(getTokenServerEncodedUrl())
      .setTransport(getTransport())
      .setJsonFactory(getJsonFactory())
      .setClock(getClock());

  builder.setClientAuthentication(getClientAuthentication());

  return builder;
}
 
Example #4
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
@Beta
private static GoogleCredential fromStreamUser(GenericJson fileContents, HttpTransport transport,
    JsonFactory jsonFactory) throws IOException {
  String clientId = (String) fileContents.get("client_id");
  String clientSecret = (String) fileContents.get("client_secret");
  String refreshToken = (String) fileContents.get("refresh_token");
  if (clientId == null || clientSecret == null || refreshToken == null) {
    throw new IOException("Error reading user credential from stream, "
        + " expecting 'client_id', 'client_secret' and 'refresh_token'.");
  }

  GoogleCredential credential = new GoogleCredential.Builder()
      .setClientSecrets(clientId, clientSecret)
      .setTransport(transport)
      .setJsonFactory(jsonFactory)
      .build();
  credential.setRefreshToken(refreshToken);

  // Do a refresh so we can fail early rather than return an unusable credential
  credential.refreshToken();
  return credential;
}
 
Example #5
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
@Beta
private static GoogleCredential fromStreamServiceAccount(GenericJson fileContents,
    HttpTransport transport, JsonFactory jsonFactory) throws IOException {
  String clientId = (String) fileContents.get("client_id");
  String clientEmail = (String) fileContents.get("client_email");
  String privateKeyPem = (String) fileContents.get("private_key");
  String privateKeyId = (String) fileContents.get("private_key_id");
  if (clientId == null || clientEmail == null || privateKeyPem == null
      || privateKeyId == null) {
    throw new IOException("Error reading service account credential from stream, "
        + "expecting  'client_id', 'client_email', 'private_key' and 'private_key_id'.");
  }

  PrivateKey privateKey = privateKeyFromPkcs8(privateKeyPem);

  Collection<String> emptyScopes = Collections.emptyList();

  Builder credentialBuilder = new GoogleCredential.Builder()
      .setTransport(transport)
      .setJsonFactory(jsonFactory)
      .setServiceAccountId(clientEmail)
      .setServiceAccountScopes(emptyScopes)
      .setServiceAccountPrivateKey(privateKey)
      .setServiceAccountPrivateKeyId(privateKeyId);
  String tokenUri = (String) fileContents.get("token_uri");
  if (tokenUri != null) {
    credentialBuilder.setTokenServerEncodedUrl(tokenUri);
  }
  String projectId = (String) fileContents.get("project_id");
  if (projectId != null) {
    credentialBuilder.setServiceAccountProjectId(projectId);
  }

  // Don't do a refresh at this point, as it will always fail before the scopes are added.
  return credentialBuilder.build();
}
 
Example #6
Source File: ClassWithBetaMethod.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Beta
public void betaMethod() {
  BetaClass2 exp2 = new BetaClass2();
  exp2.foo();

  betaField = 20;
}
 
Example #7
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
@Override
@Beta
protected TokenResponse executeRefreshToken() throws IOException {
  if (serviceAccountPrivateKey == null) {
    return super.executeRefreshToken();
  }
  // service accounts: no refresh token; instead use private key to request new access token
  JsonWebSignature.Header header = new JsonWebSignature.Header();
  header.setAlgorithm("RS256");
  header.setType("JWT");
  header.setKeyId(serviceAccountPrivateKeyId);
  JsonWebToken.Payload payload = new JsonWebToken.Payload();
  long currentTime = getClock().currentTimeMillis();
  payload.setIssuer(serviceAccountId);
  payload.setAudience(getTokenServerEncodedUrl());
  payload.setIssuedAtTimeSeconds(currentTime / 1000);
  payload.setExpirationTimeSeconds(currentTime / 1000 + 3600);
  payload.setSubject(serviceAccountUser);
  payload.put("scope", Joiner.on(' ').join(serviceAccountScopes));
  try {
    String assertion = JsonWebSignature.signUsingRsaSha256(
        serviceAccountPrivateKey, getJsonFactory(), header, payload);
    TokenRequest request = new TokenRequest(
        getTransport(), getJsonFactory(), new GenericUrl(getTokenServerEncodedUrl()),
        "urn:ietf:params:oauth:grant-type:jwt-bearer");
    request.put("assertion", assertion);
    return request.execute();
  } catch (GeneralSecurityException exception) {
    IOException e = new IOException();
    e.initCause(exception);
    throw e;
  }
}
 
Example #8
Source File: HttpRequest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Beta} <br>
 * Executes this request asynchronously in a single separate thread using the supplied executor.
 *
 * @param executor executor to run the asynchronous request
 * @return future for accessing the HTTP response
 * @since 1.13
 */
@Beta
public Future<HttpResponse> executeAsync(Executor executor) {
  FutureTask<HttpResponse> future =
      new FutureTask<HttpResponse>(
          new Callable<HttpResponse>() {

            public HttpResponse call() throws Exception {
              return execute();
            }
          });
  executor.execute(future);
  return future;
}
 
Example #9
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Beta} <br/>
 * Indicates whether the credential requires scopes to be specified by calling createScoped
 * before use.
 */
@Beta
public boolean createScopedRequired() {
  if (serviceAccountPrivateKey == null) {
    return false;
  }
  return (serviceAccountScopes == null || serviceAccountScopes.isEmpty());
}
 
Example #10
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Beta} <br/>
 * For credentials that require scopes, creates a copy of the credential with the specified
 * scopes.
 */
@Beta
public GoogleCredential createScoped(Collection<String> scopes) {
  if (serviceAccountPrivateKey == null) {
    return this;
  }
  return toBuilder()
      .setServiceAccountScopes(scopes)
      .build();
}
 
Example #11
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Beta} <br/>
 * For service accounts that need to delegate to a specific user, create a
 * copy of the credential with the specified user.
 */
@Beta
public GoogleCredential createDelegated(String user) {
  if (serviceAccountPrivateKey == null) {
    return this;
  }
  return toBuilder()
      .setServiceAccountUser(user)
      .build();
}
 
Example #12
Source File: JsonParser.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Beta} <br>
 * Parse a JSON Array from the given JSON parser into the given destination collection, optionally
 * using the given parser customizer.
 *
 * @param destinationCollectionClass class of destination collection (must have a public default
 *     constructor)
 * @param destinationItemClass class of destination collection item (must have a public default
 *     constructor)
 * @param customizeParser optional parser customizer or {@code null} for none
 */
@Beta
public final <T> Collection<T> parseArray(
    Class<?> destinationCollectionClass,
    Class<T> destinationItemClass,
    CustomizeJsonParser customizeParser)
    throws IOException {
  @SuppressWarnings("unchecked")
  Collection<T> destinationCollection =
      (Collection<T>) Data.newCollectionInstance(destinationCollectionClass);
  parseArray(destinationCollection, destinationItemClass, customizeParser);
  return destinationCollection;
}
 
Example #13
Source File: JsonParser.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Beta} <br>
 * Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into
 * the given destination collection, optionally using the given parser customizer.
 *
 * @param destinationCollection destination collection
 * @param destinationItemClass class of destination collection item (must have a public default
 *     constructor)
 * @param customizeParser optional parser customizer or {@code null} for none
 */
@Beta
public final <T> void parseArrayAndClose(
    Collection<? super T> destinationCollection,
    Class<T> destinationItemClass,
    CustomizeJsonParser customizeParser)
    throws IOException {
  try {
    parseArray(destinationCollection, destinationItemClass, customizeParser);
  } finally {
    close();
  }
}
 
Example #14
Source File: JsonParser.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Beta} <br>
 * Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into
 * the given destination collection, optionally using the given parser customizer.
 *
 * @param destinationCollectionClass class of destination collection (must have a public default
 *     constructor)
 * @param destinationItemClass class of destination collection item (must have a public default
 *     constructor)
 * @param customizeParser optional parser customizer or {@code null} for none
 */
@Beta
public final <T> Collection<T> parseArrayAndClose(
    Class<?> destinationCollectionClass,
    Class<T> destinationItemClass,
    CustomizeJsonParser customizeParser)
    throws IOException {
  try {
    return parseArray(destinationCollectionClass, destinationItemClass, customizeParser);
  } finally {
    close();
  }
}
 
Example #15
Source File: JsonWebSignature.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Beta} <br>
 * Verifies the signature of the content using the certificate chain embedded in the signature.
 *
 * <p>Currently only {@code "RS256"} and {@code "ES256"} algorithms are verified, but others may
 * be added in the future. For any other algorithm it returns {@code null}.
 *
 * <p>The leaf certificate of the certificate chain must be an SSL server certificate.
 *
 * @param trustManager trust manager used to verify the X509 certificate chain embedded in this
 *     message
 * @return the signature certificate if the signature could be verified, null otherwise
 * @throws GeneralSecurityException
 * @since 1.19.1
 */
@Beta
public final X509Certificate verifySignature(X509TrustManager trustManager)
    throws GeneralSecurityException {
  List<String> x509Certificates = getHeader().getX509Certificates();
  if (x509Certificates == null || x509Certificates.isEmpty()) {
    return null;
  }
  String algorithm = getHeader().getAlgorithm();
  if ("RS256".equals(algorithm)) {
    return SecurityUtils.verify(
        SecurityUtils.getSha256WithRsaSignatureAlgorithm(),
        trustManager,
        x509Certificates,
        signatureBytes,
        signedContentBytes);
  } else if ("ES256".equals(algorithm)) {
    return SecurityUtils.verify(
        SecurityUtils.getEs256SignatureAlgorithm(),
        trustManager,
        x509Certificates,
        DerEncoder.encode(signatureBytes),
        signedContentBytes);
  } else {
    return null;
  }
}
 
Example #16
Source File: GoogleTokenResponse.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * {@link Beta} <br/>
 * Returns the ID token.
 */
@Beta
public final String getIdToken() {
  return idToken;
}
 
Example #17
Source File: AuthorizationFlow.java    From android-oauth-client with Apache License 2.0 4 votes vote down vote up
@Beta
@Deprecated
@Override
public Builder setScopes(String... scopes) {
    return (Builder) super.setScopes(scopes);
}
 
Example #18
Source File: AuthorizationCodeFlow.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * {@link Beta} <br/>
 * Returns the credential persistence store or {@code null} for none.
 * @deprecated (to be removed in the future) Use {@link #getCredentialDataStore()} instead.
 */
@Beta
@Deprecated
public final CredentialStore getCredentialStore() {
  return credentialStore;
}
 
Example #19
Source File: AuthorizationCodeFlow.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * {@link Beta} <br/>
 * Returns the credential persistence store or {@code null} for none.
 * @deprecated (to be removed in the future) Use {@link #getCredentialDataStore()} instead.
 */
@Beta
@Deprecated
public final CredentialStore getCredentialStore() {
  return credentialStore;
}
 
Example #20
Source File: AuthorizationCodeFlow.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * Enables Proof Key for Code Exchange (PKCE) for this Athorization Code Flow.
 * @since 1.31
 */
@Beta
public Builder enablePKCE() {
  this.pkce = new PKCE();
  return this;
}
 
Example #21
Source File: AuthorizationFlow.java    From android-oauth-client with Apache License 2.0 4 votes vote down vote up
@Beta
@Override
public Builder setCredentialStore(CredentialStore credentialStore) {
    return (Builder) super.setCredentialStore(credentialStore);
}
 
Example #22
Source File: AuthorizationFlow.java    From android-oauth-client with Apache License 2.0 4 votes vote down vote up
@Beta
@Deprecated
@Override
public Builder setScopes(Iterable<String> scopes) {
    return (Builder) super.setScopes(scopes);
}
 
Example #23
Source File: GoogleAuthorizationCodeFlow.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
@Beta
@Override
@Deprecated
public Builder setCredentialStore(CredentialStore credentialStore) {
  return (Builder) super.setCredentialStore(credentialStore);
}
 
Example #24
Source File: AuthorizationFlow.java    From mirror with Apache License 2.0 4 votes vote down vote up
@Beta
@Override
public Builder setCredentialStore(CredentialStore credentialStore) {
    return (Builder) super.setCredentialStore(credentialStore);
}
 
Example #25
Source File: ClassWithBetaMethod.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Beta
public static void staticBetaMethod() {}
 
Example #26
Source File: BetaClass.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Beta
public void betaMethod() {
  BetaClass2 exp2 = new BetaClass2();
  exp2.foo();
}
 
Example #27
Source File: HttpRequest.java    From google-http-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * {@link Beta} <br>
 * Sets the {@link BackOffPolicy} to use between retry attempts or {@code null} for none.
 *
 * @since 1.7
 * @deprecated (scheduled to be removed in 1.18). Use {@link
 *     #setUnsuccessfulResponseHandler(HttpUnsuccessfulResponseHandler)} with a new {@link
 *     HttpBackOffUnsuccessfulResponseHandler} instead.
 */
@Deprecated
@Beta
public HttpRequest setBackOffPolicy(BackOffPolicy backOffPolicy) {
  this.backOffPolicy = backOffPolicy;
  return this;
}
 
Example #28
Source File: JsonWebSignature.java    From google-http-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * {@link Beta} <br>
 * Verifies the signature of the content using the certificate chain embedded in the signature.
 *
 * <p>Currently only {@code "RS256"} algorithm is verified, but others may be added in the future.
 * For any other algorithm it returns {@code null}.
 *
 * <p>The certificate chain is verified using the system default trust manager.
 *
 * <p>The leaf certificate of the certificate chain must be an SSL server certificate.
 *
 * @return the signature certificate if the signature could be verified, null otherwise
 * @throws GeneralSecurityException
 * @since 1.19.1.
 */
@Beta
public final X509Certificate verifySignature() throws GeneralSecurityException {
  X509TrustManager trustManager = getDefaultX509TrustManager();
  if (trustManager == null) {
    return null;
  }
  return verifySignature(trustManager);
}
 
Example #29
Source File: JsonParser.java    From google-http-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * {@link Beta} <br>
 * Parse a JSON object, array, or value into a new instance of the given destination class using
 * {@link JsonParser#parse(Class, CustomizeJsonParser)}, and then closes the parser.
 *
 * @param <T> destination class
 * @param destinationClass destination class that has a public default constructor to use to
 *     create a new instance
 * @param customizeParser optional parser customizer or {@code null} for none
 * @return new instance of the parsed destination class
 */
@Beta
public final <T> T parseAndClose(Class<T> destinationClass, CustomizeJsonParser customizeParser)
    throws IOException {
  try {
    return parse(destinationClass, customizeParser);
  } finally {
    close();
  }
}
 
Example #30
Source File: JsonParser.java    From google-http-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * {@link Beta} <br>
 * Parse a JSON Object from the given JSON parser -- which is closed after parsing completes --
 * into the given destination object, optionally using the given parser customizer.
 *
 * <p>Before this method is called, the parser must either point to the start or end of a JSON
 * object or to a field name.
 *
 * @param destination destination object
 * @param customizeParser optional parser customizer or {@code null} for none
 */
@Beta
public final void parseAndClose(Object destination, CustomizeJsonParser customizeParser)
    throws IOException {
  try {
    parse(destination, customizeParser);
  } finally {
    close();
  }
}