Skip to content

Instantly share code, notes, and snippets.

@Skyost
Last active June 20, 2017 22:26
Show Gist options
  • Save Skyost/643dfec283359165a4abe610b5fcf24d to your computer and use it in GitHub Desktop.
Save Skyost/643dfec283359165a4abe610b5fcf24d to your computer and use it in GitHub Desktop.
An update checker based based on my Github updater.
package fr.skyost.bitbucket;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;
/**
* An update checker based based on my Github updater.
*
* @author Skyost.
*/
public class BitbucketUpdater extends Thread {
public static final String ENDPOINT = "https://api.bitbucket.org/2.0/repositories/%s/%s/downloads";
public static final String USER_AGENT = "BitbucketUpdater by Skyost";
private final String username;
private final String repo;
private final String projectName;
private final String localVersion;
private final BitbucketResultListener caller;
/**
* Creates a new <b>BitbucketUpdater</b> instance.
*
* @param username Bitbucket username.
* @param repo Bitbucket repo.
* @param projectName Your project name (<i>projectName</i> v<i>localVersion</i>).
* @param localVersion Your project's local version (<i>name</i> v<i>localVersion</i>).
* @param caller The caller.
*/
public BitbucketUpdater(final String username, final String repo, final String projectName, final String localVersion, final BitbucketResultListener caller) {
this.username = username;
this.repo = repo;
this.projectName = projectName;
this.localVersion = localVersion;
this.caller = caller;
}
@Override
public final void run() {
caller.updaterStarted();
try {
final HttpURLConnection connection = (HttpURLConnection)new URL(String.format(ENDPOINT, username, repo)).openConnection();
connection.addRequestProperty("User-Agent", USER_AGENT);
final String response = connection.getResponseCode() + " " + connection.getResponseMessage();
caller.updaterResponse(response);
if(!response.startsWith("2")) {
throw new Exception("Invalid response : " + response);
}
final InputStream input = connection.getInputStream();
final InputStreamReader inputStreamReader = new InputStreamReader(input, StandardCharsets.UTF_8);
final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
final JsonArray releases = Json.parse(bufferedReader.readLine()).asObject().get("values").asArray();
input.close();
inputStreamReader.close();
bufferedReader.close();
if(releases.size() < 1) {
caller.updaterNoUpdate(localVersion, localVersion);
return;
}
final String remoteVersion = releases.get(0).asObject().get("name").asString().replace(projectName + " v", "");
if(compareVersions(remoteVersion, localVersion)) {
caller.updaterUpdateAvailable(localVersion, remoteVersion);
}
else {
caller.updaterNoUpdate(localVersion, remoteVersion);
}
}
catch(final Exception ex) {
caller.updaterException(ex);
}
}
/**
* Compares two versions.
*
* @param versionTo The version you want to compare to.
* @param versionWith The version you want to compare with.
*
* @return <b>true</b> If <b>versionTo</b> is inferior than <b>versionWith</b>.
* <br><b>false</b> If <b>versionTo</b> is superior or equals to <b>versionWith</b>.
*/
private static final boolean compareVersions(final String versionTo, final String versionWith) {
return normalisedVersion(versionTo, ".", 4).compareTo(normalisedVersion(versionWith, ".", 4)) > 0;
}
/**
* Gets the formatted name of a version.
* <br>Used for the method <b>compareVersions(...)</b> of this class.
*
* @param version The version you want to format.
* @param separator The separator between the numbers of this version.
* @param maxWidth The max width of the formatted version.
*
* @return A string which the formatted version of your version.
*
* @author Peter Lawrey.
*/
private static final String normalisedVersion(final String version, final String separator, final int maxWidth) {
final StringBuilder stringBuilder = new StringBuilder();
for(final String normalised : Pattern.compile(separator, Pattern.LITERAL).split(version)) {
stringBuilder.append(String.format("%" + maxWidth + 's', normalised));
}
return stringBuilder.toString();
}
public interface BitbucketResultListener {
/**
* When the updater starts.
*/
public void updaterStarted();
/**
* When an Exception occurs.
*
* @param ex The Exception.
*/
public void updaterException(final Exception ex);
/**
* The response of the request.
*
* @param response The response.
*/
public void updaterResponse(final String response);
/**
* If an update is available.
*
* @param localVersion The local version (used to create the updater).
* @param remoteVersion The remote version.
*/
public void updaterUpdateAvailable(final String localVersion, final String remoteVersion);
/**
* If there is no update.
*
* @param localVersion The local version (used to create the updater).
* @param remoteVersion The remote version.
*/
public void updaterNoUpdate(final String localVersion, final String remoteVersion);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment