Skip to content

Commit

Permalink
Adds a way to load themes with a timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Jul 22, 2023
1 parent e2bf73b commit e2a6fec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
33 changes: 31 additions & 2 deletions structurizr-client/src/com/structurizr/view/ThemeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e2a6fec

Please sign in to comment.