Discussion:
[mule-scm] [mule][24166] branches/mule-3.x: adding wildcard support to basic functionality elements
Daniel Feist
2012-03-25 00:58:07 UTC
Permalink
Shouldn't we reuse org.mule.routing.filters.WildcardFilter (or abstract logic in WildcardFilter into something common), rather than having this in two places?
Revision
24166
Author
pablo.lagreca
Date
2012-03-24 18:55:54 -0500 (Sat, 24 Mar 2012)
Log Message
adding wildcard support to basic functionality elements
Modified Paths
branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/AbstractRemoveVariablePropertyTransformer.java
branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/CopyAttachmentsTransformer.java
branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/CopyPropertiesTransformer.java
branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/RemoveAttachmentTransformer.java
branches/mule-3.x/tests/integration/src/test/java/org/mule/properties/AttachmentTransformerTestCase.java
branches/mule-3.x/tests/integration/src/test/java/org/mule/properties/MessagePropertyTransformerTestCase.java
branches/mule-3.x/tests/integration/src/test/resources/org/mule/properties/attachment-transformer-test-case.xml
branches/mule-3.x/tests/integration/src/test/resources/org/mule/properties/message-properties-transformer-test-case.xml
Added Paths
branches/mule-3.x/core/src/main/java/org/mule/util/WildcardAttributeEvaluator.java
branches/mule-3.x/core/src/test/java/org/mule/util/WildcardAttributeEvaluatorTestCase.java
Diff
Modified: branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/AbstractRemoveVariablePropertyTransformer.java (24165 => 24166)
--- branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/AbstractRemoveVariablePropertyTransformer.java 2012-03-24 20:19:52 UTC (rev 24165)
+++ branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/AbstractRemoveVariablePropertyTransformer.java 2012-03-24 23:55:54 UTC (rev 24166)
@@ -16,10 +16,12 @@
import org.mule.transformer.AbstractMessageTransformer;
import org.mule.transformer.types.DataTypeFactory;
import org.mule.util.AttributeEvaluator;
+import org.mule.util.WildcardAttributeEvaluator;
public abstract class AbstractRemoveVariablePropertyTransformer extends AbstractMessageTransformer
{
private AttributeEvaluator identifierEvaluator;
+ private WildcardAttributeEvaluator wildcardAttributeEvaluator;
public AbstractRemoveVariablePropertyTransformer()
{
@@ -35,10 +37,25 @@
}
@Override
- public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
+ public Object transformMessage(final MuleMessage message, String outputEncoding) throws TransformerException
{
- if (true)
+ if (wildcardAttributeEvaluator.hasWildcards())
{
+ wildcardAttributeEvaluator.processValues(message.getPropertyNames(getScope()),new WildcardAttributeEvaluator.MatchCallback()
+ {
+ public void processMatch(String matchedValue)
+ {
+ message.removeProperty(matchedValue,getScope());
+ if (logger.isDebugEnabled())
+ {
+ logger.debug(String.format("Removing property: '%s' from scope: '%s'", matchedValue, getScope().getScopeName()));
+ }
+ }
+ });
+ }
+ else
+ {
Object keyValue = identifierEvaluator.resolveValue(message);
if (keyValue != null)
{
@@ -49,21 +66,6 @@
logger.info("Key expression return null, no property will be removed");
}
}
-// else
-// {
-// final Set<String> propertyNames = new HashSet<String>(message.getPropertyNames(getScope()));
-// for (String key : propertyNames)
-// {
-// if (identifierEvaluator.matches(key))
-// {
-// if (logger.isDebugEnabled())
-// {
-// logger.debug(String.format("Removing property: '%s' from scope: '%s'", key, getScope().getScopeName()));
-// }
-// message.removeProperty(key, getScope());
-// }
-// }
-// }
return message;
}
@@ -82,6 +84,7 @@
throw new IllegalArgumentException("Remove with null identifier is not supported");
}
this.identifierEvaluator = new AttributeEvaluator(identifier);
+ this.wildcardAttributeEvaluator = new WildcardAttributeEvaluator(identifier);
}
public abstract PropertyScope getScope();
Modified: branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/CopyAttachmentsTransformer.java (24165 => 24166)
--- branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/CopyAttachmentsTransformer.java 2012-03-24 20:19:52 UTC (rev 24165)
+++ branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/CopyAttachmentsTransformer.java 2012-03-24 23:55:54 UTC (rev 24166)
@@ -10,17 +10,20 @@
package org.mule.transformer.simple;
import org.mule.api.MuleMessage;
+import org.mule.api.MuleRuntimeException;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
import org.mule.transformer.types.DataTypeFactory;
import org.mule.util.AttributeEvaluator;
+import org.mule.util.WildcardAttributeEvaluator;
import javax.activation.DataHandler;
public class CopyAttachmentsTransformer extends AbstractMessageTransformer
{
private AttributeEvaluator attachmentNameEvaluator;
+ private WildcardAttributeEvaluator wildcardAttachmentNameEvaluator;
public CopyAttachmentsTransformer()
{
@@ -36,26 +39,40 @@
}
@Override
- public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
+ public Object transformMessage(final MuleMessage message, String outputEncoding) throws TransformerException
{
try
{
- if (true)
+ if (wildcardAttachmentNameEvaluator.hasWildcards())
{
+ try
+ {
+ wildcardAttachmentNameEvaluator.processValues(message.getInboundAttachmentNames(),new WildcardAttributeEvaluator.MatchCallback()
+ {
+ public void processMatch(String matchedValue)
+ {
+ try
+ {
+ message.addOutboundAttachment(matchedValue,message.getInboundAttachment(matchedValue));
+ } catch (Exception e)
+ {
+ throw new MuleRuntimeException(e);
+ }
+ }
+ });
+ }
+ catch (Exception e)
+ {
+ throw new TransformerException(this,e);
+ }
+ }
+ else
+ {
String attachmentName = attachmentNameEvaluator.resolveValue(message).toString();
DataHandler inboundAttachment = message.getInboundAttachment(attachmentName);
message.addOutboundAttachment(attachmentName, inboundAttachment);
}
-// else
-// {
-// for (String inboundAttachmentName : message.getInboundAttachmentNames())
-// {
-// if (attachmentNameEvaluator.matches(inboundAttachmentName))
-// {
-// message.addOutboundAttachment(inboundAttachmentName,message.getInboundAttachment(inboundAttachmentName));
-// }
-// }
-// }
}
catch (Exception e)
{
@@ -75,6 +92,7 @@
public void setAttachmentName(String attachmentName)
{
this.attachmentNameEvaluator = new AttributeEvaluator(attachmentName);
+ this.wildcardAttachmentNameEvaluator = new WildcardAttributeEvaluator(attachmentName);
}
}
Modified: branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/CopyPropertiesTransformer.java (24165 => 24166)
--- branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/CopyPropertiesTransformer.java 2012-03-24 20:19:52 UTC (rev 24165)
+++ branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/CopyPropertiesTransformer.java 2012-03-24 23:55:54 UTC (rev 24166)
@@ -15,10 +15,12 @@
import org.mule.transformer.AbstractMessageTransformer;
import org.mule.transformer.types.DataTypeFactory;
import org.mule.util.AttributeEvaluator;
+import org.mule.util.WildcardAttributeEvaluator;
public class CopyPropertiesTransformer extends AbstractMessageTransformer
{
private AttributeEvaluator propertyNameEvaluator;
+ private WildcardAttributeEvaluator wildcardPropertyNameEvaluator;
public CopyPropertiesTransformer()
{
@@ -34,10 +36,21 @@
}
@Override
- public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
+ public Object transformMessage(final MuleMessage message, String outputEncoding) throws TransformerException
{
- if (true)
+ if (wildcardPropertyNameEvaluator.hasWildcards())
{
+ wildcardPropertyNameEvaluator.processValues(message.getInboundPropertyNames(),new WildcardAttributeEvaluator.MatchCallback()
+ {
+ public void processMatch(String matchedValue)
+ {
+ message.setOutboundProperty(matchedValue,message.getInboundProperty(matchedValue));
+ }
+ });
+ }
+ else
+ {
Object keyValue = propertyNameEvaluator.resolveValue(message);
if (keyValue != null)
{
@@ -57,16 +70,6 @@
logger.info("Key expression return null, no property will be copied");
}
}
-// else
-// {
-// for (String inboundPropertyName : message.getInboundPropertyNames())
-// {
-// if (propertyNameEvaluator.matches(inboundPropertyName))
-// {
-// message.setOutboundProperty(inboundPropertyName,message.getInboundProperty(inboundPropertyName));
-// }
-// }
-// }
return message;
}
@@ -85,6 +88,7 @@
throw new IllegalArgumentException("Null propertyName not supported");
}
this.propertyNameEvaluator = new AttributeEvaluator(propertyName);
+ this.wildcardPropertyNameEvaluator = new WildcardAttributeEvaluator(propertyName);
}
}
Modified: branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/RemoveAttachmentTransformer.java (24165 => 24166)
--- branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/RemoveAttachmentTransformer.java 2012-03-24 20:19:52 UTC (rev 24165)
+++ branches/mule-3.x/core/src/main/java/org/mule/transformer/simple/RemoveAttachmentTransformer.java 2012-03-24 23:55:54 UTC (rev 24166)
@@ -10,15 +10,18 @@
package org.mule.transformer.simple;
import org.mule.api.MuleMessage;
+import org.mule.api.MuleRuntimeException;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
import org.mule.transformer.types.DataTypeFactory;
import org.mule.util.AttributeEvaluator;
+import org.mule.util.WildcardAttributeEvaluator;
public class RemoveAttachmentTransformer extends AbstractMessageTransformer
{
private AttributeEvaluator nameEvaluator;
+ private WildcardAttributeEvaluator wildcardAttributeEvaluator;
public RemoveAttachmentTransformer()
{
@@ -34,12 +37,36 @@
}
@Override
- public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
+ public Object transformMessage(final MuleMessage message, String outputEncoding) throws TransformerException
{
try
{
- if (true)
+ if (wildcardAttributeEvaluator.hasWildcards())
{
+ try
+ {
+ wildcardAttributeEvaluator.processValues(message.getOutboundAttachmentNames(),new WildcardAttributeEvaluator.MatchCallback()
+ {
+ public void processMatch(String matchedValue)
+ {
+ try
+ {
+ message.removeOutboundAttachment(matchedValue);
+ } catch (Exception e)
+ {
+ throw new MuleRuntimeException(e);
+ }
+ }
+ });
+ }
+ catch (Exception e)
+ {
+ throw new TransformerException(this,e);
+ }
+ }
+ else
+ {
Object keyValue = nameEvaluator.resolveValue(message);
if (keyValue != null)
{
@@ -51,17 +78,6 @@
logger.info("Attachment key expression return null, no attachment will be removed");
}
}
-// else
-// {
-// final Set<String> attachmentNames = new HashSet<String>(message.getOutboundAttachmentNames());
-// for (String attachmentName : attachmentNames)
-// {
-// if (nameEvaluator.matches(attachmentName))
-// {
-// message.removeOutboundAttachment(attachmentName);
-// }
-// }
-// }
return message;
}
catch (Exception e)
@@ -81,6 +97,7 @@
public void setAttachmentName(String attachmentName)
{
this.nameEvaluator = new AttributeEvaluator(attachmentName);
+ this.wildcardAttributeEvaluator = new WildcardAttributeEvaluator(attachmentName);
}
}
Added: branches/mule-3.x/core/src/main/java/org/mule/util/WildcardAttributeEvaluator.java (0 => 24166)
--- branches/mule-3.x/core/src/main/java/org/mule/util/WildcardAttributeEvaluator.java (rev 0)
+++ branches/mule-3.x/core/src/main/java/org/mule/util/WildcardAttributeEvaluator.java 2012-03-24 23:55:54 UTC (rev 24166)
@@ -0,0 +1,80 @@
+/*
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+package org.mule.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+public class WildcardAttributeEvaluator
+{
+ private String attributeValue;
+ private String escapedValue;
+ private Boolean hasWildcards;
+
+ public WildcardAttributeEvaluator(String attributeValue)
+ {
+ if (attributeValue == null)
+ {
+ throw new IllegalArgumentException("null not allowed");
+ }
+ this.attributeValue = attributeValue;
+ this.escapedValue = attributeValue.replaceAll("\\*","*");
+ hasWildcards = attributeValue.startsWith("*") || (attributeValue.endsWith("*") && !attributeValue.endsWith("\\*"))|| attributeValue.equals("*");
+ }
+
+ public boolean hasWildcards()
+ {
+ return hasWildcards;
+ }
+
+ public void processValues(Collection<String> values, MatchCallback matchCallback)
+ {
+ if (!hasWildcards())
+ {
+ throw new IllegalStateException("Can't call processValues with non wildcard attribute");
+ }
+ String[] valuesArray = values.toArray(new String[values.size()]);
+ for (String value : valuesArray)
+ {
+ if (matches(value))
+ {
+ matchCallback.processMatch(value);
+ }
+ }
+ }
+
+ private boolean matches(String value)
+ {
+ if (value == null)
+ {
+ return false;
+ }
+ if (escapedValue.equals("*"))
+ {
+ return true;
+ }
+ else if (escapedValue.startsWith("*"))
+ {
+ return value.endsWith(escapedValue.substring(1, escapedValue.length()));
+ }
+ else if (escapedValue.endsWith("*"))
+ {
+ return value.startsWith(escapedValue.substring(0, escapedValue.length()-1));
+ }
+ return false;
+ }
+
+ public interface MatchCallback
+ {
+ public void processMatch(String matchedValue);
+ }
+}
Property changes on: branches/mule-3.x/core/src/main/java/org/mule/util/WildcardAttributeEvaluator.java
___________________________________________________________________
Added: svn:keywords
Added: svn:eol-style
Added: branches/mule-3.x/core/src/test/java/org/mule/util/WildcardAttributeEvaluatorTestCase.java (0 => 24166)
--- branches/mule-3.x/core/src/test/java/org/mule/util/WildcardAttributeEvaluatorTestCase.java (rev 0)
+++ branches/mule-3.x/core/src/test/java/org/mule/util/WildcardAttributeEvaluatorTestCase.java 2012-03-24 23:55:54 UTC (rev 24166)
@@ -0,0 +1,89 @@
+/*
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+package org.mule.util;
+
+import static org.junit.Assert.assertThat;
+import org.mule.tck.junit4.AbstractMuleTestCase;
+import org.mule.tck.size.SmallTest;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.hamcrest.core.Is;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class WildcardAttributeEvaluatorTestCase extends AbstractMuleTestCase
+{
+ public void testStartsWithWildcard()
+ {
+ List<String> testValues = Arrays.asList("MULE", "MULEMAN", "EMULE","MULE\\*","\\*MULE");
+ List<String> expectedValues = Arrays.asList("MULE", "MULEMAN","MULE\\*");
+ testScenario("MULE*", testValues,expectedValues);
+ }
+
+ public void testEndsWithWildcard()
+ {
+ List<String> testValues = Arrays.asList("MULE", "EMULE", "MAN-MULE-MAN","\\*MULE","MULE\\*");
+ List<String> expectedValues = Arrays.asList("MULE", "EMULE","\\*MULE");
+ testScenario("*MULE", testValues,expectedValues);
+ }
+
+ public void testAllWildcard()
+ {
+ List<String> testValues = Arrays.asList("MULE", "EMULE", "MAN-MULE-MAN","\\*MULE","MULE\\*");
+ List<String> expectedValues = Arrays.asList("MULE", "EMULE", "MAN-MULE-MAN","\\*MULE","MULE\\*");
+ testScenario("*", testValues,expectedValues);
+ }
+
+ public void testWithEscapedCharactersOnly()
+ {
+ WildcardAttributeEvaluator wildcardAttributeEvaluator = new WildcardAttributeEvaluator("\\*");
+ Assert.assertThat(wildcardAttributeEvaluator.hasWildcards(),Is.is(false));
+ }
+
+ public void testWithEscapedCharactersAndWildcards()
+ {
+ List<String> testValues = Arrays.asList("\\*MULE", "EMULE", "MAN-MULE-MAN","","MULE\\*","\\*MULE\\*");
+ List<String> expectedValues = Arrays.asList("\\*MULE", "\\*MULE\\*");
+ testScenario("\\*MULE*",testValues, expectedValues);
+ }
+
+ public void testCallConstructorWithNull()
+ {
+ new WildcardAttributeEvaluator(null);
+ }
+
+ private void testScenario(String attributeValue, final List<String> testValues, final List<String> expectedValues)
+ {
+ WildcardAttributeEvaluator wildcardAttributeEvaluator = new WildcardAttributeEvaluator(attributeValue);
+ assertThat(wildcardAttributeEvaluator.hasWildcards(), Is.is(true));
+ final List<String> resultingValues = new ArrayList<String>();
+ wildcardAttributeEvaluator.processValues(testValues, new WildcardAttributeEvaluator.MatchCallback()
+ {
+ public void processMatch(String matchedValue)
+ {
+ resultingValues.add(matchedValue);
+ }
+ });
+ assertThat(expectedValues.size(),Is.is(resultingValues.size()));
+ for (String expectedValue : expectedValues)
+ {
+ assertThat(resultingValues.contains(expectedValue),Is.is(true));
+ }
+ }
+}
Property changes on: branches/mule-3.x/core/src/test/java/org/mule/util/WildcardAttributeEvaluatorTestCase.java
___________________________________________________________________
Added: svn:keywords
Added: svn:eol-style
Modified: branches/mule-3.x/tests/integration/src/test/java/org/mule/properties/AttachmentTransformerTestCase.java (24165 => 24166)
--- branches/mule-3.x/tests/integration/src/test/java/org/mule/properties/AttachmentTransformerTestCase.java 2012-03-24 20:19:52 UTC (rev 24165)
+++ branches/mule-3.x/tests/integration/src/test/java/org/mule/properties/AttachmentTransformerTestCase.java 2012-03-24 23:55:54 UTC (rev 24166)
@@ -92,27 +92,18 @@
}
@Test
public void testCopyAttachments() throws Exception
{
runScenario("copyAttachments");
}
@Test
public void testCopyAttachmentsWithWildcard() throws Exception
{
runScenario("copyAttachmentsWithWildcard");
}
- public void testCopyAttachmentsWithRegex() throws Exception
- {
- runScenario("copyAttachmentsWithRegex");
- }
-
public void runScenario(String flowName) throws Exception
{
DataHandler attach = new DataHandler("attachContent","plain/text");
Modified: branches/mule-3.x/tests/integration/src/test/java/org/mule/properties/MessagePropertyTransformerTestCase.java (24165 => 24166)
--- branches/mule-3.x/tests/integration/src/test/java/org/mule/properties/MessagePropertyTransformerTestCase.java 2012-03-24 20:19:52 UTC (rev 24165)
+++ branches/mule-3.x/tests/integration/src/test/java/org/mule/properties/MessagePropertyTransformerTestCase.java 2012-03-24 23:55:54 UTC (rev 24166)
@@ -75,12 +75,6 @@
}
@Test
- public void testCopyPropertiesUsingRegex() throws Exception
- {
- runScenario("copyPropertiesUsingRegex");
- }
-
public void testCopyAllProperties() throws Exception
{
runScenario("copyAllProperties");
Modified: branches/mule-3.x/tests/integration/src/test/resources/org/mule/properties/attachment-transformer-test-case.xml (24165 => 24166)
--- branches/mule-3.x/tests/integration/src/test/resources/org/mule/properties/attachment-transformer-test-case.xml 2012-03-24 20:19:52 UTC (rev 24165)
+++ branches/mule-3.x/tests/integration/src/test/resources/org/mule/properties/attachment-transformer-test-case.xml 2012-03-24 23:55:54 UTC (rev 24166)
@@ -130,11 +130,4 @@
<test:assert expression="#[groovy:message.getOutboundAttachment('attach22') != null]"/>
</flow>
- <flow name="copyAttachmentsWithRegex" processingStrategy="synchronous">
- <copy-attachments attachmentName="^2$"/>
- <test:assert expression="#[groovy:message.getOutboundAttachment('attach') != null]"/>
- <test:assert expression="#[groovy:message.getOutboundAttachment('attach2') == null]"/>
- <test:assert expression="#[groovy:message.getOutboundAttachment('attach22') == null]"/>
- </flow>
-
</mule>
Modified: branches/mule-3.x/tests/integration/src/test/resources/org/mule/properties/message-properties-transformer-test-case.xml (24165 => 24166)
--- branches/mule-3.x/tests/integration/src/test/resources/org/mule/properties/message-properties-transformer-test-case.xml 2012-03-24 20:19:52 UTC (rev 24165)
+++ branches/mule-3.x/tests/integration/src/test/resources/org/mule/properties/message-properties-transformer-test-case.xml 2012-03-24 23:55:54 UTC (rev 24166)
@@ -94,14 +94,6 @@
<test:assert expression="#[groovy:message.getOutboundProperty('inbPropKey').equals('inbPropValue')]"/>
</flow>
- <flow name="copyPropertiesUsingRegex">
- <transformer ref="addInboundProperty"/>
- <transformer ref="addInboundProperty2"/>
- <copy-properties propertyName=".*2$"/>
- <test:assert expression="#[groovy:message.getOutboundProperty('inbPropKey') == null]"/>
- <test:assert expression="#[groovy:message.getOutboundProperty('inbPropKey2').equals('inbPropValue2')]"/>
- </flow>
-
<flow name="copyAllProperties">
<transformer ref="addInboundProperty"/>
<transformer ref="addInboundProperty2"/>
http://xircles.codehaus.org/manage_email
Loading...