use of com.github.zafarkhaja.semver.Version in project ocreader by schaal.
the class API method login.
public static void login(final Context context, final HttpUrl baseUrl, final String username, final String password, final APICallback<Status, LoginError> loginCallback) {
final HttpManager httpManager = new HttpManager(username, password, baseUrl);
final HttpUrl resolvedBaseUrl = baseUrl.resolve("");
if (resolvedBaseUrl == null) {
loginCallback.onFailure(new LoginError("Couldn't parse URL"));
return;
}
final Moshi moshi = new Moshi.Builder().build();
final JsonAdapter<NewsError> errorJsonAdapter = moshi.adapter(NewsError.class);
final Retrofit retrofit = new Retrofit.Builder().baseUrl(resolvedBaseUrl).client(httpManager.getClient()).addConverterFactory(MoshiConverterFactory.create(moshi)).build();
final CommonAPI commonAPI = retrofit.create(CommonAPI.class);
commonAPI.apiLevels().enqueue(new Callback<APILevels>() {
@Override
public void onResponse(@NonNull Call<APILevels> call, @NonNull Response<APILevels> response) {
if (response.isSuccessful()) {
loginInstance = null;
final APILevels apiLevels = response.body();
final Level apiLevel = apiLevels != null ? apiLevels.highestSupportedApi() : null;
loginInstance = Level.getAPI(context, apiLevel);
if (apiLevel == null) {
loginCallback.onFailure(new LoginError(context.getString(R.string.error_not_compatible)));
} else {
loginInstance.setupApi(httpManager);
loginInstance.metaData(new Callback<Status>() {
@Override
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
if (response.isSuccessful()) {
final Status status = response.body();
final Version version = status != null ? status.getVersion() : null;
if (version != null && MIN_VERSION.lessThanOrEqualTo(version)) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(Preferences.USERNAME.getKey(), username).putString(Preferences.PASSWORD.getKey(), password).putString(Preferences.URL.getKey(), resolvedBaseUrl.toString()).putString(Preferences.SYS_DETECTED_API_LEVEL.getKey(), apiLevel.getLevel()).apply();
instance = loginInstance;
loginInstance = null;
loginCallback.onSuccess(status);
} else {
if (version != null) {
Log.d(TAG, String.format("Nextcloud News version is less than minimally supported version: %s < %s", version.toString(), MIN_VERSION.toString()));
loginCallback.onFailure(new LoginError(context.getString(R.string.ncnews_too_old, MIN_VERSION.toString())));
} else {
Log.d(TAG, "Couldn't parse Nextcloud News version");
loginCallback.onFailure(new LoginError(context.getString(R.string.failed_detect_nc_version)));
}
}
} else {
String message = getErrorMessage(errorJsonAdapter, response);
Log.d(TAG, "Metadata call failed with error: " + message);
loginCallback.onFailure(LoginError.getError(context, response.code(), message));
}
}
@Override
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Log.e(TAG, "Failed to log in", t);
loginCallback.onFailure(LoginError.getError(context, t));
}
});
}
} else {
Log.d(TAG, "API level call failed with error: " + response.code() + " " + getErrorMessage(errorJsonAdapter, response));
// Either nextcloud news is not installed or version prior 8.8.0
loginCallback.onFailure(LoginError.getError(context, response.code(), context.getString(R.string.ncnews_too_old, MIN_VERSION.toString())));
}
}
@Override
public void onFailure(@NonNull Call<APILevels> call, @NonNull Throwable t) {
Log.e(TAG, "Failed to log in", t);
loginCallback.onFailure(LoginError.getError(context, t));
}
});
}
use of com.github.zafarkhaja.semver.Version in project data-prep by Talend.
the class UpgradeAPI method check.
@RequestMapping(value = "/api/upgrade/check", method = GET)
@ApiOperation(value = "Checks if a newer versions are available and returns them as JSON.", produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@PublicAPI
public Stream<UpgradeServerVersion> check() {
// defensive programming
if (StringUtils.isBlank(upgradeVersionLocation)) {
return Stream.empty();
}
try {
// Get current version
final Version parsedCurrentVersion = fromInternal(service.version());
// POST to URL that serves a JSON Version object
LOGGER.debug("Contacting upgrade server @ '{}'", upgradeVersionLocation);
List<UpgradeServerVersion> versions = fetchServerUpgradeVersions(service.version());
LOGGER.debug("{} available version(s) returned by update server: {}", versions.size(), toString(versions));
// Compare current version with available and filter new versions
return versions.stream().filter(v -> Version.valueOf(v.getVersion()).greaterThan(parsedCurrentVersion));
} catch (Exception e) {
LOGGER.error("Unable to check for new version (message: {}).", e.getMessage());
LOGGER.debug("Exception occurred during new version check. ", e);
return Stream.empty();
}
}
use of com.github.zafarkhaja.semver.Version in project data-prep by Talend.
the class UpgradeAPITest method checkStaticUpgradeServer.
@Test
public void checkStaticUpgradeServer() throws Exception {
// Given
// Server deliver always same response based (does *not* depend on sent content)
upgradeAPI.setUpgradeVersionLocation("http://localhost:" + port + "/upgrade/server/static");
final Version nextVersion = computeNextVersion(versionService);
UpgradeServerVersion expected = new UpgradeServerVersion();
expected.setVersion(nextVersion.toString());
expected.setTitle("My Title 2");
expected.setDownloadUrl("http://www.amazingdownload.com/2");
expected.setReleaseNoteUrl("http://www.amazingrelease.com/2");
// When
String actual = RestAssured.when().get("/api/upgrade/check").asString();
List<UpgradeServerVersion> actualParsed = mapper.readerFor(UpgradeServerVersion.class).<UpgradeServerVersion>readValues(actual).readAll();
// Then
Assert.assertEquals(1, actualParsed.size());
Assert.assertEquals(expected, actualParsed.get(0));
}
use of com.github.zafarkhaja.semver.Version in project grafikon by jub77.
the class ManifestVersionInfo method getVersionsImpl.
private Map<String, VersionData> getVersionsImpl() throws IOException {
long startTime = System.currentTimeMillis();
Map<String, VersionData> versions = new TreeMap<>();
Enumeration<URL> urls = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
for (URL url : (Iterable<URL>) () -> Iterators.forEnumeration(urls)) {
Manifest m = new Manifest(url.openStream());
String title = m.getMainAttributes().getValue("Implementation-Title");
String versionString = m.getMainAttributes().getValue("Implementation-Version");
if (title != null && title.startsWith("grafikon")) {
Version version = Version.valueOf(versionString);
versions.put(title, new VersionData() {
@Override
public Version getVersion() {
return version;
}
@Override
public String getTitle() {
return title;
}
@Override
public String toString() {
return String.format("%s: %s", getTitle(), getVersion());
}
});
}
}
log.debug("Analysis of versions finished in {}ms", System.currentTimeMillis() - startTime);
return versions;
}
use of com.github.zafarkhaja.semver.Version in project grafikon by jub77.
the class VersionInfo method getVersionWithoutBuild.
public Version getVersionWithoutBuild() {
Version completeVersion = getVersion();
Version version = new Version.Builder().setNormalVersion(completeVersion.getNormalVersion().toString()).setPreReleaseVersion(completeVersion.getPreReleaseVersion().toString()).build();
return version;
}
Aggregations