001// Generated by delombok at Thu May 02 07:34:48 CEST 2019 002package com.credibledoc.substitution.doc.module.substitution.pom; 003 004import com.credibledoc.substitution.core.content.Content; 005import com.credibledoc.substitution.core.content.ContentGenerator; 006import com.credibledoc.substitution.core.exception.SubstitutionRuntimeException; 007import com.credibledoc.substitution.core.placeholder.Placeholder; 008import org.springframework.stereotype.Service; 009import javax.inject.Inject; 010import java.io.InputStream; 011import java.net.HttpURLConnection; 012import java.net.URL; 013 014/** 015 * Generates the published jar name. Tries to find out the LATEST version in a Nexus repository, for example from 016 * the 017 * <a href="https://repo1.maven.org/maven2/com/credibledoc/plantuml-core/maven-metadata.xml">plantuml-core</a> 018 * API url. If the LATEST version not found, the {@link #NOT_PUBLISHED_YET} string will be returned. 019 * <p> 020 * Link to the repository API is defined in the <i>url</i> {@link Placeholder#getParameters()} property. 021 * <p> 022 * Example 023 * <pre>{@code 024 * &&beginPlaceholder { 025 * "className": "com.credibledoc.substitution.doc.module.substitution.pom.JarNameContentGenerator", 026 * "description": "Latest plantuml-core.jar name", 027 * "parameters": {"url": "https://repo1.maven.org/maven2/com/credibledoc/plantuml-core/maven-metadata.xml"} 028 * } &&endPlaceholder 029 * }</pre> 030 * 031 * @author Kyrylo Semenko 032 */ 033@Service 034public class JarNameContentGenerator implements ContentGenerator { 035 @java.lang.SuppressWarnings("all") 036 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(JarNameContentGenerator.class); 037 private static final String NOT_PUBLISHED_YET = "\'The artifact not published yet.\'"; 038 private static final String TEXT_XML = "text/xml"; 039 private static final String ARTIFACT_ID_BEGIN_TAG = "<artifactId>"; 040 private static final String ARTIFACT_ID_END_TAG = "</artifactId>"; 041 private static final String LATEST_BEGIN_TAG = "<latest>"; 042 private static final String LATEST_END_TAG = "</latest>"; 043 044 @Override 045 public Content generate(Placeholder placeholder) { 046 String url = placeholder.getParameters().get("url"); 047 if (url == null) { 048 throw new SubstitutionRuntimeException("Placeholder parameter \'url\' is required, but found \'null\'."); 049 } 050 return loadJarName(url); 051 } 052 053 private Content loadJarName(String urlParameter) { 054 String result; 055 try { 056 URL url = new URL(urlParameter); 057 HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 058 int responseCode = httpConn.getResponseCode(); 059 if (responseCode == HttpURLConnection.HTTP_OK) { 060 String contentType = httpConn.getContentType(); 061 if (!TEXT_XML.equals(contentType)) { 062 throw new SubstitutionRuntimeException("Expected \'" + TEXT_XML + "\', but found " + contentType); 063 } 064 InputStream inputStream = httpConn.getInputStream(); 065 String xmlString = convertStreamToString(inputStream); 066 String artifactId = parseTag(xmlString, ARTIFACT_ID_BEGIN_TAG, ARTIFACT_ID_END_TAG); 067 String latestVersion = parseTag(xmlString, LATEST_BEGIN_TAG, LATEST_END_TAG); 068 result = artifactId + "-" + latestVersion + ".jar"; 069 } else { 070 log.info("ResponseCode is " + responseCode); 071 result = NOT_PUBLISHED_YET; 072 } 073 httpConn.disconnect(); 074 } catch (Exception e) { 075 throw new SubstitutionRuntimeException(e); 076 } 077 Content content = new Content(); 078 content.setMarkdownContent(result); 079 return content; 080 } 081 082 private String parseTag(String xmlString, String beginTag, String endTag) { 083 int beginIndex = xmlString.indexOf(beginTag); 084 if (beginIndex == -1) { 085 throw new SubstitutionRuntimeException("Cannot find " + beginTag); 086 } 087 int endIndex = xmlString.indexOf(endTag, beginIndex); 088 if (endIndex == -1) { 089 throw new SubstitutionRuntimeException("Cannot find " + endTag); 090 } 091 return xmlString.substring(beginIndex + beginTag.length(), endIndex); 092 } 093 094 private String convertStreamToString(InputStream is) { 095 java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); 096 return s.hasNext() ? s.next() : ""; 097 } 098 099 @Inject 100 @java.lang.SuppressWarnings("all") 101 public JarNameContentGenerator() { 102 } 103}