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());
}
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);
});
}
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());
}
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());
}
Aggregations