From e2a6fecb1a51f597d3185ffa85c4afbe8c8211dc Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Sat, 22 Jul 2023 12:01:44 +0100 Subject: [PATCH] Adds a way to load themes with a timeout. --- docs/changelog.md | 3 +- .../src/com/structurizr/view/ThemeUtils.java | 33 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 34417546..a8f824cc 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,8 +1,9 @@ # Changelog -## 1.25.0 (unreleased to Maven Central) +## 1.25.0 (22nd July 2023) - Fixes https://github.com/structurizr/java/issues/213 (Views are not created automatically if non-English characters are used in software systems' names) +- Adds a way to load themes with a timeout. ## 1.24.1 (5th April 2023) diff --git a/structurizr-client/src/com/structurizr/view/ThemeUtils.java b/structurizr-client/src/com/structurizr/view/ThemeUtils.java index 22c177e6..41878740 100644 --- a/structurizr-client/src/com/structurizr/view/ThemeUtils.java +++ b/structurizr-client/src/com/structurizr/view/ThemeUtils.java @@ -8,13 +8,16 @@ import com.structurizr.io.WorkspaceWriterException; import com.structurizr.util.StringUtils; import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.ConnectionConfig; 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.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; import org.apache.hc.core5.http.io.entity.EntityUtils; import java.io.*; import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; /** * Some utility methods for exporting themes to JSON. @@ -23,6 +26,8 @@ public final class ThemeUtils { private static final int HTTP_OK_STATUS = 200; + private static final int DEFAULT_TIMEOUT_IN_MILLISECONDS = 10000; + /** * Serializes the theme (element and relationship styles) in the specified workspace to a file, as a JSON string. * @@ -62,13 +67,37 @@ public static String toJson(Workspace workspace) throws Exception { /** * Loads (and inlines) the element and relationship styles from the themes defined in the workspace, into the workspace itself. * This implementation simply copies the styles from all themes into the workspace. + * This uses a default timeout value of 10000ms. * * @param workspace a Workspace object * @throws Exception if something goes wrong */ public static void loadThemes(Workspace workspace) throws Exception { + loadThemes(workspace, DEFAULT_TIMEOUT_IN_MILLISECONDS); + } + + /** + * Loads (and inlines) the element and relationship styles from the themes defined in the workspace, into the workspace itself. + * This implementation simply copies the styles from all themes into the workspace. + * + * @param workspace a Workspace object + * @param timeoutInMilliseconds the timeout in milliseconds + * @throws Exception if something goes wrong + */ + public static void loadThemes(Workspace workspace, int timeoutInMilliseconds) throws Exception { for (String url : workspace.getViews().getConfiguration().getThemes()) { - CloseableHttpClient httpClient = HttpClients.createSystem(); + ConnectionConfig connectionConfig = ConnectionConfig.custom() + .setConnectTimeout(timeoutInMilliseconds, TimeUnit.MILLISECONDS) + .setSocketTimeout(timeoutInMilliseconds, TimeUnit.MILLISECONDS) + .build(); + + BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager(); + cm.setConnectionConfig(connectionConfig); + + CloseableHttpClient httpClient = HttpClientBuilder.create() + .setConnectionManager(cm) + .build(); + HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response = httpClient.execute(httpGet);