use of com.serotonin.json.type.JsonTypeReader 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();
}
use of com.serotonin.json.type.JsonTypeReader in project ma-core-public by infiniteautomation.
the class SerotoninJsonMessageConverter method readInternal.
/* (non-Javadoc)
* @see org.springframework.http.converter.AbstractHttpMessageConverter#readInternal(java.lang.Class, org.springframework.http.HttpInputMessage)
*/
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
InputStreamReader isReader = new InputStreamReader(inputMessage.getBody());
JsonTypeReader typeReader = new JsonTypeReader(isReader);
try {
JsonValue value = typeReader.read();
if (clazz.equals(JsonValue.class))
return value;
// First get the definition for the model so we can create a real object
ModelDefinition def = findModelDefinition(clazz);
AbstractRestModel<?> model = def.createModel();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, value);
if (value instanceof JsonObject) {
// TODO Should do some pre-validation or something to ensure we are
// importing the right thing?
JsonObject root = value.toJsonObject();
if (model != null) {
Object data = model.getData();
reader.readInto(data, root);
return model;
} else {
// Catchall
return root.toNative();
}
} else {
throw new IOException("Huh?");
}
} catch (JsonException e) {
throw new IOException(e);
}
}
use of com.serotonin.json.type.JsonTypeReader 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));
}
}
use of com.serotonin.json.type.JsonTypeReader in project ma-core-public by infiniteautomation.
the class StoreLatestProductionTest method testThreeTwoPreviousDevelopmentVersion.
// @Test
public void testThreeTwoPreviousDevelopmentVersion() throws JsonException, IOException, HttpException {
// Setup json
json.put("upgradeVersionState", UpgradeVersionState.DEVELOPMENT);
json.put("currentVersionState", UpgradeVersionState.DEVELOPMENT);
jsonModules.put("core", "3.2.2-SNAPSHOT");
jsonModules.put("mangoApi", "3.2.3-SNAPSHOT");
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));
}
}
use of com.serotonin.json.type.JsonTypeReader in project ma-core-public by infiniteautomation.
the class StoreLatestProductionTest method testThreeThreeDevelopmentVersion.
// @Test
public void testThreeThreeDevelopmentVersion() throws JsonException, IOException, HttpException {
// Setup json
json.put("upgradeVersionState", UpgradeVersionState.DEVELOPMENT);
json.put("currentVersionState", UpgradeVersionState.DEVELOPMENT);
jsonModules.put("core", "3.3.0-SNAPSHOT");
jsonModules.put("mangoApi", "3.3.0-SNAPSHOT");
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));
}
}
Aggregations