Commit ae524bf0 authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza

Feature #28109 Genesys documentation pages (WAR)

parent 6b0dec29
......@@ -685,6 +685,7 @@
</executions>
<configuration>
<sourceDirectory>src/main/asciidoc</sourceDirectory>
<outputDirectory>${project.build.outputDirectory}/docs</outputDirectory>
<preserveDirectories>true</preserveDirectories>
<headerFooter>true</headerFooter>
<numbered>true</numbered>
......
package org.genesys2.server.servlet.controller;
import org.apache.commons.io.IOUtils;
import org.genesys2.spring.ResourceNotFoundException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
......@@ -10,26 +11,30 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@Controller
@RequestMapping("/doc")
public class AsciiDocController extends BaseController {
private static final String GENERATED_DOCS_DIR = "/generated-docs/";
private static final String GENERATED_DOCS_SECTIONS_DIR = "sections/";
private static final String DOCS_DIR = "/docs/";
private static final String DOCS_SECTIONS_DIR = "sections/";
private static final String DOCS_IMAGES_DIR = "images/";
private static final String HTML_EXTENSION = ".html";
private static final String PNG_EXTENSION = ".png";
@RequestMapping("/0/{documentName}")
public String viewDoc(ModelMap model, @PathVariable(value = "documentName") String documentName) throws IOException {
String docPath = getDocPathByName(documentName, HTML_EXTENSION);
ClassLoader classLoader = this.getClass().getClassLoader();
String docPath = new StringBuilder(DOCS_DIR).append(documentName)
.append(HTML_EXTENSION).toString();
InputStream resourceStream = classLoader.getResourceAsStream(docPath);
if (resourceStream == null) {
throw new ResourceNotFoundException();
}
InputStream resourceStream = new FileInputStream(docPath);
String html = IOUtils.toString(resourceStream);
Document document = Jsoup.parse(html);
......@@ -61,25 +66,22 @@ public class AsciiDocController extends BaseController {
@RequestMapping("/0/sections/{documentName}")
public String viewSection(ModelMap model, @PathVariable(value = "documentName") String documentName) throws IOException {
return viewDoc(model, GENERATED_DOCS_SECTIONS_DIR + documentName);
String docPath = new StringBuilder(DOCS_SECTIONS_DIR).append(documentName).toString();
return viewDoc(model, docPath);
}
@RequestMapping("/0/images/{imageName}")
@ResponseBody
public byte[] getImage(@PathVariable(value = "imageName") String imageName) throws IOException {
String imagePath = getDocPathByName("images/" + imageName, PNG_EXTENSION);
InputStream inputStream = new FileInputStream(imagePath);
ClassLoader classLoader = this.getClass().getClassLoader();
String imgPath = new StringBuilder(DOCS_DIR).append(DOCS_IMAGES_DIR)
.append(imageName).append(PNG_EXTENSION).toString();
return IOUtils.toByteArray(inputStream);
InputStream inputStream = classLoader.getResourceAsStream(imgPath);
if (inputStream == null) {
throw new ResourceNotFoundException();
}
private String getDocPathByName(String documentName, String extension) {
URL classesFolderURL = this.getClass().getProtectionDomain()
.getCodeSource().getLocation();
File classesFolderFile = new File(classesFolderURL.getPath());
String targetFolderName = classesFolderFile.getParent();
return targetFolderName + GENERATED_DOCS_DIR + documentName + extension;
return IOUtils.toByteArray(inputStream);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment