Search in sources :

Example 1 with JsonTypeReader

use of com.serotonin.json.type.JsonTypeReader in project ma-core-public by infiniteautomation.

the class StoreLatestProductionTest method testThreeThreeReleaseVersion.

public void testThreeThreeReleaseVersion() throws JsonException, IOException, HttpException {
    // Setup json
    json.put("upgradeVersionState", UpgradeVersionState.PRODUCTION);
    json.put("currentVersionState", UpgradeVersionState.PRODUCTION);
    jsonModules.put("core", "3.3.0");
    jsonModules.put("mangoApi", "3.3.0");
    String url = baseUrl + "/servlet/versionCheck";
    HttpPost post = new HttpPost(url);
    StringWriter stringWriter = new StringWriter();
    new JsonWriter(JSON_CONTEXT, stringWriter).writeObject(json);
    String requestData = stringWriter.toString();
    post.setEntity(new StringEntity(requestData));
    String responseData = HttpUtils4.getTextContent(getHttpClient(30000), post, 1);
    JsonTypeReader jsonReader = new JsonTypeReader(responseData);
    JsonValue response = jsonReader.read();
    // printResponse(response);
    Assert.assertNotNull(response.getJsonValue("versionState"));
    Assert.assertEquals("PRODUCTION", response.getJsonValue("versionState").toString());
    // Assert newInstalls-oldCore should be empty
    Assert.assertNotNull(response.getJsonValue("newInstalls-oldCore"));
    JsonArray newInstallsOldCore = response.getJsonValue("newInstalls-oldCore").toJsonArray();
    Assert.assertEquals(true, newInstallsOldCore.size() == 0);
    // Assert update-versionState
    Assert.assertNull(response.getJsonValue("update-versionState"));
    // Assert updates for this core iff major upgrade available
    Assert.assertNotNull(response.getJsonValue("updates"));
    JsonArray updates = response.getJsonValue("updates").toJsonArray();
    Assert.assertEquals(true, updates.size() == 0);
    // Assert upgrades
    Assert.assertNotNull(response.getJsonValue("upgrades"));
    JsonArray upgrades = response.getJsonValue("upgrades").toJsonArray();
    Assert.assertEquals(true, upgrades.size() > 0);
    for (JsonValue upgrade : upgrades) {
        Assert.assertEquals(true, upgrade.getJsonValue("fullVersion").toString().startsWith(latestProductionCorePrefix));
    }
    // Assert newInstalls should be for latest production core
    Assert.assertNotNull(response.getJsonValue("newInstalls"));
    JsonArray newInstalls = response.getJsonValue("newInstalls").toJsonArray();
    Assert.assertEquals(true, newInstalls.size() > 0);
    for (JsonValue install : newInstalls) {
        Assert.assertEquals(true, install.getJsonValue("fullVersion").toString().startsWith(latestProductionCorePrefix));
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) JsonValue(com.serotonin.json.type.JsonValue) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 2 with JsonTypeReader

use of com.serotonin.json.type.JsonTypeReader in project ma-core-public by infiniteautomation.

the class JsonEmportScriptUtility method doImportGetStatus.

public List<ProcessMessage> doImportGetStatus(String json) throws Exception {
    if (admin) {
        JsonTypeReader reader = new JsonTypeReader(json);
        JsonValue value = reader.read();
        JsonObject jo = value.toJsonObject();
        if (importExclusions != null)
            doExclusions(jo);
        ScriptImportTask sit = new ScriptImportTask(jo);
        sit.run(Common.timer.currentTimeMillis());
        return sit.getMessages();
    }
    return null;
}
Also used : JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 3 with JsonTypeReader

use of com.serotonin.json.type.JsonTypeReader in project ma-core-public by infiniteautomation.

the class JsonEmportScriptUtility method doImport.

public void doImport(String json) throws Exception {
    if (admin) {
        JsonTypeReader reader = new JsonTypeReader(json);
        JsonValue value = reader.read();
        JsonObject jo = value.toJsonObject();
        if (importExclusions != null)
            doExclusions(jo);
        ScriptImportTask sit = new ScriptImportTask(jo);
        sit.run(Common.timer.currentTimeMillis());
    }
}
Also used : JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 4 with JsonTypeReader

use of com.serotonin.json.type.JsonTypeReader in project ma-core-public by infiniteautomation.

the class EmportDwr method importData.

@DwrPermission(admin = true)
public ProcessResult importData(String data) {
    ProcessResult response = new ProcessResult();
    Translations translations = getTranslations();
    User user = Common.getHttpUser();
    JsonTypeReader reader = new JsonTypeReader(data);
    try {
        JsonValue value = reader.read();
        if (value instanceof JsonObject) {
            JsonObject root = value.toJsonObject();
            ImportTask importTask = new ImportTask(root, translations, user, true);
            user.setImportTask(importTask);
            response.addData("importStarted", true);
        } else {
            response.addGenericMessage("emport.invalidImportData");
        }
    } catch (ClassCastException e) {
        response.addGenericMessage("emport.parseError", e.getMessage());
    } catch (TranslatableJsonException e) {
        response.addMessage(e.getMsg());
    } catch (IOException e) {
        response.addGenericMessage("emport.parseError", e.getMessage());
    } catch (JsonException e) {
        response.addGenericMessage("emport.parseError", e.getMessage());
    }
    return response;
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) User(com.serotonin.m2m2.vo.User) ImportTask(com.serotonin.m2m2.web.dwr.emport.ImportTask) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) IOException(java.io.IOException) Translations(com.serotonin.m2m2.i18n.Translations) JsonTypeReader(com.serotonin.json.type.JsonTypeReader) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 5 with JsonTypeReader

