All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dropbox.core.DbxAuthInfo Maven / Gradle / Ivy

The newest version!
package com.dropbox.core;

import com.dropbox.core.json.JsonExtractionException;
import com.dropbox.core.json.JsonExtractor;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import java.io.FileInputStream;
import java.io.IOException;

public final class DbxAuthInfo
{
    public final String accessToken;
    public final DbxHost host;

    public DbxAuthInfo(String accessToken, DbxHost host)
    {
        if (accessToken == null) throw new IllegalArgumentException("'accessToken' can't be null");
        if (accessToken == null) throw new IllegalArgumentException("'host' can't be null");

        this.accessToken = accessToken;
        this.host = host;
    }

    public static final JsonExtractor Extractor = new JsonExtractor()
    {
        @Override
        public final DbxAuthInfo extract(JsonParser parser)
            throws IOException, JsonExtractionException
        {
            JsonLocation top = JsonExtractor.expectObjectStart(parser);

            DbxHost host = null;
            String accessToken = null;

            while (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
                String fieldName = parser.getCurrentName();
                parser.nextToken();

                try {
                    if (fieldName.equals("host")) {
                        host = DbxHost.Extractor.extractField(parser, fieldName, host);
                    }
                    else if (fieldName.equals("access_token")) {
                        accessToken = StringExtractor.extractField(parser, fieldName, accessToken);
                    }
                    else {
                        // Unknown field.  Skip over it.
                        JsonExtractor.skipValue(parser);
                    }
                }
                catch (JsonExtractionException ex) {
                    throw ex.addFieldContext(fieldName);
                }
            }

            JsonExtractor.expectObjectEnd(parser);

            if (accessToken == null) throw new JsonExtractionException("missing field \"access_token\"", top);
            if (host == null) host = DbxHost.Default;

            return new DbxAuthInfo(accessToken, host);
        }
    };

    public static DbxAuthInfo loadFromFile(String file)
        throws Exception
    {
        try {
            return DbxAuthInfo.Extractor.extractFullAndClose(new FileInputStream(file));
        }
        catch (IOException ex) {
            throw new Exception("unable to read file \"" + file + "\": " + ex.getMessage());
        }
        catch (JsonExtractionException ex) {
            throw new Exception("\"" + file + "\": " + ex.getMessage());
        }
    }

    public static final class LoadException
    {

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy