Search in sources :

Example 1 with JsonValue

use of com.serotonin.json.type.JsonValue 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 JsonValue

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

the class EmailEventHandlerVO method jsonRead.

@SuppressWarnings("unchecked")
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    String text = null;
    TypeDefinition recipType = new TypeDefinition(List.class, RecipientListEntryBean.class);
    JsonArray jsonActiveRecipients = jsonObject.getJsonArray("activeRecipients");
    if (jsonActiveRecipients != null)
        activeRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonActiveRecipients);
    JsonBoolean b = jsonObject.getJsonBoolean("sendEscalation");
    if (b != null)
        sendEscalation = b.booleanValue();
    if (sendEscalation) {
        text = jsonObject.getString("escalationDelayType");
        if (text != null) {
            escalationDelayType = Common.TIME_PERIOD_CODES.getId(text);
            if (escalationDelayType == -1)
                throw new TranslatableJsonException("emport.error.invalid", "escalationDelayType", text, Common.TIME_PERIOD_CODES.getCodeList());
        }
        Integer i = jsonObject.getInt("escalationDelay", 1);
        if (i != null)
            escalationDelay = i;
        JsonArray jsonEscalationRecipients = jsonObject.getJsonArray("escalationRecipients");
        if (jsonEscalationRecipients != null)
            escalationRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonEscalationRecipients);
        b = jsonObject.getJsonBoolean("keepSendingEscalations");
        if (b != null)
            repeatEscalations = b.booleanValue();
    }
    b = jsonObject.getJsonBoolean("sendInactive");
    if (b != null)
        sendInactive = b.booleanValue();
    if (sendInactive) {
        b = jsonObject.getJsonBoolean("inactiveOverride");
        if (b != null)
            inactiveOverride = b.booleanValue();
        if (inactiveOverride) {
            JsonArray jsonInactiveRecipients = jsonObject.getJsonArray("inactiveRecipients");
            if (jsonInactiveRecipients != null)
                inactiveRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonInactiveRecipients);
        }
    }
    b = jsonObject.getJsonBoolean("includeSystemInformation");
    if (b != null)
        includeSystemInfo = b.booleanValue();
    includePointValueCount = jsonObject.getInt("includePointValueCount", 0);
    b = jsonObject.getJsonBoolean("includeLogfile");
    if (b != null)
        includeSystemInfo = b.booleanValue();
    customTemplate = jsonObject.getString("customTemplate");
    JsonArray context = jsonObject.getJsonArray("additionalContext");
    if (context != null) {
        List<IntStringPair> additionalContext = new ArrayList<>();
        for (JsonValue jv : context) {
            JsonObject jo = jv.toJsonObject();
            String dataPointXid = jo.getString("dataPointXid");
            if (dataPointXid == null)
                throw new TranslatableJsonException("emport.error.context.missing", "dataPointXid");
            DataPointVO dpvo = DataPointDao.instance.getByXid(dataPointXid);
            if (dpvo == null)
                throw new TranslatableJsonException("emport.error.missingPoint", dataPointXid);
            String contextKey = jo.getString("contextKey");
            if (contextKey == null)
                throw new TranslatableJsonException("emport.error.context.missing", "contextKey");
            additionalContext.add(new IntStringPair(dpvo.getId(), contextKey));
        }
        this.additionalContext = additionalContext;
    } else
        this.additionalContext = new ArrayList<>();
    script = jsonObject.getString("script");
    JsonObject permissions = jsonObject.getJsonObject("scriptPermissions");
    ScriptPermissions scriptPermissions = new ScriptPermissions();
    if (permissions != null) {
        String perm = permissions.getString(ScriptPermissions.DATA_SOURCE);
        if (perm != null)
            scriptPermissions.setDataSourcePermissions(perm);
        perm = permissions.getString(ScriptPermissions.DATA_POINT_READ);
        if (perm != null)
            scriptPermissions.setDataPointReadPermissions(perm);
        perm = permissions.getString(ScriptPermissions.DATA_POINT_SET);
        if (perm != null)
            scriptPermissions.setDataPointSetPermissions(perm);
    }
    this.scriptPermissions = scriptPermissions;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) ScriptPermissions(com.serotonin.m2m2.rt.script.ScriptPermissions) TypeDefinition(com.serotonin.json.util.TypeDefinition) JsonArray(com.serotonin.json.type.JsonArray) ArrayList(java.util.ArrayList) List(java.util.List) JsonBoolean(com.serotonin.json.type.JsonBoolean)

Example 3 with JsonValue

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

