Search in sources :

Example 1 with DocumentationContent

use of org.talend.sdk.component.server.front.model.DocumentationContent in project component-runtime by Talend.

the class DocumentationResourceTest method getDoc.

@Test
void getDoc() {
    final DocumentationContent content = base.path("documentation/component/{id}").resolveTemplate("id", client.getJdbcId()).request(APPLICATION_JSON_TYPE).get(DocumentationContent.class);
    assertEquals("asciidoc", content.getType());
    assertEquals("= Test\n\n== Component\n\nSomething", content.getSource());
}
Also used : DocumentationContent(org.talend.sdk.component.server.front.model.DocumentationContent) Test(org.junit.jupiter.api.Test)

Example 2 with DocumentationContent

use of org.talend.sdk.component.server.front.model.DocumentationContent in project component-runtime by Talend.

the class DocumentationResource method getDocumentation.

/**
 * Returns an asciidoctor version of the documentation for the component represented by its identifier `id`.
 *
 * Format can be either asciidoc or html - if not it will fallback on asciidoc - and if html is selected you get
 * a partial document.
 *
 * IMPORTANT: it is recommended to use asciidoc format and handle the conversion on your side if you can,
 * the html flavor handles a limited set of the asciidoc syntax only like plain arrays, paragraph and titles.
 *
 * The documentation will likely be the family documentation but you can use anchors to access a particular
 * component (_componentname_inlowercase).
 *
 * @param id the component identifier.
 * @param language the expected language for the documentation (default to en if not found).
 * @param format the expected format (asciidoc or html).
 * @return the documentation for that component.
 */
@GET
@Path("component/{id}")
@Produces(MediaType.APPLICATION_JSON)
public DocumentationContent getDocumentation(@PathParam("id") final String id, @QueryParam("language") @DefaultValue("en") final String language, @QueryParam("format") @DefaultValue("asciidoc") final String format) {
    final Locale locale = localeMapper.mapLocale(language);
    final Container container = ofNullable(componentDao.findById(id)).map(meta -> manager.findPlugin(meta.getParent().getPlugin()).orElseThrow(() -> new WebApplicationException(Response.status(NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.PLUGIN_MISSING, "No plugin '" + meta.getParent().getPlugin() + "'")).build()))).orElseThrow(() -> new WebApplicationException(Response.status(NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.COMPONENT_MISSING, "No component '" + id + "'")).build()));
    // rendering to html can be slow so do it lazily and once
    DocumentationCache cache = container.get(DocumentationCache.class);
    if (cache == null) {
        synchronized (container) {
            cache = container.get(DocumentationCache.class);
            if (cache == null) {
                cache = new DocumentationCache();
                container.set(DocumentationCache.class, cache);
            }
        }
    }
    return cache.documentations.computeIfAbsent(new DocKey(id, language, format), key -> {
        // todo: handle i18n properly, for now just fallback on not suffixed version and assume the dev put it
        // in the comp
        final String content = Stream.of("documentation_" + locale.getLanguage() + ".adoc", "documentation_" + language + ".adoc", "documentation.adoc").map(name -> container.getLoader().getResource("TALEND-INF/" + name)).filter(Objects::nonNull).findFirst().map(url -> {
            try (final BufferedReader stream = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
                return stream.lines().collect(joining("\n"));
            } catch (final IOException e) {
                throw new WebApplicationException(Response.status(INTERNAL_SERVER_ERROR).entity(new ErrorPayload(ErrorDictionary.UNEXPECTED, e.getMessage())).build());
            }
        }).map(value -> {
            switch(format) {
                case "html":
                case "html5":
                    return adoc.toHtml(value);
                case "asciidoc":
                case "adoc":
                default:
                    return value;
            }
        }).orElseThrow(() -> new WebApplicationException(Response.status(NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.COMPONENT_MISSING, "No component '" + id + "'")).build()));
        return new DocumentationContent(format, content);
    });
}
Also used : Locale(java.util.Locale) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) DocumentationContent(org.talend.sdk.component.server.front.model.DocumentationContent) LocaleMapper(org.talend.sdk.component.server.service.LocaleMapper) QueryParam(javax.ws.rs.QueryParam) Locale(java.util.Locale) DefaultValue(javax.ws.rs.DefaultValue) Container(org.talend.sdk.component.container.Container) AsciidoctorService(org.talend.sdk.component.server.service.AsciidoctorService) Optional.ofNullable(java.util.Optional.ofNullable) INTERNAL_SERVER_ERROR(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors.joining(java.util.stream.Collectors.joining) StandardCharsets(java.nio.charset.StandardCharsets) ErrorDictionary(org.talend.sdk.component.server.front.model.ErrorDictionary) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) ComponentDao(org.talend.sdk.component.server.dao.ComponentDao) Data(lombok.Data) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) BufferedReader(java.io.BufferedReader) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) WebApplicationException(javax.ws.rs.WebApplicationException) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DocumentationContent(org.talend.sdk.component.server.front.model.DocumentationContent) Container(org.talend.sdk.component.container.Container) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Objects(java.util.Objects) BufferedReader(java.io.BufferedReader) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with DocumentationContent

use of org.talend.sdk.component.server.front.model.DocumentationContent in project component-runtime by Talend.

the class DocumentationResourceTest method wsDoc.

@Test
void wsDoc() {
    final DocumentationContent content = ws.read(DocumentationContent.class, "GET", "/documentation/component/" + client.getJdbcId() + "?format=html", null);
    assertEquals("html", content.getType());
    assertEquals("<h1 id=\"_test\">Test</h1>\n" + "<h2 id=\"_component\">Component</h2>\n" + "<div class=\"paragraph\">\n" + "<p>\n" + "Something\n" + "</p>\n" + "</div>\n", content.getSource());
}
Also used : DocumentationContent(org.talend.sdk.component.server.front.model.DocumentationContent) Test(org.junit.jupiter.api.Test)

Example 4 with DocumentationContent

use of org.talend.sdk.component.server.front.model.DocumentationContent in project component-runtime by Talend.

the class DocumentationResourceTest method getDocHtml.

@Test
void getDocHtml() {
    final DocumentationContent content = base.path("documentation/component/{id}").resolveTemplate("id", client.getJdbcId()).queryParam("format", "html").queryParam("headerFooter", false).request(APPLICATION_JSON_TYPE).get(DocumentationContent.class);
    assertEquals("html", content.getType());
    assertEquals("<h1 id=\"_test\">Test</h1>\n" + "<h2 id=\"_component\">Component</h2>\n" + "<div class=\"paragraph\">\n" + "<p>\n" + "Something\n" + "</p>\n" + "</div>\n", content.getSource());
}
Also used : DocumentationContent(org.talend.sdk.component.server.front.model.DocumentationContent) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentationContent (org.talend.sdk.component.server.front.model.DocumentationContent)4 Test (org.junit.jupiter.api.Test)3 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Locale (java.util.Locale)1 Objects (java.util.Objects)1 Optional.ofNullable (java.util.Optional.ofNullable)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Collectors.joining (java.util.stream.Collectors.joining)1 Stream (java.util.stream.Stream)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 DefaultValue (javax.ws.rs.DefaultValue)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 PathParam (javax.ws.rs.PathParam)1 Produces (javax.ws.rs.Produces)1