Search in sources :

Example 1 with JsonbJaxrsProvider

use of org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider in project component-runtime by Talend.

the class Github method load.

public Collection<Contributor> load() {
    final String token = "Basic " + Base64.getEncoder().encodeToString((user + ':' + password).getBytes(StandardCharsets.UTF_8));
    final Client client = ClientBuilder.newClient().register(new JsonbJaxrsProvider<>());
    final WebTarget gravatarBase = client.target(Gravatars.GRAVATAR_BASE);
    final ForkJoinPool pool = new ForkJoinPool(Math.max(4, Runtime.getRuntime().availableProcessors() * 8));
    try {
        return pool.submit(() -> contributors(client, token, "https://api.github.com/repos/talend/component-runtime/contributors").parallel().collect(toMap(e -> normalizeLogin(e.login), identity(), (c1, c2) -> {
            c1.contributions += c2.contributions;
            return c1;
        })).values().stream().map(contributor -> {
            if (contributor.url == null) {
                try {
                    final Contributor gravatar = Gravatars.loadGravatar(gravatarBase, contributor.email);
                    return Contributor.builder().name(contributor.name).commits(contributor.contributions).description(gravatar.getDescription()).gravatar(gravatar.getGravatar()).build();
                } catch (final Exception e) {
                    log.warn(e.getMessage(), e);
                    return new Contributor(contributor.email, contributor.email, "", Gravatars.url(contributor.email), contributor.contributions);
                }
            }
            final GithubUser user = client.target(contributor.url).request(APPLICATION_JSON_TYPE).header("Authorization", token).get(GithubUser.class);
            return Contributor.builder().id(contributor.login).name(ofNullable(user.name).orElse(contributor.name)).description((user.bio == null ? "" : user.bio) + (user.blog != null && !user.blog.trim().isEmpty() && (user.bio == null || !user.bio.contains(user.blog)) ? "\n\nBlog: " + user.blog : "")).commits(contributor.contributions).gravatar(ofNullable(contributor.avatarUrl).orElseGet(() -> {
                final String gravatarId = contributor.gravatarId == null || contributor.gravatarId.isEmpty() ? contributor.email : contributor.gravatarId;
                try {
                    final Contributor gravatar = Gravatars.loadGravatar(gravatarBase, gravatarId);
                    return gravatar.getGravatar();
                } catch (final Exception e) {
                    log.warn(e.getMessage(), e);
                    return Gravatars.url(gravatarId);
                }
            })).build();
        }).filter(Objects::nonNull).sorted(comparing(Contributor::getCommits).reversed()).collect(toList())).get(15, MINUTES);
    } catch (final ExecutionException ee) {
        if (WebApplicationException.class.isInstance(ee.getCause())) {
            final Response response = WebApplicationException.class.cast(ee.getCause()).getResponse();
            if (response != null && response.getEntity() != null) {
                log.error(response.readEntity(String.class));
            }
        }
        throw new IllegalStateException(ee.getCause());
    } catch (final InterruptedException | TimeoutException e) {
        throw new IllegalStateException(e);
    } finally {
        client.close();
        pool.shutdownNow();
    }
}
Also used : Client(javax.ws.rs.client.Client) TimeoutException(java.util.concurrent.TimeoutException) MINUTES(java.util.concurrent.TimeUnit.MINUTES) ClientBuilder(javax.ws.rs.client.ClientBuilder) Collectors.toMap(java.util.stream.Collectors.toMap) Comparator.comparing(java.util.Comparator.comparing) ROOT(java.util.Locale.ROOT) JsonbJaxrsProvider(org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) Server(org.talend.sdk.component.maven.Server) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) GenericType(javax.ws.rs.core.GenericType) Collectors.toList(java.util.stream.Collectors.toList) Base64(java.util.Base64) JsonbProperty(javax.json.bind.annotation.JsonbProperty) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) ForkJoinPool(java.util.concurrent.ForkJoinPool) Function.identity(java.util.function.Function.identity) Data(lombok.Data) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) WebApplicationException(javax.ws.rs.WebApplicationException) WebTarget(javax.ws.rs.client.WebTarget) MavenDecrypter(org.talend.sdk.component.maven.MavenDecrypter) AllArgsConstructor(lombok.AllArgsConstructor) WebApplicationException(javax.ws.rs.WebApplicationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException) Response(javax.ws.rs.core.Response) Objects(java.util.Objects) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) ExecutionException(java.util.concurrent.ExecutionException) ForkJoinPool(java.util.concurrent.ForkJoinPool) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with JsonbJaxrsProvider

