com.kii.cloud.sync.CloudStorage.java Source code

Java tutorial

Introduction

Here is the source code for com.kii.cloud.sync.CloudStorage.java

Source

//
//
//  Copyright 2012 Kii Corporation
//  http://kii.com
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//  
//

package com.kii.cloud.sync;

import java.io.IOException;

import org.json.JSONException;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.kii.cloud.storage.EasyClient;
import com.kii.cloud.storage.dataType.KiiUser;
import com.kii.cloud.storage.manager.AuthManager;
import com.kii.cloud.storage.response.CloudExecutionException;
import com.kii.cloud.storage.response.UserResult;
import com.kii.mobilesdk.bridge.KiiUMInfo;
import com.kii.sync.KiiClient;
import com.kii.sync.SyncMsg;

/**
 * The Authentication is using the Cloud Storage
 */
public class CloudStorage implements Authentication {

    static final String TAG = "CloudStorageAuthentication";

    static final String PROPERTY_COUNTRY = "country";

    static final String URL_PFS = "/app/sync/pfs";
    static final String URL_RESOURCE = "/app/resource";
    static final String URL_USERGRID = ":12110";

    final String mBaseURL;

    KiiClient mSyncClient = null;
    AuthManager mUserMgr = null;
    Context mContext;

    public CloudStorage(Context context, KiiClient client, String baseURL) {
        mSyncClient = client;
        mContext = context;

        mBaseURL = baseURL;

        KiiUMInfo um = new KiiUMInfo(context, null, null, null, null);
        String appId = um.getAppId();
        if (TextUtils.isEmpty(appId)) {
            throw new RuntimeException("AppId meta data is not found in Manifest");
        }
        String appKey = um.getAppKey();
        if (TextUtils.isEmpty(appKey)) {
            throw new RuntimeException("AppKey meta data is not found in Manifest");
        }

        EasyClient.start(context, appId, appKey);
        EasyClient.getInstance().setBaseURL(baseURL + URL_USERGRID);
        mUserMgr = EasyClient.getInstance().getUserManager();

    }

    @Override
    public int changePassword(String oldPassword, String newPassword) {

        if (TextUtils.isEmpty(oldPassword) || TextUtils.isEmpty(newPassword)) {
            return SyncMsg.ERROR_INVALID_INPUT;
        }

        KiiUMInfo um = mSyncClient.getKiiUMInfo();
        if (um == null) {
            return SyncMsg.ERROR_USERNAME_EMPTY;
        }

        String username = um.getUserId();
        String password = um.getPassword();

        if (oldPassword.compareTo(password) == 0) {
            if (mUserMgr.getLoginUser() == null) {

                try {
                    mUserMgr.login(username, oldPassword);
                } catch (CloudExecutionException e) {
                    return SyncMsg.ERROR_AUTHENTICAION_ERROR;
                } catch (IOException e) {
                    return SyncMsg.ERROR_IO;
                }
            }
            UserResult result = null;
            try {
                result = mUserMgr.changePassword(oldPassword, newPassword);
            } catch (CloudExecutionException e) {
                return SyncMsg.ERROR_SERVER_TEMP_ERROR;
            } catch (IOException e) {
                return SyncMsg.ERROR_IO;
            }

            if (result.getKiiUser() != null) {
                return SyncMsg.OK;
            }
        }
        return SyncMsg.ERROR_AUTHENTICAION_ERROR;
    }

    @Override
    public int register(String email, String password, String country, String nickName, String mobile) {
        if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
            return SyncMsg.ERROR_INVALID_INPUT;
        }

        KiiUser user = new KiiUser();
        String userName = "cloud" + System.currentTimeMillis();

        try {

            // auto generate the username
            user.setUsername(userName);

            user.setEmail(email);

            if (!TextUtils.isEmpty(country)) {
                user.put(PROPERTY_COUNTRY, country);
            }
            if (!TextUtils.isEmpty(nickName)) {
                user.setName(nickName);
            }
            if (!TextUtils.isEmpty(mobile)) {
                user.setName(mobile);
            }
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "IllegalArgumentException:" + e.getMessage());
            return SyncMsg.ERROR_INVALID_INPUT;
        } catch (JSONException e) {
            Log.e(TAG, "JSONException:" + e.getMessage());
            return SyncMsg.ERROR_INVALID_INPUT;
        }

        UserResult result;
        try {
            result = mUserMgr.createUser(user, password);
        } catch (CloudExecutionException e) {
            return SyncMsg.ERROR_INVALID_INPUT;
        } catch (IOException e) {
            return SyncMsg.ERROR_IO;
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "JSONException:" + e.getMessage());
            return SyncMsg.ERROR_INVALID_INPUT;
        } catch (JSONException e) {
            Log.e(TAG, "JSONException:" + e.getMessage());
            return SyncMsg.ERROR_INVALID_INPUT;
        }
        user = result.getKiiUser();

        if (userName.compareTo(user.getUsername()) == 0)
            return SyncMsg.OK;

        return SyncMsg.ERROR_UNKNOWN_STATUSCODE;
    }

    @Override
    public int login(String email, String password) {
        UserResult result;
        try {
            result = mUserMgr.login(email, password);
        } catch (CloudExecutionException e) {
            return SyncMsg.ERROR_AUTHENTICAION_ERROR;
        } catch (IOException e) {
            return SyncMsg.ERROR_IO;
        }
        String accType = "KII_ID";
        KiiUser user = result.getKiiUser();
        if (user != null) {
            if (email.contains("@")) {
                accType = "EMAIL";
            }
            KiiUMInfo info = new KiiUMInfo(mContext, email, password, accType, email);
            mSyncClient.setKiiUMInfo(info);
        }
        return 0;
    }

    @Override
    public int logout() {
        // clear the session
        mUserMgr.logout();
        return 0;
    }

}