Skip to content

Commit

Permalink
Improves the handling of remote content (e.g. extending remote worksp…
Browse files Browse the repository at this point in the history
…aces, including remote DSL content, etc).
  • Loading branch information
simonbrowndotje committed Feb 20, 2024
1 parent 45ddf5a commit 71995f7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.util.regex.Pattern;
Expand All @@ -24,19 +26,20 @@ String removeNonWordCharacters(String name) {
return name.replaceAll("\\W", "");
}

protected String readFromUrl(String url) {
protected RemoteContent readFromUrl(String url) {
try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);

if (response.getCode() == HTTP_OK_STATUS) {
return EntityUtils.toString(response.getEntity());
int httpStatus = response.getCode();
if (httpStatus == HTTP_OK_STATUS) {
return new RemoteContent(EntityUtils.toString(response.getEntity()), response.getEntity().getContentType());
} else {
throw new RuntimeException("The content from " + url + " could not be loaded: HTTP status=" + httpStatus);
}
} catch (Exception ioe) {
ioe.printStackTrace();
throw new RuntimeException("The content from " + url + " could not be loaded: " + ioe.getMessage());
}

return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void parsePlantUML(ImageViewDslContext context, File dslFile, Tokens tokens) {

try {
if (Url.isUrl(source)) {
String content = readFromUrl(source);
new PlantUMLImporter().importDiagram(context.getView(), content);
RemoteContent content = readFromUrl(source);
new PlantUMLImporter().importDiagram(context.getView(), content.getContent());
context.getView().setTitle(source.substring(source.lastIndexOf("/")+1));
} else {
if (!restricted) {
Expand Down Expand Up @@ -77,8 +77,8 @@ void parseMermaid(ImageViewDslContext context, File dslFile, Tokens tokens) {

try {
if (Url.isUrl(source)) {
String content = readFromUrl(source);
new MermaidImporter().importDiagram(context.getView(), content);
RemoteContent content = readFromUrl(source);
new MermaidImporter().importDiagram(context.getView(), content.getContent());
context.getView().setTitle(source.substring(source.lastIndexOf("/")+1));
} else {
if (!restricted) {
Expand Down Expand Up @@ -110,8 +110,8 @@ void parseKroki(ImageViewDslContext context, File dslFile, Tokens tokens) {

try {
if (Url.isUrl(source)) {
String content = readFromUrl(source);
new KrokiImporter().importDiagram(context.getView(), format, content);
RemoteContent content = readFromUrl(source);
new KrokiImporter().importDiagram(context.getView(), format, content.getContent());
context.getView().setTitle(source.substring(source.lastIndexOf("/")+1));
} else {
if (!restricted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ void parse(IncludedDslContext context, Tokens tokens) {
}

String source = tokens.get(SOURCE_INDEX);
if (source.startsWith("https://")) {
String dsl = readFromUrl(source);
List<String> lines = Arrays.asList(dsl.split("\n"));
if (source.startsWith("https://") || source.startsWith("http://")) {
RemoteContent content = readFromUrl(source);
List<String> lines = Arrays.asList(content.getContent().split("\n"));
context.addFile(context.getParentFile(), lines);
} else {
if (context.getParentFile() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.structurizr.dsl;

final class RemoteContent {

static final String CONTENT_TYPE_JSON = "application/json";
static final String TEXT_PLAIN_JSON = "text/plain";

private final String content;
private final String contentType;

RemoteContent(String content, String contentType) {
this.content = content;
this.contentType = contentType;
}

String getContent() {
return content;
}

String getContentType() {
return contentType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void parse(List<String> lines, File dslFile) throws StructurizrDslParserExceptio
endContext();

} else if (INCLUDE_FILE_TOKEN.equalsIgnoreCase(firstToken)) {
if (!restricted || tokens.get(1).startsWith("https://")) {
if (!restricted || tokens.get(1).startsWith("https://") || tokens.get(1).startsWith("http://")) {
String leadingSpace = line.substring(0, line.indexOf(INCLUDE_FILE_TOKEN));

IncludedDslContext context = new IncludedDslContext(dslFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ Workspace parse(DslParserContext context, Tokens tokens) {
String source = tokens.get(SECOND_INDEX);

try {
if (source.startsWith("https://")) {
if (source.endsWith(".json")) {
String json = readFromUrl(source);
if (source.startsWith("https://") || source.startsWith("http://")) {
RemoteContent content = readFromUrl(source);

if (source.endsWith(".json") || content.getContentType().startsWith(RemoteContent.CONTENT_TYPE_JSON)) {
String json = content.getContent();
workspace = WorkspaceUtils.fromJson(json);
registerIdentifiers(workspace, context);
} else {
String dsl = readFromUrl(source);
String dsl = content.getContent();
StructurizrDslParser structurizrDslParser = new StructurizrDslParser();
structurizrDslParser.parse(context, dsl);
workspace = structurizrDslParser.getWorkspace();
Expand Down

0 comments on commit 71995f7

Please sign in to comment.