Search in sources :

Example 11 with JSONArray

use of net.minidev.json.JSONArray in project mica2 by obiba.

the class SchemaFormContentFileService method getPathFilesMap.

private Map<String, JSONArray> getPathFilesMap(DocumentContext context, Object json) {
    DocumentContext reader = new JsonContext(defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES)).parse(json);
    JSONArray paths = null;
    try {
        paths = context.read("$..obibaFiles");
    } catch (PathNotFoundException e) {
        return null;
    }
    return paths.stream().collect(Collectors.toMap(Object::toString, p -> (JSONArray) reader.read(p.toString())));
}
Also used : Arrays(java.util.Arrays) Collection(java.util.Collection) Option(com.jayway.jsonpath.Option) NotNull(javax.validation.constraints.NotNull) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) JsonContext(com.jayway.jsonpath.internal.JsonContext) FileStoreService(org.obiba.mica.file.FileStoreService) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) DocumentContext(com.jayway.jsonpath.DocumentContext) SchemaFormContentAware(org.obiba.mica.core.domain.SchemaFormContentAware) JSONArray(net.minidev.json.JSONArray) Service(org.springframework.stereotype.Service) Map(java.util.Map) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) Configuration.defaultConfiguration(com.jayway.jsonpath.Configuration.defaultConfiguration) Assert(org.springframework.util.Assert) JsonContext(com.jayway.jsonpath.internal.JsonContext) JSONArray(net.minidev.json.JSONArray) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 12 with JSONArray

use of net.minidev.json.JSONArray in project mica2 by obiba.

the class SchemaFormContentFileService method save.

public void save(@NotNull SchemaFormContentAware newEntity, SchemaFormContentAware oldEntity, String entityPath) {
    Assert.notNull(newEntity, "New content cannot be null");
    Object json = defaultConfiguration().jsonProvider().parse(newEntity.getContent());
    DocumentContext newContext = JsonPath.using(defaultConfiguration().addOptions(Option.AS_PATH_LIST)).parse(json);
    Map<String, JSONArray> newPaths = getPathFilesMap(newContext, json);
    // content does not have any file field
    if (newPaths == null)
        return;
    if (oldEntity != null) {
        Object oldJson = defaultConfiguration().jsonProvider().parse(oldEntity.getContent());
        DocumentContext oldContext = JsonPath.using(defaultConfiguration().addOptions(Option.AS_PATH_LIST)).parse(oldJson);
        Map<String, JSONArray> oldPaths = getPathFilesMap(oldContext, oldJson);
        if (oldPaths != null) {
            saveAndDelete(oldPaths, newPaths, entityPath);
        } else {
            // schema and definition now have files
            newPaths.values().forEach(v -> saveFiles(v, entityPath));
        }
    } else {
        newPaths.values().forEach(v -> saveFiles(v, entityPath));
    }
    cleanup(newPaths, newContext);
    newEntity.setContent(newContext.jsonString());
}
Also used : JSONArray(net.minidev.json.JSONArray) DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 13 with JSONArray

use of net.minidev.json.JSONArray in project knox by apache.

the class AmbariClientCommon method getActiveServiceConfigurations.

