Hi

some days ago I asked for an implementation of the Content-Enricher pattern on the ServiceMix user list. (http://www.enterpriseintegrationpatterns.com/DataEnricher.html). But I never received any reply to that mail. So I started to implement that pattern and attached the first version as patch file to this mail.

Hopefully someone could look inside and give me feedback if such an implementation is interesting for ServiceMix. ;-)

Kristian
Index: 
D:/work/UrlaubsIntegration/servicemix-eip/src/test/java/org/apache/servicemix/eip/ContentEnricherTest.java
===================================================================
--- 
D:/work/UrlaubsIntegration/servicemix-eip/src/test/java/org/apache/servicemix/eip/ContentEnricherTest.java
  (revision 0)
+++ 
D:/work/UrlaubsIntegration/servicemix-eip/src/test/java/org/apache/servicemix/eip/ContentEnricherTest.java
  (revision 0)
@@ -0,0 +1,75 @@
+/*
+ * 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.servicemix.eip;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.namespace.QName;
+import javax.xml.transform.dom.DOMSource;
+
+import org.apache.servicemix.eip.patterns.ContentEnricher;
+import org.apache.servicemix.jbi.util.DOMUtil;
+import org.apache.servicemix.tck.ReceiverComponent;
+
+public class ContentEnricherTest extends AbstractEIPTest {
+       
+    protected ContentEnricher enricher;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        enricher = new ContentEnricher();
+        enricher.setEnricherTarget(
+                       createServiceExchangeTarget(new 
QName("enricherTarget")));
+        enricher.setTarget(
+                       createServiceExchangeTarget(new QName("target")));
+        
+        configurePattern(enricher);
+        activateComponent(enricher, "enricher");
+    }
+    
+    public void testInOnly() throws Exception {
+
+       activateComponent(new ReturnMockComponent("<halloMock/>"), 
+                       "enricherTarget");
+
+       ReceiverComponent rec = activateReceiver("target");
+       
+        InOnly me = client.createInOnlyExchange();
+                
+        me.setService(new QName("enricher"));
+        me.getInMessage().setContent(createSource("<hello/>"));
+        client.sendSync(me);
+
+        assertEquals(ExchangeStatus.DONE, me.getStatus());
+                        
+        assertEquals(1, rec.getMessageList().getMessageCount());
+        
+        NormalizedMessage object = 
+               (NormalizedMessage) rec.getMessageList().getMessages().get(0);
+        
+        DOMSource domSource = (DOMSource) object.getContent();
+        
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+                       "<enricher><request><hello/></request><result>" +
+                       "<halloMock/></result></enricher>", 
+                       DOMUtil.asXML(domSource.getNode()));
+                
+    }
+    
+}
Index: 
D:/work/UrlaubsIntegration/servicemix-eip/src/main/java/org/apache/servicemix/eip/patterns/ContentEnricher.java
===================================================================
--- 
D:/work/UrlaubsIntegration/servicemix-eip/src/main/java/org/apache/servicemix/eip/patterns/ContentEnricher.java
     (revision 0)
+++ 
D:/work/UrlaubsIntegration/servicemix-eip/src/main/java/org/apache/servicemix/eip/patterns/ContentEnricher.java
     (revision 0)
@@ -0,0 +1,154 @@
+package org.apache.servicemix.eip.patterns;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOut;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+
+import org.apache.servicemix.eip.EIPEndpoint;
+import org.apache.servicemix.eip.support.ExchangeTarget;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.util.MessageUtil;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * Implementation of the 
+ * <a 
href="http://www.enterpriseintegrationpatterns.com/DataEnricher.html";>'Content-Enricher'</a>
 
+ * Pattern. 
+ *  
+ * @org.apache.xbean.XBean element="content-enricher"
+ *                  description="A Content Enricher"
+ */
+public class ContentEnricher extends EIPEndpoint {
+
+    /**
+     * The address of the target endpoint
+     */
+    private ExchangeTarget target;
+    
+    /**
+     * the target to enrich the request
+     */
+    private ExchangeTarget enricherTarget;
+    
+    @Override
+    protected void processAsync(MessageExchange exchange) throws Exception {
+        throw new IllegalStateException();
+    }
+
+    @Override
+    protected void processSync(MessageExchange exchange) throws Exception {
+        throw new IllegalStateException();
+    }
+
+    @Override
+    public void process(MessageExchange exchange) throws Exception {
+               
+        // Skip done exchanges
+        if (exchange.getStatus() == ExchangeStatus.DONE) {
+            return;
+        // Handle error exchanges
+        } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
+            return;
+        }
+                       
+       InOut enricherTargetME = exchangeFactory.createInOutExchange();
+        enricherTarget.configureTarget(enricherTargetME, getContext());
+        MessageUtil.transferInToIn(exchange, enricherTargetME);
+                
+        sendSync(enricherTargetME);
+
+        if (enricherTargetME.getStatus() == ExchangeStatus.ERROR) {
+            fail(exchange, enricherTargetME.getError());
+            return;
+        }
+     
+        Document document = combineToDOMDocument(exchange.getMessage("in"), 
+                       enricherTargetME.getMessage("out"));
+                
+        done(enricherTargetME);
+
+       MessageExchange outExchange = 
+               getExchangeFactory().createInOnlyExchange();
+       NormalizedMessage out = outExchange.createMessage();
+       target.configureTarget(outExchange, getContext());
+        out.setContent(new DOMSource(document));
+        
+        outExchange.setMessage(out, "in");
+                
+        sendSync(outExchange);
+        done(exchange);
+        
+    }
+
+    /**
+     * Combines two NormalizedMessages to one DOM Document.
+     * 
+     * Example:
+     *    Content of Message1 :
+     *    
+     *        <hello/>
+     *        
+     *    Content of Message 2:
+     *    
+     *           <message2/>
+     * 
+     *    Result of this method a DOM Document containing the following:
+     *    
+     *    <enricher>
+     *      <request>
+     *         <hello/>
+     *      </request>
+     *      <result>
+     *         <message2/>
+     *      </result>
+     *    </enricher>
+     * 
+     */
+       private Document combineToDOMDocument(NormalizedMessage requestMessage, 
NormalizedMessage targetResultMessage) 
+               throws Exception, ParserConfigurationException {
+               
+        Node originalDocumentNode = getDOMNode(requestMessage.getContent());
+        Node targetResultNode = getDOMNode(targetResultMessage.getContent());
+                
+        Document document = new SourceTransformer().createDocument();
+        Element enricherElement = document.createElement("enricher");
+        Element requestElement = document.createElement("request");
+        
+        Node node = document.importNode(originalDocumentNode, true);
+        requestElement.appendChild(node);
+        enricherElement.appendChild(requestElement);
+        document.appendChild(enricherElement);
+        
+        Element resultElement = document.createElement("result");
+        
+        Node node2 = document.importNode(targetResultNode, true);
+        
+        resultElement.appendChild(node2);
+        enricherElement.appendChild(resultElement);
+               return document;
+               
+       }
+    
+    private Node getDOMNode(Source source) throws Exception {
+       SourceTransformer sourceTransformer = new SourceTransformer();
+       Node node = sourceTransformer.toDOMNode(source);
+        if (node.getNodeType() == Node.DOCUMENT_NODE) {
+               node = ((Document)node).getDocumentElement();
+        }
+        return node;
+    }
+        
+    public void setTarget(ExchangeTarget target) {
+        this.target = target;
+    }
+    
+    public void setEnricherTarget(ExchangeTarget enricherTarget) {
+        this.enricherTarget = enricherTarget;
+    }
+}

Reply via email to