Java Code Examples for com.google.api.client.util.Preconditions#checkNotNull()

The following examples show how to use com.google.api.client.util.Preconditions#checkNotNull() . 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: AuthorizationCodeFlow.java    From google-oauth-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * @param builder authorization code flow builder
 *
 * @since 1.14
 */
protected AuthorizationCodeFlow(Builder builder) {
  method = Preconditions.checkNotNull(builder.method);
  transport = Preconditions.checkNotNull(builder.transport);
  jsonFactory = Preconditions.checkNotNull(builder.jsonFactory);
  tokenServerEncodedUrl = Preconditions.checkNotNull(builder.tokenServerUrl).build();
  clientAuthentication = builder.clientAuthentication;
  clientId = Preconditions.checkNotNull(builder.clientId);
  authorizationServerEncodedUrl =
      Preconditions.checkNotNull(builder.authorizationServerEncodedUrl);
  requestInitializer = builder.requestInitializer;
  credentialStore = builder.credentialStore;
  credentialDataStore = builder.credentialDataStore;
  scopes = Collections.unmodifiableCollection(builder.scopes);
  clock = Preconditions.checkNotNull(builder.clock);
  credentialCreatedListener = builder.credentialCreatedListener;
  refreshListeners = Collections.unmodifiableCollection(builder.refreshListeners);
  pkce = builder.pkce;
}
 
Example 2
Source File: AbstractGoogleClient.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * If the specified service path does not end with a "/" then a "/" is added to the end. If the
 * specified service path begins with a "/" then the "/" is removed.
 */
static String normalizeServicePath(String servicePath) {
  Preconditions.checkNotNull(servicePath, "service path cannot be null");
  if (servicePath.length() == 1) {
    Preconditions.checkArgument(
        "/".equals(servicePath), "service path must equal \"/\" if it is of length 1.");
    servicePath = "";
  } else if (servicePath.length() > 0) {
    if (!servicePath.endsWith("/")) {
      servicePath += "/";
    }
    if (servicePath.startsWith("/")) {
      servicePath = servicePath.substring(1);
    }
  }
  return servicePath;
}
 
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/>
 * 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 4
Source File: FilePersistedCredentials.java    From android-oauth-client with Apache License 2.0 5 votes vote down vote up
/**
 * @param userId user ID whose credential needs to be loaded
 * @param credential credential whose {@link Credential#setAccessToken access token},
 *        {@link Credential#setRefreshToken refresh token}, and
 *        {@link Credential#setExpirationTimeMilliseconds expiration time} need to be set if the
 *        credential already exists in storage
 */
boolean load(String userId, Credential credential) {
  Preconditions.checkNotNull(userId);
  FilePersistedCredential fileCredential = credentials.get(userId);
  if (fileCredential == null) {
    return false;
  }
  fileCredential.load(credential);
  return true;
}
 
Example 5
Source File: IosAppMetadata.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
IosAppMetadata(
    String name, String appId, String displayName, String projectId, String bundleId) {
  this.name = Preconditions.checkNotNull(name, "Null name");
  this.appId = Preconditions.checkNotNull(appId, "Null appId");
  this.displayName = displayName;
  this.projectId = Preconditions.checkNotNull(projectId, "Null projectId");
  this.bundleId = Preconditions.checkNotNull(bundleId, "Null bundleId");
}
 
Example 6
Source File: ByteArrayContent.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor from byte array content that has already been encoded, specifying a range of bytes
 * to read from the input byte array.
 *
 * @param type content type or {@code null} for none
 * @param array byte array content
 * @param offset starting offset into the byte array
 * @param length of bytes to read from byte array
 * @since 1.7
 */
public ByteArrayContent(String type, byte[] array, int offset, int length) {
  super(type);
  this.byteArray = Preconditions.checkNotNull(array);
  Preconditions.checkArgument(
      offset >= 0 && length >= 0 && offset + length <= array.length,
      "offset %s, length %s, array length %s",
      offset,
      length,
      array.length);
  this.offset = offset;
  this.length = length;
}
 
Example 7
Source File: AbstractGoogleClient.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/** If the specified root URL does not end with a "/" then a "/" is added to the end. */
static String normalizeRootUrl(String rootUrl) {
  Preconditions.checkNotNull(rootUrl, "root URL cannot be null.");
  if (!rootUrl.endsWith("/")) {
    rootUrl += "/";
  }
  return rootUrl;
}
 
Example 8
Source File: AtomContent.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * @param namespaceDictionary XML namespace dictionary
 * @param entry key/value pair data for the Atom entry
 * @param isEntry {@code true} for an Atom entry or {@code false} for an Atom feed
 * @since 1.5
 */
protected AtomContent(XmlNamespaceDictionary namespaceDictionary, Object entry, boolean isEntry) {
  super(namespaceDictionary);
  setMediaType(new HttpMediaType(Atom.MEDIA_TYPE));
  this.entry = Preconditions.checkNotNull(entry);
  this.isEntry = isEntry;
}
 
Example 9
Source File: AtomPatchRelativeToOriginalContent.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * @param namespaceDictionary XML namespace dictionary
 * @since 1.5
 */
public AtomPatchRelativeToOriginalContent(
    XmlNamespaceDictionary namespaceDictionary, Object originalEntry, Object patchedEntry) {
  super(namespaceDictionary);
  this.originalEntry = Preconditions.checkNotNull(originalEntry);
  this.patchedEntry = Preconditions.checkNotNull(patchedEntry);
}
 
Example 10
Source File: OAuthManager.java    From android-oauth-client with Apache License 2.0 4 votes vote down vote up
/**
 * Authorizes the Android application to access user's protected data using
 * the authorization flow in OAuth 1.0a.
 * 
 * @param userId user ID or {@code null} if not using a persisted credential
 *            store
 * @param callback Callback to invoke when the request completes,
 *            {@code null} for no callback
 * @param handler {@link Handler} identifying the callback thread,
 *            {@code null} for the main thread
 * @return An {@link OAuthFuture} which resolves to a {@link Credential}
 */
public OAuthFuture<Credential> authorize10a(final String userId,
        final OAuthCallback<Credential> callback, Handler handler) {
    Preconditions.checkNotNull(userId);

    final Future2Task<Credential> task = new Future2Task<Credential>(handler, callback) {

        @Override
        public void doWork() throws Exception {
            try {
                LOGGER.info("authorize10a");
                OAuthHmacCredential credential = mFlow.load10aCredential(userId);
                if (credential != null && credential.getAccessToken() != null
                        && (credential.getRefreshToken() != null
                                || credential.getExpiresInSeconds() == null
                                || credential.getExpiresInSeconds() > 60)) {
                    set(credential);
                    return;
                }

                String redirectUri = mUIController.getRedirectUri();

                OAuthCredentialsResponse tempCredentials =
                        mFlow.new10aTemporaryTokenRequest(redirectUri);
                OAuthAuthorizeTemporaryTokenUrl authorizationUrl =
                        mFlow.new10aAuthorizationUrl(tempCredentials.token);
                mUIController.requestAuthorization(authorizationUrl);

                String code = mUIController.waitForVerifierCode();
                OAuthCredentialsResponse response =
                        mFlow.new10aTokenRequest(tempCredentials, code).execute();
                credential = mFlow.createAndStoreCredential(response, userId);
                set(credential);
            } finally {
                mUIController.stop();
            }
        }

    };

    // run the task in a background thread
    submitTaskToExecutor(task);

    return task;
}
 
Example 11
Source File: JsonWebSignature.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/** @param jsonFactory JSON factory */
public Parser(JsonFactory jsonFactory) {
  this.jsonFactory = Preconditions.checkNotNull(jsonFactory);
}
 
Example 12
Source File: OAuth10aResponseUrl.java    From android-oauth-client with Apache License 2.0 4 votes vote down vote up
public OAuth10aResponseUrl setVerifier(String verifier) {
    this.verifier = Preconditions.checkNotNull(verifier);
    return this;
}
 
Example 13
Source File: JacksonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonParser createJsonParser(InputStream in, Charset charset) throws IOException {
  Preconditions.checkNotNull(in);
  return new JacksonParser(this, factory.createJsonParser(in));
}
 
Example 14
Source File: ContentEntity.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * @param contentLength content length or less than zero if not known
 * @param streamingContent streaming content
 */
ContentEntity(long contentLength, StreamingContent streamingContent) {
  this.contentLength = contentLength;
  this.streamingContent = Preconditions.checkNotNull(streamingContent);
}
 
Example 15
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * {@link Beta} <br/>
 * Returns the Application Default Credentials.
 *
 * <p>Returns the Application Default Credentials which are credentials that identify and
 * authorize the whole application. This is the built-in service account if running on Google
 * Compute Engine or the credentials file from the path in the environment variable
 * GOOGLE_APPLICATION_CREDENTIALS.</p>
 *
 * @param transport the transport for Http calls.
 * @param jsonFactory the factory for Json parsing and formatting.
 * @return the credential instance.
 * @throws IOException if the credential cannot be created in the current environment.
 */
@Beta
public static GoogleCredential getApplicationDefault(
    HttpTransport transport, JsonFactory jsonFactory) throws IOException {
  Preconditions.checkNotNull(transport);
  Preconditions.checkNotNull(jsonFactory);
  return defaultCredentialProvider.getDefaultCredential(transport, jsonFactory);
}
 
Example 16
Source File: GooglePublicKeysManager.java    From google-api-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an instance of a new builder.
 *
 * @param transport HTTP transport
 * @param jsonFactory JSON factory
 */
public Builder(HttpTransport transport, JsonFactory jsonFactory) {
  this.transport = Preconditions.checkNotNull(transport);
  this.jsonFactory = Preconditions.checkNotNull(jsonFactory);
}
 
Example 17
Source File: AuthorizationCodeTokenRequest.java    From google-oauth-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the authorization code generated by the authorization server.
 *
 * <p>
 * Overriding is only supported for the purpose of calling the super implementation and changing
 * the return type, but nothing else.
 * </p>
 */
public AuthorizationCodeTokenRequest setCode(String code) {
  this.code = Preconditions.checkNotNull(code);
  return this;
}
 
Example 18
Source File: Credential.java    From google-oauth-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the clock to use for expiration checks.
 *
 * <p>
 * The default value is Clock.SYSTEM.
 * </p>
 *
 * @since 1.9
 */
public Builder setClock(Clock clock) {
  this.clock = Preconditions.checkNotNull(clock);
  return this;
}
 
Example 19
Source File: AuthorizationCodeFlow.java    From google-oauth-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the token server URL.
 *
 * <p>
 * Overriding is only supported for the purpose of calling the super implementation and changing
 * the return type, but nothing else.
 * </p>
 * @since 1.11
 */
public Builder setTokenServerUrl(GenericUrl tokenServerUrl) {
  this.tokenServerUrl = Preconditions.checkNotNull(tokenServerUrl);
  return this;
}
 
Example 20
Source File: OAuth10aResponseUrl.java    From android-oauth-client with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the temporary access token issued by the authorization server.
 * <p>
 * Overriding is only supported for the purpose of calling the super
 * implementation and changing the return type, but nothing else.
 * </p>
 */
public OAuth10aResponseUrl setToken(String token) {
    this.token = Preconditions.checkNotNull(token);
    return this;
}