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 @@ ...@@ -685,6 +685,7 @@
</executions> </executions>
<configuration> <configuration>
<sourceDirectory>src/main/asciidoc</sourceDirectory> <sourceDirectory>src/main/asciidoc</sourceDirectory>
<outputDirectory>${project.build.outputDirectory}/docs</outputDirectory>
<preserveDirectories>true</preserveDirectories> <preserveDirectories>true</preserveDirectories>
<headerFooter>true</headerFooter> <headerFooter>true</headerFooter>
<numbered>true</numbered> <numbered>true</numbered>
......
package org.genesys2.server.servlet.controller; package org.genesys2.server.servlet.controller;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.genesys2.spring.ResourceNotFoundException;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
...@@ -10,26 +11,30 @@ import org.springframework.web.bind.annotation.PathVariable; ...@@ -10,26 +11,30 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
@Controller @Controller
@RequestMapping("/doc") @RequestMapping("/doc")
public class AsciiDocController extends BaseController { public class AsciiDocController extends BaseController {
private static final String GENERATED_DOCS_DIR = "/generated-docs/"; private static final String DOCS_DIR = "/docs/";
private static final String GENERATED_DOCS_SECTIONS_DIR = "sections/"; 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 HTML_EXTENSION = ".html";
private static final String PNG_EXTENSION = ".png"; private static final String PNG_EXTENSION = ".png";
@RequestMapping("/0/{documentName}") @RequestMapping("/0/{documentName}")
public String viewDoc(ModelMap model, @PathVariable(value = "documentName") String documentName) throws IOException { 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); String html = IOUtils.toString(resourceStream);
Document document = Jsoup.parse(html); Document document = Jsoup.parse(html);
...@@ -61,25 +66,22 @@ public class AsciiDocController extends BaseController { ...@@ -61,25 +66,22 @@ public class AsciiDocController extends BaseController {
@RequestMapping("/0/sections/{documentName}") @RequestMapping("/0/sections/{documentName}")
public String viewSection(ModelMap model, @PathVariable(value = "documentName") String documentName) throws IOException { 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}") @RequestMapping("/0/images/{imageName}")
@ResponseBody @ResponseBody
public byte[] getImage(@PathVariable(value = "imageName") String imageName) throws IOException { public byte[] getImage(@PathVariable(value = "imageName") String imageName) throws IOException {
String imagePath = getDocPathByName("images/" + imageName, PNG_EXTENSION); ClassLoader classLoader = this.getClass().getClassLoader();
InputStream inputStream = new FileInputStream(imagePath); String imgPath = new StringBuilder(DOCS_DIR).append(DOCS_IMAGES_DIR)
.append(imageName).append(PNG_EXTENSION).toString();
return IOUtils.toByteArray(inputStream);
}
private String getDocPathByName(String documentName, String extension) { InputStream inputStream = classLoader.getResourceAsStream(imgPath);
URL classesFolderURL = this.getClass().getProtectionDomain() if (inputStream == null) {
.getCodeSource().getLocation(); throw new ResourceNotFoundException();
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