Java Code Examples for org.json.JSONArray#isEmpty()

The following examples show how to use org.json.JSONArray#isEmpty() . 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: NotificationHandler.java    From bitfinex-v2-wss-api-java with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void handleChannelData(final String action, final JSONArray payload) throws BitfinexClientException {
    logger.debug("Got notification callback {}", payload.toString());

    if (payload.isEmpty()) {
        return;
    }

    // Test for order error callback
    // [0,"n",[null,"on-req",null,null,[null,null,1513970684865000,"tBTCUSD",null,null,0.001,0.001,"EXCHANGE MARKET",null,null,null,null,null,null,null,12940,null,null,null,null,null,null,0,null,null],null,"ERROR","Invalid order: minimum size for BTC/USD is 0.002"]]
    if ("on-req".equals(payload.getString(1))) {
        final String state = payload.optString(6);
        if ("ERROR".equals(state)) {
            BitfinexSubmittedOrder exchangeOrder = jsonToBitfinexSubmittedOrder(payload);
            submittedOrderConsumer.accept(symbol, exchangeOrder);
        }
    }
}
 
Example 2
Source File: RestClient.java    From nbp with Apache License 2.0 6 votes vote down vote up
JSONObject getHost(String hostName) throws Exception {
    logger.info(String.format("----------OpenSDS Getting Host %s----------", hostName));

    JSONArray response = (JSONArray)request.get(String.format("/host/hosts?hostName=%s", hostName));
    logger.debug(String.format("OpenSDS Getting Host %s Response: %s", hostName,
                response));

    if (response.isEmpty()) {
        String msg = String.format("No Host Found");
        logger.info(String.format("OpenSDS Get Host for %s Error: %s", hostName, msg));
        return new JSONObject();
    }

    JSONObject host = (JSONObject) response.get(0);
    return host;
}
 
Example 3
Source File: RestClient.java    From nbp with Apache License 2.0 6 votes vote down vote up
JSONArray listVolumes(String poolId) throws Exception {
    String volumeUrl;
    if (!poolId.isEmpty()) {
        volumeUrl = String.format("/block/volumes?PoolId=%s", poolId);
    } else {
        volumeUrl = String.format("/block/volumes");
    }

    logger.info("----------OpenSDS Listing Volumes----------");

    JSONArray response = (JSONArray)request.get(volumeUrl);
    logger.debug(String.format("OpenSDS List Volumes Response: %s", response));

    if(response.isEmpty()) {
        String msg = String.format("No Volumes Found");
        logger.error(String.format("OpenSDS List Volumes: %s", msg));
    }

    return response;
}
 
Example 4
Source File: RestClient.java    From nbp with Apache License 2.0 6 votes vote down vote up
JSONArray listVolumes(String filterKey, String filterValue) throws Exception {
    logger.info("----------OpenSDS Listing Volumes----------");

    String volumeUrl;
    volumeUrl = String.format("/block/volumes?%s=%s", filterKey, filterValue);
    JSONArray response = (JSONArray)request.get(volumeUrl);
    logger.debug(String.format("OpenSDS List Volumes for the filter %s Response: %s",
                filterKey, response));

    if(response.isEmpty()) {
        String msg = String.format("No Volumes Found");
        logger.error(String.format("OpenSDS List Volumes for the filter Error: %s %s",
                filterKey, msg));
    }

    return response;
}
 
Example 5
Source File: RestClient.java    From nbp with Apache License 2.0 6 votes vote down vote up
JSONObject getHost(String hostName) throws Exception {
    logger.info(String.format("----------OpenSDS Getting Host %s----------", hostName));

    // replace all  "_" to "-" from hostName as OpenSDS does not support. 
    String osdsHostName = hostName.replaceAll("_","-");
    JSONArray response = (JSONArray)request.get(String.format("/host/hosts?hostName=%s", osdsHostName));
    logger.debug(String.format("OpenSDS Getting Host %s Response: %s", osdsHostName,
                response));

    if (response.isEmpty()) {
        String msg = String.format("No Host Found");
        logger.info(String.format("OpenSDS Get Host for %s Error: %s", osdsHostName, msg));
        return new JSONObject();
    }

    JSONObject host = (JSONObject) response.get(0);
    return host;
}
 
Example 6
Source File: RestClient.java    From nbp with Apache License 2.0 6 votes vote down vote up
JSONObject getVolumeByIdentifier(String identifier) throws Exception {
    logger.info(String.format("----------OpenSDS Getting Volume for Identifier %s----------",
                identifier));

    JSONArray response = (JSONArray)request.get(String.format("/block/volumes?DurableName=%s", identifier));
    logger.debug(String.format("OpenSDS Getting Volume for Identifier %s Response: %s", identifier,
                response));

    if (response.isEmpty()) {
        String msg = String.format("No Volumes Found");
        logger.error(String.format("OpenSDS Get Volume for %s Error: %s", identifier, msg));
    }

    JSONObject volume = (JSONObject) response.get(0);
    return volume;
}
 
Example 7
Source File: RestClient.java    From nbp with Apache License 2.0 6 votes vote down vote up
JSONArray listVolumeSnapshot(String volumeId) throws Exception {
    String snapshotUrl;
    if (!volumeId.isEmpty()) {
        snapshotUrl = String.format("/block/snapshots?VolumeId=%s", volumeId);
    } else {
        snapshotUrl = String.format("/block/snapshots");
    }

    logger.info(String.format("----------OpenSDS Listing Snapshots for VolumeId %s----------",
                volumeId));

    JSONArray response = (JSONArray)request.get(snapshotUrl);
    logger.debug(String.format("OpenSDS List Volume Snapshots for VolumeId %s Response: %s",
                volumeId, response));

    if(response.isEmpty()) {
        String msg = String.format("No Snapshots Found");
        logger.error(String.format("OpenSDS List Volume Snapshots: %s", msg));
    }

    return response;
}
 
Example 8
Source File: OceanStor.java    From nbp with Apache License 2.0 5 votes vote down vote up
private JSONObject getMappingView(JSONObject hostGroup, ConnectMO connect, boolean onlyQuery) throws Exception {
    try {
        JSONObject mappingView = null;

        if (hostGroup.getBoolean("ISADD2MAPPINGVIEW")) {
            JSONArray mappingViews = client.getMappingViewsByHostGroup(hostGroup.getString("ID"));
            if (mappingViews != null && !mappingViews.isEmpty()) {
                mappingView = mappingViews.getJSONObject(0);
            }
        }

        if (mappingView != null) {
            return mappingView;
        }

        mappingView = client.getMappingViewByName(connect.name);
        if (onlyQuery) {
            return mappingView;
        }

        if (mappingView == null) {
            mappingView = client.createMappingView(connect.name);
        }

        client.associateGroupToMappingView(
                hostGroup.getString("ID"), 14, mappingView.getString("ID"));

        return mappingView;
    }
    catch(Exception e) {
        logger.error(String.format("Error in Getting MappingView, Error Message is: %s", e));
        throw new Exception("Error in Getting MappingView", e);
    }
}
 
Example 9
Source File: RestClient.java    From nbp with Apache License 2.0 5 votes vote down vote up
JSONArray listStoragePools() throws Exception {
    logger.info("----------OpenSDS Listing Storage Pools----------");

    JSONArray response = (JSONArray)request.get("/pools");
    logger.debug(String.format("OpenSDS List Storage Pools Response: %s", response));

    if(response.isEmpty()) {
         String msg = String.format("No Pools Found");
         logger.error(String.format("OpenSDS List Storage Pools: %s", msg));
    }

    return response;
}
 
Example 10
Source File: EntityHelper.java    From FROST-Server with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Tries to fetch the given entity type, with the given query options,
 * retrying a maximum of retries times, waiting MqttHelper.WAIT_AFTER_INSERT
 * milliseconds between retries.
 *
 * @param entityType The entity type to fetch the first entity of.
 * @param queryOptions The query options to use while fetching.
 * @param retries The maximum number of retries.
 * @return The first entity found, or null after all retries.
 */
public JSONObject getAnyEntity(EntityType entityType, String queryOptions, int retries) {
    String urlString = ServiceUrlHelper.buildURLString(rootUri, entityType, null, null, null) + "?$top=1";
    if (queryOptions != null && !queryOptions.isEmpty()) {
        urlString += "&" + queryOptions;
    }
    try {
        int retry = 0;
        while (retry < retries) {
            String json = HTTPMethods.doGet(urlString).response;
            JSONArray items = new JSONObject(json).getJSONArray("value");
            if (!items.isEmpty()) {
                return items.getJSONObject(0);
            } else {
                retry++;
                LOGGER.debug("No data yet. Retries: {}, URL: {}", retry, urlString);
                MqttHelper.waitMillis(MqttHelper.WAIT_AFTER_INSERT);
            }
        }
        LOGGER.error("Failed to read an entity from url after {} tries: {}", retries, urlString);
        return null;
    } catch (JSONException e) {
        LOGGER.error("Failed while reading from url {}", urlString);
        LOGGER.error("Exception:", e);
        Assert.fail("An Exception occurred during testing!: " + e.getMessage());
        return null;
    }
}