All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jelastic.api.users.response.ApiTokenPermissionsResponse Maven / Gradle / Ivy

The newest version!
/*Server class MD5: 3dfa71050e0fae5db9cb70d8538a7339*/
package com.jelastic.api.users.response;

import com.jelastic.api.Response;
import com.jelastic.api.core.persistence.entity.ApiListItem;
import com.jelastic.api.core.persistence.entity.PolicyBehavior;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashSet;
import java.util.Set;

/**
 * @name Jelastic API Client
 * @version 8.11.2
 * @copyright Jelastic, Inc.
 */
public class ApiTokenPermissionsResponse extends Response {

    public static final String ROLE_ID = "roleId";

    public static final String API_LIST = "apiList";

    public static final String BEHAVIOR = "behavior";

    private Integer roleId;

    private PolicyBehavior behavior;

    private Set tokenPermissions;

    public ApiTokenPermissionsResponse() {
        super(Response.OK);
    }

    public ApiTokenPermissionsResponse(Set apiListItems) {
        super(Response.OK);
        this.tokenPermissions = apiListItems;
    }

    public ApiTokenPermissionsResponse(PolicyBehavior behavior, Set apiListItems) {
        super(Response.OK);
        this.behavior = behavior;
        this.tokenPermissions = apiListItems;
    }

    public ApiTokenPermissionsResponse(int roleId, Set apiListItems) {
        super(Response.OK);
        this.roleId = roleId;
        this.tokenPermissions = apiListItems;
    }

    public ApiTokenPermissionsResponse(int result, String error) {
        super(result, error);
    }

    public Set getTokenPermissions() {
        return tokenPermissions;
    }

    public void setTokenPermissions(Set tokenPermissions) {
        this.tokenPermissions = tokenPermissions;
    }

    @Override
    public JSONObject _toJSON() throws JSONException {
        JSONObject json = super._toJSON();
        json.putOpt(ROLE_ID, roleId);
        if (behavior != null) {
            json.put(BEHAVIOR, behavior.toString());
        }
        if (tokenPermissions != null) {
            JSONArray permissions = new JSONArray();
            for (ApiListItem apiListItem : tokenPermissions) {
                permissions.put(apiListItem);
            }
            json.put(API_LIST, permissions);
        }
        return json;
    }

    @Override
    protected ApiTokenPermissionsResponse _fromJSON(JSONObject json) throws JSONException {
        super._fromJSON(json);
        if (json.has(ROLE_ID)) {
            this.roleId = json.getInt(ROLE_ID);
        }
        if (json.has(BEHAVIOR)) {
            this.behavior = PolicyBehavior.valueOf(json.getString(BEHAVIOR));
        }
        JSONArray apiListArray = json.optJSONArray(API_LIST);
        if (apiListArray != null) {
            this.tokenPermissions = new HashSet<>();
            for (int i = 0; i < apiListArray.length(); i++) {
                this.tokenPermissions.add(new ApiListItem(apiListArray.getString(i)));
            }
        }
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy