Example usage for com.google.gson JsonArray add

List of usage examples for com.google.gson JsonArray add

Introduction

In this page you can find the example usage for com.google.gson JsonArray add.

Prototype

public void add(JsonElement element) 

Source Link

Document

Adds the specified element to self.

Usage

From source file:com.confighub.api.user.dashboard.MyRepositories.java

License:Open Source License

@GET
@Produces("application/json")
public Response getAssignments(@HeaderParam("Authorization") String token) {
    JsonArray json = new JsonArray();
    Store store = new Store();

    try {//from   w  ww  .jav a 2 s .co m
        UserAccount user = TokenState.getUser(token, store);
        if (null == user)
            return Response.status(401).build();

        if (null != user.getRepositories()) {
            for (Repository repository : user.getRepositories()) {
                JsonObject repositoryJ = new JsonObject();

                repositoryJ.addProperty("name", repository.getName());
                repositoryJ.addProperty("description",
                        null == repository.getDescription() ? "" : repository.getDescription());
                repositoryJ.addProperty("owner", repository.getAccountName());
                repositoryJ.addProperty("isPrivate", repository.isPrivate());
                repositoryJ.addProperty("userCount", repository.getUserCount());

                json.add(repositoryJ);
            }
        }

        // if a user is registered as admin/owner of an organization, then
        // they have access to all their repositories
        Set<Organization> orgs = user.getOrganizations();
        if (null != orgs) {
            for (Organization org : orgs) {
                Set<Repository> orgRepos = org.getRepositories();
                if (null == orgRepos)
                    continue;

                for (Repository repository : orgRepos) {
                    JsonObject repositoryJ = new JsonObject();

                    repositoryJ.addProperty("name", repository.getName());
                    repositoryJ.addProperty("description",
                            null == repository.getDescription() ? "" : repository.getDescription());
                    repositoryJ.addProperty("owner", repository.getAccountName());
                    repositoryJ.addProperty("isPrivate", repository.isPrivate());
                    repositoryJ.addProperty("userCount", repository.getUserCount());

                    json.add(repositoryJ);
                }
            }
        }

        Gson gson = new Gson();
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } finally {
        store.close();
    }
}

From source file:com.confighub.api.user.GetAccount.java

License:Open Source License

private void getPersonalAccountData(JsonObject json, UserAccount loggedInUser, Account account, Store store)
        throws ConfigException {
    UserAccount accUser = account.getUser();

    json.addProperty("t", "u");
    json.addProperty("name", accUser.getName());
    json.addProperty("creationTs", accUser.getCreateDate().getTime());

    boolean isLoggedInUsersViewingPersonalAccount = null == loggedInUser ? false
            : loggedInUser.getAccount().equals(account);
    json.addProperty("own", isLoggedInUsersViewingPersonalAccount);

    if (isLoggedInUsersViewingPersonalAccount) {
        json.addProperty("email", accUser.getEmail());
        json.addProperty("blogSub", accUser.isEmailBlog());
        json.addProperty("repoSub", accUser.isEmailRepoCritical());
    }/* w  w  w .j  a  v  a  2  s.  c  o  m*/

    // Organizations
    if (isLoggedInUsersViewingPersonalAccount && null != accUser.getOrganizations()) {
        JsonArray organizationsJson = new JsonArray();
        for (Organization o : accUser.getOrganizations()) {
            JsonObject oj = new JsonObject();
            oj.addProperty("un", o.getAccountName());
            oj.addProperty("name", o.getName());
            oj.addProperty("id", o.getId());
            oj.addProperty("creationTs", o.getCreateDate().getTime());
            oj.addProperty("role", o.isOwner(accUser) ? "Owner" : "Admin");

            getOrganizationManagement(oj, o);

            if (null != o.getRepositories()) {
                JsonArray repos = new JsonArray();
                o.getRepositories().parallelStream().forEach(r -> repos.add(r.getName()));
                oj.add("repos", repos);
            }

            organizationsJson.add(oj);
        }
        json.add("orgs", organizationsJson);

    }

    // Repositories
    Set<Repository> repositories = new HashSet<>();
    repositories.addAll(accUser.getRepositories());
    Set<Organization> organizations = accUser.getOrganizations();

    if (null != organizations)
        organizations.forEach(o -> repositories.addAll(o.getRepositories()));

    JsonArray repos = new JsonArray();
    if (null != repositories) {
        for (Repository repo : repositories) {

            if (repo.hasReadAccess(loggedInUser)) {
                JsonObject rj = getRepoJson(repo);

                if (repo.isOwner(accUser))
                    rj.addProperty("role", "Owner");
                else if (repo.isAdmin(accUser))
                    rj.addProperty("role", "Admin");
                else if (repo.isMember(loggedInUser))
                    rj.addProperty("role", "Team Member");

                if (isLoggedInUsersViewingPersonalAccount)
                    rj.addProperty("canLeave", !repo.isAdminOrOwner(accUser));

                rj.addProperty("ut", repo.getUserType(loggedInUser).name());
                rj.addProperty("demo", repo.isDemo());

                Team team = store.getTeamForMember(repo, accUser);
                if (null != team)
                    rj.add("team", GsonHelper.teamToJson(team));

                repos.add(rj);
            }
        }
    }

    json.add("repos", repos);
}