the class DataPointVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    // Not reading XID so can't do this: super.jsonRead(reader, jsonObject);
    name = jsonObject.getString("name");
    enabled = jsonObject.getBoolean("enabled");
    String text = jsonObject.getString("loggingType");
    if (text != null) {
        loggingType = LOGGING_TYPE_CODES.getId(text);
        if (loggingType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "loggingType", text, LOGGING_TYPE_CODES.getCodeList());
    }
    text = jsonObject.getString("intervalLoggingPeriodType");
    if (text != null) {
        intervalLoggingPeriodType = Common.TIME_PERIOD_CODES.getId(text);
        if (intervalLoggingPeriodType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "intervalLoggingPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
    text = jsonObject.getString("intervalLoggingType");
    if (text != null) {
        intervalLoggingType = INTERVAL_LOGGING_TYPE_CODES.getId(text);
        if (intervalLoggingType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "intervalLoggingType", text, INTERVAL_LOGGING_TYPE_CODES.getCodeList());
    }
    text = jsonObject.getString("purgeType");
    if (text != null) {
        purgeType = Common.TIME_PERIOD_CODES.getId(text);
        if (purgeType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "purgeType", text, Common.TIME_PERIOD_CODES.getCodeList(TimePeriods.MILLISECONDS, TimePeriods.SECONDS, TimePeriods.MINUTES, TimePeriods.HOURS));
    }
    JsonObject locatorJson = jsonObject.getJsonObject("pointLocator");
    if (locatorJson != null)
        reader.readInto(pointLocator, locatorJson);
    JsonArray pedArray = jsonObject.getJsonArray("eventDetectors");
    if (pedArray != null) {
        for (JsonValue jv : pedArray) {
            JsonObject pedObject = jv.toJsonObject();
            String pedXid = pedObject.getString("xid");
            if (StringUtils.isBlank(pedXid))
                throw new TranslatableJsonException("emport.error.ped.missingAttr", "xid");
            // Use the ped xid to lookup an existing ped.
            AbstractEventDetectorVO<?> ped = null;
            for (AbstractPointEventDetectorVO<?> existing : eventDetectors) {
                if (StringUtils.equals(pedXid, existing.getXid())) {
                    ped = existing;
                    break;
                }
            }
            JsonArray handlerXids = pedObject.getJsonArray("handlers");
            if (handlerXids != null)
                for (int k = 0; k < handlerXids.size(); k += 1) {
                    AbstractEventHandlerVO<?> eh = EventHandlerDao.instance.getByXid(handlerXids.getString(k));
                    if (eh == null) {
                        throw new TranslatableJsonException("emport.eventHandler.missing", handlerXids.getString(k));
                    }
                }
            if (ped == null) {
                String typeStr = pedObject.getString("type");
                if (typeStr == null)
                    throw new TranslatableJsonException("emport.error.ped.missingAttr", "type");
                EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
                if (def == null)
                    throw new TranslatableJsonException("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
                else {
                    ped = def.baseCreateEventDetectorVO();
                    ped.setDefinition(def);
                }
                // Create a new one
                ped.setId(Common.NEW_ID);
                ped.setXid(pedXid);
                AbstractPointEventDetectorVO<?> dped = (AbstractPointEventDetectorVO<?>) ped;
                dped.njbSetDataPoint(this);
                eventDetectors.add(dped);
            }
            reader.readInto(ped, pedObject);
        }
    }
    text = jsonObject.getString("unit");
    if (text != null) {
        unit = parseUnitString(text, "unit");
        unitString = UnitUtil.formatUcum(unit);
    }
    text = jsonObject.getString("integralUnit");
    if (text != null) {
        useIntegralUnit = true;
        integralUnit = parseUnitString(text, "integralUnit");
        integralUnitString = UnitUtil.formatUcum(integralUnit);
    }
    text = jsonObject.getString("renderedUnit");
    if (text != null) {
        useRenderedUnit = true;
        renderedUnit = parseUnitString(text, "renderedUnit");
        renderedUnitString = UnitUtil.formatUcum(renderedUnit);
    }
    text = jsonObject.getString("plotType");
    if (text != null) {
        plotType = PLOT_TYPE_CODES.getId(text);
        if (plotType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "plotType", text, PLOT_TYPE_CODES.getCodeList());
    }
    // Rollup
    text = jsonObject.getString("rollup");
    if (text != null) {
        rollup = Common.ROLLUP_CODES.getId(text);
        if (rollup == -1)
            throw new TranslatableJsonException("emport.error.chart.invalid", "rollup", text, Common.ROLLUP_CODES.getCodeList());
    }
    // Simplify
    text = jsonObject.getString("simplifyType");
    if (text != null) {
        simplifyType = SIMPLIFY_TYPE_CODES.getId(text);
        if (simplifyType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "simplifyType", text, SIMPLIFY_TYPE_CODES.getCodeList());
    }
    int simplifyTarget = jsonObject.getInt("simplifyTarget", Integer.MIN_VALUE);
    if (simplifyTarget != Integer.MIN_VALUE)
        this.simplifyTarget = simplifyTarget;
    double simplifyTolerance = jsonObject.getDouble("simplifyTolerance", Double.NaN);
    if (simplifyTolerance != Double.NaN)
        this.simplifyTolerance = simplifyTolerance;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)

Example 4 with JsonValue

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

the class ModulesDwr method upgradesAvailable.

/**
 * How many upgrades are available
 * @return
 * @throws Exception
 */
public static int upgradesAvailable() throws Exception {
    JsonValue jsonResponse = getAvailableUpgrades();
    if (jsonResponse instanceof JsonString)
        throw new Exception("Mango Store Response Error: " + jsonResponse.toString());
    JsonObject root = jsonResponse.toJsonObject();
    int size = root.getJsonArray("upgrades").size();
    if (size > 0) {
        // Notify the listeners
        JsonValue jsonUpgrades = root.get("upgrades");
        JsonArray jsonUpgradesArray = jsonUpgrades.toJsonArray();
        for (JsonValue v : jsonUpgradesArray) {
            for (ModuleNotificationListener l : listeners) l.moduleUpgradeAvailable(v.getJsonValue("name").toString(), v.getJsonValue("version").toString());
        }
        JsonValue jsonInstalls = root.get("newInstalls");
        JsonArray jsonInstallsArray = jsonInstalls.toJsonArray();
        for (JsonValue v : jsonInstallsArray) {
            for (ModuleNotificationListener l : listeners) l.newModuleAvailable(v.getJsonValue("name").toString(), v.getJsonValue("version").toString());
        }
    }
    return size;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) ModuleNotificationListener(com.serotonin.m2m2.module.ModuleNotificationListener) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) JsonException(com.serotonin.json.JsonException) HttpException(org.apache.http.HttpException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 5 with JsonValue

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

the class ModulesDwr method versionCheck.

@DwrPermission(admin = true)
public ProcessResult versionCheck() {
    ProcessResult result = new ProcessResult();
    if (UPGRADE_DOWNLOADER != null) {
        result.addData("error", Common.translate("modules.versionCheck.occupied"));
        return result;
    }
    try {
        JsonValue jsonResponse = getAvailableUpgrades();
        if (jsonResponse instanceof JsonString)
            result.addData("error", jsonResponse.toString());
        else {
            JsonObject root = jsonResponse.toJsonObject();
            result.addData("upgrades", root.get("upgrades").toNative());
            result.addData("newInstalls", root.get("newInstalls").toNative());
            if (root.containsKey("upgradesError"))
                result.addData("upgradesError", root.getString("upgradesError"));
            if (root.containsKey("updates")) {
                result.addData("updates", root.get("updates").toNative());
                result.addData("newInstalls-oldCore", root.get("newInstalls-oldCore").toNative());
            }
            if (root.containsKey("missingModules"))
                result.addData("missingModules", root.getJsonArray("missingModules").toNative());
        }
    } catch (UnknownHostException e) {
        LOG.error("", e);
        result.addData("unknownHost", e.getMessage());
    } catch (Exception e) {
        LOG.error("", e);
        result.addData("error", e.getMessage());
    }
    return result;
}
Also used : UnknownHostException(java.net.UnknownHostException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) JsonException(com.serotonin.json.JsonException) HttpException(org.apache.http.HttpException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

JsonValue (com.serotonin.json.type.JsonValue)41 JsonArray (com.serotonin.json.type.JsonArray)26 JsonObject (com.serotonin.json.type.JsonObject)21 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)15 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)15 JsonWriter (com.serotonin.json.JsonWriter)10 StringWriter (java.io.StringWriter)10 HttpPost (org.apache.http.client.methods.HttpPost)10 StringEntity (org.apache.http.entity.StringEntity)10 JsonException (com.serotonin.json.JsonException)8 IOException (java.io.IOException)7 JsonString (com.serotonin.json.type.JsonString)5 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)5 User (com.serotonin.m2m2.vo.User)5 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 IntStringPair (com.serotonin.db.pair.IntStringPair)3 JsonReader (com.serotonin.json.JsonReader)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 Type (java.lang.reflect.Type)3