Android Open Source - cloudmine-android Request D B Object






From Project

Back to project page cloudmine-android.

License

The source code is released under:

Copyright (c) 2012 CloudMine LLC, http://cloudmine.me Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software")...

If you think the Android project cloudmine-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cloudmine.api.db;
/*from w  w  w .  j  a  v a 2s  .c o m*/
import android.content.ContentValues;
import com.cloudmine.api.CMApiCredentials;
import com.cloudmine.api.LibrarySpecificClassCreator;
import com.cloudmine.api.Strings;
import com.cloudmine.api.CMApiCredentials;
import org.apache.http.Header;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import static com.cloudmine.api.db.RequestDBOpenHelper.*;


/**
 * Encapsulates all of the information needed to perform a request.
 *
 * <br>
 * Copyright CloudMine LLC. All rights reserved<br>
 * See LICENSE file included with SDK for details.
 */
public class RequestDBObject {


    public static enum Verb {
        GET("get"), PUT("put");
        private final String representation;

        private Verb(String representation) {
            this.representation = representation;
        }

        public String getRepresentation() {
            return representation;
        }

        public boolean is(String representation) {
            return getRepresentation().equalsIgnoreCase(representation);
        }

        public static Verb getVerb(String name) {
            for (Verb verb : values()) {
                if (verb.is(name)) {
                    return verb;
                }
            }
            return GET;
        }
    }

    public static enum SyncStatus {
        UNSYNCED, IN_PROGRESS, SYNCED;

        public static SyncStatus getSyncStatus(int value) {
            switch (value) {
                case 0:
                    return UNSYNCED;
                case 1:
                    return IN_PROGRESS;
                case 2:
                    return SYNCED;
                default:
                    return UNSYNCED;
            }
        }
    }

    public static RequestDBObject createApplicationObjectRequest(String objectId) {
        RequestDBObject request = new RequestDBObject(RequestConstants.APP_SAVE_URL, Verb.PUT, (String)null, objectId, -1, SyncStatus.UNSYNCED, new ArrayList<Header>(LibrarySpecificClassCreator.getCreator().getHeaderFactory().getCloudMineHeaders(CMApiCredentials.getApplicationApiKey())));
        return request;
    }

    public static RequestDBObject createApplicationFileRequest(String fileId) {
        RequestDBObject request = new RequestDBObject(RequestConstants.APP_SAVE_FILE_URL.copy().binary(fileId).asUrlString(), Verb.PUT, null, null, fileId, -1, SyncStatus.UNSYNCED, new ArrayList<Header>(LibrarySpecificClassCreator.getCreator().getHeaderFactory().getCloudMineHeaders(CMApiCredentials.getApplicationApiKey())));
        return request;
    }

    private final String requestUrl;
    private final Verb requestType;
    private String jsonBody;
    private byte[] body;
    private final String objectId;
    private final String fileId;
    private final int id;
    private final SyncStatus syncStatus;
    private final List<Header> headers;


    public RequestDBObject(String requestUrl, Verb requestType, String jsonBody) {
        this(requestUrl, requestType, jsonBody, new ArrayList<Header>());
    }

    /**
     * Create a new request that is unsynced
     * @param requestUrl
     * @param requestType
     * @param jsonBody
     * @param headers
     */
    public RequestDBObject(String requestUrl, Verb requestType, String jsonBody, List<Header> headers) {
        this(requestUrl, requestType, jsonBody, null, -1, SyncStatus.UNSYNCED, headers);
    }

    public RequestDBObject(String requestUrl, Verb requestType, String jsonBody, String objectId, int id, SyncStatus syncStatus, List<Header> headers) {
        this(requestUrl, requestType, jsonBody, objectId, null, id, syncStatus, headers);
    }

