Title / Description
Code package ru.omickron.rest; import com.google.common.collect.ImmutableMap; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Context; import lombok.AllArgsConstructor; import lombok.NonNull; import lombok.SneakyThrows; import org.apache.commons.io.IOUtils; import org.asciidoctor.Asciidoctor; import org.asciidoctor.OptionsBuilder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import ru.omickron.beans.CurrentUser; import ru.omickron.exception.BadRequestException; import ru.omickron.service.FeedbackService; import ru.omickron.system.permissions.Anonymous; import ru.omickron.util.Const; import static java.util.Objects.isNull; @RestController @RequestMapping(Const.REST + "/docs") @Anonymous @AllArgsConstructor public class DocsFacade { @NonNull private final Asciidoctor asciidoctor; @NonNull private final CurrentUser currentUser; @NonNull private final FeedbackService feedbackService; @PostMapping("/request") public void requestDocs( @RequestParam("id") String id ) { String message = String.format( "Привет, разработчики!\n\nБыло бы здорово увидеть документацию по разделу %s.\n\nСпасибо!", id ); feedbackService.createForUser( currentUser.getUser(), message ); } @GetMapping(value = "/content/**") @SneakyThrows public Map<String, String> getDocs( @Context HttpServletRequest request ) { String name = request.getRequestURI().replaceFirst( Const.REST + "/docs/content", "/docs" ); InputStream inputStream = getClass().getResourceAsStream( name + ".adoc" ); if (isNull( inputStream )) { throw new BadRequestException( "No document found for '%s'", name ); } String adocContent = IOUtils.toString( inputStream, StandardCharsets.UTF_8 ); return ImmutableMap.of( "html", asciidoctor.convert( adocContent, OptionsBuilder.options() ) ); } }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code