use of org.openecard.common.util.InvalidUpdateDefinition in project open-ecard by ecsec.
the class TestRealVersionUpdate method versionUpdateDownloadUrlNotHTTP.
@Test(enabled = true)
public void versionUpdateDownloadUrlNotHTTP() {
try {
String invalidDownloadURL = "file://test";
JSONObject obj = new JSONObjectBuilder().downloadUrl(invalidDownloadURL).build();
VersionUpdate update = VersionUpdate.fromJson(obj);
// Exception expected
Assert.fail();
} catch (InvalidUpdateDefinition ex) {
Assert.assertEquals(ex.getMessage(), "Download URL is not an http URL.");
}
}
use of org.openecard.common.util.InvalidUpdateDefinition in project open-ecard by ecsec.
the class TestRealVersionUpdate method versionUpdateDownloadPageNotHTTP.
@Test(enabled = true)
public void versionUpdateDownloadPageNotHTTP() {
try {
String invalidDownloadPage = "file://test";
JSONObject obj = new JSONObjectBuilder().downloadPage(invalidDownloadPage).build();
VersionUpdate update = VersionUpdate.fromJson(obj);
// Exception expected
Assert.fail();
} catch (InvalidUpdateDefinition ex) {
Assert.assertEquals(ex.getMessage(), "Download Page URL is not an http URL.");
}
}
use of org.openecard.common.util.InvalidUpdateDefinition in project open-ecard by ecsec.
the class TestRealVersionUpdate method versionUpdateinvalidSemanticVersionSpecified.
@Test(enabled = true)
public void versionUpdateinvalidSemanticVersionSpecified() {
try {
String invalidVersion = "0.0.0";
JSONObject obj = new JSONObjectBuilder().version(invalidVersion).build();
VersionUpdate update = VersionUpdate.fromJson(obj);
// Exception expected
Assert.fail();
} catch (InvalidUpdateDefinition ex) {
Assert.assertEquals(ex.getMessage(), "Invalid version specified.");
}
}
use of org.openecard.common.util.InvalidUpdateDefinition in project open-ecard by ecsec.
the class VersionUpdate method fromJson.
public static VersionUpdate fromJson(JSONObject jsonObject) throws InvalidUpdateDefinition {
try {
SemanticVersion version = new SemanticVersion((String) jsonObject.get("version"));
URL dlPage = new URL((String) jsonObject.get("download_page"));
URL dlUrl = new URL((String) jsonObject.get("download_url"));
Status status;
try {
status = Status.valueOf((String) jsonObject.get("status"));
} catch (IllegalArgumentException ex) {
status = Status.UNKNOWN;
}
if (version.getMajor() == 0 && version.getMinor() == 0 && version.getPatch() == 0) {
throw new InvalidUpdateDefinition("Invalid version specified.");
}
if (!"http".equalsIgnoreCase(dlPage.getProtocol()) && !"https".equalsIgnoreCase(dlPage.getProtocol())) {
throw new InvalidUpdateDefinition("Download Page URL is not an http URL.");
}
if (!"http".equalsIgnoreCase(dlUrl.getProtocol()) && !"https".equalsIgnoreCase(dlUrl.getProtocol())) {
throw new InvalidUpdateDefinition("Download URL is not an http URL.");
}
return new VersionUpdate(version, dlPage, dlUrl, status);
} catch (MalformedURLException | NullPointerException ex) {
throw new InvalidUpdateDefinition("Incomplete JSON data received.", ex);
}
}
use of org.openecard.common.util.InvalidUpdateDefinition in project open-ecard by ecsec.
the class VersionUpdateLoader method loadVersionUpdateList.
public VersionUpdateList loadVersionUpdateList() throws IllegalArgumentException {
try {
// load proxy if one is available
// make sure it is initialized
ProxySettings.getDefault();
List<Proxy> proxies = ProxySelector.getDefault().select(updateUrl.toURI());
Proxy p = Proxy.NO_PROXY;
for (Proxy next : proxies) {
if (next.type() != Proxy.Type.DIRECT) {
LOG.debug("Found a proxy for the update connection.");
p = next;
break;
}
}
LOG.info("Trying to load version list.");
URLConnection con = updateUrl.openConnection(p);
con.connect();
InputStream in = con.getInputStream();
Reader r = new InputStreamReader(in, StandardCharsets.UTF_8);
JSONObject rootObj = (JSONObject) new JSONParser().parse(r);
// get package specific download page
String downloadPageString = (String) rootObj.get(pkgType + "_download_page");
// access package specific list
JSONArray updatesRaw = (JSONArray) rootObj.get(pkgType);
ArrayList<VersionUpdate> updates = new ArrayList<>();
for (Object ur : updatesRaw) {
try {
VersionUpdate next = VersionUpdate.fromJson((JSONObject) ur);
updates.add(next);
} catch (InvalidUpdateDefinition ex) {
LOG.warn("Invalid version info contained in update list.", ex);
throw new IllegalArgumentException("Invalid version info contained in update list.", ex);
}
}
// make sure the versions are in the correct order
Collections.sort(updates);
VersionUpdateList list = new VersionUpdateList(updates, new URL(downloadPageString));
LOG.info("Successfully retrieved version update list.");
return list;
} catch (IOException ex) {
LOG.error("Failed to retrieve update list from server.", ex);
throw new IllegalArgumentException("Failed to retrieve update list from server.", ex);
} catch (NullPointerException ex) {
LOG.warn("Package type {} not supported in update list.", pkgType);
throw new IllegalArgumentException("Package type " + pkgType + " not supported in update list.", ex);
} catch (URISyntaxException ex) {
String msg = "Failed to convert Update URL to a URI.";
LOG.error(msg, ex);
throw new IllegalArgumentException(msg, ex);
} catch (ParseException ex) {
String msg = "Failed to deserialize JSON data.";
LOG.error(msg, ex);
throw new IllegalArgumentException(msg, ex);
}
}
Aggregations