Added error handling for a variety of errors.

This commit is contained in:
unknown
2018-08-10 16:27:26 -06:00
parent 804c460981
commit 27dcf6d8bf
2 changed files with 62 additions and 35 deletions

View File

@@ -24,6 +24,7 @@ import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.webresource.api.assembler.PageBuilderService; import com.atlassian.webresource.api.assembler.PageBuilderService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.vladsch.flexmark.ast.Node; import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension; import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension; 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.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet; import com.vladsch.flexmark.util.options.MutableDataSet;
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
@@ -78,37 +80,44 @@ public class MarkdownFromURLMacro extends BaseMacro implements Macro
public String execute(Map<String, String> parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException public String execute(Map<String, String> 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 = "<script>\n" +
"AJS.$('[data-macro-name=\"markdown\"] code').each(function(i, block) {\n" +
" hljs.highlightBlock(block);\n" +
" });\n" +
"</script>";
if (bodyContent != null) { 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 = "<script>\n" +
"AJS.$('[data-macro-name=\"markdown\"] code').each(function(i, block) {\n" +
" hljs.highlightBlock(block);\n" +
" });\n" +
"</script>";
class privateRepositoryException extends Exception {
public privateRepositoryException(String message) {
super(message);
}
}
Parser parser = Parser.builder(options).build(); Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build(); HtmlRenderer renderer = HtmlRenderer.builder(options).build();
String exceptionsToReturn = "";
String html = "";
String toParse = ""; String toParse = "";
try { try {
URL importFrom = new URL(bodyContent); URL importFrom = new URL(bodyContent);
@@ -116,21 +125,39 @@ public class MarkdownFromURLMacro extends BaseMacro implements Macro
new InputStreamReader(importFrom.openStream()) new InputStreamReader(importFrom.openStream())
); );
String inputLine; String inputLine;
toParse = "";
while ((inputLine = in.readLine()) != null) { while ((inputLine = in.readLine()) != null) {
toParse = toParse + "\n" + inputLine; toParse = toParse + "\n" + inputLine;
} }
in.close(); in.close();
toParse = toParse.trim();
if (toParse.startsWith("<html>\n<head>\n <title>OpenID transaction in progress</title>")) {
throw new privateRepositoryException("Cannot import from private repository.");
}else {
Node document = parser.parse(toParse);
html = renderer.render(document) + highlightjs;
}
}
catch (MalformedURLException u) {
exceptionsToReturn = exceptionsToReturn + "<strong>Error with Markdown From URL macro: Invalid URL.</strong><br>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.<br>For support <a href='https://community.atlassian.com/t5/tag/addon-com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/tg-p'>visit our Q&A in the Atlassian Community</a>. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.<br>";
}
catch (privateRepositoryException p) {
exceptionsToReturn = exceptionsToReturn + "<strong>Error with Markdown From URL macro: Importing from private Bitbucket repositories is not supported.</strong><br>Please make your repository public before importing. Alternatively, you can copy and paste your markdown into the Markdown macro.<br>If you are allowed access, you can find the markdown file <a href='" + bodyContent + "'>here</a>.<br>For support <a href='https://community.atlassian.com/t5/tag/addon-com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/tg-p'>visit our Q&A in the Atlassian Community</a>. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.<br>";
}
catch (FileNotFoundException f) {
exceptionsToReturn = exceptionsToReturn + "<strong>Error with Markdown From URL macro: URL does not exist.</strong><br>" + bodyContent + "<br>Please double check your URL. Perhaps you made a typo or perhaps the page has been moved.<br>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.<br>For support <a href='https://community.atlassian.com/t5/tag/addon-com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/tg-p'>visit our Q&A in the Atlassian Community</a>. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.<br>";
}
catch (IOException e) {
exceptionsToReturn = exceptionsToReturn + "<strong>Error with Markdown From URL macro: Unexpected error.</strong><br>" + e.toString() + "<br>For support <a href='https://community.atlassian.com/t5/tag/addon-com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/tg-p'>visit our Q&A in the Atlassian Community</a>. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.<br>";
}
finally {
if (exceptionsToReturn != "") {
html = "<p style='background: #ffe0e0; border-radius: 5px; padding: 10px;'>" + exceptionsToReturn + "</p>";
}
return html;
} }
catch (IOException e) {}
Node document = parser.parse(toParse);
String html = renderer.render(document) + highlightjs;
return html;
}else { }else {
return ""; return "";
} }
} }
@Override @Override

View File

@@ -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.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. 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.