Search in sources :

Example 1 with EncryptedJsonWriter

use of com.structurizr.io.json.EncryptedJsonWriter 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)

Aggregations

Workspace (com.structurizr.Workspace)1 EncryptedWorkspace (com.structurizr.encryption.EncryptedWorkspace)1 EncryptedJsonWriter (com.structurizr.io.json.EncryptedJsonWriter)1 JsonWriter (com.structurizr.io.json.JsonWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)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