AsciiDocController.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.server.api.v1;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.exception.NotFoundElement;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

/**
 * AsciiDoc API v1.
 */
@RestController("asciiDocApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(AsciiDocController.CONTROLLER_URL)
@Api(tags = { "asciiDoc" })
public class AsciiDocController {

	/** The Constant DOCS_DIR. */
	private static final String DOCS_DIR = "docs/";

	/** The Constant DOCS_SECTIONS_DIR. */
	private static final String DOCS_SECTIONS_DIR = "sections/";

	/** The Constant DOCS_IMAGES_DIR. */
	private static final String DOCS_IMAGES_DIR = "images/";

	/** The Constant HTML_EXTENSION. */
	private static final String HTML_EXTENSION = ".html";

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/cms/d";

	/**
	 * View doc.
	 *
	 * @param documentName the document name
	 * @return the map
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value = "/{documentName}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ADoc viewDoc(@PathVariable(value = "documentName") String documentName) throws IOException {
		ADoc resultDoc = new ADoc();

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

		String html = IOUtils.toString(resourceStream, "UTF8");

		Document document = Jsoup.parse(html);

		resultDoc.title = StringUtils.stripToNull(document.getElementsByTag("title").html());

		Element toc = document.getElementById("toc");
		if (toc != null) {
			toc.getElementById("toctitle").remove();
			toc.removeAttr("id").removeAttr("class");
			resultDoc.toc = StringUtils.stripToNull(toc.html());
			toc.remove();
		}

		Element header = document.getElementById("header");
		if (header != null) {
			Elements headerH1 = header.getElementsByTag("h1");
			if (headerH1 != null) {
				// Use HTML title if available
				resultDoc.title = StringUtils.defaultIfBlank(headerH1.html(), resultDoc.title);
			}
			resultDoc.header = StringUtils.stripToNull(header.getElementsByClass("details").removeAttr("id").html());
		}

		Element content = document.getElementById("content");
		if (content != null) {
			resultDoc.content = StringUtils.stripToNull(content.html());
		}

		Element footer = document.getElementById("footer-text");
		if (footer != null) {
			resultDoc.footer = StringUtils.stripToNull(footer.html());
		}

		return resultDoc;
	}

	/**
	 * View section.
	 *
	 * @param documentName the document name
	 * @return the map
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value = "/sections/{documentName}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ADoc viewSection(@PathVariable(value = "documentName") String documentName) throws IOException {
		String docPath = new StringBuilder(DOCS_SECTIONS_DIR).append(documentName).toString();
		return viewDoc(docPath);
	}

	/**
	 * Gets the image.
	 *
	 * @param imageName the image name
	 * @return the image
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value = "/images/{imageName}", method = RequestMethod.GET, produces = { MediaType.IMAGE_JPEG_VALUE })
	@ResponseBody
	public byte[] getImage(@PathVariable(value = "imageName") String imageName) throws IOException {
		ClassLoader classLoader = this.getClass().getClassLoader();
		String imgPath = new StringBuilder(DOCS_DIR).append(DOCS_IMAGES_DIR).append(imageName).toString();

		InputStream inputStream = classLoader.getResourceAsStream(imgPath);
		if (inputStream == null) {
			throw new NotFoundElement();
		}

		return IOUtils.toByteArray(inputStream);
	}

	public static class ADoc {
		public String title;
		public String toc;

		public String header;
		public String content;
		public String footer;
	}
}