Example usage for com.google.gson JsonObject has

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

Introduction

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

Prototype

public boolean has(String memberName) 

Source Link

Document

Convenience method to check if a member with the specified name is present in this object.

Usage

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String getErrorInfo(String jsonParams) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonObject jo;
    try {/*from  ww w  . java  2s.com*/
        jo = (JsonObject) jp.parse(jsonParams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }
    if (jo.has("errorInfo")) {
        String value = jo.getAsJsonPrimitive("errorInfo").getAsString();
        return value;
    } else {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static int getErrorCode(String jsonParams) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonObject jo;
    try {//from w  w w. j  a  v a2s .c  om
        jo = (JsonObject) jp.parse(jsonParams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }
    if (jo.has("errorCode")) {
        String value = jo.getAsJsonPrimitive("errorCode").getAsString();
        return Integer.parseInt(value);
    } else {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static JsonObject getParams(String jsonParams) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonObject jo;
    try {//  ww  w. ja v a  2s.  co  m
        jo = (JsonObject) jp.parse(jsonParams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    if (jo.has("params")) {
        return jo.getAsJsonObject("params");
    } else {
        return null;
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String checkSignAndRemove(String p, String signKey) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonElement jop = null;/*w  ww.ja v a 2 s  .c o  m*/
    try {
        jop = jp.parse(p);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        //jse.printStackTrace();
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    JsonObject jo = (JsonObject) jop;
    String np = null;
    if (jo.has("sign") || jo.has("params")) {
        String sign = jo.getAsJsonPrimitive("sign").getAsString();
        np = jo.getAsJsonObject("params").toString();
        String signValue = Security.crossingSign(np, signKey, "");
        if (!sign.equals(signValue)) {
            throw new CrossException(ErrorCode.COMMON_ERR_SIGN_BAD.getCode(),
                    ErrorCode.COMMON_ERR_SIGN_BAD.getInfo());
        }

        //remove sign field only and return
        Iterator<Entry<String, JsonElement>> it = jo.entrySet().iterator();
        JsonObject rjo = new JsonObject();
        Entry<String, JsonElement> en;
        while (it.hasNext()) {
            en = it.next();
            if (!en.getKey().equals("sign")) {
                rjo.add(en.getKey(), en.getValue());
            }
        }
        return rjo.toString();
    }

    return p;
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String buildDecryptedParams(String encryptParams, String decryptKey) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonElement jop = null;//from   w w  w . ja va2 s . c o  m
    try {
        jop = jp.parse(encryptParams);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        //jse.printStackTrace();
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    JsonObject jo = (JsonObject) jop;
    String np = null;
    if (jo.has("encrypted")) {
        String encrypted = jo.getAsJsonPrimitive("encrypted").getAsString();
        String paramsValue = Security.crossingDecrypt(encrypted, decryptKey, "DES");
        int ec = jo.getAsJsonPrimitive("errorCode").getAsInt();
        String ed = jo.getAsJsonPrimitive("errorInfo").getAsString();
        JsonParser jpr = new JsonParser();
        JsonObject jor = null;
        try {
            jor = (JsonObject) jpr.parse(paramsValue);
        } catch (JsonSyntaxException je) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
        jor.addProperty("errorCode", ec);
        jor.addProperty("errorInfo", ed);
        np = jor.toString();
        return np;
    }

    return encryptParams;
}

From source file:co.cask.cdap.common.conf.PluginClassDeserializer.java

License:Apache License

@Override
public PluginClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("PluginClass should be a JSON Object");
    }/*from  ww w.j a va  2s. co m*/

    JsonObject jsonObj = json.getAsJsonObject();

    String type = jsonObj.has("type") ? jsonObj.get("type").getAsString() : Plugin.DEFAULT_TYPE;
    String name = getRequired(jsonObj, "name").getAsString();
    String description = jsonObj.has("description") ? jsonObj.get("description").getAsString() : "";
    String className = getRequired(jsonObj, "className").getAsString();

    Set<String> endpointsSet = new HashSet<>();
    if (jsonObj.has("endpoints")) {
        endpointsSet = context.deserialize(jsonObj.get("endpoints"), ENDPOINTS_TYPE);
    }

    Map<String, PluginPropertyField> properties = jsonObj.has("properties")
            ? context.<Map<String, PluginPropertyField>>deserialize(jsonObj.get("properties"), PROPERTIES_TYPE)
            : ImmutableMap.<String, PluginPropertyField>of();

    return new PluginClass(type, name, description, className, null, properties, endpointsSet);
}

From source file:co.cask.cdap.common.conf.PluginClassDeserializer.java

License:Apache License

private JsonElement getRequired(JsonObject jsonObj, String name) {
    if (!jsonObj.has(name)) {
        throw new JsonParseException("Property '" + name + "' is missing from PluginClass.");
    }//from  w  w  w .j a v a 2 s  . co  m
    return jsonObj.get(name);
}

From source file:co.cask.cdap.gateway.GatewayTestBase.java

License:Apache License

protected static String getState(String programType, String appId, String programId) throws Exception {
    HttpResponse response = GatewayFastTestsSuite
            .doGet(String.format("/v3/namespaces/default/apps/%s/%s/%s/status", appId, programType, programId));
    JsonObject status = GSON.fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class);
    if (status != null && status.has("status")) {
        return status.get("status").getAsString();
    }//from w w  w . j av  a2s .  c  o  m
    return null;
}

From source file:co.cask.cdap.internal.app.ApplicationSpecificationCodec.java

License:Apache License

@Override
public ApplicationSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();

    String name = jsonObj.get("name").getAsString();

    String description = jsonObj.get("description").getAsString();
    String configuration = null;/*ww  w .j  av a  2s. co  m*/
    if (jsonObj.has("configuration")) {
        configuration = jsonObj.get("configuration").getAsString();
    }

    ArtifactId artifactId = context.deserialize(jsonObj.get("artifactId"), ArtifactId.class);

    Map<String, StreamSpecification> streams = deserializeMap(jsonObj.get("streams"), context,
            StreamSpecification.class);
    Map<String, String> datasetModules = deserializeMap(jsonObj.get("datasetModules"), context, String.class);
    Map<String, DatasetCreationSpec> datasetInstances = deserializeMap(jsonObj.get("datasetInstances"), context,
            DatasetCreationSpec.class);
    Map<String, FlowSpecification> flows = deserializeMap(jsonObj.get("flows"), context,
            FlowSpecification.class);
    Map<String, MapReduceSpecification> mapReduces = deserializeMap(jsonObj.get("mapReduces"), context,
            MapReduceSpecification.class);
    Map<String, SparkSpecification> sparks = deserializeMap(jsonObj.get("sparks"), context,
            SparkSpecification.class);
    Map<String, WorkflowSpecification> workflows = deserializeMap(jsonObj.get("workflows"), context,
            WorkflowSpecification.class);

    Map<String, ServiceSpecification> services = deserializeMap(jsonObj.get("services"), context,
            ServiceSpecification.class);

    Map<String, ScheduleSpecification> schedules = deserializeMap(jsonObj.get("schedules"), context,
            ScheduleSpecification.class);

    Map<String, WorkerSpecification> workers = deserializeMap(jsonObj.get("workers"), context,
            WorkerSpecification.class);
    Map<String, Plugin> plugins = deserializeMap(jsonObj.get("plugins"), context, Plugin.class);

    return new DefaultApplicationSpecification(name, description, configuration, artifactId, streams,
            datasetModules, datasetInstances, flows, mapReduces, sparks, workflows, services, schedules,
            workers, plugins);
}

From source file:co.cask.cdap.internal.app.services.http.AppFabricTestBase.java

License:Apache License

/**
 * Waits for the given program to transit to the given state.
 *//*from www  .j a  v a 2 s  .  c  o m*/
protected void waitState(final Id.Program programId, String state) throws Exception {
    Tasks.waitFor(state, new Callable<String>() {
        @Override
        public String call() throws Exception {
            String path = String.format("apps/%s/%s/%s/status", programId.getApplicationId(),
                    programId.getType().getCategoryName(), programId.getId());
            HttpResponse response = doGet(getVersionedAPIPath(path, programId.getNamespaceId()));
            JsonObject status = GSON.fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class);
            if (status == null || !status.has("status")) {
                return null;
            }
            return status.get("status").getAsString();
        }
    }, 60, TimeUnit.SECONDS);
}