Map<String, Map<String, AmbariCluster.ServiceConfiguration>> getActiveServiceConfigurations(String discoveryAddress, String clusterName, String discoveryUser, String discoveryPwdAlias) {
    Map<String, Map<String, AmbariCluster.ServiceConfiguration>> serviceConfigurations = new HashMap<>();
    String serviceConfigsURL = String.format("%s" + AMBARI_SERVICECONFIGS_URI, discoveryAddress, clusterName);
    JSONObject serviceConfigsJSON = restClient.invoke(serviceConfigsURL, discoveryUser, discoveryPwdAlias);
    if (serviceConfigsJSON != null) {
        // Process the service configurations
        JSONArray serviceConfigs = (JSONArray) serviceConfigsJSON.get("items");
        for (Object serviceConfig : serviceConfigs) {
            String serviceName = (String) ((JSONObject) serviceConfig).get("service_name");
            JSONArray configurations = (JSONArray) ((JSONObject) serviceConfig).get("configurations");
            for (Object configuration : configurations) {
                String configType = (String) ((JSONObject) configuration).get("type");
                String configVersion = String.valueOf(((JSONObject) configuration).get("version"));
                Map<String, String> configProps = new HashMap<>();
                JSONObject configProperties = (JSONObject) ((JSONObject) configuration).get("properties");
                for (Entry<String, Object> entry : configProperties.entrySet()) {
                    configProps.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
                if (!serviceConfigurations.containsKey(serviceName)) {
                    serviceConfigurations.put(serviceName, new HashMap<>());
                }
                serviceConfigurations.get(serviceName).put(configType, new AmbariCluster.ServiceConfiguration(configType, configVersion, configProps));
            }
        }
    }
    return serviceConfigurations;
}
Also used : JSONObject(net.minidev.json.JSONObject) HashMap(java.util.HashMap) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with JSONArray

use of net.minidev.json.JSONArray in project knox by apache.

the class AmbariServiceDiscovery method discover.

@Override
public Map<String, Cluster> discover(GatewayConfig gatewayConfig, ServiceDiscoveryConfig discoveryConfig) {
    Map<String, Cluster> clusters = new HashMap<>();
    init(gatewayConfig);
    String discoveryAddress = discoveryConfig.getAddress();
    // Invoke Ambari REST API to discover the available clusters
    String clustersDiscoveryURL = String.format("%s" + AMBARI_CLUSTERS_URI, discoveryAddress);
    JSONObject json = restClient.invoke(clustersDiscoveryURL, discoveryConfig.getUser(), discoveryConfig.getPasswordAlias());
    // Parse the cluster names from the response, and perform the cluster discovery
    JSONArray clusterItems = (JSONArray) json.get("items");
    for (Object clusterItem : clusterItems) {
        String clusterName = (String) ((JSONObject) ((JSONObject) clusterItem).get("Clusters")).get("cluster_name");
        try {
            Cluster c = discover(gatewayConfig, discoveryConfig, clusterName);
            clusters.put(clusterName, c);
        } catch (Exception e) {
            log.clusterDiscoveryError(clusterName, e);
        }
    }
    return clusters;
}
Also used : JSONObject(net.minidev.json.JSONObject) HashMap(java.util.HashMap) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject)

Example 15 with JSONArray

use of net.minidev.json.JSONArray in project connectors-workspace-one by vmware.

the class AirWatchController method toGreenBoxApp.

private GreenBoxApp toGreenBoxApp(JsonDocument document, String appName) {
    int RIGHT_APP_COUNT = 1;
    JSONArray jsonArray = document.read("$._embedded.entitlements");
    if (jsonArray.size() != RIGHT_APP_COUNT) {
        throw new GbAppMapException("Unable to map " + appName + " to a single GreenBox app");
    }
    return new GreenBoxApp(document.read("$._embedded.entitlements[0].name"), document.read("$._embedded.entitlements[0]._links.install.href"));
}
Also used : GreenBoxApp(com.vmware.connectors.airwatch.greenbox.GreenBoxApp) GbAppMapException(com.vmware.connectors.airwatch.exceptions.GbAppMapException) JSONArray(net.minidev.json.JSONArray)

Aggregations

JSONArray (net.minidev.json.JSONArray)55 JSONObject (net.minidev.json.JSONObject)41 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 JSONParser (net.minidev.json.parser.JSONParser)5 Test (org.testng.annotations.Test)5 DocumentContext (com.jayway.jsonpath.DocumentContext)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONConverterException (org.btrplace.json.JSONConverterException)3 VM (org.btrplace.model.VM)3 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)2 ContentType (ddf.catalog.data.ContentType)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)2 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)2 SourceDescriptor (ddf.catalog.source.SourceDescriptor)2 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)2