    public RequestDBObject(String requestUrl, Verb requestType, String jsonBody, String objectId, String fileId, int id, SyncStatus syncStatus, List<Header> headers) {
        this.requestUrl = requestUrl;
        this.requestType = requestType;
        this.jsonBody = jsonBody;
        this.id = id;
        this.syncStatus = syncStatus;
        if(headers == null)
            headers = new ArrayList<Header>();
        this.headers = headers;
        this.objectId = objectId;
        this.fileId = fileId;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public Verb getRequestType() {
        return requestType;
    }

    public String getJsonBody() {
        return jsonBody;
    }

    public void setJsonBody(String jsonBody) {
        this.jsonBody = jsonBody;
    }

    public int getId() {
        return id;
    }

    public SyncStatus getSyncStatus() {
        return syncStatus;
    }

    public List<Header> getHeaders() {
        return headers;
    }

    public void addHeader(Header toAdd) {
        headers.add(toAdd);
    }

    public String getObjectId() {
        return objectId;
    }

    public String getFileId() {
        return fileId;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public ContentValues[] toContentValues() {
        int numberOfValues = headers == null ?
                1 :
                headers.size() + 1;
        ContentValues[] values = new ContentValues[numberOfValues];
        values[0] = toRequestContentValues();
        int i = 1;
        for(ContentValues contentValues : toHeaderContentValues()) {
            values[i] = contentValues;
            i++;
        }
        return values;
    }

    public ContentValues toRequestContentValues() {
        ContentValues requestContentValues = new ContentValues();
        requestContentValues.put(KEY_REQUEST_JSON_BODY, jsonBody);
        requestContentValues.put(KEY_REQUEST_SYNCHRONIZED, syncStatus.ordinal());
        requestContentValues.put(KEY_REQUEST_TARGET_URL, requestUrl);
        requestContentValues.put(KEY_REQUEST_VERB, requestType.name());
        requestContentValues.put(KEY_REQUEST_OBJECT_ID, objectId);
        requestContentValues.put(KEY_REQUEST_FILE_ID, fileId);
        return requestContentValues;
    }

    public ContentValues[] toHeaderContentValues() {
        if(headers == null || headers.size() == 0) {
            return new ContentValues[0];
        }
        ContentValues[] headerContentValues = new ContentValues[headers.size()];
        int i = 0;
        for(Header header : headers) {
            headerContentValues[i] = headerToContentValues(header);
            i++;
        }
        return headerContentValues;
    }

    public HttpUriRequest toHttpRequest() {
        HttpUriRequest request = null;
        switch(requestType) {
            case GET:
                request = new HttpGet(requestUrl);
                break;
            case PUT:
                request = new HttpPut(requestUrl);
                try {
                    if(Strings.isNotEmpty(jsonBody)) {
                        ((HttpPut)request).setEntity(new StringEntity(jsonBody));
                        request.addHeader("Content-Type", "application/json");
                    }
                } catch (UnsupportedEncodingException e) {
                }
                if(body != null && body.length < 0) {
                    ((HttpPut)request).setEntity(new ByteArrayEntity(body));
                }
        }
        for(Header header : headers) {
            request.addHeader(header);
        }
        return request;
    }

    private static ContentValues headerToContentValues(Header header) {
        ContentValues headerValues = new ContentValues();
        if(header == null) {
            return headerValues;
        }
        headerValues.put(KEY_HEADER_NAME, header.getName());
        headerValues.put(KEY_HEADER_VALUE, header.getValue());
        return headerValues;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RequestDBObject that = (RequestDBObject) o;

        if (id != that.id) return false;
        if (fileId != null ? !fileId.equals(that.fileId) : that.fileId != null) return false;
        if (headers != null ? !headers.equals(that.headers) : that.headers != null) return false;
        if (jsonBody != null ? !jsonBody.equals(that.jsonBody) : that.jsonBody != null) return false;
        if (objectId != null ? !objectId.equals(that.objectId) : that.objectId != null) return false;
        if (requestType != that.requestType) return false;
        if (requestUrl != null ? !requestUrl.equals(that.requestUrl) : that.requestUrl != null) return false;
        if (syncStatus != that.syncStatus) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = requestUrl != null ? requestUrl.hashCode() : 0;
        result = 31 * result + (requestType != null ? requestType.hashCode() : 0);
        result = 31 * result + (jsonBody != null ? jsonBody.hashCode() : 0);
        result = 31 * result + (objectId != null ? objectId.hashCode() : 0);
        result = 31 * result + (fileId != null ? fileId.hashCode() : 0);
        result = 31 * result + id;
        result = 31 * result + (syncStatus != null ? syncStatus.hashCode() : 0);
        result = 31 * result + (headers != null ? headers.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "RequestDBObject{" +
                "requestUrl='" + requestUrl + '\'' +
                ", requestType=" + requestType +
                ", jsonBody='" + jsonBody + '\'' +
                ", objectId='" + objectId + '\'' +
                ", fileId='" + fileId + '\'' +
                ", id=" + id +
                ", syncStatus=" + syncStatus +
                ", headers=" + headers +
                '}';
    }
}




Java Source Code List

com.cloudmine.api.AccessListController.java
com.cloudmine.api.BaseCMUser.java
com.cloudmine.api.BaseCacheableCMFile.java
com.cloudmine.api.CMAndroidSocial.java
com.cloudmine.api.CMCreditCard.java
com.cloudmine.api.CMSharedPreferencesConstants.java
com.cloudmine.api.CurrentHandlerHavers.java
com.cloudmine.api.DeviceIdentifier.java
com.cloudmine.api.HasHandler.java
com.cloudmine.api.LocallySavable.java
com.cloudmine.api.db.BaseLocallySavableCMAccessList.java
com.cloudmine.api.db.BaseLocallySavableCMObject.java
com.cloudmine.api.db.CMObjectDBOpenHelper.java
com.cloudmine.api.db.LocallySavableCMGeoPoint.java
com.cloudmine.api.db.RequestConstants.java
com.cloudmine.api.db.RequestDBObject.java
com.cloudmine.api.db.RequestDBOpenHelper.java
com.cloudmine.api.db.RequestPerformerService.java
com.cloudmine.api.gui.AuthenticationDialog.java
com.cloudmine.api.gui.VolleyAuthenticationDialog.java
com.cloudmine.api.loopj.AsyncHttpClient.java
com.cloudmine.api.loopj.AsyncHttpRequest.java
com.cloudmine.api.loopj.AsyncHttpResponseHandler.java
com.cloudmine.api.loopj.PersistentCookieStore.java
com.cloudmine.api.loopj.RequestParams.java
com.cloudmine.api.loopj.RetryHandler.java
com.cloudmine.api.loopj.SerializableCookie.java
com.cloudmine.api.loopj.SimpleMultipartEntity.java
com.cloudmine.api.rest.AndroidAsynchronousHttpClient.java
com.cloudmine.api.rest.AndroidBase64Encoder.java
com.cloudmine.api.rest.AndroidHeaderFactory.java
com.cloudmine.api.rest.AndroidUserCMWebService.java
com.cloudmine.api.rest.BaseAccessListCreateRequest.java
com.cloudmine.api.rest.BaseAccessListLoadRequest.java
com.cloudmine.api.rest.BaseAccessListModificationRequest.java
com.cloudmine.api.rest.BaseAddPaymentMethodRequest.java
com.cloudmine.api.rest.BaseChangeUserIdentifierRequest.java
com.cloudmine.api.rest.BaseChangeUserPasswordRequest.java
com.cloudmine.api.rest.BaseChargeCardRequest.java
com.cloudmine.api.rest.BaseFileCreationRequest.java
com.cloudmine.api.rest.BaseFileDeleteRequest.java
com.cloudmine.api.rest.BaseFileLoadRequest.java
com.cloudmine.api.rest.BaseImageLoadRequest.java
com.cloudmine.api.rest.BaseLoadPaymentMethodsRequest.java
com.cloudmine.api.rest.BaseLoadUserProfilesRequest.java
com.cloudmine.api.rest.BaseObjectDeleteRequest.java
com.cloudmine.api.rest.BaseObjectLoadRequest.java
com.cloudmine.api.rest.BaseObjectModificationRequest.java
com.cloudmine.api.rest.BaseProfileLoadRequest.java
com.cloudmine.api.rest.BaseProfileUpdateRequest.java
com.cloudmine.api.rest.BaseRemovePaymentMethodRequest.java
com.cloudmine.api.rest.BaseResetPasswordRequest.java
com.cloudmine.api.rest.BaseSnippetRequest.java
com.cloudmine.api.rest.BaseUserCreationRequest.java
com.cloudmine.api.rest.BaseUserLoginRequest.java
com.cloudmine.api.rest.BaseUserLogoutRequest.java
com.cloudmine.api.rest.CMImageLoader.java
com.cloudmine.api.rest.CMNetwork.java
com.cloudmine.api.rest.CloudMineRequest.java
com.cloudmine.api.rest.CompleteSocialLoginRequest.java
com.cloudmine.api.rest.DialogListener.java
com.cloudmine.api.rest.DiskBitmapCache.java
com.cloudmine.api.rest.ObjectLoadRequestBuilder.java
com.cloudmine.api.rest.OkHttpStack.java
com.cloudmine.api.rest.RequestBuilder.java
com.cloudmine.api.rest.SharedRequestQueueHolders.java
com.cloudmine.api.rest.VolleyAsynchronousHttpClient.java
com.cloudmine.api.rest.callbacks.AndroidCallback.java
com.cloudmine.api.rest.callbacks.PaymentResponseCallback.java
com.cloudmine.api.rest.response.PaymentResponse.java