Search in sources :

Example 11 with JsonWriter

use of com.serotonin.json.JsonWriter 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)

Example 12 with JsonWriter

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

the class ModulesDwr method getAvailableUpgrades.

public static JsonValue getAvailableUpgrades() throws JsonException, IOException, HttpException {
    // Create the request
    List<Module> modules = ModuleRegistry.getModules();
    Module.sortByName(modules);
    Map<String, Object> json = new HashMap<>();
    json.put("guid", Providers.get(ICoreLicense.class).getGuid());
    json.put("description", SystemSettingsDao.getValue(SystemSettingsDao.INSTANCE_DESCRIPTION));
    json.put("distributor", Common.envProps.getString("distributor"));
    json.put("upgradeVersionState", SystemSettingsDao.getIntValue(SystemSettingsDao.UPGRADE_VERSION_STATE));
    Properties props = new Properties();
    File propFile = new File(Common.MA_HOME + File.separator + "release.properties");
    int versionState = UpgradeVersionState.DEVELOPMENT;
    if (propFile.exists()) {
        InputStream in = new FileInputStream(propFile);
        try {
            props.load(in);
        } finally {
            in.close();
        }
        String currentVersionState = props.getProperty("versionState");
        try {
            if (currentVersionState != null)
                versionState = Integer.valueOf(currentVersionState);
        } catch (NumberFormatException e) {
        }
    }
    json.put("currentVersionState", versionState);
    Map<String, String> jsonModules = new HashMap<>();
    json.put("modules", jsonModules);
    Version coreVersion = Common.getVersion();
    jsonModules.put("core", coreVersion.toString());
    for (Module module : modules) if (!module.isMarkedForDeletion())
        jsonModules.put(module.getName(), module.getVersion().toString());
    // Add in the unloaded modules so we don't re-download them if we don't have to
    for (Module module : ModuleRegistry.getUnloadedModules()) if (!module.isMarkedForDeletion())
        jsonModules.put(module.getName(), module.getVersion().toString());
    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/versionCheck";
    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);
    return jsonReader.read();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonString(com.serotonin.json.type.JsonString) Properties(java.util.Properties) JsonWriter(com.serotonin.json.JsonWriter) FileInputStream(java.io.FileInputStream) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) Version(com.github.zafarkhaja.semver.Version) JsonObject(com.serotonin.json.type.JsonObject) Module(com.serotonin.m2m2.module.Module) File(java.io.File) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 13 with JsonWriter

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

the class SerotoninJsonMessageConverter method writeInternal.

/* (non-Javadoc)
	 * @see org.springframework.http.converter.AbstractHttpMessageConverter#writeInternal(java.lang.Object, org.springframework.http.HttpOutputMessage)
	 */
@Override
protected void writeInternal(Object t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    OutputStreamWriter osWriter = new OutputStreamWriter(outputMessage.getBody());
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, osWriter);
    try {
        writer.writeObject(t);
        writer.flush();
    } catch (JsonException e) {
        throw new IOException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter)

Example 14 with JsonWriter

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

the class JsonSerializableUtility method digestsDiffer.

private boolean digestsDiffer(Object fromValue, Object toValue) throws IOException, JsonException {
    try (DigestOutputStream dos = new DigestOutputStream(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        // no-op, just digesting
        }
    }, MessageDigest.getInstance("MD5"));
        OutputStreamWriter toStreamWriter = new OutputStreamWriter(dos);
        OutputStreamWriter fromStreamWriter = new OutputStreamWriter(dos)) {
        JsonWriter fromWriter = new JsonWriter(Common.JSON_CONTEXT, fromStreamWriter);
        // We need fresh writers to avoid miscellaneous commas or whatnot
        JsonWriter toWriter = new JsonWriter(Common.JSON_CONTEXT, toStreamWriter);
        fromWriter.writeObject(fromValue);
        fromWriter.flush();
        byte[] fromDigest = dos.getMessageDigest().digest();
        fromDigest = Arrays.copyOf(fromDigest, fromDigest.length);
        toWriter.writeObject(toValue);
        toWriter.flush();
        byte[] toDigest = dos.getMessageDigest().digest();
        return !Arrays.equals(fromDigest, toDigest);
    } catch (NoSuchAlgorithmException e) {
        // Required to implement MD5, really shouldn't happen
        throw new ShouldNeverHappenException(e);
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonWriter(com.serotonin.json.JsonWriter)

Example 15 with JsonWriter

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

the class StoreLatestProductionTest method testThreeTwoReleaseVersion.

// @Test
public void testThreeTwoReleaseVersion() throws JsonException, IOException, HttpException {
    // Setup json
    json.put("upgradeVersionState", UpgradeVersionState.PRODUCTION);
    json.put("currentVersionState", UpgradeVersionState.PRODUCTION);
    jsonModules.put("core", "3.2.0");
    jsonModules.put("mangoApi", "3.2.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() == 2);
    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)

Aggregations

JsonWriter (com.serotonin.json.JsonWriter)29 StringWriter (java.io.StringWriter)26 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)12 HttpPost (org.apache.http.client.methods.HttpPost)12 StringEntity (org.apache.http.entity.StringEntity)12 JsonArray (com.serotonin.json.type.JsonArray)10 JsonValue (com.serotonin.json.type.JsonValue)10 IOException (java.io.IOException)7 JsonException (com.serotonin.json.JsonException)6 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)5 HashMap (java.util.HashMap)4 Version (com.github.zafarkhaja.semver.Version)2 JsonContext (com.serotonin.json.JsonContext)2 JsonReader (com.serotonin.json.JsonReader)2 JsonObject (com.serotonin.json.type.JsonObject)2 JsonString (com.serotonin.json.type.JsonString)2 Module (com.serotonin.m2m2.module.Module)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 OutputStreamWriter (java.io.OutputStreamWriter)2 JsonRawValue (com.fasterxml.jackson.annotation.JsonRawValue)1