From 9cd319e3e5ca19cbdf92bad60e761ef680f2297c Mon Sep 17 00:00:00 2001 From: David Handermann Date: Wed, 7 Aug 2024 16:22:39 -0500 Subject: [PATCH] NIFI-13646 Corrected Current User Replicated Response Handling (#9164) Signed-off-by: David Handermann --- .../org/apache/nifi/web/api/FlowResource.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/FlowResource.java b/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/FlowResource.java index dfe6399d1149..722c49c449ff 100644 --- a/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/FlowResource.java +++ b/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/FlowResource.java @@ -16,6 +16,7 @@ */ package org.apache.nifi.web.api; +import com.fasterxml.jackson.databind.ObjectMapper; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.exporter.common.TextFormat; import io.swagger.v3.oas.annotations.Operation; @@ -152,6 +153,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; import java.text.Collator; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; @@ -188,6 +192,8 @@ public class FlowResource extends ApplicationResource { private static final String VERSIONED_REPORTING_TASK_SNAPSHOT_FILENAME_PATTERN = "VersionedReportingTaskSnapshot-%s.json"; private static final String VERSIONED_REPORTING_TASK_SNAPSHOT_DATE_FORMAT = "yyyyMMddHHmmss"; + private static final ObjectMapper objectMapper = new ObjectMapper(); + private NiFiServiceFacade serviceFacade; private Authorizer authorizer; @@ -343,7 +349,7 @@ public Response getCurrentUser() { final CurrentUserEntity entity; if (isReplicateRequest()) { try (Response replicatedResponse = replicate(HttpMethod.GET)) { - final CurrentUserEntity replicatedCurrentUserEntity = (CurrentUserEntity) replicatedResponse.getEntity(); + final CurrentUserEntity replicatedCurrentUserEntity = readReplicatedCurrentUserEntity(replicatedResponse); final CurrentUserEntity currentUserEntity = serviceFacade.getCurrentUser(); // Set Logout Supported based on local client information instead of replicated and merged responses replicatedCurrentUserEntity.setLogoutSupported(currentUserEntity.isLogoutSupported()); @@ -356,6 +362,25 @@ public Response getCurrentUser() { return generateOkResponse(entity).build(); } + private CurrentUserEntity readReplicatedCurrentUserEntity(final Response replicatedResponse) { + final Object entity = replicatedResponse.getEntity(); + if (entity instanceof CurrentUserEntity replicatedCurrentUserEntity) { + return replicatedCurrentUserEntity; + } else if (entity instanceof StreamingOutput streamingOutput) { + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + try { + streamingOutput.write(outputStream); + + final byte[] bytes = outputStream.toByteArray(); + return objectMapper.readValue(bytes, CurrentUserEntity.class); + } catch (final IOException e) { + throw new UncheckedIOException("Read Current User Entity failed", e); + } + } else { + throw new IllegalStateException("Current User Entity not expected [%s]".formatted(entity)); + } + } + /** * Retrieves the contents of the specified group. *