Repository: incubator-usergrid Updated Branches: refs/heads/master ed0c9dcec -> 327f69295
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/AggregateCounterSet.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/AggregateCounterSet.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/AggregateCounterSet.java new file mode 100755 index 0000000..06772f5 --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/AggregateCounterSet.java @@ -0,0 +1,98 @@ +package org.apache.usergrid.android.sdk.response; + +import static org.apache.usergrid.android.sdk.utils.JsonUtils.toJsonString; + +import java.util.List; +import java.util.UUID; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion; + +public class AggregateCounterSet { + private String name; + private UUID user; + private UUID group; + private UUID queue; + private String category; + private List<AggregateCounter> values; + + public AggregateCounterSet() { + } + + public AggregateCounterSet(String name, UUID user, UUID group, + String category, List<AggregateCounter> values) { + this.name = name; + this.user = user; + this.group = group; + this.category = category; + this.values = values; + } + + public AggregateCounterSet(String name, UUID queue, String category, + List<AggregateCounter> values) { + this.name = name; + setQueue(queue); + this.category = category; + this.values = values; + } + + @JsonSerialize(include = Inclusion.NON_NULL) + public UUID getUser() { + return user; + } + + public void setUser(UUID user) { + this.user = user; + } + + @JsonSerialize(include = Inclusion.NON_NULL) + public UUID getGroup() { + return group; + } + + public void setGroup(UUID group) { + this.group = group; + } + + @JsonSerialize(include = Inclusion.NON_NULL) + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + @JsonSerialize(include = Inclusion.NON_NULL) + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @JsonSerialize(include = Inclusion.NON_NULL) + public List<AggregateCounter> getValues() { + return values; + } + + public void setValues(List<AggregateCounter> values) { + this.values = values; + } + + @JsonSerialize(include = Inclusion.NON_NULL) + public UUID getQueue() { + return queue; + } + + public void setQueue(UUID queue) { + this.queue = queue; + } + + @Override + public String toString() { + return toJsonString(this); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/ApiResponse.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/ApiResponse.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/ApiResponse.java new file mode 100755 index 0000000..4f89912 --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/ApiResponse.java @@ -0,0 +1,755 @@ +package org.apache.usergrid.android.sdk.response; + +import static org.apache.usergrid.android.sdk.utils.JsonUtils.toJsonString; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.apache.usergrid.android.sdk.UGClient; +import org.apache.usergrid.android.sdk.entities.Entity; +import org.apache.usergrid.android.sdk.entities.Message; +import org.apache.usergrid.android.sdk.entities.User; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion; + +public class ApiResponse { + + private String accessToken; + + private String error; + private String errorDescription; + private String errorUri; + private String exception; + + private String path; + private String uri; + private String status; + private long timestamp; + private UUID application; + private List<Entity> entities; + private UUID next; + private String cursor; + private String action; + private List<Object> list; + private Object data; + private Map<String, UUID> applications; + private Map<String, JsonNode> metadata; + private Map<String, List<String>> params; + private List<AggregateCounterSet> counters; + private ClientCredentialsInfo credentials; + + private List<Message> messages; + private List<QueueInfo> queues; + private UUID last; + private UUID queue; + private UUID consumer; + + private User user; + private String rawResponse; + + private final Map<String, JsonNode> properties = new HashMap<String, JsonNode>(); + + /** + * @y.exclude + */ + public ApiResponse() { + } + + /** + * Returns the 'properties' property of the request. + * + * @return a Map object of the properties + */ + @JsonAnyGetter + public Map<String, JsonNode> getProperties() { + return properties; + } + + /** + * @y.exclude + */ + @JsonAnySetter + public void setProperty(String key, JsonNode value) { + properties.put(key, value); + } + + /** + * Returns the OAuth token that was sent with the request + * + * @return the OAuth token + */ + @JsonProperty("access_token") + @JsonSerialize(include = Inclusion.NON_NULL) + public String getAccessToken() { + return accessToken; + } + + /** + * @y.exclude + */ + @JsonProperty("access_token") + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } + + /** + * Returns the 'error' property of the response. + * + * @return the error + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public String getError() { + return error; + } + + /** + * Sets the 'error' property of the response. + * + * @param error the error + */ + public void setError(String error) { + this.error = error; + } + + /** + * Returns the 'error_description' property of the response. + * + * @return the error description + */ + @JsonSerialize(include = Inclusion.NON_NULL) + @JsonProperty("error_description") + public String getErrorDescription() { + return errorDescription; + } + + /** + * Sets the 'error_description' property of the response. + * + * @param errorDescription the error description + */ + @JsonProperty("error_description") + public void setErrorDescription(String errorDescription) { + this.errorDescription = errorDescription; + } + + /** + * Returns the 'error_uri' property of the response. + * + * @return the error URI + */ + @JsonSerialize(include = Inclusion.NON_NULL) + @JsonProperty("error_uri") + public String getErrorUri() { + return errorUri; + } + + /** + * Sets the 'error_uri' property of the response. + * + * @param errorUri the error URI + */ + @JsonProperty("error_uri") + public void setErrorUri(String errorUri) { + this.errorUri = errorUri; + } + + /** + * Returns the 'exception' property of the response. + * + * @return the exception + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public String getException() { + return exception; + } + + /** + * Sets the 'exception' property of the response. + * + * @param exception the exception + */ + public void setException(String exception) { + this.exception = exception; + } + + /** + * Returns the path of the request, i.e. the portion of the + * request URI after the application name. + * + * @return the request path + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public String getPath() { + return path; + } + + /** + * @y.exclude + */ + public void setPath(String path) { + this.path = path; + } + + /** + * Returns the full URI of the request. + * + * @return the full request URI + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public String getUri() { + return uri; + } + + /** + * @y.exclude + */ + public void setUri(String uri) { + this.uri = uri; + } + + /** + * Returns the status property from the response. Only + * applies to certain organization and application-level requests. + * + * @return the status + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public String getStatus() { + return status; + } + + /** + * @y.exclude + */ + public void setStatus(String status) { + this.status = status; + } + + /** + * Returns the timestamp of the response + * + * @return the UNIX timestamp + */ + public long getTimestamp() { + return timestamp; + } + + /** + * @y.exclude + */ + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + /** + * Returns the UUID of the application that was targeted + * by the request. + * + * @return the application UUID + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public UUID getApplication() { + return application; + } + + /** + * @y.exclude + */ + public void setApplication(UUID application) { + this.application = application; + } + + /** + * Returns the entities from the response as a List + * of Entity objects. + * + * @return the entities + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public List<Entity> getEntities() { + return entities; + } + + /** + * @y.exclude + */ + public void setEntities(List<Entity> entities) { + this.entities = entities; + } + + /** + * Returns a count of the number of entities in the response. + * + * @return the number of entities in the response + */ + public int getEntityCount() { + if (entities == null) { + return 0; + } + return entities.size(); + } + + /** + * Returns the first entity in the result set, or null + * if there were no entities. + * + * @return an Entity object + * @see org.apache.usergrid.android.sdk.entities.Entity + */ + public Entity getFirstEntity() { + if ((entities != null) && (entities.size() > 0)) { + return entities.get(0); + } + return null; + } + + /** + * Returns the first entity in the result set. + * + * @return an Entity object + * @see org.apache.usergrid.android.sdk.entities.Entity + */ + public <T extends Entity> T getFirstEntity(Class<T> t) { + return Entity.toType(getFirstEntity(), t); + } + + /** + * Returns the last entity in the result set. + * + * @return an Entity object + * @see org.apache.usergrid.android.sdk.entities.Entity + */ + public Entity getLastEntity() { + if ((entities != null) && (entities.size() > 0)) { + return entities.get(entities.size() - 1); + } + return null; + } + + /** + * Returns the last entity in the result set. + * + * @return an Entity object + * @see org.apache.usergrid.android.sdk.entities.Entity + */ + public <T extends Entity> T getLastEntity(Class<T> t) { + return Entity.toType(getLastEntity(), t); + } + + /** + * Returns the a List of all entitie from the response. + * + * @return a List object + * @see org.apache.usergrid.android.sdk.entities.Entity + */ + public <T extends Entity> List<T> getEntities(Class<T> t) { + return Entity.toType(entities, t); + } + + /** + * Returns the 'next' property of the response. + * + * @return the 'next' property + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public UUID getNext() { + return next; + } + + /** + * @y.exclude + */ + public void setNext(UUID next) { + this.next = next; + } + + /** + * Returns the cursor for retrieving the next page of results + * + * @return the cursor + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public String getCursor() { + return cursor; + } + + /** + * @y.exclude + */ + public void setCursor(String cursor) { + this.cursor = cursor; + } + + /** + * Returns the 'action' property from the response. + * + * @return the 'action' property + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public String getAction() { + return action; + } + + /** + * @y.exclude + */ + public void setAction(String action) { + this.action = action; + } + + /** + * Returns the 'list' property from the response. + * + * @return the 'list' property + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public List<Object> getList() { + return list; + } + + /** + * @y.exclude + */ + public void setList(List<Object> list) { + this.list = list; + } + + /** + * Returns the 'data' property of the response from a + * request to create, retrieve or update an admin user. + * + * @return the 'data' property of the user entity + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public Object getData() { + return data; + } + + /** + * @y.exclude + */ + public void setData(Object data) { + this.data = data; + } + + /** + * For requests to get all applications in an organization, returns + * the applications and their UUIDs as a Map object. + * + * @return the applications in the organization + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public Map<String, UUID> getApplications() { + return applications; + } + + /** + * @y.exclude + */ + public void setApplications(Map<String, UUID> applications) { + this.applications = applications; + } + + /** + * Returns the 'metadata' property of the response as a Map object. + * + * @return the 'metadata' property + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public Map<String, JsonNode> getMetadata() { + return metadata; + } + + /** + * @y.exclude + */ + public void setMetadata(Map<String, JsonNode> metadata) { + this.metadata = metadata; + } + + /** + * Returns the URL parameters that were sent with the request. + * + * @return the URL parameters of the request + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public Map<String, List<String>> getParams() { + return params; + } + + /** + * @y.exclude + */ + public void setParams(Map<String, List<String>> params) { + this.params = params; + } + + /** + * Returns the counters from the response. + * + * @return a List of the counters + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public List<AggregateCounterSet> getCounters() { + return counters; + } + + /** + * @y.exclude + */ + public void setCounters(List<AggregateCounterSet> counters) { + this.counters = counters; + } + + /** + * Returns the client id and client secret from the response. This is + * used only for requests that generate or retrieve credentials. + * + * @return the client id and client secret + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public ClientCredentialsInfo getCredentials() { + return credentials; + } + + /** + * @y.exclude + */ + public void setCredentials(ClientCredentialsInfo credentials) { + this.credentials = credentials; + } + + + /** + * For requests to retrieve the admin users in an organization, returns + * the 'user' property from the response. + * + * @return a User object + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public User getUser() { + return user; + } + + /** + * @y.exclude + */ + public void setUser(User user) { + this.user = user; + } + + /** + * Returns the ApiResponse as a String + * + * @return the ApiResponse in String format + */ + @Override + public String toString() { + return toJsonString(this); + } + + /** + * For messaging queue requests, returns the 'messages' property. + * + * @return the 'messages' property + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public List<Message> getMessages() { + return messages; + } + + /** + * @y.exclude + */ + public void setMessages(List<Message> messages) { + this.messages = messages; + } + + /** + * For messaging queue requests, returns the number of messages + * in the response. + * + * @return the number of messages in the 'messages' property + */ + @JsonIgnore + public int getMessageCount() { + if (messages == null) { + return 0; + } + return messages.size(); + } + + /** + * For messaging queue requests, returns the first message + * in the response. + * + * @return the first message in the 'messages' property + */ + @JsonIgnore + public Message getFirstMessage() { + if ((messages != null) && (messages.size() > 0)) { + return messages.get(0); + } + return null; + } + + /** + * For messaging queue requests, returns the last message + * in the response. + * + * @return the last message in the 'messages' property + */ + @JsonIgnore + public Entity getLastMessage() { + if ((messages != null) && (messages.size() > 0)) { + return messages.get(messages.size() - 1); + } + return null; + } + + @JsonSerialize(include = Inclusion.NON_NULL) + public UUID getLast() { + return last; + } + + /** + * @y.exclude + */ + public void setLast(UUID last) { + this.last = last; + } + + /** + * For messaging queue requests, returns the queues + * in the response. + * + * @return the 'queues' property + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public List<QueueInfo> getQueues() { + return queues; + } + + /** + * @y.exclude + */ + public void setQueues(List<QueueInfo> queues) { + this.queues = queues; + } + + /** + * For messaging queue requests, returns the first queue + * in the response. + * + * @return the first queue in the 'queues' property + */ + @JsonIgnore + public QueueInfo getFirstQueue() { + if ((queues != null) && (queues.size() > 0)) { + return queues.get(0); + } + return null; + } + + /** + * For messaging queue requests, returns the last queue + * in the response. + * + * @return the last queue in the 'queues' property + */ + @JsonIgnore + public QueueInfo getLastQueue() { + if ((queues != null) && (queues.size() > 0)) { + return queues.get(queues.size() - 1); + } + return null; + } + + /** + * For messaging queue requests, returns the UUID of the + * last queue in the response. + * + * @return the queue UUID + */ + @JsonIgnore + public UUID getLastQueueId() { + QueueInfo q = getLastQueue(); + if (q != null) { + return q.getQueue(); + } + return null; + } + + /** + * For messaging queue requests, returns the UUID of the + * queue in the response. + * + * @return the queue UUID + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public UUID getQueue() { + return queue; + } + + /** + * @y.exclude + */ + public void setQueue(UUID queue) { + this.queue = queue; + } + + /** + * Returns the 'consumer' property from message queue requests. + * + * @return the 'consumer' property + */ + @JsonSerialize(include = Inclusion.NON_NULL) + public UUID getConsumer() { + return consumer; + } + + /** + * @y.exclude + */ + public void setConsumer(UUID consumer) { + this.consumer = consumer; + } + + /** + * @y.exclude + */ + public void setRawResponse(String rawResponse) { + this.rawResponse = rawResponse; + } + + /** + * Returns the raw JSON response as a String. + * + * @return the JSON response + */ + public String getRawResponse() { + return rawResponse; + } + + /** + * Sets the UGClient instance for all Entity objects in the response. + * + * @param UGClient an instance of UGClient + */ + public void setUGClient(UGClient client) { + if( (entities != null) && !entities.isEmpty() ) { + for ( Entity entity : entities ) { + entity.setUGClient(client); + } + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/ClientCredentialsInfo.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/ClientCredentialsInfo.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/ClientCredentialsInfo.java new file mode 100755 index 0000000..ffc2fad --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/ClientCredentialsInfo.java @@ -0,0 +1,45 @@ +package org.apache.usergrid.android.sdk.response; + +import static org.apache.usergrid.android.sdk.utils.JsonUtils.toJsonString; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * @y.exclude + */ +public class ClientCredentialsInfo { + + private String id; + private String secret; + + public ClientCredentialsInfo(String id, String secret) { + this.id = id; + this.secret = secret; + } + + @JsonProperty("client_id") + public String getId() { + return id; + } + + @JsonProperty("client_id") + public void setId(String id) { + this.id = id; + } + + @JsonProperty("client_secret") + public String getSecret() { + return secret; + } + + @JsonProperty("client_secret") + public void setSecret(String secret) { + this.secret = secret; + } + + @Override + public String toString() { + return toJsonString(this); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/QueueInfo.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/QueueInfo.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/QueueInfo.java new file mode 100755 index 0000000..59acf50 --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/response/QueueInfo.java @@ -0,0 +1,28 @@ +package org.apache.usergrid.android.sdk.response; + +import java.util.UUID; + +public class QueueInfo { + + String path; + UUID queue; + + public QueueInfo() { + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public UUID getQueue() { + return queue; + } + + public void setQueue(UUID queue) { + this.queue = queue; + } +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/DeviceUuidFactory.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/DeviceUuidFactory.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/DeviceUuidFactory.java new file mode 100755 index 0000000..304285a --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/DeviceUuidFactory.java @@ -0,0 +1,154 @@ +package org.apache.usergrid.android.sdk.utils; + +import static org.apache.usergrid.android.sdk.utils.ObjectUtils.isEmpty; + +import java.io.UnsupportedEncodingException; +import java.util.UUID; + +import android.content.Context; +import android.content.SharedPreferences; +import android.net.wifi.WifiManager; +import android.os.Build; +import android.provider.Settings.Secure; +import android.telephony.TelephonyManager; + +/** + * Tries to get the device ID as a UUID and fallsback to a generated UUID value + * if it doesn't work. + * + * @see http + * ://stackoverflow.com/questions/2785485/is-there-a-unique-android-device + * -id + * + */ +public class DeviceUuidFactory { + protected static final String PREFS_FILE = "device_id.xml"; + protected static final String PREFS_DEVICE_ID = "device_id"; + + protected static UUID uuid; + + public DeviceUuidFactory(Context context) { + + if (uuid == null) { + synchronized (DeviceUuidFactory.class) { + if (uuid == null) { + final SharedPreferences prefs = context + .getSharedPreferences(PREFS_FILE, 0); + final String id = prefs.getString(PREFS_DEVICE_ID, null); + + if (id != null) { + // Use the ids previously computed and stored in the + // prefs file + uuid = UUID.fromString(id); + + } else { + + final String androidId = Secure + .getString(context.getContentResolver(), + Secure.ANDROID_ID); + + // Use the Android ID unless it's broken, in which case + // fallback on deviceId, + // unless it's not available, then fallback on a random + // number which we store + // to a prefs file + try { + if (!"9774d56d682e549c".equals(androidId)) { + uuid = UUID.nameUUIDFromBytes(androidId + .getBytes("utf8")); + } else { + final String deviceId = ((TelephonyManager) context + .getSystemService(Context.TELEPHONY_SERVICE)) + .getDeviceId(); + uuid = deviceId != null ? UUID + .nameUUIDFromBytes(deviceId + .getBytes("utf8")) + : generateDeviceUuid(context); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + // Write the value out to the prefs file + prefs.edit() + .putString(PREFS_DEVICE_ID, uuid.toString()) + .commit(); + + } + + } + } + } + + } + + private UUID generateDeviceUuid(Context context) { + + // Get some of the hardware information + String buildParams = Build.BOARD + Build.BRAND + Build.CPU_ABI + + Build.DEVICE + Build.DISPLAY + Build.FINGERPRINT + Build.HOST + + Build.ID + Build.MANUFACTURER + Build.MODEL + Build.PRODUCT + + Build.TAGS + Build.TYPE + Build.USER; + + // Requires READ_PHONE_STATE + TelephonyManager tm = (TelephonyManager) context + .getSystemService(Context.TELEPHONY_SERVICE); + + // gets the imei (GSM) or MEID/ESN (CDMA) + String imei = tm.getDeviceId(); + + // gets the android-assigned id + String androidId = Secure.getString(context.getContentResolver(), + Secure.ANDROID_ID); + + // requires ACCESS_WIFI_STATE + WifiManager wm = (WifiManager) context + .getSystemService(Context.WIFI_SERVICE); + + // gets the MAC address + String mac = wm.getConnectionInfo().getMacAddress(); + + // if we've got nothing, return a random UUID + if (isEmpty(imei) && isEmpty(androidId) && isEmpty(mac)) { + return UUID.randomUUID(); + } + + // concatenate the string + String fullHash = buildParams.toString() + imei + androidId + mac; + + return UUID.nameUUIDFromBytes(fullHash.getBytes()); + } + + /** + * Returns a unique UUID for the current android device. As with all UUIDs, + * this unique ID is "very highly likely" to be unique across all Android + * devices. Much more so than ANDROID_ID is. + * + * The UUID is generated by using ANDROID_ID as the base key if appropriate, + * falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to + * be incorrect, and finally falling back on a random UUID that's persisted + * to SharedPreferences if getDeviceID() does not return a usable value. + * + * In some rare circumstances, this ID may change. In particular, if the + * device is factory reset a new device ID may be generated. In addition, if + * a user upgrades their phone from certain buggy implementations of Android + * 2.2 to a newer, non-buggy version of Android, the device ID may change. + * Or, if a user uninstalls your app on a device that has neither a proper + * Android ID nor a Device ID, this ID may change on reinstallation. + * + * Note that if the code falls back on using TelephonyManager.getDeviceId(), + * the resulting ID will NOT change after a factory reset. Something to be + * aware of. + * + * Works around a bug in Android 2.2 for many devices when using ANDROID_ID + * directly. + * + * @see http://code.google.com/p/android/issues/detail?id=10603 + * + * @return a UUID that may be used to uniquely identify your device for most + * purposes. + */ + public UUID getDeviceUuid() { + return uuid; + } +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/JsonUtils.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/JsonUtils.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/JsonUtils.java new file mode 100755 index 0000000..f1b8f5b --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/JsonUtils.java @@ -0,0 +1,166 @@ +package org.apache.usergrid.android.sdk.utils; + +import java.io.IOException; +import java.util.Map; +import java.util.UUID; + +import org.apache.usergrid.android.sdk.exception.ClientException; +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; + +public class JsonUtils { + + + static ObjectMapper mapper = new ObjectMapper(); + + public static String getStringProperty(Map<String, JsonNode> properties, + String name) { + JsonNode value = properties.get(name); + if (value != null) { + return value.asText(); + } + return null; + } + + public static void setStringProperty(Map<String, JsonNode> properties, + String name, String value) { + if (value == null) { + properties.remove(name); + } else { + properties.put(name, JsonNodeFactory.instance.textNode(value)); + } + } + + public static Long getLongProperty(Map<String, JsonNode> properties, + String name) { + JsonNode value = properties.get(name); + if (value != null) { + return value.asLong(0); + } + return null; + } + + public static void setLongProperty(Map<String, JsonNode> properties, + String name, Long value) { + if (value == null) { + properties.remove(name); + } else { + properties.put(name, JsonNodeFactory.instance.numberNode(value)); + } + } + + public static void setFloatProperty(Map<String, JsonNode> properties, String name, Float value){ + if(value == null){ + properties.remove(name); + }else{ + properties.put(name, JsonNodeFactory.instance.numberNode(value)); + } + } + + public static Boolean getBooleanProperty(Map<String, JsonNode> properties, + String name) { + JsonNode value = properties.get(name); + if (value != null) { + return value.asBoolean(); + } + return false; + } + + public static void setBooleanProperty(Map<String, JsonNode> properties, + String name, Boolean value) { + if (value == null) { + properties.remove(name); + } else { + properties.put(name, JsonNodeFactory.instance.booleanNode(value)); + } + } + + public static UUID getUUIDProperty(Map<String, JsonNode> properties, + String name) { + JsonNode value = properties.get(name); + if (value != null) { + UUID uuid = null; + try { + uuid = UUID.fromString(value.asText()); + } catch (Exception e) { + } + return uuid; + } + return null; + } + + public static void setUUIDProperty(Map<String, JsonNode> properties, + String name, UUID value) { + if (value == null) { + properties.remove(name); + } else { + properties.put(name, + JsonNodeFactory.instance.textNode(value.toString())); + } + } + + public static String toJsonString(Object obj) { + try { + return mapper.writeValueAsString(obj); + } catch (JsonGenerationException e) { + throw new ClientException("Unable to generate json", e); + } catch (JsonMappingException e) { + throw new ClientException("Unable to map json", e); + } catch (IOException e) { + throw new ClientException("IO error", e); + } + } + + public static <T> T parse(String json, Class<T> c) { + try { + return mapper.readValue(json, c); + } catch (JsonGenerationException e) { + throw new ClientException("Unable to generate json", e); + } catch (JsonMappingException e) { + throw new ClientException("Unable to map json", e); + } catch (IOException e) { + throw new ClientException("IO error", e); + } + } + + public static JsonNode toJsonNode(Object obj) { + return mapper.convertValue(obj, JsonNode.class); + } + + public static <T> T fromJsonNode(JsonNode json, Class<T> c) { + try { + JsonParser jp = json.traverse(); + return mapper.readValue(jp, c); + } catch (JsonGenerationException e) { + throw new ClientException("Unable to generate json", e); + } catch (JsonMappingException e) { + throw new ClientException("Unable to map json", e); + } catch (IOException e) { + throw new ClientException("IO error", e); + } + } + + public static <T> T getObjectProperty(Map<String, JsonNode> properties, + String name, Class<T> c) { + JsonNode value = properties.get(name); + if (value != null) { + return fromJsonNode(value, c); + } + return null; + } + + public static void setObjectProperty(Map<String, JsonNode> properties, + String name, Object value) { + if (value == null) { + properties.remove(name); + } else { + properties.put(name, + JsonNodeFactory.instance.textNode(value.toString())); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/MapUtils.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/MapUtils.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/MapUtils.java new file mode 100755 index 0000000..f9727e9 --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/MapUtils.java @@ -0,0 +1,23 @@ +package org.apache.usergrid.android.sdk.utils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MapUtils { + + public static <T> Map<String, T> newMapWithoutKeys(Map<String, T> map, + List<String> keys) { + Map<String, T> newMap = null; + if (map != null) { + newMap = new HashMap<String, T>(map); + } else { + newMap = new HashMap<String, T>(); + } + for (String key : keys) { + newMap.remove(key); + } + return newMap; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/ObjectUtils.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/ObjectUtils.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/ObjectUtils.java new file mode 100755 index 0000000..b8156aa --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/ObjectUtils.java @@ -0,0 +1,20 @@ +package org.apache.usergrid.android.sdk.utils; + +import java.util.Map; + +public class ObjectUtils { + + public static boolean isEmpty(Object s) { + if (s == null) { + return true; + } + if ((s instanceof String) && (((String) s).trim().length() == 0)) { + return true; + } + if (s instanceof Map) { + return ((Map<?, ?>) s).isEmpty(); + } + return false; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/21b60593/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/UrlUtils.java ---------------------------------------------------------------------- diff --git a/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/UrlUtils.java b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/UrlUtils.java new file mode 100755 index 0000000..9046624 --- /dev/null +++ b/sdks/android/src/main/java/org/apache/usergrid/android/sdk/utils/UrlUtils.java @@ -0,0 +1,108 @@ +package org.apache.usergrid.android.sdk.utils; + +import static org.apache.usergrid.android.sdk.utils.ObjectUtils.isEmpty; +import static java.net.URLEncoder.encode; + +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.usergrid.android.sdk.exception.ClientException; + +public class UrlUtils { + + + public static URL url(String s) { + try { + return new URL(s); + } catch (MalformedURLException e) { + throw new ClientException("Incorrect URL format", e); + } + } + + public static URL url(URL u, String s) { + try { + return new URL(u, s); + } catch (MalformedURLException e) { + throw new ClientException("Incorrect URL format", e); + } + } + + public static String path(Object... segments) { + String path = ""; + boolean first = true; + for (Object segment : segments) { + if (segment instanceof Object[]) { + segment = path((Object[]) segment); + } + if (!isEmpty(segment)) { + if (first) { + path = segment.toString(); + first = false; + } else { + if (!path.endsWith("/")) { + path += "/"; + } + path += segment.toString(); + } + } + } + return path; + } + + @SuppressWarnings("rawtypes") + public static String encodeParams(Map<String, Object> params) { + if (params == null) { + return ""; + } + boolean first = true; + StringBuilder results = new StringBuilder(); + for (Entry<String, Object> entry : params.entrySet()) { + if (entry.getValue() instanceof List) { + for (Object o : (List) entry.getValue()) { + if (!isEmpty(o)) { + if (!first) { + results.append('&'); + } + first = false; + results.append(entry.getKey()); + results.append("="); + try { + results.append(encode(o.toString(), "UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new ClientException("Unknown encoding", e); + } + } + } + } else if (!isEmpty(entry.getValue())) { + if (!first) { + results.append('&'); + } + first = false; + results.append(entry.getKey()); + results.append("="); + try { + results.append(encode(entry.getValue().toString(), "UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new ClientException("Unsupported string encoding", e); + } + } + } + return results.toString(); + } + + public static String addQueryParams(String url, Map<String, Object> params) { + if (params == null) { + return url; + } + if (!url.contains("?")) { + url += "?"; + } + url += encodeParams(params); + return url; + } + +}