use of com.serotonin.json.type.JsonTypeReader in project ma-core-public by infiniteautomation.

the class ModulesDwr method startDownloads.

@DwrPermission(admin = true)
public static String startDownloads(List<StringStringPair> modules, boolean backup, boolean restart) {
    synchronized (UPGRADE_DOWNLOADER_LOCK) {
        if (UPGRADE_DOWNLOADER != null)
            return Common.translate("modules.versionCheck.occupied");
    }
    // Check if the selected modules will result in a version-consistent system.
    try {
        // Create the request
        Map<String, Object> json = new HashMap<>();
        Map<String, String> jsonModules = new HashMap<>();
        json.put("modules", jsonModules);
        Version coreVersion = Common.getVersion();
        jsonModules.put("core", coreVersion.toString());
        for (StringStringPair module : modules) jsonModules.put(module.getKey(), module.getValue());
        StringWriter stringWriter = new StringWriter();
        new JsonWriter(Common.JSON_CONTEXT, stringWriter).writeObject(json);
        String requestData = stringWriter.toString();
        // Send the request
        String baseUrl = Common.envProps.getString("store.url");
        baseUrl += "/servlet/consistencyCheck";
        HttpPost post = new HttpPost(baseUrl);
        post.setEntity(new StringEntity(requestData));
        String responseData = HttpUtils4.getTextContent(Common.getHttpClient(), post, 1);
        // Parse the response
        JsonTypeReader jsonReader = new JsonTypeReader(responseData);
        String result = jsonReader.read().toString();
        if (!"ok".equals(result))
            return result;
    } catch (Exception e) {
        LOG.error("", e);
        return e.getMessage();
    }
    synchronized (UPGRADE_DOWNLOADER_LOCK) {
        // Ensure that 2 downloads cannot start at the same time.
        if (UPGRADE_DOWNLOADER == null) {
            UPGRADE_DOWNLOADER = new UpgradeDownloader(modules, backup, restart, Common.getHttpUser());
            // Clear out common info
            resetUpgradeStatus();
            Common.backgroundProcessing.execute(UPGRADE_DOWNLOADER);
        } else
            return Common.translate("modules.versionCheck.occupied");
    }
    return null;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringStringPair(com.serotonin.db.pair.StringStringPair) HashMap(java.util.HashMap) JsonString(com.serotonin.json.type.JsonString) JsonWriter(com.serotonin.json.JsonWriter) JsonException(com.serotonin.json.JsonException) HttpException(org.apache.http.HttpException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) Version(com.github.zafarkhaja.semver.Version) JsonObject(com.serotonin.json.type.JsonObject) JsonTypeReader(com.serotonin.json.type.JsonTypeReader) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

JsonTypeReader (com.serotonin.json.type.JsonTypeReader)19 JsonValue (com.serotonin.json.type.JsonValue)15 StringWriter (java.io.StringWriter)13 JsonWriter (com.serotonin.json.JsonWriter)12 HttpPost (org.apache.http.client.methods.HttpPost)12 StringEntity (org.apache.http.entity.StringEntity)12 JsonArray (com.serotonin.json.type.JsonArray)10 JsonObject (com.serotonin.json.type.JsonObject)7 JsonException (com.serotonin.json.JsonException)4 IOException (java.io.IOException)4 Version (com.github.zafarkhaja.semver.Version)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 JsonReader (com.serotonin.json.JsonReader)2 JsonString (com.serotonin.json.type.JsonString)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 HashMap (java.util.HashMap)2 StringStringPair (com.serotonin.db.pair.StringStringPair)1 JsonStreamWriter (com.serotonin.json.JsonStreamWriter)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1