com.hazelcast.internal.json.JsonObject Java Examples

The following examples show how to use com.hazelcast.internal.json.JsonObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlightDataSource.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
private JsonArray pollForAircraft() throws IOException {
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
    StringBuilder response = new StringBuilder();
    try {
        con.setRequestMethod("GET");
        con.addRequestProperty("User-Agent", "Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 40.0.2214.91 Safari / 537.36");
        con.addRequestProperty("api-auth", API_AUTHENTICATION_KEY);
        int responseCode = con.getResponseCode();
        try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
        }
        if (responseCode != 200) {
            logger.info("API returned error: " + responseCode + " " + response);
            return new JsonArray();
        }
    } finally {
        con.disconnect();
    }

    JsonValue value = Json.parse(response.toString());
    JsonObject object = value.asObject();
    return object.get("acList").asArray();
}
 
Example #2
Source File: TwitterSources.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
private void fillTimestampedBuffer(SourceBuilder.TimestampedSourceBuffer<String> sourceBuffer) {
    queue.drainTo(buffer, MAX_FILL_ELEMENTS);
    for (String item : buffer) {
        try {
            JsonObject object = Json.parse(item).asObject();
            String timestampStr = object.getString("timestamp_ms", null);
            if (timestampStr != null) {
                long timestamp = Long.parseLong(timestampStr);
                sourceBuffer.add(item, timestamp);
            } else {
                logger.warning("The tweet doesn't contain 'timestamp_ms' field\n" + item);
            }
        } catch (Exception e) {
            logger.warning("Error getting 'timestamp_ms' field from the tweet: " + e + "\n" + item, e);
        }
    }
    buffer.clear();
}
 
Example #3
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 6 votes vote down vote up
private static Map<EndpointAddress, String> extractServices(JsonObject endpointsListJson,
                                                            List<EndpointAddress> privateAddresses) {
    Map<EndpointAddress, String> result = new HashMap<EndpointAddress, String>();
    Set<EndpointAddress> left = new HashSet<EndpointAddress>(privateAddresses);
    for (JsonValue item : toJsonArray(endpointsListJson.get("items"))) {
        String service = toString(item.asObject().get("metadata").asObject().get("name"));
        List<Endpoint> endpoints = parseEndpoints(item);
        // Service must point to exactly one endpoint address, otherwise the public IP would be ambiguous.
        if (endpoints.size() == 1) {
            EndpointAddress address = endpoints.get(0).getPrivateAddress();
            if (left.contains(address)) {
                result.put(address, service);
                left.remove(address);
            }
        }
    }
    if (!left.isEmpty()) {
        // At least one Hazelcast Member POD does not have a corresponding service.
        throw new KubernetesClientException(String.format("Cannot fetch services dedicated to the following PODs: %s", left));
    }
    return result;
}
 
Example #4
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
/**
 * Makes a REST call to Kubernetes API and returns the result JSON.
 *
 * @param urlString Kubernetes API REST endpoint
 * @return parsed JSON
 * @throws KubernetesClientException if Kubernetes API didn't respond with 200 and a valid JSON content
 */
private JsonObject callGet(final String urlString) {
    return RetryUtils.retry(new Callable<JsonObject>() {
        @Override
        public JsonObject call() {
            return Json
                    .parse(RestClient.create(urlString).withHeader("Authorization", String.format("Bearer %s", apiToken))
                                     .withCaCertificates(caCertificate)
                                     .get())
                    .asObject();
        }
    }, retries, NON_RETRYABLE_KEYWORDS);
}
 
Example #5
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static String extractNodePublicIp(JsonObject nodeJson) {
    for (JsonValue address : toJsonArray(nodeJson.get("status").asObject().get("addresses"))) {
        if ("ExternalIP".equals(address.asObject().get("type").asString())) {
            return address.asObject().get("address").asString();
        }
    }
    throw new KubernetesClientException("Node does not have ExternalIP assigned");
}
 
Example #6
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static Integer extractNodePort(JsonObject serviceJson) {
    JsonArray ports = toJsonArray(serviceJson.get("spec").asObject().get("ports"));
    // Service must have one and only one Node Port assigned.
    if (ports.size() != 1) {
        throw new KubernetesClientException("Cannot fetch nodePort from the service");
    }
    return ports.get(0).asObject().get("nodePort").asInt();
}
 
Example #7
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static Integer extractServicePort(JsonObject serviceJson) {
    JsonArray ports = toJsonArray(serviceJson.get("spec").asObject().get("ports"));
    // Service must have one and only one Node Port assigned.
    if (ports.size() != 1) {
        throw new KubernetesClientException("Cannot fetch nodePort from the service");
    }
    return ports.get(0).asObject().get("port").asInt();
}
 
Example #8
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static Map<EndpointAddress, String> extractNodes(JsonObject endpointsListJson,
                                                         List<EndpointAddress> privateAddresses) {
    Map<EndpointAddress, String> result = new HashMap<EndpointAddress, String>();
    Set<EndpointAddress> left = new HashSet<EndpointAddress>(privateAddresses);
    for (JsonValue item : toJsonArray(endpointsListJson.get("items"))) {
        for (JsonValue subset : toJsonArray(item.asObject().get("subsets"))) {
            JsonObject subsetObject = subset.asObject();
            List<Integer> ports = new ArrayList<Integer>();
            for (JsonValue port : toJsonArray(subsetObject.get("ports"))) {
                ports.add(port.asObject().get("port").asInt());
            }

            Map<EndpointAddress, String> nodes = new HashMap<EndpointAddress, String>();
            nodes.putAll(extractNodes(subsetObject.get("addresses"), ports));
            nodes.putAll(extractNodes(subsetObject.get("notReadyAddresses"), ports));
            for (Map.Entry<EndpointAddress, String> nodeEntry : nodes.entrySet()) {
                EndpointAddress address = nodeEntry.getKey();
                if (privateAddresses.contains(address)) {
                    result.put(address, nodes.get(address));
                    left.remove(address);
                }
            }
        }
    }
    if (!left.isEmpty()) {
        // At least one Hazelcast Member POD does not have 'nodeName' assigned.
        throw new KubernetesClientException(String.format("Cannot fetch nodeName from the following PODs: %s", left));
    }
    return result;
}
 
