Search in sources :

Example 1 with Version

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));
        }
    });
}
Also used : NewsError(email.schaal.ocreader.api.json.NewsError) Status(email.schaal.ocreader.api.json.Status) Call(retrofit2.Call) Moshi(com.squareup.moshi.Moshi) HttpManager(email.schaal.ocreader.http.HttpManager) LoginError(email.schaal.ocreader.util.LoginError) HttpUrl(okhttp3.HttpUrl) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) APILevels(email.schaal.ocreader.api.json.APILevels) Callback(retrofit2.Callback) Version(com.github.zafarkhaja.semver.Version) NonNull(android.support.annotation.NonNull)

Example 2 with Version

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();
    }
}
Also used : Version(com.github.zafarkhaja.semver.Version) UpgradeServerVersion(org.talend.dataprep.api.service.upgrade.UpgradeServerVersion) UpgradeServerVersion(org.talend.dataprep.api.service.upgrade.UpgradeServerVersion) IOException(java.io.IOException) Timed(org.talend.dataprep.metrics.Timed) ApiOperation(io.swagger.annotations.ApiOperation) PublicAPI(org.talend.dataprep.security.PublicAPI) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Version

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));
}
Also used : Version(com.github.zafarkhaja.semver.Version) UpgradeServerVersion(org.talend.dataprep.api.service.upgrade.UpgradeServerVersion) UpgradeServerVersion(org.talend.dataprep.api.service.upgrade.UpgradeServerVersion) Test(org.junit.Test)

Example 4 with Version

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;
}
Also used : Version(com.github.zafarkhaja.semver.Version) TreeMap(java.util.TreeMap) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 5 with Version

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;
}
Also used : Version(com.github.zafarkhaja.semver.Version)

Aggregations

Version (com.github.zafarkhaja.semver.Version)27 Test (org.junit.Test)6 File (java.io.File)4 IOException (java.io.IOException)4 BrokerFilterBadVersionException (org.apache.pulsar.broker.loadbalance.BrokerFilterBadVersionException)3 JsonWriter (com.serotonin.json.JsonWriter)2 JsonObject (com.serotonin.json.type.JsonObject)2 JsonString (com.serotonin.json.type.JsonString)2 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)2 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2 BrokerData (org.apache.pulsar.broker.BrokerData)2 Plugin (org.syncany.plugins.Plugin)2 UpgradeServerVersion (org.talend.dataprep.api.service.upgrade.UpgradeServerVersion)2 NonNull (android.support.annotation.NonNull)1 MongoCluster (com.antwerkz.bottlerocket.clusters.MongoCluster)1