Example usage for com.google.gson JsonObject get

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

Introduction

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

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:co.cask.cdap.proto.codec.ScheduleSpecificationCodec.java

License:Apache License

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

    JsonElement scheduleTypeJson = jsonObj.get("scheduleType");
    ScheduleType scheduleType;//from w  w w  .j  ava 2  s . c o m
    if (scheduleTypeJson == null) {
        // For backwards compatibility with spec persisted with older versions than 2.8, we need these lines
        scheduleType = null;
    } else {
        scheduleType = context.deserialize(jsonObj.get("scheduleType"), ScheduleType.class);
    }

    Schedule schedule = null;
    if (scheduleType == null) {
        JsonObject scheduleObj = jsonObj.get("schedule").getAsJsonObject();
        String name = context.deserialize(scheduleObj.get("name"), String.class);
        String description = context.deserialize(scheduleObj.get("description"), String.class);
        String cronEntry = context.deserialize(scheduleObj.get("cronEntry"), String.class);
        schedule = Schedules.builder(name).setDescription(description).createTimeSchedule(cronEntry);
    } else {
        switch (scheduleType) {
        case TIME:
            schedule = context.deserialize(jsonObj.get("schedule"), TimeSchedule.class);
            break;
        case STREAM:
            schedule = context.deserialize(jsonObj.get("schedule"), StreamSizeSchedule.class);
            break;
        }
    }

    ScheduleProgramInfo program = context.deserialize(jsonObj.get("program"), ScheduleProgramInfo.class);
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    return new ScheduleSpecification(schedule, program, properties);
}

From source file:co.cask.cdap.proto.codec.SparkSpecificationCodec.java

License:Apache License

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

    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    String mainClassName = jsonObj.get("mainClassName").getAsString();
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);

    Resources driverResources = deserializeResources(jsonObj, "driver", context);
    Resources executorResources = deserializeResources(jsonObj, "executor", context);

    return new SparkSpecification(className, name, description, mainClassName, properties, driverResources,
            executorResources);/*from  ww  w.  java  2  s . c  o  m*/
}

From source file:co.cask.cdap.proto.codec.SparkSpecificationCodec.java

License:Apache License

/**
 * Deserialize {@link Resources} object from a json property named with {@code <prefix>Resources}.
 * A {@code null} value will be returned if no such property exist.
 *//* w ww. j a  v a 2 s  .co  m*/
@Nullable
private Resources deserializeResources(JsonObject jsonObj, String prefix, JsonDeserializationContext context) {
    String name = prefix + "Resources";
    JsonElement element = jsonObj.get(name);
    return element == null ? null : (Resources) context.deserialize(element, Resources.class);
}

From source file:co.cask.cdap.proto.codec.WorkerSpecificationCodec.java

License:Apache License

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

    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    Resources resources = context.deserialize(jsonObj.get("resources"), Resources.class);
    Set<String> datasets = deserializeSet(jsonObj.get("datasets"), context, String.class);
    int instances = jsonObj.get("instances").getAsInt();

    return new WorkerSpecification(className, name, description, properties, datasets, resources, instances);
}

From source file:co.cask.cdap.proto.codec.WorkflowActionSpecificationCodec.java

License:Apache License

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

    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    Set<String> datasets = deserializeSet(jsonObj.get("datasets"), context, String.class);
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);

    return new DefaultWorkflowActionSpecification(className, name, description, properties, datasets);
}

From source file:co.cask.cdap.proto.codec.WorkflowNodeCodec.java

License:Apache License

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

    WorkflowNodeType type = context.deserialize(jsonObj.get("nodeType"), WorkflowNodeType.class);

    switch (type) {
    case ACTION:/*from w  w w  .  ja  v  a2 s.c om*/
        return context.deserialize(json, WorkflowActionNode.class);
    case FORK:
        return context.deserialize(json, WorkflowForkNode.class);
    case CONDITION:
        return context.deserialize(json, WorkflowConditionNode.class);
    }
    return null;
}

From source file:co.cask.cdap.proto.codec.WorkflowNodeThrowableCodec.java

License:Apache License

@Override
public WorkflowNodeThrowable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String className = jsonObj.get("className").getAsString();
    String message = jsonObj.get("message").getAsString();
    JsonArray stackTraces = jsonObj.get("stackTraces").getAsJsonArray();
    StackTraceElement[] stackTraceElements = context.deserialize(stackTraces, StackTraceElement[].class);
    JsonElement cause = jsonObj.get("cause");
    WorkflowNodeThrowable dfc = null;/*from  www  .j av  a2 s . c  o m*/
    if (cause != null) {
        dfc = context.deserialize(cause, WorkflowNodeThrowable.class);
    }
    return new WorkflowNodeThrowable(className, message, stackTraceElements, dfc);
}

From source file:co.cask.cdap.proto.codec.WorkflowSpecificationCodec.java

License:Apache License

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

    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    List<WorkflowNode> nodes = deserializeList(jsonObj.get("nodes"), context, WorkflowNode.class);
    Map<String, DatasetCreationSpec> localDatasetSpec = deserializeMap(jsonObj.get("localDatasetSpecs"),
            context, DatasetCreationSpec.class);

    return new WorkflowSpecification(className, name, description, properties, nodes, localDatasetSpec);
}

From source file:co.cask.cdap.security.tools.AccessTokenClient.java

License:Apache License

private String getAuthenticationServerAddress() throws IOException {
    HttpClient client = new DefaultHttpClient();
    String baseUrl = "http";
    // ssl settings
    if (useSsl) {
        baseUrl = "https";
        if (disableCertCheck) {
            try {
                client = getHTTPClient();
            } catch (Exception e) {
                errorDebugExit("Could not create HTTP Client with SSL enabled", e);
                System.exit(1);/*www  . j  a va 2 s.  c  o m*/
            }
        }
    }
    HttpGet get = new HttpGet(String.format("%s://%s:%d", baseUrl, host, port));
    HttpResponse response = client.execute(get);

    if (response.getStatusLine().getStatusCode() == 200) {
        System.out.println("Security is not enabled. No Access Token may be acquired");
        System.exit(0);
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteStreams.copy(response.getEntity().getContent(), bos);
    String responseBody = bos.toString("UTF-8");
    bos.close();
    JsonParser parser = new JsonParser();
    JsonObject responseJson = (JsonObject) parser.parse(responseBody);
    JsonArray addresses = responseJson.get("auth_uri").getAsJsonArray();
    ArrayList<String> list = new ArrayList<String>();
    for (JsonElement e : addresses) {
        list.add(e.getAsString());
    }
    return list.get(new Random().nextInt(list.size()));
}

From source file:co.cask.cdap.security.tools.AccessTokenClient.java

License:Apache License

public String execute0(String[] args) {
    buildOptions();/*  w w w  .j  a v a 2s .c  o m*/
    parseArguments(args);
    if (help) {
        return "";
    }

    String baseUrl;
    try {
        baseUrl = getAuthenticationServerAddress();
    } catch (IOException e) {
        errorDebugExit("Could not find Authentication service to connect to.", e);
        return null;
    }

    System.out.println(String.format("Authentication server address is: %s", baseUrl));
    System.out.println(String.format("Authenticating as: %s", username));

    HttpClient client = new DefaultHttpClient();
    if (useSsl && disableCertCheck) {
        try {
            client = getHTTPClient();
        } catch (Exception e) {
            errorDebugExit("Could not create HTTP Client with SSL enabled", e);
            return null;
        }
    }

    // construct the full URL and verify its well-formedness
    try {
        URI.create(baseUrl);
    } catch (IllegalArgumentException e) {
        System.err.println(
                "Invalid base URL '" + baseUrl + "'. Check the validity of --host or --port arguments.");
        return null;
    }

    HttpGet get = new HttpGet(baseUrl);
    String auth = Base64.encodeBase64String(String.format("%s:%s", username, password).getBytes());
    auth = auth.replaceAll("(\r|\n)", "");
    get.addHeader("Authorization", String.format("Basic %s", auth));
    HttpResponse response;
    try {
        response = client.execute(get);
    } catch (IOException e) {
        errorDebugExit("Error sending HTTP request: " + e.getMessage(), e);
        return null;
    }
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        System.out.println(
                "Authentication failed. Please ensure that the username and password provided are correct.");
        return null;
    } else {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ByteStreams.copy(response.getEntity().getContent(), bos);
            String responseBody = bos.toString("UTF-8");
            bos.close();
            JsonParser parser = new JsonParser();
            JsonObject responseJson = (JsonObject) parser.parse(responseBody);
            String token = responseJson.get(ExternalAuthenticationServer.ResponseFields.ACCESS_TOKEN)
                    .getAsString();

            PrintWriter writer = new PrintWriter(filePath, "UTF-8");
            writer.write(token);
            writer.close();
            System.out.println("Your Access Token is:" + token);
            System.out.println("Access Token saved to file " + filePath);
        } catch (Exception e) {
            System.err.println("Could not parse response contents.");
            e.printStackTrace(System.err);
            return null;
        }
    }
    client.getConnectionManager().shutdown();
    return "OK.";
}