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

com.dropbox.core.DbxHost 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.IOException;

/**
 * This is for mocking things out during testing.  Most of the time you won't have to deal with
 * this class; just use the default value: {@link DbxHost#Default}.
 */
public final class DbxHost
{
    /**
     * The standard Dropbox hosts: "api.dropbox.com", "api-content.dropbox.com",
     * and "www.dropbox.com"
     */
	public static final DbxHost Default = new DbxHost("api.dropbox.com", "api-content.dropbox.com", "www.dropbox.com");

    /**
     * The host name of the main Dropbox API server.
     * The default is "api.dropbox.com".
     */
	public final String api;

    /**
     * The host name of the Dropbox API content server.
     * The default is "api-content.dropbox.com".
     */
	public final String content;

    /**
     * The host name of the Dropbox web server.  Used during user authorization.
     * The default is "www.dropbox.com".
     */
	public final String web;

    /**
     * @param api {@link #api}
     * @param content {@link #content}
     * @param web {@link #web}
     */
	public DbxHost(String api, String content, String web)
	{
		this.api = api;
		this.content = content;
		this.web = web;
	}

    public boolean equals(Object o)
    {
        return getClass().equals(o.getClass()) && equals((DbxHost)o);
    }

    public boolean equals(DbxHost o)
    {
        return api.equals(o.api) && content.equals(o.content) && web.equals(o.web);
    }

    private static DbxHost fromBaseHost(String s)
    {
        return new DbxHost("api-" + s, "api-content-" + s, "meta-" + s);
    }

    public static final JsonExtractor Extractor = new JsonExtractor()
    {
        @Override
        public DbxHost extract(JsonParser parser) throws IOException, JsonExtractionException
        {
            JsonToken t = parser.getCurrentToken();
            if (t == JsonToken.VALUE_STRING) {
                String s = parser.getText();
                JsonExtractor.nextToken(parser);
                return DbxHost.fromBaseHost(s);
            }
            else if (t == JsonToken.START_OBJECT) {
                JsonLocation top = parser.getTokenLocation();
                nextToken(parser);

                String api = null;
                String content = null;
                String web = null;

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

                    try {
                        if (fieldName.equals("api")) {
                            api = JsonExtractor.StringExtractor.extractField(parser, fieldName, api);
                        }
                        else if (fieldName.equals("content")) {
                            content = JsonExtractor.StringExtractor.extractField(parser, fieldName, content);
                        }
                        else if (fieldName.equals("web")) {
                            web = JsonExtractor.StringExtractor.extractField(parser, fieldName, web);
                        }
                        else {
                            throw new JsonExtractionException("unknown field", parser.getCurrentLocation());
                        }
                    }
                    catch (JsonExtractionException ex) {
                        throw ex.addFieldContext(fieldName);
                    }
                }

                JsonExtractor.expectObjectEnd(parser);

                if (api == null) throw new JsonExtractionException("missing field \"api\"", top);
                if (content == null) throw new JsonExtractionException("missing field \"content\"", top);
                if (web == null) throw new JsonExtractionException("missing field \"web\"", top);

                return new DbxHost(api, content, web);
            }
            else {
                throw new JsonExtractionException("expecting a string or an object", parser.getTokenLocation());
            }
        }
    };
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy