This is an automated email from the ASF dual-hosted git repository.

exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new 84df025ccf NIFI-12875 Added Response Handling Strategy to 
RestLookupService
84df025ccf is described below

commit 84df025ccfe68bf9eefc0a43ad14fdc727e3723d
Author: Gregory M. Foreman <[email protected]>
AuthorDate: Thu Mar 7 17:49:32 2024 -0600

    NIFI-12875 Added Response Handling Strategy to RestLookupService
    
    This closes #8484
    
    Signed-off-by: David Handermann <[email protected]>
---
 .../nifi/lookup/ResponseHandlingStrategy.java      | 49 ++++++++++++++++++++++
 .../org/apache/nifi/lookup/RestLookupService.java  | 21 +++++++++-
 .../apache/nifi/lookup/TestRestLookupService.java  | 27 ++++++++++++
 3 files changed, 96 insertions(+), 1 deletion(-)

diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/ResponseHandlingStrategy.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/ResponseHandlingStrategy.java
new file mode 100644
index 0000000000..241737a1bc
--- /dev/null
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/ResponseHandlingStrategy.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.lookup;
+
+import org.apache.nifi.components.DescribedValue;
+
+public enum ResponseHandlingStrategy implements DescribedValue {
+    RETURNED("Returned", "Successful and unsuccessful HTTP responses are 
returned."),
+    EVALUATED("Evaluated", "Successful HTTP responses are returned and 
unsuccessful HTTP responses generate an exception.");
+
+    private final String displayName;
+    private final String description;
+
+    ResponseHandlingStrategy(final String displayName, final String 
description) {
+        this.displayName = displayName;
+        this.description = description;
+    }
+
+    @Override
+    public String getValue() {
+        return name();
+    }
+
+    @Override
+    public String getDisplayName() {
+        return displayName;
+    }
+
+    @Override
+    public String getDescription() {
+        return description;
+    }
+
+}
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/RestLookupService.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/RestLookupService.java
index 30d7939f2d..aa3d0864b2 100644
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/RestLookupService.java
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/RestLookupService.java
@@ -172,6 +172,15 @@ public class RestLookupService extends 
AbstractControllerService implements Reco
         .addValidator(StandardValidators.TIME_PERIOD_VALIDATOR)
         .build();
 
+    public static final PropertyDescriptor RESPONSE_HANDLING_STRATEGY = new 
PropertyDescriptor.Builder()
+        .name("rest-lookup-response-handling-strategy")
+        .displayName("Response Handling Strategy")
+        .description("Whether to return all responses or throw errors for 
unsuccessful HTTP status codes.")
+        .required(true)
+        .defaultValue(ResponseHandlingStrategy.RETURNED)
+        .allowableValues(ResponseHandlingStrategy.class)
+        .build();
+
     private static final ProxySpec[] PROXY_SPECS = {ProxySpec.HTTP_AUTH, 
ProxySpec.SOCKS};
     public static final PropertyDescriptor PROXY_CONFIGURATION_SERVICE
             = ProxyConfiguration.createProxyConfigPropertyDescriptor(true, 
PROXY_SPECS);
@@ -190,6 +199,7 @@ public class RestLookupService extends 
AbstractControllerService implements Reco
             URL,
             RECORD_READER,
             RECORD_PATH,
+            RESPONSE_HANDLING_STRATEGY,
             SSL_CONTEXT_SERVICE,
             PROXY_CONFIGURATION_SERVICE,
             PROP_BASIC_AUTH_USERNAME,
@@ -214,6 +224,7 @@ public class RestLookupService extends 
AbstractControllerService implements Reco
     private volatile String basicUser;
     private volatile String basicPass;
     private volatile boolean isDigest;
+    private volatile ResponseHandlingStrategy responseHandlingStrategy;
 
     @OnEnabled
     public void onEnabled(final ConfigurationContext context) {
@@ -251,6 +262,8 @@ public class RestLookupService extends 
AbstractControllerService implements Reco
         buildHeaders(context);
 
         urlTemplate = context.getProperty(URL);
+
+        responseHandlingStrategy = 
context.getProperty(RESPONSE_HANDLING_STRATEGY).asAllowableValue(ResponseHandlingStrategy.class);
     }
 
     @OnDisabled
@@ -320,13 +333,19 @@ public class RestLookupService extends 
AbstractControllerService implements Reco
         Request request = buildRequest(mimeType, method, body, endpoint, 
context);
         try {
             Response response = executeRequest(request);
+            final ResponseBody responseBody = response.body();
 
             if (getLogger().isDebugEnabled()) {
                 getLogger().debug("Response code {} was returned for 
coordinate {}",
                         new Object[]{response.code(), coordinates});
             }
 
-            final ResponseBody responseBody = response.body();
+            if (!response.isSuccessful()
+                    && 
responseHandlingStrategy.equals(ResponseHandlingStrategy.EVALUATED)) {
+                final String responseText = responseBody == null ? "[No 
Message Received]" : responseBody.string();
+                throw new IOException("Request failed with HTTP %d for [%s]: 
%s".formatted(response.code(), request.url(), responseText));
+            }
+
             if (responseBody == null) {
                 return Optional.empty();
             }
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/test/java/org/apache/nifi/lookup/TestRestLookupService.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/test/java/org/apache/nifi/lookup/TestRestLookupService.java
index 5ffdd3d63a..724f5b8df0 100644
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/test/java/org/apache/nifi/lookup/TestRestLookupService.java
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/test/java/org/apache/nifi/lookup/TestRestLookupService.java
@@ -42,6 +42,7 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Optional;
 
+import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
 import static java.net.HttpURLConnection.HTTP_OK;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -170,6 +171,32 @@ class TestRestLookupService {
         assertPostRecordedRequestFound();
     }
 
+    @Test
+    void testLookupResponseHandlingStrategyReturned() throws Exception {
+        runner.setProperty(restLookupService, 
RestLookupService.RESPONSE_HANDLING_STRATEGY, 
ResponseHandlingStrategy.RETURNED);
+        runner.enableControllerService(restLookupService);
+
+        when(recordReaderFactory.createRecordReader(any(), any(), anyLong(), 
any())).thenReturn(recordReader);
+        when(recordReader.nextRecord()).thenReturn(record);
+
+        mockWebServer.enqueue(new 
MockResponse().setResponseCode(HTTP_NOT_FOUND)
+                .setHeader("Content-type", "application/json")
+                .setBody("{\"error\": { \"code\": 404 } }"));
+
+        final Optional<Record> recordFound = 
restLookupService.lookup(Collections.emptyMap());
+        assertTrue(recordFound.isPresent());
+    }
+
+    @Test
+    void testLookupResponseHandlingStrategyEvaluated() {
+        runner.setProperty(restLookupService, 
RestLookupService.RESPONSE_HANDLING_STRATEGY, 
ResponseHandlingStrategy.EVALUATED);
+        runner.enableControllerService(restLookupService);
+        mockWebServer.enqueue(new 
MockResponse().setResponseCode(HTTP_NOT_FOUND));
+
+        final LookupFailureException exception = 
assertThrows(LookupFailureException.class, () -> 
restLookupService.lookup(Collections.emptyMap()));
+        assertInstanceOf(IOException.class, exception.getCause());
+    }
+
     private void assertRecordedRequestFound() throws InterruptedException {
         final RecordedRequest request = mockWebServer.takeRequest();
 

Reply via email to