Repository: iluwatar/java-design-patterns
Issue: Wire Tap (EIP)
Labels: epic: pattern


Issue comments:
Some Informations:
- Description from EIP: http://www.enterpriseintegrationpatterns.com/patterns/messaging/WireTap.html
- Implementation inside Apache Camel: http://camel.apache.org/wire-tap.html

I can help you out with most EIP patterns, starting from this one.
Thanks @codinghog go ahead :+1: 

Pull request review comments:

Patch that resolved the issue:
diff --git a/eip-wire-tap/README.md b/eip-wire-tap/README.md
new file mode 100644
index 000000000000..d2742aa313f9
--- /dev/null
+++ b/eip-wire-tap/README.md
@@ -0,0 +1,29 @@
+---
+layout: pattern
+title: EIP Wire Tap
+folder: eip-wire-tap
+permalink: /patterns/eip-wire-tap/
+categories: Enterprise integration
+tags:
+ - Java
+ - Difficulty-Intermittent
+ - Enterprise integration
+---
+
+## Intent
+In most integration cases there is a need to monitor the messages flowing through the system. It is usually achieved
+by intercepting the message and redirecting it to a different location like console, filesystem or the database.
+It is important that such functionality should not modify the original message and influence the processing path.
+
+![alt text](./etc/wiretap.gif "Wire Tap")
+
+## Applicability
+Use the Wire Tap pattern when
+
+* You need to monitor messages flowing through the system
+* You need to redirect the same, unchanged message to two different endpoints/paths
+
+## Credits
+
+* [Gregor Hohpe, Bobby Woolf - Enterprise Integration Patterns](http://www.enterpriseintegrationpatterns.com/patterns/messaging/WireTap.html)
+* [Apache Camel - Documentation](http://camel.apache.org/wire-tap.html)
diff --git a/eip-wire-tap/pom.xml b/eip-wire-tap/pom.xml
new file mode 100644
index 000000000000..b3bc678b0f4d
--- /dev/null
+++ b/eip-wire-tap/pom.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    The MIT License
+    Copyright (c) 2014-2016 Ilkka Seppälä
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the "Software"), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>eip-wire-tap</artifactId>
+    <parent>
+        <groupId>com.iluwatar</groupId>
+        <artifactId>java-design-patterns</artifactId>
+        <version>1.18.0-SNAPSHOT</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-core</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-spring-boot</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+
+        <!-- Testing -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-test-spring</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/eip-wire-tap/src/main/java/com/iluwatar/eip/wiretap/App.java b/eip-wire-tap/src/main/java/com/iluwatar/eip/wiretap/App.java
new file mode 100644
index 000000000000..7bf4ce0d4991
--- /dev/null
+++ b/eip-wire-tap/src/main/java/com/iluwatar/eip/wiretap/App.java
@@ -0,0 +1,51 @@
+package com.iluwatar.eip.wiretap;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ConfigurableApplicationContext;
+
+/**
+ * In most integration cases there is a need to monitor the messages flowing through the system. It is usually achieved
+ * by intercepting the message and redirecting it to a different location like console, filesystem or the database.
+ * It is important that such functionality should not modify the original message and influence the processing path.
+ *
+ * <p>
+ * Wire Tap allows you to route messages to a separate location while they are being forwarded to the ultimate
+ * destination. It basically consumes messages of the input channel and publishes the unmodified message to both
+ * output channels.
+ * </p>
+ */
+@SpringBootApplication
+public class App {
+
+  /**
+   * Program entry point. It starts Spring Boot application and using Apache Camel it auto-configures routes.
+   *
+   * @param args command line args
+   */
+  public static void main(String[] args) throws Exception {
+    // Run Spring Boot application and obtain ApplicationContext
+    ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
+
+    // Get CamelContext from ApplicationContext
+    CamelContext camelContext = (CamelContext) context.getBean("camelContext");
+
+    // Add a new routes that will handle endpoints form WireTapRoute class.
+    camelContext.addRoutes(new RouteBuilder() {
+
+      @Override
+      public void configure() throws Exception {
+        from("{{endpoint}}").log("ENDPOINT: ${body}");
+        from("{{wireTapEndpoint}}").log("WIRETAPPED ENDPOINT: ${body}");
+      }
+
+    });
+
+    // Add producer that will send test message to an entry point in WireTapRoute
+    camelContext.createProducerTemplate().sendBody("{{entry}}", "Test message");
+
+    SpringApplication.exit(context);
+  }
+}
diff --git a/eip-wire-tap/src/main/java/com/iluwatar/eip/wiretap/routes/WireTapRoute.java b/eip-wire-tap/src/main/java/com/iluwatar/eip/wiretap/routes/WireTapRoute.java
new file mode 100644
index 000000000000..c744295ee52e
--- /dev/null
+++ b/eip-wire-tap/src/main/java/com/iluwatar/eip/wiretap/routes/WireTapRoute.java
@@ -0,0 +1,32 @@
+package com.iluwatar.eip.wiretap.routes;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.springframework.stereotype.Component;
+
+/**
+ * Sample wire tap route definition.
+ *
+ * <p>
+ * It consumes messages out of the <i>direct:entry</i> entry point and forwards them to <i>direct:endpoint</i>.
+ * Wire Tap intercepts the message and sends it to <i>direct:wireTap</i>, which in turn forwards it to
+ * <i>direct:wireTapEndpoint</i>.
+ * </p>
+ *
+ * In this example input/output endpoints names are stored in <i>application.properties</i> file.
+ */
+@Component
+public class WireTapRoute extends RouteBuilder {
+
+  /**
+   * Configures the route
+   * @throws Exception in case of exception during configuration
+   */
+  @Override
+  public void configure() throws Exception {
+    // Main route
+    from("{{entry}}").wireTap("direct:wireTap").to("{{endpoint}}");
+
+    // Wire tap route
+    from("direct:wireTap").log("Message: ${body}").to("{{wireTapEndpoint}}");
+  }
+}
diff --git a/eip-wire-tap/src/main/resources/application.properties b/eip-wire-tap/src/main/resources/application.properties
new file mode 100644
index 000000000000..6dabe6ccc9ee
--- /dev/null
+++ b/eip-wire-tap/src/main/resources/application.properties
@@ -0,0 +1,3 @@
+entry=direct:entry
+endpoint=direct:endpoint
+wireTapEndpoint=direct:wireTapEndpoint
\ No newline at end of file
diff --git a/eip-wire-tap/src/test/resources/application-test.properties b/eip-wire-tap/src/test/resources/application-test.properties
new file mode 100644
index 000000000000..1719c8ca4ab4
--- /dev/null
+++ b/eip-wire-tap/src/test/resources/application-test.properties
@@ -0,0 +1,3 @@
+entry=direct:entry
+endpoint=mock:endpoint
+wireTapEndpoint=mock:wireTapEndpoint
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index ec39a6914125..774b140190c1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -145,8 +145,9 @@
         <module>cqrs</module>
         <module>event-sourcing</module>
         <module>data-transfer-object</module>
-    <module>throttling</module>
+        <module>throttling</module>
         <module>partial-response</module>
+        <module>eip-wire-tap</module>
     </modules>
 
     <dependencyManagement>


Test patch:
diff --git a/eip-wire-tap/src/test/java/com/iluwatar/eip/wiretap/AppTest.java b/eip-wire-tap/src/test/java/com/iluwatar/eip/wiretap/AppTest.java
new file mode 100644
index 000000000000..25b9d8550f4c
--- /dev/null
+++ b/eip-wire-tap/src/test/java/com/iluwatar/eip/wiretap/AppTest.java
@@ -0,0 +1,15 @@
+package com.iluwatar.eip.wiretap;
+
+import org.junit.Test;
+
+/**
+ * Test for App class
+ */
+public class AppTest {
+
+  @Test
+  public void testMain() throws Exception {
+    String[] args = {};
+    App.main(args);
+  }
+}
diff --git a/eip-wire-tap/src/test/java/com/iluwatar/eip/wiretap/routes/WireTapRouteTest.java b/eip-wire-tap/src/test/java/com/iluwatar/eip/wiretap/routes/WireTapRouteTest.java
new file mode 100644
index 000000000000..a7b15f45eb93
--- /dev/null
+++ b/eip-wire-tap/src/test/java/com/iluwatar/eip/wiretap/routes/WireTapRouteTest.java
@@ -0,0 +1,62 @@
+package com.iluwatar.eip.wiretap.routes;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Message;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.SpringApplicationConfiguration;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test class for <i>WireTapRoute</i>.
+ * <p>
+ * In order for it to work we have to mock endpoints we want to read/write to. To mock those we need to substitute
+ * original endpoint names to mocks.
+ * </p>
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringApplicationConfiguration(classes = WireTapRouteTest.class)
+@ActiveProfiles("test")
+@EnableAutoConfiguration
+@ComponentScan
+public class WireTapRouteTest {
+
+  @EndpointInject(uri = "{{entry}}")
+  private ProducerTemplate entry;
+
+  @EndpointInject(uri = "{{endpoint}}")
+  private MockEndpoint endpoint;
+
+  @EndpointInject(uri = "{{wireTapEndpoint}}")
+  private MockEndpoint wireTapEndpoint;
+
+  /**
+   * Test if both endpoints receive exactly one message containing the same, unchanged body.
+   * @throws Exception in case of en exception during the test
+   */
+  @Test
+  @DirtiesContext
+  public void testWireTap() throws Exception {
+    entry.sendBody("TEST");
+
+    endpoint.expectedMessageCount(1);
+    wireTapEndpoint.expectedMessageCount(1);
+
+    endpoint.assertIsSatisfied();
+    wireTapEndpoint.assertIsSatisfied();
+
+    Message endpointIn = endpoint.getExchanges().get(0).getIn();
+    Message wireTapEndpointIn = wireTapEndpoint.getExchanges().get(0).getIn();
+
+    assertEquals("TEST", endpointIn.getBody());
+    assertEquals("TEST", wireTapEndpointIn.getBody());
+  }
+}