use of com.structurizr.io.json.JsonWriter in project java by structurizr.
the class StructurizrClient method putWorkspace.
/**
* Updates the given workspace.
*
* @param workspaceId the workspace ID
* @param workspace the workspace instance to update
* @throws StructurizrClientException if there are problems related to the network, authorization, JSON serialization, etc
*/
public void putWorkspace(long workspaceId, Workspace workspace) throws StructurizrClientException {
if (workspace == null) {
throw new IllegalArgumentException("The workspace must not be null.");
} else if (workspaceId <= 0) {
throw new IllegalArgumentException("The workspace ID must be a positive integer.");
}
try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
if (mergeFromRemote) {
Workspace remoteWorkspace = getWorkspace(workspaceId);
if (remoteWorkspace != null) {
workspace.getViews().copyLayoutInformationFrom(remoteWorkspace.getViews());
workspace.getViews().getConfiguration().copyConfigurationFrom(remoteWorkspace.getViews().getConfiguration());
}
}
workspace.setId(workspaceId);
workspace.setThumbnail(null);
workspace.setLastModifiedDate(new Date());
workspace.setLastModifiedAgent(agent);
workspace.setLastModifiedUser(getUser());
workspace.countAndLogWarnings();
HttpPut httpPut = new HttpPut(url + WORKSPACE_PATH + workspaceId);
StringWriter stringWriter = new StringWriter();
if (encryptionStrategy == null) {
JsonWriter jsonWriter = new JsonWriter(false);
jsonWriter.write(workspace, stringWriter);
} else {
EncryptedWorkspace encryptedWorkspace = new EncryptedWorkspace(workspace, encryptionStrategy);
encryptionStrategy.setLocation(EncryptionLocation.Client);
EncryptedJsonWriter jsonWriter = new EncryptedJsonWriter(false);
jsonWriter.write(encryptedWorkspace, stringWriter);
}
StringEntity stringEntity = new StringEntity(stringWriter.toString(), ContentType.APPLICATION_JSON);
httpPut.setEntity(stringEntity);
addHeaders(httpPut, EntityUtils.toString(stringEntity), ContentType.APPLICATION_JSON.toString());
debugRequest(httpPut, EntityUtils.toString(stringEntity));
log.info("Putting workspace with ID " + workspaceId);
try (CloseableHttpResponse response = httpClient.execute(httpPut)) {
String json = EntityUtils.toString(response.getEntity());
if (response.getCode() == HttpStatus.SC_OK) {
debugResponse(response);
log.info(json);
} else {
ApiResponse apiResponse = ApiResponse.parse(json);
throw new StructurizrClientException(apiResponse.getMessage());
}
}
} catch (Exception e) {
log.error(e);
throw new StructurizrClientException(e);
}
}
use of com.structurizr.io.json.JsonWriter in project java by structurizr.
the class WorkspaceUtils method saveWorkspaceToJson.
/**
* Saves a workspace to a JSON definition as a file.
*
* @param workspace a Workspace object
* @param file a File representing the JSON definition
* @throws Exception if something goes wrong
*/
public static void saveWorkspaceToJson(Workspace workspace, File file) throws Exception {
if (workspace == null) {
throw new IllegalArgumentException("A workspace must be provided.");
} else if (file == null) {
throw new IllegalArgumentException("The path to a JSON file must be specified.");
}
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
new JsonWriter(true).write(workspace, writer);
writer.flush();
writer.close();
}
use of com.structurizr.io.json.JsonWriter in project java by structurizr.
the class WorkspaceUtils method toJson.
/**
* Serializes the specified workspace to a JSON string.
*
* @param workspace a Workspace instance
* @param indentOutput whether to indent the output (prettify)
* @return a JSON string
* @throws Exception if something goes wrong
*/
public static String toJson(Workspace workspace, boolean indentOutput) throws Exception {
if (workspace == null) {
throw new IllegalArgumentException("A workspace must be provided.");
}
JsonWriter jsonWriter = new JsonWriter(indentOutput);
StringWriter stringWriter = new StringWriter();
jsonWriter.write(workspace, stringWriter);
stringWriter.flush();
stringWriter.close();
return stringWriter.toString();
}
use of com.structurizr.io.json.JsonWriter in project java by structurizr.
the class EncryptedWorkspaceTests method test_construction_WhenTwoParametersAreSpecified.
@Test
public void test_construction_WhenTwoParametersAreSpecified() throws Exception {
encryptedWorkspace = new EncryptedWorkspace(workspace, encryptionStrategy);
assertEquals("Name", encryptedWorkspace.getName());
assertEquals("Description", encryptedWorkspace.getDescription());
assertEquals("1.2.3", encryptedWorkspace.getVersion());
assertEquals("simon", encryptedWorkspace.getLastModifiedUser());
assertEquals("structurizr-java", encryptedWorkspace.getLastModifiedAgent());
assertEquals(1234, encryptedWorkspace.getId());
assertEquals("user@domain.com", encryptedWorkspace.getConfiguration().getUsers().iterator().next().getUsername());
assertNull(workspace.getConfiguration());
assertSame(workspace, encryptedWorkspace.getWorkspace());
assertSame(encryptionStrategy, encryptedWorkspace.getEncryptionStrategy());
JsonWriter jsonWriter = new JsonWriter(false);
StringWriter stringWriter = new StringWriter();
jsonWriter.write(workspace, stringWriter);
assertEquals(stringWriter.toString(), encryptedWorkspace.getPlaintext());
assertEquals(encryptionStrategy.encrypt(stringWriter.toString()), encryptedWorkspace.getCiphertext());
}
use of com.structurizr.io.json.JsonWriter in project java by structurizr.
the class EncryptedWorkspaceTests method test_getWorkspace_ReturnsTheWorkspace_WhenACipherextIsSpecified.
@Test
public void test_getWorkspace_ReturnsTheWorkspace_WhenACipherextIsSpecified() throws Exception {
JsonWriter jsonWriter = new JsonWriter(false);
StringWriter stringWriter = new StringWriter();
jsonWriter.write(workspace, stringWriter);
String expected = stringWriter.toString();
encryptedWorkspace = new EncryptedWorkspace();
encryptedWorkspace.setEncryptionStrategy(encryptionStrategy);
encryptedWorkspace.setCiphertext(encryptionStrategy.encrypt(expected));
workspace = encryptedWorkspace.getWorkspace();
assertEquals("Name", workspace.getName());
stringWriter = new StringWriter();
jsonWriter.write(workspace, stringWriter);
assertEquals(expected, stringWriter.toString());
}
Aggregations