JavaCloud.Cloud.java Source code

Java tutorial

Introduction

Here is the source code for JavaCloud.Cloud.java

Source

/**
 * Copyright (c) 2014 Marta Nabozny, Maciej Nabozny
 *
 * This file is part of OverCluster project.
 *
 * OverCluster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package JavaCloud;

import JavaCloud.Models.Token;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;

public class Cloud {
    private String address;
    private String login;
    private String password;
    private String seed;

    public Cloud(String address, String login, String password) throws CoreException {
        this.address = address;
        this.login = login;
        this.password = password;
        JSONObject object = new JSONObject();
        object.put("login", login);

        this.seed = ((JSONObject) Utils.request(address, "/user/user/get_seed/", object)).get("seed").toString();
    }

    public static Cloud register(String address, String login, String password, String name, String surname,
            String email) throws CoreException {
        JSONObject object = new JSONObject();
        object.put("login", login);
        object.put("password", password);
        object.put("name", name);
        object.put("surname", surname);
        object.put("email", email);
        Utils.request(address, "/user/user/register/", object);
        return new Cloud(address, login, password);
    }

    public ArrayList<Token> tokenList() throws CoreException {
        JSONObject object = new JSONObject();
        object.put("login", login);
        object.put("pw_hash", Utils.calcHash(password, seed));

        JSONArray jsontokens = (JSONArray) Utils.request(address, "/user/token/get_list/", object);

        ArrayList<Token> tokens = new ArrayList<Token>();
        for (int i = 0; i < jsontokens.size(); i++) {
            tokens.add(new Token(address, login, password, seed, (JSONObject) jsontokens.get(i)));
        }

        return tokens;
    }

    public Token tokenCreate(String name, String funciton_filter, String token_valid_to) throws CoreException {
        JSONObject object = new JSONObject();
        object.put("login", login);
        object.put("pw_hash", Utils.calcHash(password, seed));
        object.put("name", name);
        object.put("function_filter", funciton_filter);
        if (token_valid_to != null)
            object.put("token_valid_to", token_valid_to);

        JSONObject jsontoken = (JSONObject) Utils.request(address, "/user/token/create/", object);
        return new Token(address, login, password, seed, jsontoken);
    }

    public Api getApi() throws CoreException {
        ArrayList<Token> tokens = tokenList();
        for (Token token : tokens) {
            if (token.name.equals("JavaCloud")) {
                return new Api(address, token.token);
            }
        }

        Token token = tokenCreate("JavaCloud", "^", "");
        return new Api(address, token.token);
    }
}