From source file:com.confighub.api.user.GetAccount.java

License:Open Source License

private void getOrganizationManagement(JsonObject json, Organization organization) {
    JsonArray adminsJson = new JsonArray();
    if (null != organization.getAdministrators())
        organization.getAdministrators().forEach(admin -> adminsJson.add(getUser(admin)));

    JsonArray ownersJson = new JsonArray();
    if (null != organization.getOwners())
        organization.getOwners().forEach(owner -> ownersJson.add(getUser(owner)));

    json.add("admins", adminsJson);
    json.add("owners", ownersJson);
}

From source file:com.confighub.api.user.GetAccount.java

License:Open Source License

private void getOrganizationAccountData(JsonObject json, UserAccount loggedInUser, Account account, Store store)
        throws ConfigException {
    Organization organization = account.getOrganization();

    json.addProperty("t", "o");

    json.addProperty("un", organization.getAccountName());
    json.addProperty("name", organization.getName());
    json.addProperty("creationTs", organization.getCreateDate().getTime());

    boolean adminOrOwner = false;

    if (organization.isAdmin(loggedInUser)) {
        adminOrOwner = true;/*from   w w  w . j  a  v  a2  s .co  m*/
        json.addProperty("own", "adm");
    } else if (organization.isOwner(loggedInUser)) {
        adminOrOwner = true;
        json.addProperty("own", "own");
    }

    if (adminOrOwner) {
        getOrganizationManagement(json, organization);
    }

    JsonArray repos = new JsonArray();
    if (null != account.getRepositories()) {
        for (Repository repo : account.getRepositories()) {
            if (adminOrOwner || repo.hasReadAccess(loggedInUser)) {
                JsonObject rj = getRepoJson(repo);

                boolean canLeave = false;
                if (repo.isOwner(loggedInUser))
                    rj.addProperty("role", "Owner");
                else if (repo.isAdmin(loggedInUser))
                    rj.addProperty("role", "Admin");
                else if (repo.isMember(loggedInUser)) {
                    rj.addProperty("role", "Team Member");
                    canLeave = true;
                }

                rj.addProperty("ut", repo.getUserType(loggedInUser).name());
                rj.addProperty("demo", repo.isDemo());

                rj.addProperty("canLeave", canLeave);

                Team team = store.getTeamForMember(repo, loggedInUser);
                if (null != team)
                    rj.add("team", GsonHelper.teamToJson(team));

                repos.add(rj);
            }
        }
    }

    json.add("repos", repos);
}

From source file:com.confighub.api.user.GetAdministeredAccounts.java

License:Open Source License

@GET
@Produces("application/json")
public Response get(@HeaderParam("Authorization") String token) {
    Store store = new Store();

    try {// www .  j ava  2s .c o m
        UserAccount user = TokenState.getUser(token, store);
        if (null == user)
            return Response.status(401).build();

        JsonArray json = new JsonArray();

        JsonObject i = new JsonObject();
        i.addProperty("un", user.getUsername());
        i.addProperty("type", "user");
        json.add(i);

        Set<Organization> orgs = user.getOrganizations();
        if (null != orgs) {
            for (Organization o : orgs) {
                i = new JsonObject();
                i.addProperty("un", o.getAccountName());
                i.addProperty("type", "org");
                json.add(i);
            }
        }

        Gson gson = new Gson();
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } finally {
        store.close();
    }

}

