All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kicksolutions.confluence.SwaggerConfluenceUploader Maven / Gradle / Ivy

package com.kicksolutions.confluence;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;

import com.kicksolutions.confluence.vo.ConfluenceVo;

import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;

/**
 * MSANTOSH
 *
 */
public class SwaggerConfluenceUploader {
	private static final Logger LOGGER = Logger.getLogger(SwaggerConfluenceUploader.class.getName());

	public SwaggerConfluenceUploader() {
		super();
	}

	/**
	 * Title: | |----- V1.0 Title | |----- V1.1 Title
	 * 
	 * @param specFile
	 * @param parentPageID
	 * @param userName
	 * @param password
	 * @param confluenceURL
	 * @param spaceKey
	 * @param alternateURL
	 * @param clientkitURL
	 * @param htmlDocumentationURL
	 * @param prefixForConfluencePage
	 * @param isHierarchy
	 * @return
	 */
	public String processSwagger2Confluence(String specFile, String parentPageID, String userName, String password,
			String confluenceURL, String spaceKey, String alternateURL, String clientkitURL,
			String htmlDocumentationURL, String prefixForConfluencePage, boolean isHierarchy) {
		Swagger swaggerObject = new SwaggerParser().read(specFile);

		if (swaggerObject != null) {

			String version = swaggerObject.getInfo().getVersion();
			String title = swaggerObject.getInfo().getTitle();

			if (StringUtils.isNotEmpty(version) && StringUtils.isNotEmpty(title)) {
				String parentTitle = StringUtils.isEmpty(prefixForConfluencePage)
						? new StringBuilder().append(title).toString()
						: new StringBuilder(prefixForConfluencePage).append(" - ").append(title).toString();
				String versionTitle = new StringBuilder().append("V").append(version).append(" - ").append(parentTitle)
						.toString();
				
				if (isHierarchy) {
					
					return generatePagesinHierarchyMode(specFile, parentPageID, userName, password, confluenceURL,
							spaceKey, alternateURL, clientkitURL, htmlDocumentationURL, parentTitle, versionTitle);
				}else{
					return generatePagesInNonHierarchyMode(specFile, parentPageID, userName, password, confluenceURL,
							spaceKey, alternateURL, clientkitURL, htmlDocumentationURL, parentTitle, versionTitle);
				}
			} else {
				throw new RuntimeException("Swagger Definition is missing version and title information");
			}
		} else {
			throw new RuntimeException("Cannot Process Swagger Definition for the given URL");
		}
	}

	/**
	 * 
	 * @param specFile
	 * @param parentPageID
	 * @param userName
	 * @param password
	 * @param confluenceURL
	 * @param spaceKey
	 * @param alternateURL
	 * @param clientkitURL
	 * @param htmlDocumentationURL
	 * @param parentTitle
	 * @param versionTitle
	 * @return
	 */
	private String generatePagesInNonHierarchyMode(String specFile, String parentPageID, String userName,
			String password, String confluenceURL, String spaceKey, String alternateURL, String clientkitURL,
			String htmlDocumentationURL, String parentTitle, String versionTitle) {
		LOGGER.log(Level.INFO, "Hierarchy Mode is Set to False!!!");
		
		LOGGER.log(Level.INFO, "About to generate Page --> " + parentTitle);
		
		String swaggerPageContent = StringUtils.isNotEmpty(alternateURL)
				? swaggerMacroContent(alternateURL, clientkitURL, htmlDocumentationURL)
				: swaggerMacroContent(specFile, clientkitURL, htmlDocumentationURL);
		
		ConfluenceVo childPageVo = createSwaggerPage(
				new ConfluenceVo(userName, password, confluenceURL, "", parentPageID, specFile,
						versionTitle, "0", swaggerPageContent, spaceKey, false));
		
		LOGGER.log(Level.INFO, "Done.... by generating Page " + childPageVo.getPageID());

		return childPageVo.getPageID();
	}

	/**
	 * 
	 * @param specFile
	 * @param parentPageID
	 * @param userName
	 * @param password
	 * @param confluenceURL
	 * @param spaceKey
	 * @param alternateURL
	 * @param clientkitURL
	 * @param htmlDocumentationURL
	 * @param parentTitle
	 * @param versionTitle
	 * @return
	 */
	private String generatePagesinHierarchyMode(String specFile, String parentPageID, String userName, String password,
			String confluenceURL, String spaceKey, String alternateURL, String clientkitURL,
			String htmlDocumentationURL, String parentTitle, String versionTitle) {
		LOGGER.log(Level.INFO, "Hierarchy Mode is Set to True!!!");
		
		String swaggerPageContent = StringUtils.isNotEmpty(alternateURL)
				? swaggerMacroContent(alternateURL, clientkitURL, htmlDocumentationURL)
				: swaggerMacroContent(specFile, clientkitURL, htmlDocumentationURL);

		// Create a Page whose name is same as Swagger Title Ex: Pet
		// Store
		LOGGER.log(Level.INFO, "About to generate Page -->" + parentTitle);

		ConfluenceVo parentPageVo = createSwaggerPage(new ConfluenceVo(userName, password, confluenceURL,
				"", parentPageID, "", parentTitle, "0", parentPageContent(versionTitle), spaceKey, false));

		LOGGER.log(Level.INFO, "About to generate Page --> " + versionTitle);

		// Create Child Page Under Parent ex: V1.0.0 Pet Store
		ConfluenceVo childPageVo = createSwaggerPage(
				new ConfluenceVo(userName, password, confluenceURL, "", parentPageVo.getPageID(), specFile,
						versionTitle, "0", swaggerPageContent, spaceKey, false));

		LOGGER.log(Level.INFO, "Done.... by generating Pages " + parentPageVo.getPageID() + " and "
				+ childPageVo.getPageID());

		return childPageVo.getPageID();
	}

	/**
	 * 
	 * @param versionTitle
	 * @return
	 */
	private String parentPageContent(String versionTitle) {
		return new StringBuilder().append("
Latest Version").append(versionTitle) .append("
").toString(); } /** * * @param swaggerLoctaion * @param htmlDocumentationURL * @param clientkitURL * @return */ private String swaggerMacroContent(String swaggerLoctaion, String clientkitURL, String htmlDocumentationURL) { StringBuilder macroString = new StringBuilder(); macroString .append("") .append("pub_api_operation") .append("INLINE") .append("") .append("").append("") .append("").append("") .append("").append("").append("") .append(""); if (StringUtils.isNotEmpty(clientkitURL)) { macroString.append("").append("").append("").append(""); } if (StringUtils.isNotEmpty(htmlDocumentationURL)) { macroString.append("").append("").append("").append(""); } if (StringUtils.isNotEmpty(swaggerLoctaion)) { macroString.append("").append("") .append("").append(""); } macroString.append("
DescriptionLinks
").append("Java Client") .append("").append("").append("here").append("").append("
").append("Html Documentation") .append("").append("").append("here").append("").append("
").append("Swagger").append("").append("") .append("here").append("").append("
") .append("") .append("None") .append("").append(swaggerLoctaion) .append("
"); return macroString.toString(); } /** * * @param title * @return */ private ConfluenceVo createSwaggerPage(ConfluenceVo confluenceVo) { ConfluenceUtils confluenceUtils = new ConfluenceUtils(); if (!confluenceUtils.isPageExists(confluenceVo)) { LOGGER.log(Level.INFO, "Page Doesn't Exists, so Creating Page"); String pageID = confluenceUtils.createPage(confluenceVo); confluenceVo.setPageID(pageID); } else { LOGGER.log(Level.INFO, "Page Exists, so Updating Page"); String pageID = confluenceUtils.updatePage(confluenceVo); confluenceVo.setPageID(pageID); } return confluenceVo; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy