Skip to content

Commit

Permalink
Port UpdateCheckManager to httpclient5 (#39)
Browse files Browse the repository at this point in the history
cf. #38
  • Loading branch information
iaik-jheher authored Jan 12, 2023
1 parent 4ed8b71 commit 4d9d84e
Showing 1 changed file with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

import at.asit.pdfover.commons.Constants;
import at.asit.pdfover.commons.Messages;
import at.asit.pdfover.gui.bku.BKUHelper;
import at.asit.pdfover.gui.controls.Dialog;
import at.asit.pdfover.gui.controls.Dialog.BUTTONS;
import at.asit.pdfover.gui.controls.Dialog.ICON;
import at.asit.pdfover.gui.utils.HttpClientUtils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;

Expand Down Expand Up @@ -49,31 +51,27 @@ private static void setStatus(Status status) {

private static String latestVersionNotified = null;
private static Status runCheck(Shell shell) {
HttpClient client = (HttpClient) BKUHelper.getHttpClient();
GetMethod method = new GetMethod(Constants.CURRENT_RELEASE_URL);
try {
client.executeMethod(method);
final String version = method.getResponseBodyAsString().trim();
if (!VersionComparator.lessThan(Constants.APP_VERSION, version))
return Status.UP_TO_DATE;
try (final CloseableHttpClient httpClient = HttpClientUtils.builderWithSettings().build()) {
try (final CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(Constants.CURRENT_RELEASE_URL))) {
final String latestVersion = EntityUtils.toString(httpResponse.getEntity()).trim();
if (!VersionComparator.lessThan(Constants.APP_VERSION, latestVersion))
return Status.UP_TO_DATE;

if ((latestVersionNotified == null) || VersionComparator.lessThan(latestVersionNotified, version)) {
latestVersionNotified = version;
// invoke GUI message in main thread
shell.getDisplay().asyncExec(() -> {
Dialog info = new Dialog(shell,
Messages.getString("version_check.UpdateTitle"),
Messages.formatString("version_check.UpdateText", version),
BUTTONS.OK_CANCEL, ICON.INFORMATION);
if ((latestVersionNotified == null) || VersionComparator.lessThan(latestVersionNotified, latestVersion)) {
latestVersionNotified = latestVersion;
// invoke GUI message in main thread
shell.getDisplay().asyncExec(() -> {
Dialog info = new Dialog(shell, Messages.getString("version_check.UpdateTitle"), Messages.formatString("version_check.UpdateText", latestVersion),
BUTTONS.OK_CANCEL, ICON.INFORMATION);
if (info.open() == SWT.OK)
SWTUtils.openURL(Constants.UPDATE_URL);
});
}

if (info.open() == SWT.OK)
SWTUtils.openURL(Constants.UPDATE_URL);
});
return Status.OUTDATED;
}

return Status.OUTDATED;
} catch (Exception e) {
log.error("Error downloading update information: ", e);
log.warn("Error downloading update information: ", e);
return Status.FAILED;
}
}
Expand Down

0 comments on commit 4d9d84e

Please sign in to comment.