Skip to content

Commit 8ad78b1

Browse files
structurizr-client: Adds a getWorkspaceAsJson() to WorkspaceApiClient.
1 parent 04def03 commit 8ad78b1

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v5.0.2 (unreleased)
4+
5+
- structurizr-client: Adds a `getWorkspaceAsJson()` to `WorkspaceApiClient`.
6+
37
## v5.0.1 (1st November 2025)
48

59
-structurizr-core: Fixes https://github.com/structurizr/java/issues/449 (allow text/plain content types when loading themes).

structurizr-client/src/main/java/com/structurizr/api/WorkspaceApiClient.java

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,44 @@ private boolean manageLockForWorkspace(long workspaceId, boolean lock) throws St
227227
* @throws StructurizrClientException if there are problems related to the network, authorization, JSON deserialization, etc
228228
*/
229229
public Workspace getWorkspace(long workspaceId) throws StructurizrClientException {
230+
String json = getWorkspaceAsJson(workspaceId);
231+
232+
try {
233+
if (encryptionStrategy == null) {
234+
if (json.contains("\"encryptionStrategy\"") && json.contains("\"ciphertext\"")) {
235+
log.warn("The JSON may contain a client-side encrypted workspace, but no passphrase has been specified.");
236+
}
237+
238+
JsonReader jsonReader = new JsonReader();
239+
jsonReader.setIdGenerator(idGenerator);
240+
return jsonReader.read(new StringReader(json));
241+
} else {
242+
EncryptedWorkspace encryptedWorkspace = new EncryptedJsonReader().read(new StringReader(json));
243+
244+
if (encryptedWorkspace.getEncryptionStrategy() != null) {
245+
encryptedWorkspace.getEncryptionStrategy().setPassphrase(encryptionStrategy.getPassphrase());
246+
return encryptedWorkspace.getWorkspace();
247+
} else {
248+
// this workspace isn't encrypted, even though the client has an encryption strategy set
249+
JsonReader jsonReader = new JsonReader();
250+
jsonReader.setIdGenerator(idGenerator);
251+
return jsonReader.read(new StringReader(json));
252+
}
253+
}
254+
} catch (Exception e) {
255+
log.error(e);
256+
throw new StructurizrClientException(e);
257+
}
258+
}
259+
260+
/**
261+
* Gets the workspace with the given ID, as a JSON string.
262+
*
263+
* @param workspaceId the workspace ID
264+
* @return a JSON string
265+
* @throws StructurizrClientException if there are problems related to the network, authorization, JSON deserialization, etc
266+
*/
267+
public String getWorkspaceAsJson(long workspaceId) throws StructurizrClientException {
230268
if (workspaceId <= 0) {
231269
throw new IllegalArgumentException("The workspace ID must be a positive integer.");
232270
}
@@ -251,27 +289,7 @@ public Workspace getWorkspace(long workspaceId) throws StructurizrClientExceptio
251289
if (response.getCode() == HttpStatus.SC_OK) {
252290
archiveWorkspace(workspaceId, json);
253291

254-
if (encryptionStrategy == null) {
255-
if (json.contains("\"encryptionStrategy\"") && json.contains("\"ciphertext\"")) {
256-
log.warn("The JSON may contain a client-side encrypted workspace, but no passphrase has been specified.");
257-
}
258-
259-
JsonReader jsonReader = new JsonReader();
260-
jsonReader.setIdGenerator(idGenerator);
261-
return jsonReader.read(new StringReader(json));
262-
} else {
263-
EncryptedWorkspace encryptedWorkspace = new EncryptedJsonReader().read(new StringReader(json));
264-
265-
if (encryptedWorkspace.getEncryptionStrategy() != null) {
266-
encryptedWorkspace.getEncryptionStrategy().setPassphrase(encryptionStrategy.getPassphrase());
267-
return encryptedWorkspace.getWorkspace();
268-
} else {
269-
// this workspace isn't encrypted, even though the client has an encryption strategy set
270-
JsonReader jsonReader = new JsonReader();
271-
jsonReader.setIdGenerator(idGenerator);
272-
return jsonReader.read(new StringReader(json));
273-
}
274-
}
292+
return json;
275293
} else {
276294
ApiResponse apiResponse = ApiResponse.parse(json);
277295
throw new StructurizrClientException(apiResponse.getMessage());

0 commit comments

Comments
 (0)