Search in sources :

Example 1 with JsonReader

use of com.structurizr.io.json.JsonReader in project java by structurizr.

the class StructurizrClientIntegrationTests method test_putAndGetWorkspace_WithoutEncryption.

@Test
public void test_putAndGetWorkspace_WithoutEncryption() throws Exception {
    Workspace workspace = new Workspace("Structurizr client library tests - without encryption", "A test workspace for the Structurizr client library");
    SoftwareSystem softwareSystem = workspace.getModel().addSoftwareSystem("Software System", "Description");
    Person person = workspace.getModel().addPerson("Person", "Description");
    person.uses(softwareSystem, "Uses");
    SystemContextView systemContextView = workspace.getViews().createSystemContextView(softwareSystem, "SystemContext", "Description");
    systemContextView.addAllElements();
    structurizrClient.putWorkspace(20081, workspace);
    workspace = structurizrClient.getWorkspace(20081);
    assertNotNull(workspace.getModel().getSoftwareSystemWithName("Software System"));
    assertNotNull(workspace.getModel().getPersonWithName("Person"));
    assertEquals(1, workspace.getModel().getRelationships().size());
    assertEquals(1, workspace.getViews().getSystemContextViews().size());
    // and check the archive version is readable
    Workspace archivedWorkspace = new JsonReader().read(new FileReader(getArchivedWorkspace()));
    assertEquals(20081, archivedWorkspace.getId());
    assertEquals("Structurizr client library tests - without encryption", archivedWorkspace.getName());
    assertEquals(1, archivedWorkspace.getModel().getSoftwareSystems().size());
    assertEquals(1, workspaceArchiveLocation.listFiles().length);
}
Also used : SystemContextView(com.structurizr.view.SystemContextView) SoftwareSystem(com.structurizr.model.SoftwareSystem) JsonReader(com.structurizr.io.json.JsonReader) EncryptedJsonReader(com.structurizr.io.json.EncryptedJsonReader) FileReader(java.io.FileReader) Person(com.structurizr.model.Person) Workspace(com.structurizr.Workspace) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) Test(org.junit.Test)

Example 2 with JsonReader

use of com.structurizr.io.json.JsonReader in project java by structurizr.

the class StructurizrClient method getWorkspace.

/**
 * Gets the workspace with the given ID.
 *
 * @param workspaceId the workspace ID
 * @return a Workspace instance
 * @throws StructurizrClientException   if there are problems related to the network, authorization, JSON deserialization, etc
 */
public Workspace getWorkspace(long workspaceId) throws StructurizrClientException {
    if (workspaceId <= 0) {
        throw new IllegalArgumentException("The workspace ID must be a positive integer.");
    }
    try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
        log.info("Getting workspace with ID " + workspaceId);
        HttpGet httpGet = new HttpGet(url + WORKSPACE_PATH + workspaceId);
        addHeaders(httpGet, "", "");
        debugRequest(httpGet, null);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            debugResponse(response);
            String json = EntityUtils.toString(response.getEntity());
            if (response.getCode() == HttpStatus.SC_OK) {
                archiveWorkspace(workspaceId, json);
                if (encryptionStrategy == null) {
                    if (json.contains("\"encryptionStrategy\"") && json.contains("\"ciphertext\"")) {
                        log.warn("The JSON may contain a client-side encrypted workspace, but no passphrase has been specified.");
                    }
                    JsonReader jsonReader = new JsonReader();
                    jsonReader.setIdGenerator(idGenerator);
                    return jsonReader.read(new StringReader(json));
                } else {
                    EncryptedWorkspace encryptedWorkspace = new EncryptedJsonReader().read(new StringReader(json));
                    if (encryptedWorkspace.getEncryptionStrategy() != null) {
                        encryptedWorkspace.getEncryptionStrategy().setPassphrase(encryptionStrategy.getPassphrase());
                        return encryptedWorkspace.getWorkspace();
                    } else {
                        // this workspace isn't encrypted, even though the client has an encryption strategy set
                        JsonReader jsonReader = new JsonReader();
                        jsonReader.setIdGenerator(idGenerator);
                        return jsonReader.read(new StringReader(json));
                    }
                }
            } else {
                ApiResponse apiResponse = ApiResponse.parse(json);
                throw new StructurizrClientException(apiResponse.getMessage());
            }
        }
    } catch (Exception e) {
        log.error(e);
        throw new StructurizrClientException(e);
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) EncryptedJsonReader(com.structurizr.io.json.EncryptedJsonReader) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) StringReader(java.io.StringReader) JsonReader(com.structurizr.io.json.JsonReader) EncryptedJsonReader(com.structurizr.io.json.EncryptedJsonReader) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

EncryptedWorkspace (com.structurizr.encryption.EncryptedWorkspace)2 EncryptedJsonReader (com.structurizr.io.json.EncryptedJsonReader)2 JsonReader (com.structurizr.io.json.JsonReader)2 Workspace (com.structurizr.Workspace)1 Person (com.structurizr.model.Person)1 SoftwareSystem (com.structurizr.model.SoftwareSystem)1 SystemContextView (com.structurizr.view.SystemContextView)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 UnknownHostException (java.net.UnknownHostException)1 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)1 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)1 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)1 Test (org.junit.Test)1