
com.dropbox.core.DbxAppInfo Maven / Gradle / Ivy
Show all versions of sdk Show documentation
package com.dropbox.core;
import java.io.IOException;
import static com.dropbox.core.util.StringUtil.jq;
import com.dropbox.core.json.JsonExtractionException;
import com.dropbox.core.json.JsonExtractor;
import com.dropbox.core.util.DumpWriter;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
/**
* Identifying information about your application.
*/
public class DbxAppInfo extends DbxDataObject implements java.io.Serializable
{
public static final long serialVersionUID = 0;
/**
* Your Dropbox app key (OAuth calls this the consumer key). You can
* create an app key and secret on the Dropbox developer website.
*/
public final String key;
/**
* Your Dropbox app secret (OAuth calls this the consumer secret). You can
* create an app key and secret on the Dropbox developer website.
*
*
* Make sure that this is kept a secret. Someone with your app secret can impesonate your
* application. People sometimes ask for help on the Dropbox API forums and
* copy/paste their code, which sometimes includes their app secret. Do not do that.
*
*/
public final String secret;
/**
* This is almost always {@link DbxHost#Default}. This is only set differently for testing
* purposes.
*/
public final DbxHost host;
/**
* @param key {@link #key}
* @param secret {@link #secret}
*/
public DbxAppInfo(String key, String secret)
{
checkKeyArg(key);
checkSecretArg(secret);
this.key = key;
this.secret = secret;
this.host = DbxHost.Default;
}
/**
* @param key {@link #key}
* @param secret {@link #secret}
* @param host {@link #host}
*/
public DbxAppInfo(String key, String secret, DbxHost host)
{
checkKeyArg(key);
checkSecretArg(secret);
this.key = key;
this.secret = secret;
this.host = host;
}
@Override
protected void dumpFields(DumpWriter out)
{
out.field("key");
dump(out, key);
out.field("secret");
dump(out, secret);
}
/**
* If they key's format looks correct, return {@code null}. Otherwise return
* a string that describes what the problem is.
*
*
* NOTE: This function only performs some cursory checks on the format of the key.
* Even if it * returns {@code null} (which means "no problem"), it doesn't mean
* that what you passed in is an actual valid Dropbox API app key.
*
*/
public static String getKeyFormatError(String key) { return getTokenPartError(key); }
/**
* If they secret's format looks correct, return {@code null}. Otherwise return
* a string that describes what the problem is.
*
*
* NOTE: This function only performs some cursory checks on the format. Even if it
* returns {@code null} (which means "no problem"), it doesn't mean that what
* you passed in is an actual valid Dropbox API app key.
*
*/
public static String getSecretFormatError(String key) { return getTokenPartError(key); }
// ------------------------------------------------------
// JSON parsing
public static final JsonExtractor Extractor = new JsonExtractor()
{
@Override
public final DbxAppInfo extract(JsonParser parser)
throws IOException, JsonExtractionException
{
JsonLocation top = JsonExtractor.expectObjectStart(parser);
String key = null;
String secret = null;
DbxHost host = null;
while (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
String fieldName = parser.getCurrentName();
parser.nextToken();
try {
if (fieldName.equals("key")) {
key = KeyExtractor.extractField(parser, fieldName, key);
}
else if (fieldName.equals("secret")) {
secret = SecretExtractor.extractField(parser, fieldName, secret);
}
else if (fieldName.equals("host")) {
host = DbxHost.Extractor.extractField(parser, fieldName, host);
}
else {
// Unknown field. Skip over it.
JsonExtractor.skipValue(parser);
}
}
catch (JsonExtractionException ex) {
throw ex.addFieldContext(fieldName);
}
}
JsonExtractor.expectObjectEnd(parser);
if (key == null) throw new JsonExtractionException("missing field \"key\"", top);
if (secret == null) throw new JsonExtractionException("missing field \"secret\"", top);
if (host == null) host = DbxHost.Default;
return new DbxAppInfo(key, secret, host);
}
};
public static final JsonExtractor KeyExtractor = new JsonExtractor()
{
@Override
public String extract(JsonParser parser) throws IOException, JsonExtractionException
{
try {
String v = parser.getText();
String error = getKeyFormatError(v);
if (error != null) {
throw new JsonExtractionException("bad format for app key: " + error, parser.getTokenLocation());
}
parser.nextToken();
return v;
}
catch (JsonParseException ex) {
throw JsonExtractionException.fromJackson(ex);
}
}
};
public static final JsonExtractor SecretExtractor = new JsonExtractor()
{
@Override
public String extract(JsonParser parser) throws IOException, JsonExtractionException
{
try {
String v = parser.getText();
String error = getKeyFormatError(v);
if (error != null) {
throw new JsonExtractionException("bad format for app secret: " + error, parser.getTokenLocation());
}
parser.nextToken();
return v;
}
catch (JsonParseException ex) {
throw JsonExtractionException.fromJackson(ex);
}
}
};
public static String getTokenPartError(String s)
{
if (s == null) return "can't be null";
if (s.length() == 0) return "can't be empty";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c < 0x21 || c > 0x7e) {
// Only allow normal visible ASCII characters.
return "invalid character at index " + i + ": " + jq("" + c);
}
}
return null;
}
public static void checkKeyArg(String key)
{
String error = getTokenPartError(key);
if (error == null) return;
throw new IllegalArgumentException("Bad 'key': " + error);
}
public static void checkSecretArg(String secret)
{
String error = getTokenPartError(secret);
if (error == null) return;
throw new IllegalArgumentException("Bad 'secret': " + error);
}
}