use of org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider in project meecrowave by apache.

the class MeecrowaveClientLifecycleListenerTest method autoClose.

@Test
public void autoClose() throws NoSuchFieldException, IllegalAccessException {
    try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder().randomHttpPort().includePackages(MeecrowaveClientLifecycleListenerTest.class.getName())).bake()) {
        final Field jsonbs = JohnzonCdiExtension.class.getDeclaredField("jsonbs");
        jsonbs.setAccessible(true);
        final BeanManager beanManager = CDI.current().getBeanManager();
        final JohnzonCdiExtension extensionInstance = JohnzonCdiExtension.class.cast(beanManager.getContext(ApplicationScoped.class).get(beanManager.resolve(beanManager.getBeans(JohnzonCdiExtension.class))));
        final Collection<?> o = Collection.class.cast(jsonbs.get(extensionInstance));
        {
            // ensure server is init whatever test suite we run in
            final Client client = ClientBuilder.newClient();
            get(meecrowave, client);
            client.close();
        }
        final int origin = o.size();
        final Client client = ClientBuilder.newClient();
        final JsonbJaxrsProvider<?> provider = new JsonbJaxrsProvider<>();
        client.register(provider);
        get(meecrowave, client);
        assertEquals(origin + 1, o.size());
        client.close();
        assertEquals(origin, o.size());
    }
}
Also used : Field(java.lang.reflect.Field) JsonbJaxrsProvider(org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider) ClientBuilder(javax.ws.rs.client.ClientBuilder) Client(javax.ws.rs.client.Client) BeanManager(javax.enterprise.inject.spi.BeanManager) Meecrowave(org.apache.meecrowave.Meecrowave) JohnzonCdiExtension(org.apache.johnzon.jsonb.cdi.JohnzonCdiExtension) Test(org.junit.Test)

Example 3 with JsonbJaxrsProvider

use of org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider in project component-runtime by Talend.

the class Generator method generatedJira.

private static void generatedJira(final File generatedDir, final String username, final String password, final String version) {
    if (username == null || username.trim().isEmpty() || "skip".equals(username)) {
        log.error("No JIRA credentials, will skip changelog generation");
        return;
    }
    final String project = "TCOMP";
    final String jiraBase = "https://jira.talendforge.org";
    final File file = new File(generatedDir, "generated_changelog.adoc");
    final Client client = ClientBuilder.newClient().register(new JsonbJaxrsProvider<>());
    final String auth = "Basic " + Base64.getEncoder().encodeToString((username + ':' + password).getBytes(StandardCharsets.UTF_8));
    try {
        final WebTarget restApi = client.target(jiraBase + "/rest/api/2").property("http.connection.timeout", 60000L);
        final List<JiraVersion> versions = restApi.path("project/{project}/versions").resolveTemplate("project", project).request(APPLICATION_JSON_TYPE).header("Authorization", auth).get(new GenericType<List<JiraVersion>>() {
        });
        final String currentVersion = version.replace("-SNAPSHOT", "");
        final List<JiraVersion> loggedVersions = versions.stream().filter(v -> (v.isReleased() || jiraVersionMatches(currentVersion, v.getName()))).collect(toList());
        if (loggedVersions.isEmpty()) {
            try (final PrintStream stream = new PrintStream(new WriteIfDifferentStream(file))) {
                stream.println("No version found.");
            }
            return;
        }
        final String jql = "project=" + project + " AND labels=\"changelog\"" + loggedVersions.stream().map(v -> "fixVersion=" + v.getName()).collect(joining(" OR ", " AND (", ")"));
        final Function<Long, JiraIssues> searchFrom = startAt -> restApi.path("search").queryParam("jql", jql).queryParam("startAt", startAt).request(APPLICATION_JSON_TYPE).header("Authorization", auth).get(JiraIssues.class);
        final Function<JiraIssues, Stream<JiraIssues>> paginate = new Function<JiraIssues, Stream<JiraIssues>>() {

            @Override
            public Stream<JiraIssues> apply(final JiraIssues issues) {
                final long nextStartAt = issues.getStartAt() + issues.getMaxResults();
                final Stream<JiraIssues> fetched = Stream.of(issues);
                return issues.getTotal() > nextStartAt ? Stream.concat(fetched, apply(searchFrom.apply(nextStartAt))) : fetched;
            }
        };
        final Set<String> includeStatus = Stream.of("closed", "resolved", "development done", "qa done", "done").collect(toSet());
        final Map<String, TreeMap<String, List<JiraIssue>>> issues = Stream.of(searchFrom.apply(0L)).flatMap(paginate).flatMap(i -> ofNullable(i.getIssues()).map(Collection::stream).orElseGet(Stream::empty)).filter(issue -> includeStatus.contains(issue.getFields().getStatus().getName().toLowerCase(ENGLISH))).flatMap(i -> i.getFields().getFixVersions().stream().map(v -> Pair.of(v, i))).collect(groupingBy(pair -> pair.getKey().getName(), () -> new TreeMap<>(versionComparator()), groupingBy(pair -> pair.getValue().getFields().getIssuetype().getName(), TreeMap::new, collectingAndThen(mapping(Pair::getValue, toList()), l -> {
            l.sort(comparing(JiraIssue::getKey));
            return l;
        }))));
        final String changelog = issues.entrySet().stream().map(versionnedIssues -> new StringBuilder("\n\n== Version ").append(versionnedIssues.getKey()).append(versionnedIssues.getValue().entrySet().stream().collect((Supplier<StringBuilder>) StringBuilder::new, (builder, issuesByType) -> builder.append("\n\n=== ").append(issuesByType.getKey()).append("\n\n").append(issuesByType.getValue().stream().collect((Supplier<StringBuilder>) StringBuilder::new, // useful
        (a, i) -> a.append("- link:").append(jiraBase).append("/browse/").append(i.getKey()).append("[").append(i.getKey()).append("^]").append(": ").append(i.getFields().getSummary()).append("\n"), StringBuilder::append)), StringBuilder::append))).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();
        try (final PrintStream stream = new PrintStream(new WriteIfDifferentStream(file))) {
            stream.println(changelog);
        }
    } finally {
        client.close();
    }
}
Also used : Condition(org.talend.sdk.component.api.configuration.condition.meta.Condition) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) ConfigurationTypeParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ConfigurationTypeParameterEnricher) Type(org.talend.sdk.component.api.service.schema.Type) Collectors.toMap(java.util.stream.Collectors.toMap) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Defaults(org.talend.sdk.component.runtime.reflect.Defaults) ENGLISH(java.util.Locale.ENGLISH) UiParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.UiParameterEnricher) Collectors.toSet(java.util.stream.Collectors.toSet) Validations(org.talend.sdk.component.api.configuration.constraint.meta.Validations) JsonbBuilder(javax.json.bind.JsonbBuilder) ActiveIf(org.talend.sdk.component.api.configuration.condition.ActiveIf) Collection(java.util.Collection) Ui(org.talend.sdk.component.api.configuration.ui.meta.Ui) Mapper(org.apache.johnzon.mapper.Mapper) Set(java.util.Set) FilterOutputStream(java.io.FilterOutputStream) PropertyOrderStrategy(javax.json.bind.config.PropertyOrderStrategy) Collectors.joining(java.util.stream.Collectors.joining) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) GenericType(javax.ws.rs.core.GenericType) Base64(java.util.Base64) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Documentation(org.talend.sdk.component.api.meta.Documentation) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Configuration(org.apache.deltaspike.core.api.config.Configuration) PRIVATE(lombok.AccessLevel.PRIVATE) MapperBuilder(org.apache.johnzon.mapper.MapperBuilder) Proxy(java.lang.reflect.Proxy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Client(javax.ws.rs.client.Client) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) Function(java.util.function.Function) Supplier(java.util.function.Supplier) JsonbConfig(javax.json.bind.JsonbConfig) AutoLayout(org.talend.sdk.component.api.configuration.ui.layout.AutoLayout) Schema(org.talend.sdk.component.api.service.schema.Schema) ValidationParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ValidationParameterEnricher) ConfigProperty(org.apache.deltaspike.core.api.config.ConfigProperty) FileArchive(org.apache.xbean.finder.archive.FileArchive) ArrayList(java.util.ArrayList) ClientBuilder(javax.ws.rs.client.ClientBuilder) ComponentServerConfiguration(org.talend.sdk.component.server.configuration.ComponentServerConfiguration) Collectors.mapping(java.util.stream.Collectors.mapping) Validation(org.talend.sdk.component.api.configuration.constraint.meta.Validation) ConfigurationType(org.talend.sdk.component.api.configuration.type.meta.ConfigurationType) GridLayout(org.talend.sdk.component.api.configuration.ui.layout.GridLayout) Comparator.comparing(java.util.Comparator.comparing) JsonbJaxrsProvider(org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider) ValidationResult(org.talend.sdk.component.api.service.asyncvalidation.ValidationResult) ConditionParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ConditionParameterEnricher) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) BaseEnvironmentProvider(org.talend.sdk.component.junit.environment.BaseEnvironmentProvider) ParameterExtensionEnricher(org.talend.sdk.component.spi.parameter.ParameterExtensionEnricher) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) JarArchive(org.apache.xbean.finder.archive.JarArchive) Values(org.talend.sdk.component.api.service.completion.Values) Optional.ofNullable(java.util.Optional.ofNullable) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) HealthCheckStatus(org.talend.sdk.component.api.service.healthcheck.HealthCheckStatus) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) Data(lombok.Data) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) Jsonb(javax.json.bind.Jsonb) WebTarget(javax.ws.rs.client.WebTarget) Comparator(java.util.Comparator) ActionType(org.talend.sdk.component.api.service.ActionType) NoArgsConstructor(lombok.NoArgsConstructor) Function(java.util.function.Function) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) FilterOutputStream(java.io.FilterOutputStream) Stream(java.util.stream.Stream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) FileOutputStream(java.io.FileOutputStream) Client(javax.ws.rs.client.Client) Pair(org.apache.commons.lang3.tuple.Pair) PrintStream(java.io.PrintStream) TreeMap(java.util.TreeMap) WebTarget(javax.ws.rs.client.WebTarget) File(java.io.File)

Aggregations

Client (javax.ws.rs.client.Client)3 ClientBuilder (javax.ws.rs.client.ClientBuilder)3 JsonbJaxrsProvider (org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider)3 StandardCharsets (java.nio.charset.StandardCharsets)2 Base64 (java.util.Base64)2 Collection (java.util.Collection)2 Comparator.comparing (java.util.Comparator.comparing)2 Optional.ofNullable (java.util.Optional.ofNullable)2 Collectors.toList (java.util.stream.Collectors.toList)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 Stream (java.util.stream.Stream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FilterOutputStream (java.io.FilterOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1