package io.mikekennedy.camel;

import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultProducer;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.http.client.HttpClient;


public class SlackProducer extends DefaultProducer {

    private static final transient Logger LOG = LoggerFactory.getLogger(SlackProducer.class);

    private SlackEndpoint slackEndpoint;

    public SlackProducer(SlackEndpoint endpoint) {
        super(endpoint);
        this.slackEndpoint = endpoint;
        LOG.info("webHookUrl = " + this.slackEndpoint.getWebhookUrl());
    }

    @Override
    public void process(Exchange exchange) throws Exception {
        HttpClient client = HttpClientBuilder.create().build();

        HttpPost httpPost = new HttpPost(slackEndpoint.getWebhookUrl());

        SlackMessage slackMessage = new SlackMessage();

        slackMessage.setText((String)exchange.getIn().getBody());
        slackMessage.setChannel(slackEndpoint.getChannel());
        slackMessage.setUsername(slackEndpoint.getUsername());
        slackMessage.setIconUrl(slackEndpoint.getIconUrl());
        slackMessage.setIconEmoji(slackEndpoint.getIconEmoji());

        StringEntity body = new StringEntity(slackMessage.toString());

        LOG.info(slackMessage.toString());

        httpPost.setEntity(body);

        client.execute(httpPost);
    }
}

@@ -24,16 +24,56 @@
 camel;%0A%0A
+import org.apache.camel.CamelException;%0A
 import o
@@ -140,24 +140,61 @@
 ltProducer;%0A
+import org.apache.http.HttpResponse;%0A
 import org.a
@@ -619,16 +619,96 @@
 point;%0A%0A
+    /**%0A     * Constuctor%0A     *%0A     * @param endpoint a SlackEndpoint%0A     */%0A
     publ
@@ -818,80 +818,8 @@
 nt;%0A
-        LOG.info(%22webHookUrl = %22 + this.slackEndpoint.getWebhookUrl());%0A
     
@@ -896,16 +896,65 @@
 eption %7B
+%0A%0A        // Create an HttpClient and Post object
 %0A       
@@ -1006,25 +1006,24 @@
 ().build();%0A
-%0A
         Http
@@ -1080,24 +1080,55 @@
 ookUrl());%0A%0A
+        // Build Helper object%0A
         Slac
@@ -1171,17 +1171,16 @@
 sage();%0A
-%0A
         
@@ -1204,16 +1204,8 @@
 ext(
-(String)
 exch
@@ -1225,16 +1225,28 @@
 getBody(
+String.class
 ));%0A    
@@ -1488,24 +1488,53 @@
 nEmoji());%0A%0A
+        // Set the post body%0A
         Stri
@@ -1593,24 +1593,25 @@
 ng());%0A%0A
+%0A
         
 LOG.info
@@ -1606,43 +1606,22 @@
     
-LOG.info(slackMessage.toString());%0A
+// Do the post
 %0A   
@@ -1659,16 +1659,40 @@
 %0A       
+ HttpResponse response =
  client.
@@ -1709,16 +1709,182 @@
 tpPost);
+%0A%0A        if (response.getStatusLine().getStatusCode() != 200) %7B%0A            throw new CamelException(%22Error POSTing to Slack API: %22 + response.toString());%0A        %7D
 %0A    %7D%0A%7D

Handle slack API errors a little more gracefully