From source file:com.confighub.api.user.UserSearch.java

License:Open Source License

@GET
@Produces("application/json")
public Response getAssignments(@QueryParam("t") String searchTerm) {
    Store store = new Store();

    try {/*from w  ww  . j a v  a 2  s .c  o  m*/
        JsonArray jsonUsers = new JsonArray();

        searchTerm = searchTerm.contains(",") ? searchTerm.split(",")[0] : searchTerm;
        List<UserAccount> users = store.searchUsers(searchTerm, 10);
        if (null != users) {
            for (UserAccount user : users) {
                JsonObject jsonUser = new JsonObject();
                jsonUser.addProperty("un", user.getUsername());
                jsonUser.addProperty("name", user.getName());
                jsonUsers.add(jsonUser);
            }
        }

        Gson gson = new Gson();
        return Response.ok(gson.toJson(jsonUsers), MediaType.APPLICATION_JSON).build();
    } finally {
        store.close();
    }
}

From source file:com.confighub.api.util.FileHelper.java

License:Open Source License

/**
 *
 * @param properties/*  w  w  w.jav a2s  .c o m*/
 * @param includeComments
 * @return
 */
public static JsonArray getJsonArray(final List<Property> properties, final boolean includeComments) {
    JsonArray arr = new JsonArray();

    for (Property property : properties) {
        JsonObject p = new JsonObject();

        PropertyKey key = property.getPropertyKey();
        if (key.isDeprecated())
            p.addProperty("deprecated", true);
        if (key.isEncrypted())
            p.addProperty("encryption", key.getSecurityProfile().getName());

        if (includeComments && !Utils.isBlank(property.getReadme()))
            p.addProperty("comment", Utils.jsonString(property.getReadme()));
        p.addProperty("key", property.getKey());

        if (PropertyKey.ValueDataType.List.equals(key.getValueDataType())) {
            JsonArray r = new Gson().fromJson(property.getValue(), JsonArray.class);
            p.add("value", r);
        } else
            p.addProperty("value", property.getValue());

        arr.add(p);
    }

    return arr;
}

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonArray propertyListToGSON(Repository repository, AccessRuleWrapper accessRuleWrapper,
        Collection<Property> properties, SecurityProfile ep, Pair<String, String>... attributes) {
    JsonArray jsonArray = new JsonArray();

    if (null == properties)
        return jsonArray;

    for (Property property : properties)
        jsonArray.add(propertyToGSON(repository, property, accessRuleWrapper, ep, null, attributes));

    return jsonArray;
}

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonObject levelAuditToGSON(CtxLevel l) {
    JsonObject json = levelToGSON(l);/* w w w .  j  a  v a  2  s  . c  o  m*/

    String diff = l.getDiffJson();
    if (Utils.isBlank(diff))
        json.add("diff", null);
    else
        json.add("diff", new Gson().fromJson(diff, JsonObject.class));

    switch (l.getType()) {
    case Group: {
        JsonArray assignments = new JsonArray();
        l.getMembers().forEach(c -> assignments.add(c.getName()));
        json.add("assignments", assignments);
        break;
    }
    case Member: {
        JsonArray assignments = new JsonArray();
        l.getGroups().forEach(c -> assignments.add(c.getName()));
        json.add("assignments", assignments);
        break;
    }
    }
    return json;
}

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonObject toJson(final SecurityProfile ep, String profile, Collection<PropertyKey> keys) {
    JsonObject jProfile = new JsonObject();
    jProfile.addProperty("name", ep.getName());

    if (null != ep.getCipher()) {
        jProfile.addProperty("cipher", ep.getCipher().getName());
        jProfile.addProperty("max", ep.getCipher().getMax());
        jProfile.addProperty("min", ep.getCipher().getMin());
    } else//  w  w w  .ja va2  s .c o m
        jProfile.addProperty("cipher", "None");

    JsonArray jKeys = new JsonArray();

    if (null != keys) {
        for (PropertyKey key : keys)
            jKeys.add(keyToGSON(key));
        jProfile.add("keys", jKeys);
    }

    return jProfile;
}