Example usage for com.google.gson JsonElement getAsJsonObject

List of usage examples for com.google.gson JsonElement getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonObject.

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:apim.restful.importexport.utils.APIImportUtil.java

License:Open Source License

/**
 * This method imports an API//from w  w w . jav a2s  .c  o m
 *
 * @param pathToArchive            location of the extracted folder of the API
 * @param currentUser              the current logged in user
 * @param isDefaultProviderAllowed decision to keep or replace the provider
 * @throws APIImportException     if there is an error in importing an API
 */
public static void importAPI(String pathToArchive, String currentUser, boolean isDefaultProviderAllowed)
        throws APIImportException {

    API importedApi;

    // If the original provider is preserved,
    if (isDefaultProviderAllowed) {

        FileInputStream inputStream = null;
        BufferedReader bufferedReader = null;

        try {
            inputStream = new FileInputStream(pathToArchive + APIImportExportConstants.JSON_FILE_LOCATION);
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            importedApi = new Gson().fromJson(bufferedReader, API.class);
        } catch (FileNotFoundException e) {
            log.error("Error in locating api.json file. ", e);
            throw new APIImportException("Error in locating api.json file. " + e.getMessage());
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(bufferedReader);
        }
    } else {

        String pathToJSONFile = pathToArchive + APIImportExportConstants.JSON_FILE_LOCATION;

        try {
            String jsonContent = FileUtils.readFileToString(new File(pathToJSONFile));
            JsonElement configElement = new JsonParser().parse(jsonContent);
            JsonObject configObject = configElement.getAsJsonObject();

            //locate the "providerName" within the "id" and set it as the current user
            JsonObject apiId = configObject.getAsJsonObject(APIImportExportConstants.ID_ELEMENT);
            apiId.addProperty(APIImportExportConstants.PROVIDER_ELEMENT,
                    APIUtil.replaceEmailDomain(currentUser));
            importedApi = new Gson().fromJson(configElement, API.class);

        } catch (IOException e) {
            log.error("Error in setting API provider to logged in user. ", e);
            throw new APIImportException("Error in setting API provider to logged in user. " + e.getMessage());
        }
    }

    Set<Tier> allowedTiers;
    Set<Tier> unsupportedTiersList;

    try {
        allowedTiers = provider.getTiers();
    } catch (APIManagementException e) {
        log.error("Error in retrieving tiers of the provider. ", e);
        throw new APIImportException("Error in retrieving tiers of the provider. " + e.getMessage());
    }

    if (!(allowedTiers.isEmpty())) {
        unsupportedTiersList = Sets.difference(importedApi.getAvailableTiers(), allowedTiers);

        //If at least one unsupported tier is found, it should be removed before adding API
        if (!(unsupportedTiersList.isEmpty())) {
            for (Tier unsupportedTier : unsupportedTiersList) {

                //Process is continued with a warning and only supported tiers are added to the importer API
                log.warn("Tier name : " + unsupportedTier.getName() + " is not supported.");
            }

            //Remove the unsupported tiers before adding the API
            importedApi.removeAvailableTiers(unsupportedTiersList);
        }
    }

    try {
        int tenantId = APIUtil.getTenantId(currentUser);
        provider.addAPI(importedApi);
        addSwaggerDefinition(importedApi, pathToArchive, tenantId);
    } catch (APIManagementException e) {
        //Error is logged and APIImportException is thrown because adding API and swagger are mandatory steps
        log.error("Error in adding API to the provider. ", e);
        throw new APIImportException("Error in adding API to the provider. " + e.getMessage());
    }
    //Since Image, documents, sequences and WSDL are optional, exceptions are logged and ignored in implementation
    addAPIImage(pathToArchive, importedApi);
    addAPIDocuments(pathToArchive, importedApi);
    addAPISequences(pathToArchive, importedApi, currentUser);
    addAPIWsdl(pathToArchive, importedApi, currentUser);

}

From source file:arces.unibo.SEPA.client.pattern.ApplicationProfile.java

License:Open Source License

/**
  "updates": {//  w  w  w  .  j a v a2  s .  co  m
   "ADD_PERSON":{
    "sparql":"INSERT DATA { ?person rdf:type iot:Person . ?person iot:hasName ?name }",
    "forcedBindings": {
   "person" : {"type":"uri", "value":""},
   "name" : {"type":"literal", "value":""}}}
 }, 
 */
public String update(String updateID) {
    JsonElement elem = null;
    if ((elem = doc.get("updates")) != null)
        if ((elem = elem.getAsJsonObject().get(updateID)) != null)
            if ((elem = elem.getAsJsonObject().get("sparql")) != null)
                return elem.getAsString();
    return null;
}

From source file:arces.unibo.SEPA.client.pattern.ApplicationProfile.java

License:Open Source License

public String subscribe(String subscribeID) {
    JsonElement elem = null;
    if ((elem = doc.get("subscribes")) != null)
        if ((elem = elem.getAsJsonObject().get(subscribeID)) != null)
            if ((elem = elem.getAsJsonObject().get("sparql")) != null)
                return elem.getAsString();
    return null;/*from  w  w w  .ja va 2 s .  c  o m*/
}

