You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2020/04/20 07:47:47 UTC

[GitHub] [camel] omarsmak commented on a change in pull request #3760: [CAMEL-14932] Add new Splunk HEC component

omarsmak commented on a change in pull request #3760:
URL: https://github.com/apache/camel/pull/3760#discussion_r411163810



##########
File path: components/camel-splunk-hec/src/main/java/org/apache/camel/component/splunkhec/SplunkHECProducer.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.camel.component.splunkhec;
+
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.EntityTemplate;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.ssl.SSLContextBuilder;
+
+/**
+ * The Splunk HEC producer.
+ */
+public class SplunkHECProducer extends DefaultProducer {
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+    private SplunkHECEndpoint endpoint;
+    private CloseableHttpClient httpClient;
+
+
+    public SplunkHECProducer(SplunkHECEndpoint endpoint) {
+        super(endpoint);
+        this.endpoint = endpoint;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        HttpClientBuilder builder = HttpClients.custom().
+                setUserAgent("Camel Splunk HEC/" + getEndpoint().getCamelContext().getVersion()).
+                setMaxConnTotal(10);
+        if (endpoint.getConfiguration().isSkiptlsverify()) {
+            SSLContextBuilder sslbuilder = new SSLContextBuilder();
+            sslbuilder.loadTrustMaterial(null, (chain, authType) -> true);
+            SSLConnectionSocketFactory sslsf = new
+                    SSLConnectionSocketFactory(sslbuilder.build(), NoopHostnameVerifier.INSTANCE);
+            builder.setSSLSocketFactory(sslsf);
+        }
+        httpClient = builder.build();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        Map<String, Object> payload = createPayload(exchange.getIn());
+
+        HttpPost httppost = new HttpPost((endpoint.getConfiguration().isHttps() ? "https" : "http") + "://" + endpoint.getSplunkURL() + "/services/collector/event");
+        httppost.addHeader("Authorization", " Splunk " + endpoint.getToken());
+
+        EntityTemplate entityTemplate = new EntityTemplate(outputStream -> MAPPER.writer().writeValue(outputStream, payload));
+        entityTemplate.setContentType(ContentType.APPLICATION_JSON.getMimeType());
+
+        httppost.setEntity(entityTemplate);
+        try (CloseableHttpResponse response = httpClient.execute(httppost)) {
+            if (response.getStatusLine().getStatusCode() != 200) {
+                ByteArrayOutputStream output = new ByteArrayOutputStream();
+                response.getEntity().writeTo(output);
+
+                throw new RuntimeException(response.getStatusLine().toString() + "\n" + new String(output.toByteArray(), StandardCharsets.UTF_8));
+            }
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        httpClient.close();

Review comment:
       Isn't better to have `httpClient.close` before `super.doStop()`?




----------------------------------------------------------------
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.

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