Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom check suport #250

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

package com.osiris.autoplug.client.tasks.updater.search;

import com.google.gson.JsonObject;
import com.google.gson.*;
import com.osiris.autoplug.client.utils.UtilsURL;
import com.osiris.jlib.json.Json;
import java.util.ArrayList;


public class CustomCheckURL {
Expand All @@ -34,36 +35,25 @@ public SearchResult doCustomCheck(String url, String currentVersion) {
String downloadUrl = null;
SearchResult.Type code = SearchResult.Type.UP_TO_DATE;
try {
JsonElement response = Json.get(url);
JsonObject release;
try {
release = Json.getAsJsonArray(url)
.get(0).getAsJsonObject();
} catch (Exception e) {
throw e;
}

String[] versionNaming = {"version_number", "version"};

for (String naming : versionNaming) {
if (release.has(naming)) {
String version = release.get(naming).getAsString().replaceAll("[^0-9.]", "");
if (!version.isEmpty()) {
latest = version;
break;
}
if (response.isJsonArray()) {
try {
latest = traverseJsonArray(response.getAsJsonArray(), (byte) 0);
downloadUrl = traverseJsonArray(response.getAsJsonArray(), (byte) 1);
} catch (Exception e) {
throw e;
}
}

String[] downloadNaming = {"download_url", "download", "file", "download_file"};

for (String naming : downloadNaming) {
if (release.has(naming)) {
String durl = release.get(naming).getAsString();
if (!durl.isEmpty()) {
downloadUrl = durl;
break;
}
} else if (response.isJsonObject()) {
try {
latest = traverseJsonObject(response.getAsJsonObject(), (byte) 0);
downloadUrl = traverseJsonObject(response.getAsJsonObject(), (byte) 1);
} catch (Exception e) {
throw e;
}
} else {
throw new IllegalArgumentException("Invalid JSON response format");
}

String[] pluginVersionComponents = currentVersion.split("\\.");
Expand Down Expand Up @@ -94,4 +84,73 @@ public SearchResult doCustomCheck(String url, String currentVersion) {
result.setException(exception);
return result;
}

private String getLatestVersionFromRe(JsonObject release){

String[] versionNaming = {"version_number", "version"};
String latest = null;

for (String naming : versionNaming) {
if (release.has(naming)) {
String version = release.get(naming).getAsString().replaceAll("[^0-9.]", "");
if (!version.isEmpty()) {
latest = version;
break;
}
}
}
return latest;
}

private String getDownloadFromRe(JsonObject release){

String[] downloadNaming = {"download_url", "download", "file", "download_file"};
String downloadUrl = null;

for (String naming : downloadNaming) {
if (release.has(naming)) {
String durl = release.get(naming).getAsString();
if (!durl.isEmpty()) {
downloadUrl = durl;
break;
}
}
}
return downloadUrl;
}

private String traverseJsonArray(JsonArray jsonArray, byte lookingFor) {
String r = null;
for (JsonElement element : jsonArray) {
if (element.isJsonObject()) {
r = traverseJsonObject(element.getAsJsonObject(),lookingFor);
} else if (element.isJsonArray()) {
r = traverseJsonArray(element.getAsJsonArray(),lookingFor);
}
if (r != null)
break;
}
return r;
}

private String traverseJsonObject(JsonObject jsonObject, byte lookingFor) {
String r = null;
for (String key : jsonObject.keySet()) {
JsonElement element = jsonObject.get(key);
if (element.isJsonObject()) {
r = traverseJsonObject(element.getAsJsonObject(),lookingFor);
} else if (element.isJsonArray()) {
r = traverseJsonArray(element.getAsJsonArray(),lookingFor);
}
if (r != null)
break;
}

if (lookingFor == 0) {
r = getLatestVersionFromRe(jsonObject.getAsJsonObject());
} else if (lookingFor == 1) {
r = getDownloadFromRe(jsonObject.getAsJsonObject());
}
return r;
}
}