From source file:arces.unibo.SEPA.client.pattern.ApplicationProfile.java

License:Open Source License

public Set<String> getUpdateIds() {
    JsonElement elem;
    HashSet<String> ret = new HashSet<String>();
    if ((elem = doc.get("updates")) != null)
        for (Entry<String, JsonElement> key : elem.getAsJsonObject().entrySet()) {
            ret.add(key.getKey());//from   w  w w. j  a v  a2s .  co m
        }
    return ret;
}

From source file:arces.unibo.SEPA.client.pattern.ApplicationProfile.java

License:Open Source License

public Set<String> getSubscribeIds() {
    JsonElement elem;
    HashSet<String> ret = new HashSet<String>();
    if ((elem = doc.get("subscribes")) != null)
        for (Entry<String, JsonElement> key : elem.getAsJsonObject().entrySet()) {
            ret.add(key.getKey());/*from   w  ww .j  a va2  s.c  o m*/
        }
    return ret;
}

From source file:arces.unibo.SEPA.client.pattern.ApplicationProfile.java

License:Open Source License

/**
 * "forcedBindings": {/*from   w ww .j ava  2 s.  co m*/
   "person" : {"type":"uri", "value":""},
   "name" : {"type":"literal", "value":""}}}
           
 * @param selectedValue
 * @return
 */
public Bindings updateBindings(String selectedValue) {
    JsonElement elem;
    Bindings ret = new Bindings();
    if ((elem = doc.get("updates")) != null)
        if ((elem = elem.getAsJsonObject().get(selectedValue)) != null)
            if ((elem = elem.getAsJsonObject().get("forcedBindings")) != null) {
                for (Entry<String, JsonElement> binding : elem.getAsJsonObject().entrySet()) {
                    JsonObject value = binding.getValue().getAsJsonObject();
                    RDFTerm bindingValue = null;

                    if (value.get("type") != null) {
                        if (value.get("type").getAsString().equals("uri")) {
                            bindingValue = new RDFTermURI(value.get("value").getAsString());
                        } else {
                            bindingValue = new RDFTermLiteral(value.get("value").getAsString());
                        }
                    }
                    ret.addBinding(binding.getKey(), bindingValue);
                }
            }
    return ret;
}

From source file:arces.unibo.SEPA.client.pattern.ApplicationProfile.java

License:Open Source License

public Bindings subscribeBindings(String selectedValue) {
    JsonElement elem;
    Bindings ret = new Bindings();
    if ((elem = doc.get("subscribes")) != null)
        if ((elem = elem.getAsJsonObject().get(selectedValue)) != null)
            if ((elem = elem.getAsJsonObject().get("forcedBindings")) != null) {
                for (Entry<String, JsonElement> binding : elem.getAsJsonObject().entrySet()) {
                    JsonObject value = binding.getValue().getAsJsonObject();
                    RDFTerm bindingValue = null;

                    if (value.get("type") != null) {
                        if (value.get("type").getAsString().equals("uri")) {
                            bindingValue = new RDFTermURI(value.get("value").getAsString());
                        } else {
                            bindingValue = new RDFTermLiteral(value.get("value").getAsString());
                        }//from w  ww.  j  ava2 s.  c  o m
                    }
                    ret.addBinding(binding.getKey(), bindingValue);
                }
            }
    return ret;
}

From source file:arces.unibo.SEPA.client.pattern.ApplicationProfile.java

License:Open Source License

/**
 * "namespaces" : { "iot":"http://www.arces.unibo.it/iot#","rdf":"http://www.w3.org/1999/02/22-rdf-syntax-ns#"},
 *//*from   www  .  j a  v a 2 s  .  c o  m*/

public Set<String> getPrefixes() {
    JsonElement elem;
    HashSet<String> ret = new HashSet<String>();
    if ((elem = doc.get("namespaces")) != null)
        for (Entry<String, JsonElement> key : elem.getAsJsonObject().entrySet()) {
            ret.add(key.getKey());
        }
    return ret;
}

From source file:arces.unibo.SEPA.client.pattern.ApplicationProfile.java

License:Open Source License

public String getNamespaceURI(String prefix) {
    JsonElement elem;
    String ret = null;//from ww w.ja v  a 2 s  .  c  om
    if ((elem = doc.get("namespaces")) != null)
        if ((elem = elem.getAsJsonObject().get(prefix)) != null)
            return elem.getAsString();
    return ret;
}

From source file:arces.unibo.SEPA.commons.SPARQL.BindingsResults.java

License:Open Source License

public List<Bindings> getBindings() {
    List<Bindings> list = new ArrayList<Bindings>();
    JsonArray bindings = getBindingsArray();
    if (bindings == null)
        return list;

    for (JsonElement solution : bindings) {
        list.add(new Bindings(solution.getAsJsonObject()));
    }/*from w  w  w .  j a v  a 2s  . c o  m*/
    return list;
}