Example usage for com.google.gson JsonObject add

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

Introduction

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

Prototype

public void add(String property, JsonElement value) 

Source Link

Document

Adds a member, which is a name-value pair, to self.

Usage

From source file:com.confighub.api.repository.user.files.UploadFiles.java

License:Open Source License

@POST
@Path("/{account}/{repository}")
public Response uploadFile(@PathParam("account") String account, @PathParam("repository") String repositoryName,
        @HeaderParam("Authorization") String token, @HeaderParam("path") String path,
        @HeaderParam("context") String fileContext, @HeaderParam("fileName") String fileName,
        @HeaderParam("sg") String securityGroupName, @HeaderParam("password") String password,
        @HeaderParam("changeComment") String changeComment,
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {
    JsonObject json = new JsonObject();
    Gson gson = new Gson();

    String content;//  ww w . ja va2  s. com

    try {
        StringWriter writer = new StringWriter();
        IOUtils.copy(uploadedInputStream, writer, "UTF-8");
        content = writer.toString();
    } catch (IOException e) {
        json.addProperty("message", "Failed to process file upload request");
        json.addProperty("success", false);
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    }

    Store store = new Store();

    int status = validateWrite(account, repositoryName, token, store, true);
    if (0 != status)
        return Response.status(status).build();

    try {
        Collection<CtxLevel> context = ContextParser.parseAndCreate(fileContext, repository, store, user, null);

        store.begin();
        RepoFile newFile = store.createRepoFile(user, repository, path, fileName, content, context, true,
                securityGroupName, password, changeComment);
        store.commit();

        json.add("file", newFile.toJson());
        json.addProperty("success", true);

        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } catch (ConfigException e) {
        store.rollback();

        json.addProperty("message", e.getMessage());
        json.addProperty("success", false);

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

}

From source file:com.confighub.api.repository.user.property.ContextChange.java

License:Open Source License

@POST
@Path("/value/{account}/{repository}")
@Produces("application/json")
public Response checkValue(@PathParam("account") String account, @PathParam("repository") String repositoryName,
        @FormParam("propertyId") Long propertyId, @FormParam("key") String keyString,
        @FormParam("context") String propertyContext, @HeaderParam("Authorization") String token) {
    JsonObject json = new JsonObject();
    Gson gson = new Gson();
    Store store = new Store();

    try {//from  www.  j  ava  2  s .com
        int status = validate(account, repositoryName, token, store);
        if (0 != status)
            return Response.status(status).build();

        json.addProperty("success", true);
        PropertyKey key = store.getKey(repository, keyString);
        if (null == key)
            return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();

        Set<Property> all = key.getProperties();
        Collection<CtxLevel> context = ContextParser.parseAndCreate(propertyContext, repository, store, user,
                null);
        SecurityProfile ep = null;

        int score = 0;
        for (CtxLevel l : context)
            score += l.getContextScore();

        for (Property prop : all) {
            if (prop.getId().equals(propertyId))
                continue;

            if (prop.isActive() && prop.getContextWeight() == score
                    && CollectionUtils.isEqualCollection(context, prop.getContext())) {
                json.addProperty("conflict", prop.getId());
                json.add("conflictProperty", GsonHelper.propertyToGSON(repository, prop,
                        repository.getRulesWrapper(user), ep, GsonHelper.PropertyAttn.conflict));
                break;
            }
        }

        json.addProperty("score", score);

        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();

    } catch (ConfigException e) {
        json.addProperty("success", false);
        json.addProperty("message", e.getMessage());
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } finally {
        store.close();
    }
}

From source file:com.confighub.api.repository.user.property.MergedKeyValues.java

License:Open Source License

@GET
@Path("/{account}/{repository}/{keys}")
@Produces("application/json")
public Response get(@PathParam("account") String account, @PathParam("repository") String repositoryName,
        @PathParam("keys") String keyStrings, @QueryParam("context") String contextString,
        @QueryParam("includeSiblings") boolean includeSiblings, @QueryParam("ts") Long ts,
        @HeaderParam("Authorization") String token) {
    Gson gson = new Gson();
    Store store = new Store();

    try {/*from  ww w .  ja  v  a2  s .c o m*/
        int status = validate(account, repositoryName, token, store);
        if (0 != status)
            return Response.status(status).build();

        Date dateObj = DateTimeUtils.dateFromTs(ts, repository.getCreateDate());
        String[] keys = keyStrings.split(",");
        JsonObject json = new JsonObject();

        for (String keyString : keys) {
            PropertyKey key = store.getKey(repository, keyString, dateObj);
            if (null == key) {
                json.addProperty("success", true);
                return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
            }
        }

        Collection<CtxLevel> ctx = ContextParser.parseAndCreate(contextString, repository, store, user,
                dateObj);
        Context context = new Context(store, repository, ctx, dateObj);
        SecurityProfile ep = null;

        if (includeSiblings)
            json.add("properties",
                    KeyProperties.getContextCategorizedProperties(user, repository, context, ep, keys));
        else
            json.add("properties",
                    KeyProperties.getContextRelevantProperties(user, repository, context, ep, keys));

        json.addProperty("success", true);
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();

    } catch (ConfigException e) {
        JsonObject json = new JsonObject();

        json.addProperty("success", false);
        json.addProperty("message", e.getMessage());
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } finally {
        store.close();
    }
}

From source file:com.confighub.api.repository.user.property.SaveProperty.java

License:Open Source License

@POST
@Path("/{account}/{repository}")
@Produces("application/json")
public Response update(@PathParam("account") String account, @PathParam("repository") String repositoryName,
        @HeaderParam("Authorization") String token, @FormParam("key") String key,
        @FormParam("comment") String comment, @FormParam("deprecated") boolean deprecated,
        @FormParam("vdt") String vdt, @FormParam("pushEnabled") boolean pushEnabled,
        @FormParam("value") String value, @FormParam("context") String propertyContext,
        @FormParam("changeComment") String changeComment, @FormParam("active") boolean active,
        @FormParam("spPassword") String spPassword, @FormParam("spName") String spName,
        @FormParam("propertyId") Long propertyId) {
    JsonObject json = new JsonObject();
    Gson gson = new Gson();
    Store store = new Store();

    try {/* w w w  .j a v  a  2  s  . c  om*/
        int status = validateWrite(account, repositoryName, token, store, true);
        if (0 != status)
            return Response.status(status).build();

        Collection<CtxLevel> context = ContextParser.parseAndCreate(propertyContext, repository, store, user,
                null);

        if (null != propertyId && propertyId >= 0) {
            store.begin();
            store.updateProperty(propertyId, value, context, changeComment, active, spPassword, user);
            store.commit();
        } else {
            if (Utils.isBlank(key)) {
                json.addProperty("message", "Key cannot be blank.");
                json.addProperty("success", false);
                return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
            }

            PropertyKey.ValueDataType valueDataType;
            try {
                valueDataType = PropertyKey.ValueDataType.valueOf(vdt);
            } catch (Exception e) {
                valueDataType = PropertyKey.ValueDataType.Text;
            }

            store.begin();
            propertyId = store.createProperty(key, value, active, comment, deprecated, pushEnabled,
                    valueDataType, context, changeComment, spPassword, spName, repository, user).getId();
            store.commit();
        }

        json.addProperty("success", true);
        json.addProperty("id", propertyId);

        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();

    } catch (ConfigException e) {
        store.rollback();

        switch (e.getErrorCode()) {
        case PROP_DUPLICATION_CONTEXT:
        case PROP_PARENT_LOCK:
        case PROP_CHILD_LOCK:
        case PROP_CONTEXT_DUPLICATE_DEPTH:
            json.addProperty("status", Store.KeyUpdateStatus.MERGE.name());
            break;

        case FILE_CIRCULAR_REFERENCE:
            json.add("circularRef", e.getJson());

        default:
            json.addProperty("status", "ERROR");
        }

        json.addProperty("message", e.getMessage());
        json.addProperty("success", false);

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

From source file:com.confighub.api.repository.user.security.DeleteSecurityProfile.java

License:Open Source License

@POST
@Path("/{account}/{repository}")
@Produces("application/json")
public Response update(@HeaderParam("Authorization") String token, @PathParam("account") String account,
        @PathParam("repository") String repositoryName, @FormParam("name") String name,
        @FormParam("password") String password) {
    JsonObject json = new JsonObject();
    Gson gson = new Gson();
    Store store = new Store();

    try {//  w ww . j  a v  a  2s .c  o  m
        int status = validateWrite(account, repositoryName, token, store, true);
        if (0 != status)
            return Response.status(status).build();

        store.begin();
        store.deleteSecurityProfile(user, repository, name, password);
        store.commit();

        json.addProperty("success", true);
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } catch (ConfigException e) {
        e.printStackTrace();
        store.rollback();

        json.addProperty("message", e.getMessage());
        json.add("errJson", e.getJson());
        json.addProperty("success", false);

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

From source file:com.confighub.api.repository.user.security.GetSecurityGroup.java

License:Open Source License

@POST
@Path("/{account}/{repository}")
@Produces("application/json")
public Response update(@HeaderParam("Authorization") String userToken, @PathParam("account") String account,
        @PathParam("repository") String repositoryName, @FormParam("profile") String profile,
        @FormParam("all") boolean allKeys) {
    JsonObject json = new JsonObject();
    Gson gson = new GsonBuilder().serializeNulls().create();
    Store store = new Store();

    try {// ww w  . j  a va2 s.  c o m
        int status = validate(account, repositoryName, userToken, store);
        if (0 != status)
            return Response.status(status).build();

        SecurityProfile ep = store.getSecurityProfile(user, repository, null, profile);
        if (null == ep) {
            json.addProperty("message", "Unable to find specified profile");
            json.addProperty("success", false);

            return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
        }

        json.add("profile", GsonHelper.toJson(ep, profile, null));
        json.addProperty("privAcc", repository.isOwner(user));

        JsonArray files = new JsonArray();
        if (null != ep.getFiles()) {
            for (RepoFile file : ep.getFiles()) {

                JsonObject fileJson = file.toJson();
                fileJson.addProperty("fullPath", file.getAbsPath());
                files.add(fileJson);
            }
        }

        json.add("files", files);

        JsonArray tokens = new JsonArray();
        if (null != ep.getTokens())
            for (Token token : ep.getTokens())
                tokens.add(GsonHelper.tokenToJson(token, false));
        json.add("tokens", tokens);

        json.addProperty("success", true);
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } catch (ConfigException e) {
        json.addProperty("message", e.getMessage());
        json.addProperty("success", false);

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

From source file:com.confighub.api.repository.user.security.GetSecurityProfiles.java

License:Open Source License

@GET
@Path("/{account}/{repository}")
@Produces("application/json")
public Response get(@HeaderParam("Authorization") String token, @PathParam("account") String account,
        @PathParam("repository") String repositoryName) {
    JsonObject json = new JsonObject();
    Gson gson = new Gson();
    SecurityStore store = new SecurityStore();

    try {//from   w  w w .j av  a2  s  . co  m
        int status = validate(account, repositoryName, token, store);
        if (0 != status)
            return Response.status(status).build();

        Collection<SecurityProfile> groups = store.getAllRepoEncryptionProfiles(user, repository);
        JsonArray groupJson = new JsonArray();

        for (SecurityProfile group : groups) {
            JsonObject g = new JsonObject();
            g.addProperty("name", group.getName());
            g.addProperty("cipher", null == group.getCipher() ? "" : group.getCipher().getName());
            g.addProperty("tokens", null == group.getTokens() ? 0 : group.getTokens().size());
            g.addProperty("files", null == group.getFiles() ? 0 : group.getFiles().size());
            g.addProperty("keys", null == group.getKeys() ? 0 : group.getKeys().size());
            groupJson.add(g);
        }

        json.add("groups", groupJson);
        json.addProperty("success", true);

        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } catch (ConfigException e) {
        json.addProperty("message", e.getMessage());
        json.addProperty("success", false);

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

From source file:com.confighub.api.repository.user.tags.GetTags.java

License:Open Source License

@GET
@Path("/{account}/{repository}")
@Produces("application/json")
public Response get(@HeaderParam("Authorization") String token, @PathParam("account") String account,
        @PathParam("repository") String repositoryName) {
    JsonObject json = new JsonObject();
    Gson gson = new Gson();
    TagStore store = new TagStore();

    try {//from   ww  w .j  a  v  a  2s  . co m
        int status = validate(account, repositoryName, token, store);
        if (0 != status)
            return Response.status(status).build();

        JsonArray tagsArr = new JsonArray();
        Set<Tag> tags = repository.getTags();
        if (null != tags)
            tags.forEach(tag -> tagsArr.add(tag.toJson()));

        json.add("tags", tagsArr);
        json.addProperty("success", true);
        return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build();
    } catch (ConfigException e) {
        store.rollback();

        json.addProperty("message", e.getMessage());
        json.addProperty("success", false);

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

From source file:com.confighub.api.repository.user.team.GetAllMembers.java

License:Open Source License

@GET
@Path("/{account}/{repository}")
@Produces("application/json")
public Response update(@PathParam("account") String account, @PathParam("repository") String repositoryName,
        @HeaderParam("Authorization") String token) {
    JsonObject json = new JsonObject();
    Store store = new Store();

    try {//from  w  ww  .j  ava  2s . c o m
        int status = validate(account, repositoryName, token, store);
        if (0 != status)
            return Response.status(status).build();
        json.add("members", GsonHelper.allMembers(repository));
    } finally {
        store.close();
    }

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

From source file:com.confighub.api.repository.user.team.GetTeams.java

License:Open Source License

@AuthenticationNotRequired
@GET/*www  . jav  a2  s  .  co  m*/
@Path("/{account}/{repository}")
@Produces("application/json")
public Response get(@PathParam("account") String account, @PathParam("repository") String repositoryName,
        @HeaderParam("Authorization") String token) {
    Store store = new Store();

    try {
        int status = validate(account, repositoryName, token, store);
        if (0 != status)
            return Response.status(status).build();

        JsonObject json = new JsonObject();
        json.add("teams", GsonHelper.getTeams(repository.getTeams()));
        json.addProperty("accessControlEnabled", repository.isAccessControlEnabled());

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