You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/09/02 13:04:49 UTC

[GitHub] [nifi] simonbence commented on a change in pull request #5330: NIFI-9066 Supporting flow file attributes in PutSplunkHTTP

simonbence commented on a change in pull request #5330:
URL: https://github.com/apache/nifi/pull/5330#discussion_r701061896



##########
File path: nifi-nar-bundles/nifi-splunk-bundle/nifi-splunk-processors/src/main/java/org/apache/nifi/processors/splunk/PutSplunkHTTP.java
##########
@@ -288,4 +232,76 @@ private FlowFile enrichFlowFile(final ProcessSession session, final FlowFile flo
         attributes.put(SplunkAPICall.RESPONDED_AT_ATTRIBUTE, String.valueOf(System.currentTimeMillis()));
         return session.putAllAttributes(flowFile, attributes);
     }
+
+    private static class RequestDetails {
+        private static final String ENDPOINT = "/services/collector/raw";
+
+        private final String endpoint;
+        private final String contentType;
+        private final String charset;
+
+        private RequestDetails(final String endpoint, final String contentType, final String charset) {
+            this.endpoint = endpoint;
+            this.contentType = contentType;
+            this.charset = charset;
+        }
+
+        public String getEndpoint() {
+            return endpoint;
+        }
+
+        public String getContentType() {
+            return contentType;
+        }
+
+        public String getCharset() {
+            return charset;
+        }
+
+        public static RequestDetails getInstance(final ComponentLog logger, final ProcessContext context, final FlowFile flowFile) {
+            final String contentType = (context.getProperty(CONTENT_TYPE).isSet()) ? context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions(flowFile).getValue() : null;
+            final String charset = context.getProperty(CHARSET).evaluateAttributeExpressions(flowFile).getValue();
+            final Map<String, String> queryParameters = new HashMap<>();
+
+            if (context.getProperty(SOURCE_TYPE).isSet()) {
+                queryParameters.put("sourcetype", context.getProperty(SOURCE_TYPE).evaluateAttributeExpressions(flowFile).getValue());
+            }
+
+            if (context.getProperty(SOURCE).isSet()) {
+                queryParameters.put("source", context.getProperty(SOURCE).evaluateAttributeExpressions(flowFile).getValue());
+            }
+
+            if (context.getProperty(HOST).isSet()) {
+                queryParameters.put("host", context.getProperty(HOST).evaluateAttributeExpressions(flowFile).getValue());
+            }
+
+            if (context.getProperty(INDEX).isSet()) {
+                queryParameters.put("index", context.getProperty(INDEX).evaluateAttributeExpressions(flowFile).getValue());
+            }
+
+            return new RequestDetails(getEndpoint(logger, queryParameters), contentType, charset);
+        }
+
+        private static String getEndpoint(final ComponentLog logger, final Map<String, String> queryParameters) {
+            final StringBuilder result = new StringBuilder(ENDPOINT);
+
+            if (!queryParameters.isEmpty()) {
+                final List<String> parameters = new LinkedList<>();
+
+                try {
+                    for (final Map.Entry<String, String> parameter : queryParameters.entrySet()) {
+                        parameters.add(URLEncoder.encode(parameter.getKey(), "UTF-8") + '=' + URLEncoder.encode(parameter.getValue(), "UTF-8"));
+                    }
+                } catch (final UnsupportedEncodingException e) {
+                    logger.error("Could not be initialized because of: {}", new Object[]{e.getMessage()}, e);

Review comment:
       As I touched the code due to @tpalfy's comments, I realised that this is an instance of `ComponentLogger` not a generic `Logger`. My understanding i. that the suggested parametrisation is not supported by this class, thus I will keep it as it is.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org