servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r534395 - in /incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix: expression/JAXPXPathExpression.java expression/JaxenXPathExpression.java jbi/util/MessageUtil.java
Date Wed, 02 May 2007 11:43:07 GMT
Author: gnodet
Date: Wed May  2 04:43:06 2007
New Revision: 534395

URL: http://svn.apache.org/viewvc?view=rev&rev=534395
Log:
SM-902: File Binding example error with JaxenStringXPathExpression

Modified:
    incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathExpression.java
    incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JaxenXPathExpression.java
    incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/MessageUtil.java

Modified: incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathExpression.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathExpression.java?view=diff&rev=534395&r1=534394&r2=534395
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathExpression.java
(original)
+++ incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathExpression.java
Wed May  2 04:43:06 2007
@@ -17,6 +17,8 @@
 package org.apache.servicemix.expression;
 
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.util.MessageUtil;
 import org.springframework.beans.factory.InitializingBean;
 import org.xml.sax.SAXException;
 
@@ -43,6 +45,7 @@
  */
 public class JAXPXPathExpression implements Expression, InitializingBean {
     private String xpath;
+    private boolean useMessageContent = true;
     private SourceTransformer transformer = new SourceTransformer();
     private MessageVariableResolver variableResolver = new MessageVariableResolver();
     private XPathExpression xPathExpression;
@@ -124,6 +127,22 @@
     public void setXPath(String xpath) {
         this.xpath = xpath;
     }
+    
+    public boolean isUseMessageContent() {
+        return useMessageContent;
+    }
+    
+    /**
+     * Specifies whether or not the XPath expression uses the message content.  
+     * 
+     * By default, this property is <code>true</code>, but you can set it to
<code>false</code> to avoid that the message content
+     * is converted to {@link StringSource}  
+     * 
+     * @param useMessageContent specify <code>false</code> if this expression
does not access the message content
+     */
+    public void setUseMessageContent(boolean useMessageContent) {
+        this.useMessageContent = useMessageContent;
+    }
 
     public SourceTransformer getTransformer() {
         return transformer;
@@ -176,6 +195,10 @@
     }
 
     protected Object getXMLNode(MessageExchange exchange, NormalizedMessage message) throws
TransformerException, MessagingException, ParserConfigurationException, IOException, SAXException
{
+        //ensure re-readability of the content if the expression also needs to access the
content 
+        if (useMessageContent) {
+            MessageUtil.enableContentRereadability(message);
+        }
         return transformer.toDOMNode(message);
     }
 }

Modified: incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JaxenXPathExpression.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JaxenXPathExpression.java?view=diff&rev=534395&r1=534394&r2=534395
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JaxenXPathExpression.java
(original)
+++ incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JaxenXPathExpression.java
Wed May  2 04:43:06 2007
@@ -19,6 +19,8 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.util.MessageUtil;
 import org.jaxen.FunctionContext;
 import org.jaxen.JaxenException;
 import org.jaxen.NamespaceContext;
@@ -46,6 +48,7 @@
     private static final transient Log log = LogFactory.getLog(JaxenXPathExpression.class);
     
     private String xpath;
+    private boolean useMessageContent = true;
     private SourceTransformer transformer = new SourceTransformer();
     private JaxenVariableContext variableContext = new JaxenVariableContext();
     private XPath xpathObject;
@@ -156,6 +159,22 @@
         this.xpath = xpath;
     }
 
+    public boolean isUseMessageContent() {
+        return useMessageContent;
+    }
+
+    /**
+     * Specifies whether or not the XPath expression uses the message content.  
+     * 
+     * By default, this property is <code>true</code>, but you can set it to
<code>false</code> to avoid that the message content
+     * is converted to {@link StringSource}  
+     * 
+     * @param useMessageContent specify <code>false</code> if this expression
does not access the message content
+     */
+    public void setUseMessageContent(boolean useMessageContent) {
+        this.useMessageContent = useMessageContent;
+    }
+
     public SourceTransformer getTransformer() {
         return transformer;
     }
@@ -205,6 +224,10 @@
 
     protected Object getXMLNode(MessageExchange exchange, NormalizedMessage message) throws
