[jboss-svn-commits] JBL Code SVN: r19629 - in labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta: src/org/jboss/soa/esb/notification and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Apr 18 12:14:10 EDT 2008
Author: tcunning
Date: 2008-04-18 12:14:10 -0400 (Fri, 18 Apr 2008)
New Revision: 19629
Added:
labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/src/org/jboss/internal/soa/esb/notification/PropertySubstituter.java
labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/tests/src/org/jboss/soa/esb/notification/PropertySubstituterUnitTest.java
Modified:
labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/src/org/jboss/soa/esb/notification/NotifyFTP.java
Log:
bug:JBESB-1589
Add Object Mapper substitution.
Added: labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/src/org/jboss/internal/soa/esb/notification/PropertySubstituter.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/src/org/jboss/internal/soa/esb/notification/PropertySubstituter.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/src/org/jboss/internal/soa/esb/notification/PropertySubstituter.java 2008-04-18 16:14:10 UTC (rev 19629)
@@ -0,0 +1,64 @@
+package org.jboss.internal.soa.esb.notification;
+
+import java.net.URI;
+
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.mapping.ObjectMapper;
+import org.jboss.soa.esb.message.mapping.ObjectMappingException;
+
+public class PropertySubstituter {
+ public static String replaceArguments (String field, Message message) {
+ String current = field;
+ boolean foundReplacement = false;
+ String replaced = replaceArgument(field, message);
+ while (!current.equals(replaced)) {
+ current = replaced;
+ foundReplacement = true;
+ replaced = replaceArgument(current, message);
+ }
+
+ // See if the filename is an object in the message - if it is a
+ // String in the message, then we can use that value
+ ObjectMapper om = new ObjectMapper();
+ if (!foundReplacement) {
+ try {
+ Object obj = om.getObjectFromMessage(message, field);
+ if (obj != null) {
+ if (obj instanceof String) {
+ replaced = (String) obj;
+ } else if (obj instanceof URI) {
+ replaced = obj.toString();
+ }
+ }
+ } catch (ObjectMappingException e) {
+ } catch (Exception e) {
+ }
+ }
+ return replaced;
+ }
+
+ /**
+ * Look for arguments in the form '{prop.name}' and replace them with
+ * corresponding message properties.
+ *
+ * @param value
+ * @param message
+ * @return String
+ */
+ protected static String replaceArgument(String value, Message message) {
+ int startIndex = value.indexOf('{');
+ if (startIndex == -1) {
+ return value;
+ }
+ int endIndex = value.indexOf('}');
+ if (endIndex == -1) {
+ return value;
+ }
+ String propName = value.substring(startIndex + 1, endIndex);
+ Object propValue = message.getProperties().getProperty(propName);
+ if (propValue == null) {
+ return value;
+ }
+ return value.substring(0, startIndex) + propValue + value.substring(endIndex + 1);
+ }
+}
Modified: labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/src/org/jboss/soa/esb/notification/NotifyFTP.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/src/org/jboss/soa/esb/notification/NotifyFTP.java 2008-04-18 16:09:06 UTC (rev 19628)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/src/org/jboss/soa/esb/notification/NotifyFTP.java 2008-04-18 16:14:10 UTC (rev 19629)
@@ -29,6 +29,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
+import org.jboss.internal.soa.esb.notification.PropertySubstituter;
import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.addressing.eprs.FTPEpr;
import org.jboss.soa.esb.helpers.ConfigTree;
@@ -114,55 +115,12 @@
if (StringUtils.isEmpty(fileName)) {
throw new NotificationException("NotifyFTP: Filename attribute is required.");
}
- fileName = replaceArguments(fileName, message);
+ fileName = PropertySubstituter.replaceArguments(fileName, message);
}
return fileName;
}
/**
- * Replaces any arguments in the form '{prop.name}' with corresponding
- * message property values.
- *
- * @param value
- * @param message
- * @return String
- */
- protected String replaceArguments(String value, Message message) {
- String current = value;
- String replaced = replaceArgument(value, message);
- while (!current.equals(replaced)) {
- current = replaced;
- replaced = replaceArgument(current, message);
- }
- return replaced;
- }
-
- /**
- * Look for arguments in the form '{prop.name}' and replace them with
- * corresponding message properties.
- *
- * @param value
- * @param message
- * @return String
- */
- protected String replaceArgument(String value, Message message) {
- int startIndex = value.indexOf('{');
- if (startIndex == -1) {
- return value;
- }
- int endIndex = value.indexOf('}');
- if (endIndex == -1) {
- return value;
- }
- String propName = value.substring(startIndex + 1, endIndex);
- Object propValue = message.getProperties().getProperty(propName);
- if (propValue == null) {
- return value;
- }
- return value.substring(0, startIndex) + propValue + value.substring(endIndex + 1);
- }
-
- /**
* Builds an FTP EPR from the configuration data.
*
* @return FTPEpr
Added: labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/tests/src/org/jboss/soa/esb/notification/PropertySubstituterUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/tests/src/org/jboss/soa/esb/notification/PropertySubstituterUnitTest.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_FP1/product/rosetta/tests/src/org/jboss/soa/esb/notification/PropertySubstituterUnitTest.java 2008-04-18 16:14:10 UTC (rev 19629)
@@ -0,0 +1,70 @@
+package org.jboss.soa.esb.notification;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Hashtable;
+
+import junit.framework.TestCase;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.jboss.soa.esb.message.format.MessageType;
+
+import org.jboss.internal.soa.esb.notification.PropertySubstituter;
+
+public class PropertySubstituterUnitTest extends TestCase {
+ private static final String FILE_NAME = "foo.txt";
+ private static final String NAME_TWO = "two.csv";
+ private static final String MESSAGE_ID = "urn:1234";
+
+ private Message createMessage() {
+ Message msg = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
+ assertNotNull("Message is null", msg);
+
+ try {
+ msg.getHeader().getCall().setMessageID(new URI(MESSAGE_ID));
+ msg.getAttachment().addItemAt(0, FILE_NAME);
+ } catch (URISyntaxException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return msg;
+ }
+
+ public void testSubstitution() {
+ Message msg = createMessage();
+ msg.getProperties().setProperty("filename", FILE_NAME);
+ String foo = PropertySubstituter.replaceArguments("{filename}", msg);
+ assertEquals("Property substitution should have changed "+ foo +" to " + FILE_NAME, foo, FILE_NAME);
+
+ String notfound = PropertySubstituter.replaceArguments("{notfound}", msg);
+ assertEquals("Result of property substitution for " + foo + " should be the original value, not " + notfound, "{notfound}", notfound);
+
+ msg.getBody().add("filename", NAME_TWO);
+ String result = PropertySubstituter.replaceArguments("body.filename", msg);
+ assertEquals("Could not find the string in the body of the message at location body.filename", result, NAME_TWO);
+
+ msg.getBody().add("hashtable", new Hashtable());
+ String hashResult = PropertySubstituter.replaceArguments("body.hashtable", msg);
+ assertEquals(hashResult, "body.hashtable");
+
+ String propFromObjectMapper = PropertySubstituter.replaceArguments("properties.filename", msg);
+ assertEquals("Could not find property from ObjectMapper.properties - expected " + FILE_NAME + ", found " + propFromObjectMapper,
+ propFromObjectMapper, FILE_NAME);
+
+ String propNotInObjectMapper = PropertySubstituter.replaceArguments("properties.notfound", msg);
+ assertEquals("Property substitution for " + propNotInObjectMapper + " should find no property on the message", "properties.notfound", propNotInObjectMapper);
+
+ String header = PropertySubstituter.replaceArguments("header.call.MessageID", msg);
+ assertEquals("Could not find MessageID", header, MESSAGE_ID);
+
+ String notFoundHeader = PropertySubstituter.replaceArguments("header.call.NotFound", msg);
+ assertEquals("Could not find MessageID", notFoundHeader, "header.call.NotFound");
+
+ String attachment = PropertySubstituter.replaceArguments("attachment.call.MessageID", msg);
+ assertEquals("Could not find MessageID", header, MESSAGE_ID);
+
+ String notFoundAttachment = PropertySubstituter.replaceArguments("attachment.call.NotFound", msg);
+ assertEquals("Could not find MessageID", notFoundAttachment, "attachment.call.NotFound");
+ }
+}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list