From 27dcf6d8bf7b411c0a8473105942182ff34edb9c Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 10 Aug 2018 16:27:26 -0600 Subject: [PATCH] Added error handling for a variety of errors. --- .../markdown/MarkdownFromURLMacro.java | 95 ++++++++++++------- .../markdown-from-url.properties | 2 +- 2 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownFromURLMacro.java b/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownFromURLMacro.java index b4987b4..0b6a390 100644 --- a/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownFromURLMacro.java +++ b/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownFromURLMacro.java @@ -24,6 +24,7 @@ import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; import com.atlassian.webresource.api.assembler.PageBuilderService; import org.springframework.beans.factory.annotation.Autowired; + import com.vladsch.flexmark.ast.Node; import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension; import com.vladsch.flexmark.ext.tables.TablesExtension; @@ -40,6 +41,7 @@ import com.vladsch.flexmark.html.HtmlRenderer; import com.vladsch.flexmark.parser.Parser; import com.vladsch.flexmark.util.options.MutableDataSet; + import java.net.*; import java.io.*; @@ -78,37 +80,44 @@ public class MarkdownFromURLMacro extends BaseMacro implements Macro public String execute(Map parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException { - - pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs"); - - MutableDataSet options = new MutableDataSet(); - - options.set(Parser.EXTENSIONS, Arrays.asList( - TablesExtension.create(), - StrikethroughSubscriptExtension.create(), - InsExtension.create(), - TaskListExtension.create(), - FootnoteExtension.create(), - WikiLinkExtension.create(), - DefinitionExtension.create(), - AnchorLinkExtension.create(), - AutolinkExtension.create(), - SuperscriptExtension.create(), - YouTubeLinkExtension.create() - - )); - - - String highlightjs = ""; - if (bodyContent != null) { + pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs"); + + MutableDataSet options = new MutableDataSet(); + + options.set(Parser.EXTENSIONS, Arrays.asList( + TablesExtension.create(), + StrikethroughSubscriptExtension.create(), + InsExtension.create(), + TaskListExtension.create(), + FootnoteExtension.create(), + WikiLinkExtension.create(), + DefinitionExtension.create(), + AnchorLinkExtension.create(), + AutolinkExtension.create(), + SuperscriptExtension.create(), + YouTubeLinkExtension.create() + + )); + + + String highlightjs = ""; + + class privateRepositoryException extends Exception { + public privateRepositoryException(String message) { + super(message); + } + } + Parser parser = Parser.builder(options).build(); HtmlRenderer renderer = HtmlRenderer.builder(options).build(); + String exceptionsToReturn = ""; + String html = ""; String toParse = ""; try { URL importFrom = new URL(bodyContent); @@ -116,21 +125,39 @@ public class MarkdownFromURLMacro extends BaseMacro implements Macro new InputStreamReader(importFrom.openStream()) ); String inputLine; - toParse = ""; while ((inputLine = in.readLine()) != null) { toParse = toParse + "\n" + inputLine; } in.close(); + toParse = toParse.trim(); + if (toParse.startsWith("\n\n OpenID transaction in progress")) { + throw new privateRepositoryException("Cannot import from private repository."); + }else { + Node document = parser.parse(toParse); + html = renderer.render(document) + highlightjs; + } + } + catch (MalformedURLException u) { + exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Invalid URL.
Please enter a valid URL. If you are not trying to import markdown from a URL, use the Markdown macro instead of the Markdown from URL macro.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; + } + catch (privateRepositoryException p) { + exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Importing from private Bitbucket repositories is not supported.
Please make your repository public before importing. Alternatively, you can copy and paste your markdown into the Markdown macro.
If you are allowed access, you can find the markdown file here.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; + } + catch (FileNotFoundException f) { + exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: URL does not exist.
" + bodyContent + "
Please double check your URL. Perhaps you made a typo or perhaps the page has been moved.
This can also be caused by changing the Github repository containing the file from public to private. If this is the case go back to the raw file and re-copy the link.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; + } + catch (IOException e) { + exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Unexpected error.
" + e.toString() + "
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; + } + finally { + if (exceptionsToReturn != "") { + html = "

" + exceptionsToReturn + "

"; + } + return html; } - catch (IOException e) {} - - Node document = parser.parse(toParse); - String html = renderer.render(document) + highlightjs; - return html; }else { return ""; } - } @Override diff --git a/src/main/resources/markdown-from-url-properties/markdown-from-url.properties b/src/main/resources/markdown-from-url-properties/markdown-from-url.properties index 9fe6aa3..503e17c 100644 --- a/src/main/resources/markdown-from-url-properties/markdown-from-url.properties +++ b/src/main/resources/markdown-from-url-properties/markdown-from-url.properties @@ -1,2 +1,2 @@ com.atlassian.plugins.confluence.markdown.confluence-markdown-macro.markdown-from-url.label=Markdown From URL -com.atlassian.plugins.confluence.markdown.confluence-markdown-macro.markdown-from-url.desc=This macro imports Markdown from a URL and renders it into HTML. \ No newline at end of file +com.atlassian.plugins.confluence.markdown.confluence-markdown-macro.markdown-from-url.desc=This macro dynamically imports Markdown from a URL and renders it into HTML. \ No newline at end of file