TransformerException, MessagingException, ParserConfigurationException, IOException, SAXException
{
         Node node = null;
+        //ensure re-readability of the content if the expression also needs to access the
content 
+        if (useMessageContent) {
+            MessageUtil.enableContentRereadability(message);
+        }
         if (message != null) {
             node = transformer.toDOMNode(message);
         }

Modified: incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/MessageUtil.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/MessageUtil.java?view=diff&rev=534395&r1=534394&r2=534395
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/MessageUtil.java
(original)
+++ incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/MessageUtil.java
Wed May  2 04:43:06 2007
@@ -17,6 +17,7 @@
 package org.apache.servicemix.jbi.util;
 
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.Serializable;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -30,12 +31,16 @@
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
 import javax.security.auth.Subject;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.stream.StreamSource;
 
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
 import org.apache.servicemix.jbi.jaxp.StringSource;
 import org.apache.servicemix.jbi.util.ByteArrayDataSource;
 import org.apache.servicemix.jbi.util.FileUtil;
+import org.xml.sax.SAXException;
 
 /**
  * @author gnodet
@@ -55,7 +60,7 @@
         }
         dest.setSecuritySubject(source.getSecuritySubject());
     }
-    
+
     public static NormalizedMessage copy(NormalizedMessage source) throws MessagingException
{
         if (source instanceof Fault) {
             return new FaultImpl((Fault) source);
@@ -63,76 +68,104 @@
             return new NormalizedMessageImpl(source);
         }
     }
-    
+
     public static NormalizedMessage copyIn(MessageExchange exchange) throws MessagingException
{
         return copy(exchange.getMessage("in"));
     }
-    
+
     public static NormalizedMessage copyOut(MessageExchange exchange) throws MessagingException
{
         return copy(exchange.getMessage("out"));
     }
-    
+
     public static Fault copyFault(MessageExchange exchange) throws MessagingException {
         return (Fault) copy(exchange.getMessage("fault"));
     }
-    
+
     public static void transferInToIn(MessageExchange source, MessageExchange dest) throws
MessagingException {
         transferToIn(source.getMessage("in"), dest);
     }
-    
+
     public static void transferOutToIn(MessageExchange source, MessageExchange dest) throws
MessagingException {
         transferToIn(source.getMessage("out"), dest);
     }
-    
+
     public static void transferToIn(NormalizedMessage sourceMsg, MessageExchange dest) throws
MessagingException {
         transferTo(sourceMsg, dest, "in");
     }
-    
+
     public static void transferOutToOut(MessageExchange source, MessageExchange dest) throws
MessagingException {
         transferToOut(source.getMessage("out"), dest);
     }
-    
+
     public static void transferInToOut(MessageExchange source, MessageExchange dest) throws
MessagingException {
         transferToOut(source.getMessage("in"), dest);
     }
-    
+
     public static void transferToOut(NormalizedMessage sourceMsg, MessageExchange dest) throws
MessagingException {
         transferTo(sourceMsg, dest, "out");
     }
-    
+
     public static void transferFaultToFault(MessageExchange source, MessageExchange dest)
throws MessagingException {
         transferToFault(source.getFault(), dest);
     }
-    
+
     public static void transferToFault(Fault fault, MessageExchange dest) throws MessagingException
{
         transferTo(fault, dest, "fault");
     }
-    
-    public static void transferTo(NormalizedMessage sourceMsg, MessageExchange dest, String
name) throws MessagingException {
+
+    public static void transferTo(NormalizedMessage sourceMsg, MessageExchange dest, String
name)
+                    throws MessagingException {
         NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest.createFault() : dest.createMessage();
         transfer(sourceMsg, destMsg);
         dest.setMessage(destMsg, name);
     }
-    
+
     public static void transferTo(MessageExchange source, MessageExchange dest, String name)
throws MessagingException {
         NormalizedMessage sourceMsg = source.getMessage(name);
         NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest.createFault() : dest.createMessage();
         transfer(sourceMsg, destMsg);
         dest.setMessage(destMsg, name);
     }
-    
+
+    /**
+     * Convert the given {@link NormalizedMessage} instance's content to a re-readable {@link
Source} This allows the
+     * content to be read more than once (e.g. for XPath evaluation or auditing).
+     * 
+     * @param message
+     *            the {@link NormalizedMessage} to convert the content for
+     * @throws MessagingException
+     */
+    public static void enableContentRereadability(NormalizedMessage message) throws MessagingException
{
+        if (message.getContent() instanceof StreamSource) {
+            try {
+                String content = new SourceTransformer().contentToString(message);
+                if (content != null) {
+                    message.setContent(new StringSource(content));
+                }
+            } catch (TransformerException e) {
+                throw new MessagingException("Unable to convert message content into StringSource",
e);
+            } catch (ParserConfigurationException e) {
+                throw new MessagingException("Unable to convert message content into StringSource",
e);
+            } catch (IOException e) {
+                throw new MessagingException("Unable to convert message content into StringSource",
e);
+            } catch (SAXException e) {
+                throw new MessagingException("Unable to convert message content into StringSource",
e);
+            }
+        }
+    }
+
     public static class NormalizedMessageImpl implements NormalizedMessage, Serializable
{
 
         private static final long serialVersionUID = -5813947566001096708L;
-        
+
         private Subject subject;
         private Source content;
         private Map properties = new HashMap();
         private Map attachments = new HashMap();
-        
+
         public NormalizedMessageImpl() {
         }
-        
+
         public NormalizedMessageImpl(NormalizedMessage message) throws MessagingException
{
             try {
                 String str = new SourceTransformer().contentToString(message);
@@ -163,7 +196,7 @@
                 throw new MessagingException(e);
             }
         }
-        
+
         public void addAttachment(String id, DataHandler content) throws MessagingException
{
             this.attachments.put(id, content);
         }
@@ -207,18 +240,18 @@
         public Subject getSecuritySubject() {
             return this.subject;
         }
-        
+
     }
-    
+
     public static class FaultImpl extends NormalizedMessageImpl implements Fault {
         private static final long serialVersionUID = -6076815664102825860L;
 
         public FaultImpl() {
         }
-        
+
         public FaultImpl(Fault fault) throws MessagingException {
             super(fault);
         }
     }
-    
+
 }



Mime
View raw message