Search in sources :

Example 1 with JsonWriter

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);
    }
}
Also used : EncryptedJsonWriter(com.structurizr.io.json.EncryptedJsonWriter) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) EncryptedJsonWriter(com.structurizr.io.json.EncryptedJsonWriter) JsonWriter(com.structurizr.io.json.JsonWriter) Date(java.util.Date) HttpPut(org.apache.hc.client5.http.classic.methods.HttpPut) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) StringWriter(java.io.StringWriter) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) Workspace(com.structurizr.Workspace)

Example 2 with JsonWriter

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();
}
Also used : JsonWriter(com.structurizr.io.json.JsonWriter)

Example 3 with JsonWriter

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();
}
Also used : JsonWriter(com.structurizr.io.json.JsonWriter)

Example 4 with JsonWriter

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());
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(com.structurizr.io.json.JsonWriter) Test(org.junit.Test)

Example 5 with JsonWriter

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());
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(com.structurizr.io.json.JsonWriter) Test(org.junit.Test)

Aggregations

JsonWriter (com.structurizr.io.json.JsonWriter)6 StringWriter (java.io.StringWriter)4 Test (org.junit.Test)3 Workspace (com.structurizr.Workspace)1 EncryptedWorkspace (com.structurizr.encryption.EncryptedWorkspace)1 EncryptedJsonWriter (com.structurizr.io.json.EncryptedJsonWriter)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Date (java.util.Date)1 HttpPut (org.apache.hc.client5.http.classic.methods.HttpPut)1 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)1 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)1 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)1