Search in sources :

Example 1 with UpgradeServerVersion

use of org.talend.dataprep.api.service.upgrade.UpgradeServerVersion in project data-prep by Talend.

the class UpgradeAPI method fetchServerUpgradeVersions.

private List<UpgradeServerVersion> fetchServerUpgradeVersions(org.talend.dataprep.info.Version version) throws IOException {
    final HttpPost post = new HttpPost(upgradeVersionLocation);
    final String response;
    final StringWriter content = new StringWriter();
    try (final JsonGenerator generator = mapper.getFactory().createGenerator(content)) {
        generator.writeStartObject();
        generator.writeStringField("version", version.getVersionId());
        generator.writeStringField("id", token);
        generator.writeEndObject();
        generator.flush();
        post.setEntity(new StringEntity(content.toString(), ContentType.APPLICATION_JSON.withCharset(UTF_8)));
        response = IOUtils.toString(httpClient.execute(post).getEntity().getContent(), UTF_8);
    } finally {
        post.releaseConnection();
    }
    // Read upgrade server response
    return mapper.readerFor(new TypeReference<List<UpgradeServerVersion>>() {
    }).readValue(response);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) UpgradeServerVersion(org.talend.dataprep.api.service.upgrade.UpgradeServerVersion) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 2 with UpgradeServerVersion

use of org.talend.dataprep.api.service.upgrade.UpgradeServerVersion 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 UpgradeServerVersion

use of org.talend.dataprep.api.service.upgrade.UpgradeServerVersion 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 UpgradeServerVersion

use of org.talend.dataprep.api.service.upgrade.UpgradeServerVersion in project data-prep by Talend.

the class UpgradeAPITest method checkDynamicUpgradeServer.

@Test
public void checkDynamicUpgradeServer() throws Exception {
    // When
    // Server deliver response based on sent content
    upgradeAPI.setUpgradeVersionLocation("http://localhost:" + port + "/upgrade/server/dynamic");
    // Then
    String response = RestAssured.when().get("/api/upgrade/check").asString();
    final List<UpgradeServerVersion> versions = mapper.readerFor(new TypeReference<List<UpgradeServerVersion>>() {
    }).readValue(response);
    assertEquals(1, versions.size());
    final UpgradeServerVersion actual = versions.get(0);
    final String expectedVersion = versionService.version().getVersionId();
    final String expectedToken = TokenGenerator.generateMachineToken(new CryptoHelper("DataPrepIsSoCool"));
    assertEquals(expectedToken, actual.getTitle());
    assertEquals(expectedVersion, actual.getDownloadUrl());
}
Also used : UpgradeServerVersion(org.talend.dataprep.api.service.upgrade.UpgradeServerVersion) CryptoHelper(org.talend.daikon.security.CryptoHelper) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 5 with UpgradeServerVersion

use of org.talend.dataprep.api.service.upgrade.UpgradeServerVersion in project data-prep by Talend.

the class UpgradeAPI method toString.

private static String toString(List<UpgradeServerVersion> versions) {
    final StringBuilder builder = new StringBuilder();
    builder.append("[ ");
    for (UpgradeServerVersion version : versions) {
        builder.append(version.getVersion()).append(' ');
    }
    builder.append(']');
    return builder.toString();
}
Also used : UpgradeServerVersion(org.talend.dataprep.api.service.upgrade.UpgradeServerVersion)

Aggregations

UpgradeServerVersion (org.talend.dataprep.api.service.upgrade.UpgradeServerVersion)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 Version (com.github.zafarkhaja.semver.Version)2 Test (org.junit.Test)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 CryptoHelper (org.talend.daikon.security.CryptoHelper)1 Timed (org.talend.dataprep.metrics.Timed)1 PublicAPI (org.talend.dataprep.security.PublicAPI)1