Example #9
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static String extractZone(JsonObject nodeJson) {
    JsonObject labels = nodeJson.get("metadata").asObject().get("labels").asObject();
    JsonValue zone = labels.get("failure-domain.kubernetes.io/zone");
    if (zone != null) {
        return toString(zone);
    }
    return toString(labels.get("failure-domain.beta.kubernetes.io/zone"));
}
 
Example #10
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static Map<String, String> extractAdditionalPropertiesFrom(JsonValue endpointAddressJson) {
    Set<String> knownFieldNames = new HashSet<String>(
            asList("ip", "nodeName", "targetRef", "hostname", "hazelcast-service-port"));

    Map<String, String> result = new HashMap<String, String>();
    for (JsonObject.Member member : endpointAddressJson.asObject()) {
        if (!knownFieldNames.contains(member.getName())) {
            result.put(member.getName(), toString(member.getValue()));
        }
    }
    return result;
}
 
Example #11
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static List<Endpoint> parseEndpointsList(JsonObject endpointsListJson) {
    List<Endpoint> endpoints = new ArrayList<Endpoint>();
    for (JsonValue item : toJsonArray(endpointsListJson.get("items"))) {
        endpoints.addAll(parseEndpoints(item));
    }
    return endpoints;
}
 
Example #12
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static boolean isReady(JsonObject podItemStatusJson) {
    for (JsonValue containerStatus : toJsonArray(podItemStatusJson.get("containerStatuses"))) {
        // If multiple containers are in one POD, then each needs to be ready.
        if (!containerStatus.asObject().get("ready").asBoolean()) {
            return false;
        }
    }
    return true;
}
 
Example #13
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private static List<Endpoint> parsePodsList(JsonObject podsListJson) {
    List<Endpoint> addresses = new ArrayList<Endpoint>();

    for (JsonValue item : toJsonArray(podsListJson.get("items"))) {
        JsonObject status = item.asObject().get("status").asObject();
        String ip = toString(status.get("podIP"));
        if (ip != null) {
            Integer port = extractContainerPort(item);
            addresses.add(new Endpoint(new EndpointAddress(ip, port), isReady(status)));
        }
    }
    return addresses;
}
 
Example #14
Source File: Aircraft.java    From hazelcast-jet-demos with Apache License 2.0 5 votes vote down vote up
@Override
public void fromJson(JsonObject json) {
    id = asLong(json.get("Id"));
    tsecs = asLong(json.get("TSecs"));
    rcvr = asLong(json.get("Rcvr"));
    icao = asString(json.get("Icao"));
    reg = asString(json.get("Reg"));
    alt = asLong(json.get("Alt"));
    galt = asLong(json.get("GAlt"));
    talt = asLong(json.get("TAlt"));
    call = asString(json.get("Call"));
    lat = asFloat(json.get("Lat"));
    lon = asFloat(json.get("Long"));
    posTime = asLong(json.get("PosTime"));
    posStale = asBoolean(json.get("PosStale"));
    spd = asFloat(json.get("Spd"));
    spdType = SpeedType.fromId(asInt(json.get("SpdTyp")));
    vsi = asLong(json.get("Vsi"));
    vsiType = VerticalSpeedType.fromId(asInt(json.get("VsiT")));
    type = asString(json.get("Type"));
    mdl = asString(json.get("Mdl"));
    man = asString(json.get("Man"));
    from = asString(json.get("From"));
    to = asString(json.get("To"));
    stops = asStringArray(json.get("Stops"));
    op = asString(json.get("Op"));
    opCode = asString(json.get("OpCode"));
    wtc = WakeTurbulanceCategory.fromId(asInt(json.get("WTC")));
    engines = asString(json.get("Engines"));
    engtype = EngineTypes.fromId(asInt(json.get("EngType")));
    engMount = EngineMount.fromId(asInt(json.get("EngMount")));
    species = Species.fromId(asInt(json.get("Species")));
    mil = asBoolean(json.get("Mil"));
    cou = asString(json.get("Cou"));
    gnd = asBoolean(json.get("Gnd"));
    year = asString(json.get("Year"));
}
 
Example #15
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 4 votes vote down vote up
private static String extractNodeName(JsonObject podJson) {
    return toString(podJson.get("spec").asObject().get("nodeName"));
}
 
Example #16
Source File: KubernetesClient.java    From hazelcast-kubernetes with Apache License 2.0 4 votes vote down vote up
private static String extractLoadBalancerIp(JsonObject serviceResponse) {
    return serviceResponse.get("status").asObject()
                          .get("loadBalancer").asObject()
                          .get("ingress").asArray().get(0).asObject()
                          .get("ip").asString();
}
 
Example #17
Source File: Aircraft.java    From hazelcast-jet-demos with Apache License 2.0 4 votes vote down vote up
@Override
public JsonObject toJson() {
    throw new UnsupportedOperationException();
}