From 76f646fc7d3d7c05a174ad7b24471d5811c02b7a Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Sat, 30 Nov 2024 09:24:26 +0530 Subject: [PATCH 1/6] handle resource file key in xslt,jsontransform,payload,script,validate,xquery mediators --- .../org/apache/synapse/mediators/Utils.java | 17 ++++ .../builtin/JSONTransformMediator.java | 6 +- .../mediators/builtin/ValidateMediator.java | 10 ++- .../transform/PayloadFactoryMediator.java | 7 +- .../mediators/transform/XSLTMediator.java | 19 ++-- .../builtin/ValidateMediatorTest.java | 18 ++++ .../transform/JSONTransformMediatorTest.java | 33 +++++++ .../transform/PayloadFactoryMediatorTest.java | 90 +++++++++++++++++++ .../mediators/transform/XSLTMediatorTest.java | 22 +++++ .../synapse/mediators/bsf/ScriptMediator.java | 11 ++- .../mediators/xquery/XQueryMediator.java | 17 ++-- .../mediators/bsf/ScriptMediatorTest.java | 28 ++++++ .../mediators/xquery/XQueryMediatorTest.java | 33 +++++++ .../conf/sample/resources/payload/sample.xml | 6 ++ .../sample/resources/transform/schema.json | 22 +++++ 15 files changed, 314 insertions(+), 25 deletions(-) create mode 100644 modules/core/src/main/java/org/apache/synapse/mediators/Utils.java create mode 100644 modules/core/src/test/java/org/apache/synapse/mediators/transform/JSONTransformMediatorTest.java create mode 100644 repository/conf/sample/resources/payload/sample.xml create mode 100644 repository/conf/sample/resources/transform/schema.json diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/Utils.java b/modules/core/src/main/java/org/apache/synapse/mediators/Utils.java new file mode 100644 index 0000000000..09ce85b916 --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/mediators/Utils.java @@ -0,0 +1,17 @@ +package org.apache.synapse.mediators; + +import java.io.File; + +public class Utils { + + public static final String RESOURCES_IDENTIFIER = "resources:"; + public static final String CONVERTED_RESOURCES_IDENTIFIER = "gov:mi-resources" + File.separator; + + public static String transformFileKey(String fileKey) { + + if (fileKey.startsWith(RESOURCES_IDENTIFIER)) { + return CONVERTED_RESOURCES_IDENTIFIER + fileKey.substring(RESOURCES_IDENTIFIER.length()); + } + return fileKey; + } +} diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/JSONTransformMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/JSONTransformMediator.java index 1ec1eccbe6..2f634cfa0c 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/JSONTransformMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/JSONTransformMediator.java @@ -32,6 +32,7 @@ import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; import org.apache.synapse.mediators.MediatorProperty; +import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import java.io.IOException; @@ -87,9 +88,10 @@ public boolean mediate(MessageContext synCtx) { } } if (schemaKey != null) { - // Derive actual key from message context + // Derive actual key from message context and transform String generatedSchemaKey = schemaKey.evaluateValue(synCtx); - Object jsonSchemaObj = synCtx.getEntry(generatedSchemaKey); + String transformedSchemaKey = Utils.transformFileKey(generatedSchemaKey); + Object jsonSchemaObj = synCtx.getEntry(transformedSchemaKey); if (jsonSchemaObj != null) { String schema = ""; if (jsonSchemaObj instanceof OMTextImpl) { diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java index 135b0a4e98..6b97f44490 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java @@ -51,6 +51,7 @@ import org.apache.synapse.mediators.AbstractListMediator; import org.apache.synapse.mediators.FlowContinuableMediator; import org.apache.synapse.mediators.MediatorProperty; +import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.util.AXIOMUtils; import org.apache.synapse.util.jaxp.SchemaResourceResolver; @@ -207,8 +208,9 @@ public boolean mediate(MessageContext synCtx) { if (reCreate || cachedJsonSchema == null) { Object jsonSchemaObj = null; for (Value schemaKey : schemaKeys) { - // Derive actual key from message context + // Derive actual key from message context and transform String propName = schemaKey.evaluateValue(synCtx); + propName = Utils.transformFileKey(propName); jsonSchemaObj = synCtx.getEntry(propName); cachedJsonSchemaKey.append(propName); } @@ -406,8 +408,9 @@ public boolean mediate(MessageContext synCtx) { int i = 0; for (Value schemaKey : schemaKeys) { - // Derive actual key from message context + // Derive actual key from message context and transform String propName = schemaKey.evaluateValue(synCtx); + propName = Utils.transformFileKey(propName); Object schemaObject = synCtx.getEntry(propName); if (schemaObject == null) { throw new SynapseException("No Schema is available with the key : " + propName); @@ -531,8 +534,9 @@ public boolean mediate(MessageContext synCtx) { private boolean isReCreate(MessageContext synCtx, StringBuilder combinedPropertyKey) { boolean reCreate = false; for (Value schemaKey : schemaKeys) { - // Derive actual key from message context + // Derive actual key from message context and transform String propKey = schemaKey.evaluateValue(synCtx); + propKey = Utils.transformFileKey(propKey); // Generating a property key combinedPropertyKey.append(propKey); diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/transform/PayloadFactoryMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/transform/PayloadFactoryMediator.java index ef87351660..f4e9548f8d 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/transform/PayloadFactoryMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/transform/PayloadFactoryMediator.java @@ -41,6 +41,7 @@ import org.apache.synapse.config.Entry; import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; +import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.mediators.transform.pfutils.TemplateProcessor; import org.apache.synapse.mediators.transform.pfutils.TemplateProcessorException; @@ -168,14 +169,16 @@ private void transform(StringBuilder result, MessageContext synCtx, String forma boolean reCreate = false; if (isFormatDynamic()) { if (templateType.equals(FREEMARKER_TEMPLATE_TYPE)) { - Entry template = synCtx.getConfiguration().getEntryDefinition(formatKey.getKeyValue()); + String transformedFormatKey = Utils.transformFileKey(formatKey.getKeyValue()); + Entry template = synCtx.getConfiguration().getEntryDefinition(transformedFormatKey); if ((!template.isCached() || template.isExpired()) && version != template.getVersion()) { reCreate = true; version = template.getVersion(); } } String key = formatKey.evaluateValue(synCtx); - Object entry = synCtx.getEntry(key); + String transformedKey = Utils.transformFileKey(key); + Object entry = synCtx.getEntry(transformedKey); if (entry == null) { handleException("Key " + key + " not found ", synCtx); } diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java index e3a4baa302..3b23ed1710 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java @@ -35,6 +35,7 @@ import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; import org.apache.synapse.mediators.MediatorProperty; +import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.util.jaxp.DOOMResultBuilderFactory; import org.apache.synapse.util.jaxp.DOOMSourceBuilderFactory; @@ -281,10 +282,11 @@ private void performXSLT(MessageContext synCtx, SynapseLog synLog) { boolean isSoapBody = (sourceNode == synCtx.getEnvelope().getBody()); boolean isSoapHeader = (sourceNode == synCtx.getEnvelope().getHeader()); - // Derive actual key from message context + // Derive actual key from message context and transform String generatedXsltKey = xsltKey.evaluateValue(synCtx); + String transformedXsltKey = Utils.transformFileKey(generatedXsltKey); - // get templates from generatedXsltKey + // get templates from transformedXsltKey Templates cachedTemplates = null; if (synLog.isTraceTraceEnabled()) { @@ -297,16 +299,16 @@ private void performXSLT(MessageContext synCtx, SynapseLog synLog) { synchronized (transformerLock) { // only first thread should create the template if (isCreationOrRecreationRequired(synCtx)) { - cachedTemplates = createTemplate(synCtx, synLog, generatedXsltKey); + cachedTemplates = createTemplate(synCtx, synLog, transformedXsltKey); } else { - cachedTemplates = cachedTemplatesMap.get(generatedXsltKey); + cachedTemplates = cachedTemplatesMap.get(transformedXsltKey); } } } else{ //If already cached template then load it from cachedTemplatesMap synchronized (transformerLock){ - cachedTemplates = cachedTemplatesMap.get(generatedXsltKey); + cachedTemplates = cachedTemplatesMap.get(transformedXsltKey); } } @@ -499,15 +501,18 @@ private boolean isCreationOrRecreationRequired(MessageContext synCtx) { // Derive actual key from message context String generatedXsltKey = xsltKey.evaluateValue(synCtx); + // transform key if it indicates a file in resource structure + String transformedXsltKey = Utils.transformFileKey(generatedXsltKey); + // if there are no cachedTemplates inside cachedTemplatesMap or // if the template related to this generated key is not cached // then it need to be cached - if (cachedTemplatesMap.isEmpty() || !cachedTemplatesMap.containsKey(generatedXsltKey)) { + if (cachedTemplatesMap.isEmpty() || !cachedTemplatesMap.containsKey(transformedXsltKey)) { // this is a creation case return true; } else { // build transformer - if necessary - Entry dp = synCtx.getConfiguration().getEntryDefinition(generatedXsltKey); + Entry dp = synCtx.getConfiguration().getEntryDefinition(transformedXsltKey); // if the xsltKey refers to a dynamic resource, and if it has been expired // it is a recreation case return dp != null && dp.isDynamic() && (!dp.isCached() || dp.isExpired()); diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/builtin/ValidateMediatorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/builtin/ValidateMediatorTest.java index e34ba38774..a9a5ab9612 100644 --- a/modules/core/src/test/java/org/apache/synapse/mediators/builtin/ValidateMediatorTest.java +++ b/modules/core/src/test/java/org/apache/synapse/mediators/builtin/ValidateMediatorTest.java @@ -220,6 +220,24 @@ public void testValidateMediatorValidCaseTwoSchemas() throws Exception { test(validate, synCtx, false); } + public void testValidateMediatorWithTwoSchemasInResource() throws Exception { + // create a validate mediator + ValidateMediator validate = new ValidateMediator(); + + // set the schema url, source xpath and any name spaces + validate.setSchemaKeys(createKeyListFromMoreKeys("resources:sample1.xsd", "resources:sample2.xsd")); + validate.setSource(createXPath("//m1:Outer")); + + MessageContext synCtx = new TestMessageContextBuilder() + .setRequireAxis2MessageContext(true) + .addFileEntry("gov:mi-resources/sample1.xsd", "./../../repository/conf/sample/resources/validate/validate.xsd") + .addFileEntry("gov:mi-resources/sample2.xsd", "./../../repository/conf/sample/resources/validate/validate2.xsd") + .setBodyFromString(VALID_ENVELOPE_TWO_SCHEMAS).build(); + + // test validate mediator, with static envelope + test(validate, synCtx, false); + } + public void testValidateMediatorInvalidCaseTwoSchemas() throws Exception { // create a validate mediator ValidateMediator validate = new ValidateMediator(); diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/transform/JSONTransformMediatorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/transform/JSONTransformMediatorTest.java new file mode 100644 index 0000000000..2705ad7b22 --- /dev/null +++ b/modules/core/src/test/java/org/apache/synapse/mediators/transform/JSONTransformMediatorTest.java @@ -0,0 +1,33 @@ +package org.apache.synapse.mediators.transform; + +import org.apache.synapse.MessageContext; +import org.apache.synapse.TestMessageContextBuilder; +import org.apache.synapse.mediators.Value; +import org.apache.synapse.mediators.builtin.JSONTransformMediator; +import org.junit.Assert; +import org.junit.Test; + +public class JSONTransformMediatorTest { + + @Test + public void testWithResourceKey() throws Exception { + + JSONTransformMediator jsonTransformMediator = new JSONTransformMediator(); + jsonTransformMediator.setSchemaKey(new Value("resources:xslt/sample.json")); + + String payload = "\n" + + " 12345\n" + + " 10\n" + + ""; + + MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("gov:mi-resources/xslt/sample.json", + "../../repository/conf/sample/resources/transform/schema.json") + .setBodyFromString(payload).setRequireAxis2MessageContext(true).build(); + + try { + jsonTransformMediator.mediate(synCtx); + } catch (Exception ex) { + Assert.assertTrue(ex.getMessage().contains("\"required\":[\"price\"]")); + } + } +} diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/transform/PayloadFactoryMediatorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/transform/PayloadFactoryMediatorTest.java index 30279aaa5e..b5c20432b9 100644 --- a/modules/core/src/test/java/org/apache/synapse/mediators/transform/PayloadFactoryMediatorTest.java +++ b/modules/core/src/test/java/org/apache/synapse/mediators/transform/PayloadFactoryMediatorTest.java @@ -20,7 +20,9 @@ import junit.framework.TestCase; import org.apache.synapse.MessageContext; +import org.apache.synapse.TestMessageContextBuilder; import org.apache.synapse.mediators.TestUtils; +import org.apache.synapse.mediators.Value; import org.apache.synapse.mediators.transform.pfutils.RegexTemplateProcessor; import org.apache.synapse.util.xpath.SynapseXPath; @@ -138,4 +140,92 @@ public void testWithExpressionsAsArguments() throws Exception { assertEquals("PayloadFactory mediator has not " + "set expected format", expectedEnvelope, synCtx.getEnvelope().getBody().toString()); } + + public void testWithRegistryFormatKey() throws Exception { + + PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator(); + payloadFactoryMediator.setTemplateProcessor(new RegexTemplateProcessor()); + Value value = new Value("gov:payload/sample.xml"); + payloadFactoryMediator.setFormatKey(value); + payloadFactoryMediator.setFormatDynamic(true); + + //prepare arguments + Argument argument1 = new Argument(); + argument1.setValue("John"); + Argument argument2 = new Argument(); + argument2.setValue("2017.09.26"); + Argument argument3 = new Argument(); + argument3.setValue("1234564632"); + Argument argument4 = new Argument(); + argument4.setValue("Colombo, Sri Lanka"); + + //add arguments + payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument1); + payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument2); + payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument3); + payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument4); + + //do mediation + TestMessageContextBuilder builder = new TestMessageContextBuilder(); + builder.setRequireAxis2MessageContext(true); + builder.setBodyFromString(inputPayload); + MessageContext synCtx = builder.addFileEntry("gov:payload/sample.xml", + "../../repository/conf/sample/resources/payload/sample.xml").addTextAroundBody().build(); + payloadFactoryMediator.mediate(synCtx); + + String expectedEnv = "\n" + + "John\n" + + "2017.09.26\n" + + "1234564632\n" + + "Colombo, Sri Lanka\n" + + ""; + + assertEquals("PayloadFactory mediator has not " + + "set expected format", expectedEnv.replaceAll("[\\r\\n]", ""), synCtx.getEnvelope().getBody().toString()); + } + + public void testWithResourceFormatKey() throws Exception { + + PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator(); + payloadFactoryMediator.setTemplateProcessor(new RegexTemplateProcessor()); + Value value = new Value("resources:payload/sample.xml"); + payloadFactoryMediator.setFormatKey(value); + payloadFactoryMediator.setFormatDynamic(true); + + //prepare arguments + Argument argument1 = new Argument(); + argument1.setValue("John"); + Argument argument2 = new Argument(); + argument2.setValue("2017.09.26"); + Argument argument3 = new Argument(); + argument3.setValue("1234564632"); + Argument argument4 = new Argument(); + argument4.setValue("Colombo, Sri Lanka"); + + //add arguments + payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument1); + payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument2); + payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument3); + payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument4); + + //do mediation + TestMessageContextBuilder builder = new TestMessageContextBuilder(); + builder.setRequireAxis2MessageContext(true); + builder.setBodyFromString(inputPayload); + MessageContext synCtx = builder.addFileEntry("gov:mi-resources/payload/sample.xml", + "../../repository/conf/sample/resources/payload/sample.xml").addTextAroundBody().build(); + payloadFactoryMediator.mediate(synCtx); + + String expectedEnv = "\n" + + "John\n" + + "2017.09.26\n" + + "1234564632\n" + + "Colombo, Sri Lanka\n" + + ""; + + assertEquals("PayloadFactory mediator has not " + + "set expected format", expectedEnv.replaceAll("[\\r\\n]", ""), synCtx.getEnvelope().getBody().toString()); + } } \ No newline at end of file diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java index 2255be8be2..1050b3539a 100644 --- a/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java +++ b/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java @@ -173,6 +173,28 @@ public void testTransformXSLTDefaultSource() throws Exception { assertQuoteElement(synCtx.getEnvelope().getBody().getFirstOMChild().getNextOMSibling()); } + public void testResourceKeyForXSLT() throws Exception { + + XSLTMediator transformMediator = new XSLTMediator(); + setXsltTransformationURL(transformMediator, "resources:xslt/sample.xslt"); + MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("gov:mi-resources/xslt/sample.xslt", + "../../repository/conf/sample/resources/transform/transform_unittest.xslt") + .setBodyFromString(SOURCE).setRequireAxis2MessageContext(true).addTextAroundBody().build(); + transformMediator.mediate(synCtx); + assertQuoteElement(synCtx.getEnvelope().getBody().getFirstOMChild().getNextOMSibling()); + } + + public void testRegistryKeyForXSLT() throws Exception { + + XSLTMediator transformMediator = new XSLTMediator(); + setXsltTransformationURL(transformMediator, "gov:mi-resources/xslt/sample.xslt"); + MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("gov:mi-resources/xslt/sample.xslt", + "../../repository/conf/sample/resources/transform/transform_unittest.xslt") + .setBodyFromString(SOURCE).setRequireAxis2MessageContext(true).addTextAroundBody().build(); + transformMediator.mediate(synCtx); + assertQuoteElement(synCtx.getEnvelope().getBody().getFirstOMChild().getNextOMSibling()); + } + public void testTransformXSLTLargeMessagesCSV() throws Exception { // create a new switch mediator diff --git a/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java b/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java index 30d1ee615e..b48ee4d592 100644 --- a/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java +++ b/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java @@ -37,6 +37,7 @@ import org.apache.synapse.config.Entry; import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; +import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.mediators.bsf.access.control.AccessControlUtils; import org.apache.synapse.mediators.bsf.access.control.SandboxContextFactory; @@ -505,7 +506,8 @@ protected ScriptEngineWrapper prepareExternalScript(MessageContext synCtx) // Derive actual key from xpath expression or get static key String generatedScriptKey = key.evaluateValue(synCtx); - Entry entry = synCtx.getConfiguration().getEntryDefinition(generatedScriptKey); + String transformedScriptKey = Utils.transformFileKey(generatedScriptKey); + Entry entry = synCtx.getConfiguration().getEntryDefinition(transformedScriptKey); boolean needsReload = (entry != null) && entry.isDynamic() && (!entry.isCached() || entry.isExpired()); @@ -514,7 +516,7 @@ protected ScriptEngineWrapper prepareExternalScript(MessageContext synCtx) engineBinding.clear(); // if we don't do this, previous state can affect successive executions! ESBJAVA-4583 if (scriptSourceCode == null || needsReload || !sew.isInitialized()) { - Object o = synCtx.getEntry(generatedScriptKey); + Object o = synCtx.getEntry(transformedScriptKey); if (o instanceof OMElement) { scriptSourceCode = ((OMElement) (o)).getText(); sew.getEngine().eval(scriptSourceCode, engineBinding); @@ -561,13 +563,14 @@ protected ScriptEngineWrapper prepareExternalScript(MessageContext synCtx) String includeSourceCode = (String) includes.get(includeKey); String generatedKey = includeKey.evaluateValue(synCtx); + String transformedKey = Utils.transformFileKey(generatedKey); - Entry includeEntry = synCtx.getConfiguration().getEntryDefinition(generatedKey); + Entry includeEntry = synCtx.getConfiguration().getEntryDefinition(transformedKey); boolean includeEntryNeedsReload = (includeEntry != null) && includeEntry.isDynamic() && (!includeEntry.isCached() || includeEntry.isExpired()); if (includeSourceCode == null || includeEntryNeedsReload || !sew.isInitialized()) { log.debug("Re-/Loading the include script with key " + includeKey); - Object o = synCtx.getEntry(generatedKey); + Object o = synCtx.getEntry(transformedKey); if (o instanceof OMElement) { includeSourceCode = ((OMElement) (o)).getText(); sew.getEngine().eval(includeSourceCode, engineBinding); diff --git a/modules/extensions/src/main/java/org/apache/synapse/mediators/xquery/XQueryMediator.java b/modules/extensions/src/main/java/org/apache/synapse/mediators/xquery/XQueryMediator.java index 79c88c23e8..fcff3ed891 100644 --- a/modules/extensions/src/main/java/org/apache/synapse/mediators/xquery/XQueryMediator.java +++ b/modules/extensions/src/main/java/org/apache/synapse/mediators/xquery/XQueryMediator.java @@ -34,6 +34,7 @@ import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; import org.apache.synapse.mediators.MediatorProperty; +import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.util.xpath.SourceXPathSupport; import org.apache.synapse.util.xpath.SynapseXPath; @@ -152,6 +153,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { boolean needSet = false; XQueryEvaluator queryEvaluator = null; String generatedQueryKey = null; + String transformedQueryKey = null; XQueryExecutable xQueryExecutable = null; XdmValue xdmValue; boolean isQueryKeyGenerated = false; @@ -159,15 +161,16 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { if (queryKey != null) { // Derive actual key from xpath or get static key generatedQueryKey = queryKey.evaluateValue(synCtx); + transformedQueryKey = Utils.transformFileKey(generatedQueryKey); } - if (generatedQueryKey != null) { + if (transformedQueryKey != null) { isQueryKeyGenerated = true; } - if (generatedQueryKey != null && !"".equals(generatedQueryKey)) { + if (transformedQueryKey != null && !"".equals(transformedQueryKey)) { - Entry dp = synCtx.getConfiguration().getEntryDefinition(generatedQueryKey); + Entry dp = synCtx.getConfiguration().getEntryDefinition(transformedQueryKey); // if the queryKey refers to a dynamic resource if (dp != null && dp.isDynamic()) { if (!dp.isCached() || dp.isExpired()) { @@ -201,7 +204,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { //If already cached evaluator then load it from cachedXQueryEvaluatorMap if (isQueryKeyGenerated) { - queryEvaluator = cachedXQueryEvaluatorMap.get(generatedQueryKey); + queryEvaluator = cachedXQueryEvaluatorMap.get(transformedQueryKey); } if (reLoad || queryEvaluator == null) { @@ -218,7 +221,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { // if queryEvaluator is created then put it in to cachedXQueryEvaluatorMap if (isQueryKeyGenerated) { - cachedXQueryEvaluatorMap.put(generatedQueryKey, queryEvaluator); + cachedXQueryEvaluatorMap.put(transformedQueryKey, queryEvaluator); } // need set because the expression just has recreated @@ -227,7 +230,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { } else { - Object o = synCtx.getEntry(generatedQueryKey); + Object o = synCtx.getEntry(transformedQueryKey); if (o == null) { if (synLog.isTraceOrDebugEnabled()) { synLog.traceOrDebug("Couldn't find the xquery source with a key " @@ -291,7 +294,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { // if queryEvaluator is created then put it in to cachedXQueryEvaluatorMap if (isQueryKeyGenerated) { - cachedXQueryEvaluatorMap.put(generatedQueryKey, queryEvaluator); + cachedXQueryEvaluatorMap.put(transformedQueryKey, queryEvaluator); } // need set because the evaluator just has recreated diff --git a/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java b/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java index 0121ce88f7..a151eb2ec9 100644 --- a/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java +++ b/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java @@ -199,6 +199,34 @@ public void testExternalScriptWithComments() throws Exception { assertEquals(true, result); } + public void testWithResourceScriptKey() throws Exception { + + String request = "{\"status\": \"OK\"}"; + MessageContext mc = TestUtils.getTestContextJson(request, null); + String scriptSrc = "function transform(mc) {\n" + + " payload = mc.getPayloadJSON();\n" + + " var response = 'succeed';\n" + + " mc.setPayloadJSON(response);\n" + + "}"; + String scriptKey = "gov:mi-resources/repository/esb/transform.js"; + Entry e = new Entry(); + DataSource dataSource = new ByteArrayDataSource(scriptSrc.getBytes()); + DataHandler dataHandler = new DataHandler(dataSource); + OMText text = OMAbstractFactory.getOMFactory().createOMText(dataHandler, true); + e.setKey(scriptKey); + e.setValue(text); + mc.getConfiguration().addEntry(scriptKey, e); + + Value v = new Value("resources:repository/esb/transform.js"); + ScriptMediator mediator = new ScriptMediator("js", new LinkedHashMap(), v, "transform", null); + boolean result = mediator.mediate(mc); + String response = JsonUtil.jsonPayloadToString(((Axis2MessageContext) mc).getAxis2MessageContext()); + String expectedResponse = "\"succeed\""; + + assertEquals(expectedResponse, response); + assertEquals(true, result); + } + public static Test suite() { TestSuite suite = new TestSuite(); for (int i = 0; i < 10; i++) { diff --git a/modules/extensions/src/test/java/org/apache/synapse/mediators/xquery/XQueryMediatorTest.java b/modules/extensions/src/test/java/org/apache/synapse/mediators/xquery/XQueryMediatorTest.java index b629c84b0a..c465f4f037 100644 --- a/modules/extensions/src/test/java/org/apache/synapse/mediators/xquery/XQueryMediatorTest.java +++ b/modules/extensions/src/test/java/org/apache/synapse/mediators/xquery/XQueryMediatorTest.java @@ -21,10 +21,17 @@ import junit.framework.TestCase; import net.sf.saxon.s9api.ItemType; import net.sf.saxon.s9api.XdmNodeKind; +import org.apache.axiom.attachments.ByteArrayDataSource; +import org.apache.axiom.om.OMAbstractFactory; +import org.apache.axiom.om.OMText; import org.apache.synapse.MessageContext; +import org.apache.synapse.config.Entry; import org.apache.synapse.config.SynapseConfigUtils; import org.apache.synapse.mediators.TestUtils; +import org.apache.synapse.mediators.Value; +import javax.activation.DataHandler; +import javax.activation.DataSource; import javax.xml.namespace.QName; import java.util.List; import java.util.ArrayList; @@ -171,6 +178,32 @@ public void testQueryWithPayload() throws Exception { assertEquals("Everyday Italian", mc.getEnvelope().getBody().getFirstElement().getText()); } + public void testQueryWithResourceKey() throws Exception { + + MessageContext mc = TestUtils.getAxis2MessageContext(sampleXml, null); + String fileKey = "gov:mi-resources/sample/sample.txt"; + String textSrc = "declare variable $payload as document-node() external;" + + "$payload//bookstore/book/title"; + Entry e = new Entry(); + DataSource dataSource = new ByteArrayDataSource(textSrc.getBytes()); + DataHandler dataHandler = new DataHandler(dataSource); + OMText text = OMAbstractFactory.getOMFactory().createOMText(dataHandler, true); + e.setKey(fileKey); + e.setValue(text); + mc.getConfiguration().addEntry(fileKey, e); + + XQueryMediator mediator = new XQueryMediator(); + List list = new ArrayList(); + MediatorVariable variable = new MediatorCustomVariable(new QName("payload")); + // variable.setType(ItemType.XQITEMKIND_DOCUMENT); + variable.setNodeKind(XdmNodeKind.DOCUMENT); + list.add(variable); + mediator.addAllVariables(list); + mediator.setQueryKey(new Value("resources:sample/sample.txt")); + assertTrue(mediator.mediate(mc)); + assertEquals("Everyday Italian", mc.getEnvelope().getBody().getFirstElement().getText()); + } + public void testQueryWithPayloadTwo() throws Exception { MessageContext mc = TestUtils.getAxis2MessageContext(sampleXml2, null); diff --git a/repository/conf/sample/resources/payload/sample.xml b/repository/conf/sample/resources/payload/sample.xml new file mode 100644 index 0000000000..e50409bef9 --- /dev/null +++ b/repository/conf/sample/resources/payload/sample.xml @@ -0,0 +1,6 @@ + + $1 + $2 + $3 + $4 + diff --git a/repository/conf/sample/resources/transform/schema.json b/repository/conf/sample/resources/transform/schema.json new file mode 100644 index 0000000000..62b5e55b1f --- /dev/null +++ b/repository/conf/sample/resources/transform/schema.json @@ -0,0 +1,22 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "fruit": { + "type": "string", + "minLength": 4, + "maxLength": 6, + "pattern": "^[0-9]{1,45}$" + }, + "price": { + "type": "number", + "minimum": 2, + "maximum": 20, + "exclusiveMaximum": 20, + "multipleOf": 2.5 + } + }, + "required": [ + "price" + ] +} \ No newline at end of file From fe5cce3317f00abc3163b84fa6649c6802f392eb Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Thu, 5 Dec 2024 15:39:16 +0530 Subject: [PATCH 2/6] Revert "handle resource file key in xslt,jsontransform,payload,script,validate,xquery mediators" This reverts commit 76f646fc7d3d7c05a174ad7b24471d5811c02b7a. --- .../org/apache/synapse/mediators/Utils.java | 17 ---- .../builtin/JSONTransformMediator.java | 6 +- .../mediators/builtin/ValidateMediator.java | 10 +-- .../transform/PayloadFactoryMediator.java | 7 +- .../mediators/transform/XSLTMediator.java | 19 ++-- .../builtin/ValidateMediatorTest.java | 18 ---- .../transform/JSONTransformMediatorTest.java | 33 ------- .../transform/PayloadFactoryMediatorTest.java | 90 ------------------- .../mediators/transform/XSLTMediatorTest.java | 22 ----- .../synapse/mediators/bsf/ScriptMediator.java | 11 +-- .../mediators/xquery/XQueryMediator.java | 17 ++-- .../mediators/bsf/ScriptMediatorTest.java | 28 ------ .../mediators/xquery/XQueryMediatorTest.java | 33 ------- .../conf/sample/resources/payload/sample.xml | 6 -- .../sample/resources/transform/schema.json | 22 ----- 15 files changed, 25 insertions(+), 314 deletions(-) delete mode 100644 modules/core/src/main/java/org/apache/synapse/mediators/Utils.java delete mode 100644 modules/core/src/test/java/org/apache/synapse/mediators/transform/JSONTransformMediatorTest.java delete mode 100644 repository/conf/sample/resources/payload/sample.xml delete mode 100644 repository/conf/sample/resources/transform/schema.json diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/Utils.java b/modules/core/src/main/java/org/apache/synapse/mediators/Utils.java deleted file mode 100644 index 09ce85b916..0000000000 --- a/modules/core/src/main/java/org/apache/synapse/mediators/Utils.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.apache.synapse.mediators; - -import java.io.File; - -public class Utils { - - public static final String RESOURCES_IDENTIFIER = "resources:"; - public static final String CONVERTED_RESOURCES_IDENTIFIER = "gov:mi-resources" + File.separator; - - public static String transformFileKey(String fileKey) { - - if (fileKey.startsWith(RESOURCES_IDENTIFIER)) { - return CONVERTED_RESOURCES_IDENTIFIER + fileKey.substring(RESOURCES_IDENTIFIER.length()); - } - return fileKey; - } -} diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/JSONTransformMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/JSONTransformMediator.java index 2f634cfa0c..1ec1eccbe6 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/JSONTransformMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/JSONTransformMediator.java @@ -32,7 +32,6 @@ import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; import org.apache.synapse.mediators.MediatorProperty; -import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import java.io.IOException; @@ -88,10 +87,9 @@ public boolean mediate(MessageContext synCtx) { } } if (schemaKey != null) { - // Derive actual key from message context and transform + // Derive actual key from message context String generatedSchemaKey = schemaKey.evaluateValue(synCtx); - String transformedSchemaKey = Utils.transformFileKey(generatedSchemaKey); - Object jsonSchemaObj = synCtx.getEntry(transformedSchemaKey); + Object jsonSchemaObj = synCtx.getEntry(generatedSchemaKey); if (jsonSchemaObj != null) { String schema = ""; if (jsonSchemaObj instanceof OMTextImpl) { diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java index 6b97f44490..135b0a4e98 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java @@ -51,7 +51,6 @@ import org.apache.synapse.mediators.AbstractListMediator; import org.apache.synapse.mediators.FlowContinuableMediator; import org.apache.synapse.mediators.MediatorProperty; -import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.util.AXIOMUtils; import org.apache.synapse.util.jaxp.SchemaResourceResolver; @@ -208,9 +207,8 @@ public boolean mediate(MessageContext synCtx) { if (reCreate || cachedJsonSchema == null) { Object jsonSchemaObj = null; for (Value schemaKey : schemaKeys) { - // Derive actual key from message context and transform + // Derive actual key from message context String propName = schemaKey.evaluateValue(synCtx); - propName = Utils.transformFileKey(propName); jsonSchemaObj = synCtx.getEntry(propName); cachedJsonSchemaKey.append(propName); } @@ -408,9 +406,8 @@ public boolean mediate(MessageContext synCtx) { int i = 0; for (Value schemaKey : schemaKeys) { - // Derive actual key from message context and transform + // Derive actual key from message context String propName = schemaKey.evaluateValue(synCtx); - propName = Utils.transformFileKey(propName); Object schemaObject = synCtx.getEntry(propName); if (schemaObject == null) { throw new SynapseException("No Schema is available with the key : " + propName); @@ -534,9 +531,8 @@ public boolean mediate(MessageContext synCtx) { private boolean isReCreate(MessageContext synCtx, StringBuilder combinedPropertyKey) { boolean reCreate = false; for (Value schemaKey : schemaKeys) { - // Derive actual key from message context and transform + // Derive actual key from message context String propKey = schemaKey.evaluateValue(synCtx); - propKey = Utils.transformFileKey(propKey); // Generating a property key combinedPropertyKey.append(propKey); diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/transform/PayloadFactoryMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/transform/PayloadFactoryMediator.java index f4e9548f8d..ef87351660 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/transform/PayloadFactoryMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/transform/PayloadFactoryMediator.java @@ -41,7 +41,6 @@ import org.apache.synapse.config.Entry; import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; -import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.mediators.transform.pfutils.TemplateProcessor; import org.apache.synapse.mediators.transform.pfutils.TemplateProcessorException; @@ -169,16 +168,14 @@ private void transform(StringBuilder result, MessageContext synCtx, String forma boolean reCreate = false; if (isFormatDynamic()) { if (templateType.equals(FREEMARKER_TEMPLATE_TYPE)) { - String transformedFormatKey = Utils.transformFileKey(formatKey.getKeyValue()); - Entry template = synCtx.getConfiguration().getEntryDefinition(transformedFormatKey); + Entry template = synCtx.getConfiguration().getEntryDefinition(formatKey.getKeyValue()); if ((!template.isCached() || template.isExpired()) && version != template.getVersion()) { reCreate = true; version = template.getVersion(); } } String key = formatKey.evaluateValue(synCtx); - String transformedKey = Utils.transformFileKey(key); - Object entry = synCtx.getEntry(transformedKey); + Object entry = synCtx.getEntry(key); if (entry == null) { handleException("Key " + key + " not found ", synCtx); } diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java index 3b23ed1710..e3a4baa302 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java @@ -35,7 +35,6 @@ import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; import org.apache.synapse.mediators.MediatorProperty; -import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.util.jaxp.DOOMResultBuilderFactory; import org.apache.synapse.util.jaxp.DOOMSourceBuilderFactory; @@ -282,11 +281,10 @@ private void performXSLT(MessageContext synCtx, SynapseLog synLog) { boolean isSoapBody = (sourceNode == synCtx.getEnvelope().getBody()); boolean isSoapHeader = (sourceNode == synCtx.getEnvelope().getHeader()); - // Derive actual key from message context and transform + // Derive actual key from message context String generatedXsltKey = xsltKey.evaluateValue(synCtx); - String transformedXsltKey = Utils.transformFileKey(generatedXsltKey); - // get templates from transformedXsltKey + // get templates from generatedXsltKey Templates cachedTemplates = null; if (synLog.isTraceTraceEnabled()) { @@ -299,16 +297,16 @@ private void performXSLT(MessageContext synCtx, SynapseLog synLog) { synchronized (transformerLock) { // only first thread should create the template if (isCreationOrRecreationRequired(synCtx)) { - cachedTemplates = createTemplate(synCtx, synLog, transformedXsltKey); + cachedTemplates = createTemplate(synCtx, synLog, generatedXsltKey); } else { - cachedTemplates = cachedTemplatesMap.get(transformedXsltKey); + cachedTemplates = cachedTemplatesMap.get(generatedXsltKey); } } } else{ //If already cached template then load it from cachedTemplatesMap synchronized (transformerLock){ - cachedTemplates = cachedTemplatesMap.get(transformedXsltKey); + cachedTemplates = cachedTemplatesMap.get(generatedXsltKey); } } @@ -501,18 +499,15 @@ private boolean isCreationOrRecreationRequired(MessageContext synCtx) { // Derive actual key from message context String generatedXsltKey = xsltKey.evaluateValue(synCtx); - // transform key if it indicates a file in resource structure - String transformedXsltKey = Utils.transformFileKey(generatedXsltKey); - // if there are no cachedTemplates inside cachedTemplatesMap or // if the template related to this generated key is not cached // then it need to be cached - if (cachedTemplatesMap.isEmpty() || !cachedTemplatesMap.containsKey(transformedXsltKey)) { + if (cachedTemplatesMap.isEmpty() || !cachedTemplatesMap.containsKey(generatedXsltKey)) { // this is a creation case return true; } else { // build transformer - if necessary - Entry dp = synCtx.getConfiguration().getEntryDefinition(transformedXsltKey); + Entry dp = synCtx.getConfiguration().getEntryDefinition(generatedXsltKey); // if the xsltKey refers to a dynamic resource, and if it has been expired // it is a recreation case return dp != null && dp.isDynamic() && (!dp.isCached() || dp.isExpired()); diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/builtin/ValidateMediatorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/builtin/ValidateMediatorTest.java index a9a5ab9612..e34ba38774 100644 --- a/modules/core/src/test/java/org/apache/synapse/mediators/builtin/ValidateMediatorTest.java +++ b/modules/core/src/test/java/org/apache/synapse/mediators/builtin/ValidateMediatorTest.java @@ -220,24 +220,6 @@ public void testValidateMediatorValidCaseTwoSchemas() throws Exception { test(validate, synCtx, false); } - public void testValidateMediatorWithTwoSchemasInResource() throws Exception { - // create a validate mediator - ValidateMediator validate = new ValidateMediator(); - - // set the schema url, source xpath and any name spaces - validate.setSchemaKeys(createKeyListFromMoreKeys("resources:sample1.xsd", "resources:sample2.xsd")); - validate.setSource(createXPath("//m1:Outer")); - - MessageContext synCtx = new TestMessageContextBuilder() - .setRequireAxis2MessageContext(true) - .addFileEntry("gov:mi-resources/sample1.xsd", "./../../repository/conf/sample/resources/validate/validate.xsd") - .addFileEntry("gov:mi-resources/sample2.xsd", "./../../repository/conf/sample/resources/validate/validate2.xsd") - .setBodyFromString(VALID_ENVELOPE_TWO_SCHEMAS).build(); - - // test validate mediator, with static envelope - test(validate, synCtx, false); - } - public void testValidateMediatorInvalidCaseTwoSchemas() throws Exception { // create a validate mediator ValidateMediator validate = new ValidateMediator(); diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/transform/JSONTransformMediatorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/transform/JSONTransformMediatorTest.java deleted file mode 100644 index 2705ad7b22..0000000000 --- a/modules/core/src/test/java/org/apache/synapse/mediators/transform/JSONTransformMediatorTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.apache.synapse.mediators.transform; - -import org.apache.synapse.MessageContext; -import org.apache.synapse.TestMessageContextBuilder; -import org.apache.synapse.mediators.Value; -import org.apache.synapse.mediators.builtin.JSONTransformMediator; -import org.junit.Assert; -import org.junit.Test; - -public class JSONTransformMediatorTest { - - @Test - public void testWithResourceKey() throws Exception { - - JSONTransformMediator jsonTransformMediator = new JSONTransformMediator(); - jsonTransformMediator.setSchemaKey(new Value("resources:xslt/sample.json")); - - String payload = "\n" + - " 12345\n" + - " 10\n" + - ""; - - MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("gov:mi-resources/xslt/sample.json", - "../../repository/conf/sample/resources/transform/schema.json") - .setBodyFromString(payload).setRequireAxis2MessageContext(true).build(); - - try { - jsonTransformMediator.mediate(synCtx); - } catch (Exception ex) { - Assert.assertTrue(ex.getMessage().contains("\"required\":[\"price\"]")); - } - } -} diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/transform/PayloadFactoryMediatorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/transform/PayloadFactoryMediatorTest.java index b5c20432b9..30279aaa5e 100644 --- a/modules/core/src/test/java/org/apache/synapse/mediators/transform/PayloadFactoryMediatorTest.java +++ b/modules/core/src/test/java/org/apache/synapse/mediators/transform/PayloadFactoryMediatorTest.java @@ -20,9 +20,7 @@ import junit.framework.TestCase; import org.apache.synapse.MessageContext; -import org.apache.synapse.TestMessageContextBuilder; import org.apache.synapse.mediators.TestUtils; -import org.apache.synapse.mediators.Value; import org.apache.synapse.mediators.transform.pfutils.RegexTemplateProcessor; import org.apache.synapse.util.xpath.SynapseXPath; @@ -140,92 +138,4 @@ public void testWithExpressionsAsArguments() throws Exception { assertEquals("PayloadFactory mediator has not " + "set expected format", expectedEnvelope, synCtx.getEnvelope().getBody().toString()); } - - public void testWithRegistryFormatKey() throws Exception { - - PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator(); - payloadFactoryMediator.setTemplateProcessor(new RegexTemplateProcessor()); - Value value = new Value("gov:payload/sample.xml"); - payloadFactoryMediator.setFormatKey(value); - payloadFactoryMediator.setFormatDynamic(true); - - //prepare arguments - Argument argument1 = new Argument(); - argument1.setValue("John"); - Argument argument2 = new Argument(); - argument2.setValue("2017.09.26"); - Argument argument3 = new Argument(); - argument3.setValue("1234564632"); - Argument argument4 = new Argument(); - argument4.setValue("Colombo, Sri Lanka"); - - //add arguments - payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument1); - payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument2); - payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument3); - payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument4); - - //do mediation - TestMessageContextBuilder builder = new TestMessageContextBuilder(); - builder.setRequireAxis2MessageContext(true); - builder.setBodyFromString(inputPayload); - MessageContext synCtx = builder.addFileEntry("gov:payload/sample.xml", - "../../repository/conf/sample/resources/payload/sample.xml").addTextAroundBody().build(); - payloadFactoryMediator.mediate(synCtx); - - String expectedEnv = "\n" - + "John\n" - + "2017.09.26\n" - + "1234564632\n" - + "Colombo, Sri Lanka\n" - + ""; - - assertEquals("PayloadFactory mediator has not " - + "set expected format", expectedEnv.replaceAll("[\\r\\n]", ""), synCtx.getEnvelope().getBody().toString()); - } - - public void testWithResourceFormatKey() throws Exception { - - PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator(); - payloadFactoryMediator.setTemplateProcessor(new RegexTemplateProcessor()); - Value value = new Value("resources:payload/sample.xml"); - payloadFactoryMediator.setFormatKey(value); - payloadFactoryMediator.setFormatDynamic(true); - - //prepare arguments - Argument argument1 = new Argument(); - argument1.setValue("John"); - Argument argument2 = new Argument(); - argument2.setValue("2017.09.26"); - Argument argument3 = new Argument(); - argument3.setValue("1234564632"); - Argument argument4 = new Argument(); - argument4.setValue("Colombo, Sri Lanka"); - - //add arguments - payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument1); - payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument2); - payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument3); - payloadFactoryMediator.getTemplateProcessor().addPathArgument(argument4); - - //do mediation - TestMessageContextBuilder builder = new TestMessageContextBuilder(); - builder.setRequireAxis2MessageContext(true); - builder.setBodyFromString(inputPayload); - MessageContext synCtx = builder.addFileEntry("gov:mi-resources/payload/sample.xml", - "../../repository/conf/sample/resources/payload/sample.xml").addTextAroundBody().build(); - payloadFactoryMediator.mediate(synCtx); - - String expectedEnv = "\n" - + "John\n" - + "2017.09.26\n" - + "1234564632\n" - + "Colombo, Sri Lanka\n" - + ""; - - assertEquals("PayloadFactory mediator has not " - + "set expected format", expectedEnv.replaceAll("[\\r\\n]", ""), synCtx.getEnvelope().getBody().toString()); - } } \ No newline at end of file diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java index 1050b3539a..2255be8be2 100644 --- a/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java +++ b/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java @@ -173,28 +173,6 @@ public void testTransformXSLTDefaultSource() throws Exception { assertQuoteElement(synCtx.getEnvelope().getBody().getFirstOMChild().getNextOMSibling()); } - public void testResourceKeyForXSLT() throws Exception { - - XSLTMediator transformMediator = new XSLTMediator(); - setXsltTransformationURL(transformMediator, "resources:xslt/sample.xslt"); - MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("gov:mi-resources/xslt/sample.xslt", - "../../repository/conf/sample/resources/transform/transform_unittest.xslt") - .setBodyFromString(SOURCE).setRequireAxis2MessageContext(true).addTextAroundBody().build(); - transformMediator.mediate(synCtx); - assertQuoteElement(synCtx.getEnvelope().getBody().getFirstOMChild().getNextOMSibling()); - } - - public void testRegistryKeyForXSLT() throws Exception { - - XSLTMediator transformMediator = new XSLTMediator(); - setXsltTransformationURL(transformMediator, "gov:mi-resources/xslt/sample.xslt"); - MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("gov:mi-resources/xslt/sample.xslt", - "../../repository/conf/sample/resources/transform/transform_unittest.xslt") - .setBodyFromString(SOURCE).setRequireAxis2MessageContext(true).addTextAroundBody().build(); - transformMediator.mediate(synCtx); - assertQuoteElement(synCtx.getEnvelope().getBody().getFirstOMChild().getNextOMSibling()); - } - public void testTransformXSLTLargeMessagesCSV() throws Exception { // create a new switch mediator diff --git a/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java b/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java index b48ee4d592..30d1ee615e 100644 --- a/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java +++ b/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java @@ -37,7 +37,6 @@ import org.apache.synapse.config.Entry; import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; -import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.mediators.bsf.access.control.AccessControlUtils; import org.apache.synapse.mediators.bsf.access.control.SandboxContextFactory; @@ -506,8 +505,7 @@ protected ScriptEngineWrapper prepareExternalScript(MessageContext synCtx) // Derive actual key from xpath expression or get static key String generatedScriptKey = key.evaluateValue(synCtx); - String transformedScriptKey = Utils.transformFileKey(generatedScriptKey); - Entry entry = synCtx.getConfiguration().getEntryDefinition(transformedScriptKey); + Entry entry = synCtx.getConfiguration().getEntryDefinition(generatedScriptKey); boolean needsReload = (entry != null) && entry.isDynamic() && (!entry.isCached() || entry.isExpired()); @@ -516,7 +514,7 @@ protected ScriptEngineWrapper prepareExternalScript(MessageContext synCtx) engineBinding.clear(); // if we don't do this, previous state can affect successive executions! ESBJAVA-4583 if (scriptSourceCode == null || needsReload || !sew.isInitialized()) { - Object o = synCtx.getEntry(transformedScriptKey); + Object o = synCtx.getEntry(generatedScriptKey); if (o instanceof OMElement) { scriptSourceCode = ((OMElement) (o)).getText(); sew.getEngine().eval(scriptSourceCode, engineBinding); @@ -563,14 +561,13 @@ protected ScriptEngineWrapper prepareExternalScript(MessageContext synCtx) String includeSourceCode = (String) includes.get(includeKey); String generatedKey = includeKey.evaluateValue(synCtx); - String transformedKey = Utils.transformFileKey(generatedKey); - Entry includeEntry = synCtx.getConfiguration().getEntryDefinition(transformedKey); + Entry includeEntry = synCtx.getConfiguration().getEntryDefinition(generatedKey); boolean includeEntryNeedsReload = (includeEntry != null) && includeEntry.isDynamic() && (!includeEntry.isCached() || includeEntry.isExpired()); if (includeSourceCode == null || includeEntryNeedsReload || !sew.isInitialized()) { log.debug("Re-/Loading the include script with key " + includeKey); - Object o = synCtx.getEntry(transformedKey); + Object o = synCtx.getEntry(generatedKey); if (o instanceof OMElement) { includeSourceCode = ((OMElement) (o)).getText(); sew.getEngine().eval(includeSourceCode, engineBinding); diff --git a/modules/extensions/src/main/java/org/apache/synapse/mediators/xquery/XQueryMediator.java b/modules/extensions/src/main/java/org/apache/synapse/mediators/xquery/XQueryMediator.java index fcff3ed891..79c88c23e8 100644 --- a/modules/extensions/src/main/java/org/apache/synapse/mediators/xquery/XQueryMediator.java +++ b/modules/extensions/src/main/java/org/apache/synapse/mediators/xquery/XQueryMediator.java @@ -34,7 +34,6 @@ import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; import org.apache.synapse.mediators.MediatorProperty; -import org.apache.synapse.mediators.Utils; import org.apache.synapse.mediators.Value; import org.apache.synapse.util.xpath.SourceXPathSupport; import org.apache.synapse.util.xpath.SynapseXPath; @@ -153,7 +152,6 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { boolean needSet = false; XQueryEvaluator queryEvaluator = null; String generatedQueryKey = null; - String transformedQueryKey = null; XQueryExecutable xQueryExecutable = null; XdmValue xdmValue; boolean isQueryKeyGenerated = false; @@ -161,16 +159,15 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { if (queryKey != null) { // Derive actual key from xpath or get static key generatedQueryKey = queryKey.evaluateValue(synCtx); - transformedQueryKey = Utils.transformFileKey(generatedQueryKey); } - if (transformedQueryKey != null) { + if (generatedQueryKey != null) { isQueryKeyGenerated = true; } - if (transformedQueryKey != null && !"".equals(transformedQueryKey)) { + if (generatedQueryKey != null && !"".equals(generatedQueryKey)) { - Entry dp = synCtx.getConfiguration().getEntryDefinition(transformedQueryKey); + Entry dp = synCtx.getConfiguration().getEntryDefinition(generatedQueryKey); // if the queryKey refers to a dynamic resource if (dp != null && dp.isDynamic()) { if (!dp.isCached() || dp.isExpired()) { @@ -204,7 +201,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { //If already cached evaluator then load it from cachedXQueryEvaluatorMap if (isQueryKeyGenerated) { - queryEvaluator = cachedXQueryEvaluatorMap.get(transformedQueryKey); + queryEvaluator = cachedXQueryEvaluatorMap.get(generatedQueryKey); } if (reLoad || queryEvaluator == null) { @@ -221,7 +218,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { // if queryEvaluator is created then put it in to cachedXQueryEvaluatorMap if (isQueryKeyGenerated) { - cachedXQueryEvaluatorMap.put(transformedQueryKey, queryEvaluator); + cachedXQueryEvaluatorMap.put(generatedQueryKey, queryEvaluator); } // need set because the expression just has recreated @@ -230,7 +227,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { } else { - Object o = synCtx.getEntry(transformedQueryKey); + Object o = synCtx.getEntry(generatedQueryKey); if (o == null) { if (synLog.isTraceOrDebugEnabled()) { synLog.traceOrDebug("Couldn't find the xquery source with a key " @@ -294,7 +291,7 @@ private void performQuery(MessageContext synCtx, SynapseLog synLog) { // if queryEvaluator is created then put it in to cachedXQueryEvaluatorMap if (isQueryKeyGenerated) { - cachedXQueryEvaluatorMap.put(transformedQueryKey, queryEvaluator); + cachedXQueryEvaluatorMap.put(generatedQueryKey, queryEvaluator); } // need set because the evaluator just has recreated diff --git a/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java b/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java index a151eb2ec9..0121ce88f7 100644 --- a/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java +++ b/modules/extensions/src/test/java/org/apache/synapse/mediators/bsf/ScriptMediatorTest.java @@ -199,34 +199,6 @@ public void testExternalScriptWithComments() throws Exception { assertEquals(true, result); } - public void testWithResourceScriptKey() throws Exception { - - String request = "{\"status\": \"OK\"}"; - MessageContext mc = TestUtils.getTestContextJson(request, null); - String scriptSrc = "function transform(mc) {\n" - + " payload = mc.getPayloadJSON();\n" - + " var response = 'succeed';\n" - + " mc.setPayloadJSON(response);\n" - + "}"; - String scriptKey = "gov:mi-resources/repository/esb/transform.js"; - Entry e = new Entry(); - DataSource dataSource = new ByteArrayDataSource(scriptSrc.getBytes()); - DataHandler dataHandler = new DataHandler(dataSource); - OMText text = OMAbstractFactory.getOMFactory().createOMText(dataHandler, true); - e.setKey(scriptKey); - e.setValue(text); - mc.getConfiguration().addEntry(scriptKey, e); - - Value v = new Value("resources:repository/esb/transform.js"); - ScriptMediator mediator = new ScriptMediator("js", new LinkedHashMap(), v, "transform", null); - boolean result = mediator.mediate(mc); - String response = JsonUtil.jsonPayloadToString(((Axis2MessageContext) mc).getAxis2MessageContext()); - String expectedResponse = "\"succeed\""; - - assertEquals(expectedResponse, response); - assertEquals(true, result); - } - public static Test suite() { TestSuite suite = new TestSuite(); for (int i = 0; i < 10; i++) { diff --git a/modules/extensions/src/test/java/org/apache/synapse/mediators/xquery/XQueryMediatorTest.java b/modules/extensions/src/test/java/org/apache/synapse/mediators/xquery/XQueryMediatorTest.java index c465f4f037..b629c84b0a 100644 --- a/modules/extensions/src/test/java/org/apache/synapse/mediators/xquery/XQueryMediatorTest.java +++ b/modules/extensions/src/test/java/org/apache/synapse/mediators/xquery/XQueryMediatorTest.java @@ -21,17 +21,10 @@ import junit.framework.TestCase; import net.sf.saxon.s9api.ItemType; import net.sf.saxon.s9api.XdmNodeKind; -import org.apache.axiom.attachments.ByteArrayDataSource; -import org.apache.axiom.om.OMAbstractFactory; -import org.apache.axiom.om.OMText; import org.apache.synapse.MessageContext; -import org.apache.synapse.config.Entry; import org.apache.synapse.config.SynapseConfigUtils; import org.apache.synapse.mediators.TestUtils; -import org.apache.synapse.mediators.Value; -import javax.activation.DataHandler; -import javax.activation.DataSource; import javax.xml.namespace.QName; import java.util.List; import java.util.ArrayList; @@ -178,32 +171,6 @@ public void testQueryWithPayload() throws Exception { assertEquals("Everyday Italian", mc.getEnvelope().getBody().getFirstElement().getText()); } - public void testQueryWithResourceKey() throws Exception { - - MessageContext mc = TestUtils.getAxis2MessageContext(sampleXml, null); - String fileKey = "gov:mi-resources/sample/sample.txt"; - String textSrc = "declare variable $payload as document-node() external;" + - "$payload//bookstore/book/title"; - Entry e = new Entry(); - DataSource dataSource = new ByteArrayDataSource(textSrc.getBytes()); - DataHandler dataHandler = new DataHandler(dataSource); - OMText text = OMAbstractFactory.getOMFactory().createOMText(dataHandler, true); - e.setKey(fileKey); - e.setValue(text); - mc.getConfiguration().addEntry(fileKey, e); - - XQueryMediator mediator = new XQueryMediator(); - List list = new ArrayList(); - MediatorVariable variable = new MediatorCustomVariable(new QName("payload")); - // variable.setType(ItemType.XQITEMKIND_DOCUMENT); - variable.setNodeKind(XdmNodeKind.DOCUMENT); - list.add(variable); - mediator.addAllVariables(list); - mediator.setQueryKey(new Value("resources:sample/sample.txt")); - assertTrue(mediator.mediate(mc)); - assertEquals("Everyday Italian", mc.getEnvelope().getBody().getFirstElement().getText()); - } - public void testQueryWithPayloadTwo() throws Exception { MessageContext mc = TestUtils.getAxis2MessageContext(sampleXml2, null); diff --git a/repository/conf/sample/resources/payload/sample.xml b/repository/conf/sample/resources/payload/sample.xml deleted file mode 100644 index e50409bef9..0000000000 --- a/repository/conf/sample/resources/payload/sample.xml +++ /dev/null @@ -1,6 +0,0 @@ - - $1 - $2 - $3 - $4 - diff --git a/repository/conf/sample/resources/transform/schema.json b/repository/conf/sample/resources/transform/schema.json deleted file mode 100644 index 62b5e55b1f..0000000000 --- a/repository/conf/sample/resources/transform/schema.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "type": "object", - "properties": { - "fruit": { - "type": "string", - "minLength": 4, - "maxLength": 6, - "pattern": "^[0-9]{1,45}$" - }, - "price": { - "type": "number", - "minimum": 2, - "maximum": 20, - "exclusiveMaximum": 20, - "multipleOf": 2.5 - } - }, - "required": [ - "price" - ] -} \ No newline at end of file From 8ba0433c9b787e85d6e7f5a6fdead89d2b48db3c Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Thu, 5 Dec 2024 16:13:33 +0530 Subject: [PATCH 3/6] transform key if it indicates resource: --- .../synapse/config/SynapseConfiguration.java | 3 ++- .../java/org/apache/synapse/config/Utils.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 modules/core/src/main/java/org/apache/synapse/config/Utils.java diff --git a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java index aa072cb125..ada66b0803 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java +++ b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java @@ -1018,6 +1018,7 @@ public Object getEntry(String key) { * @return its value */ public Entry getEntryDefinition(String key) { + String transformedKey = Utils.transformFileKey(key); Object o = localRegistry.get(key); if (o == null || o instanceof Entry) { if (o == null) { @@ -1025,7 +1026,7 @@ public Entry getEntryDefinition(String key) { synchronized (this) { o = localRegistry.get(key); if (o == null) { - Entry entry = new Entry(key); + Entry entry = new Entry(transformedKey); entry.setType(Entry.REMOTE_ENTRY); addEntry(key, entry); return entry; diff --git a/modules/core/src/main/java/org/apache/synapse/config/Utils.java b/modules/core/src/main/java/org/apache/synapse/config/Utils.java new file mode 100644 index 0000000000..561c4d92fb --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/config/Utils.java @@ -0,0 +1,17 @@ +package org.apache.synapse.config; + +import java.io.File; + +public class Utils { + + public static final String RESOURCES_IDENTIFIER = "resources:"; + public static final String CONVERTED_RESOURCES_IDENTIFIER = "gov:mi-resources/"; + + public static String transformFileKey(String fileKey) { + + if (fileKey.startsWith(RESOURCES_IDENTIFIER)) { + return CONVERTED_RESOURCES_IDENTIFIER + fileKey.substring(RESOURCES_IDENTIFIER.length()); + } + return fileKey; + } +} From 3e10b771876272d12e1fd9f082819386ca92eb24 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Thu, 5 Dec 2024 21:27:24 +0530 Subject: [PATCH 4/6] add test for transforming key --- .../synapse/config/SynapseConfigurationTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/modules/core/src/test/java/org/apache/synapse/config/SynapseConfigurationTest.java b/modules/core/src/test/java/org/apache/synapse/config/SynapseConfigurationTest.java index 5b1f7dde1b..016c8e8878 100644 --- a/modules/core/src/test/java/org/apache/synapse/config/SynapseConfigurationTest.java +++ b/modules/core/src/test/java/org/apache/synapse/config/SynapseConfigurationTest.java @@ -23,6 +23,8 @@ import junit.framework.TestCase; import org.apache.synapse.endpoints.HTTPEndpoint; +import org.apache.synapse.registry.url.SimpleURLRegistry; +import org.apache.synapse.registry.url.SimpleURLRegistryTest; public class SynapseConfigurationTest extends TestCase { @@ -120,4 +122,12 @@ public void run() { throw new RuntimeException(e); } } + + public void testGetEntryDefinition() throws Exception { + + String key = "resources:xslt/sample.xslt"; + SynapseConfiguration config = new SynapseConfiguration(); + Entry entry = config.getEntryDefinition(key); + assertEquals("Key of entry should be transformed.", "gov:mi-resources/xslt/sample.xslt", entry.getKey()); + } } From 74ad88a5d9ce8fcd63f777d632b6ff4bc00084fb Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Thu, 5 Dec 2024 22:23:59 +0530 Subject: [PATCH 5/6] add license headers --- .../java/org/apache/synapse/config/Utils.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/Utils.java b/modules/core/src/main/java/org/apache/synapse/config/Utils.java index 561c4d92fb..10f4d23b22 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/Utils.java +++ b/modules/core/src/main/java/org/apache/synapse/config/Utils.java @@ -1,6 +1,22 @@ -package org.apache.synapse.config; +/* + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. 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. + */ -import java.io.File; +package org.apache.synapse.config; public class Utils { From eb46402698fc34fc7b4469fabe01f7adde2f7e61 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Sat, 7 Dec 2024 12:18:06 +0530 Subject: [PATCH 6/6] address reviewed changes --- .../synapse/config/SynapseConfigUtils.java | 10 ++++++ .../synapse/config/SynapseConfiguration.java | 2 +- .../java/org/apache/synapse/config/Utils.java | 33 ------------------- 3 files changed, 11 insertions(+), 34 deletions(-) delete mode 100644 modules/core/src/main/java/org/apache/synapse/config/Utils.java diff --git a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java index f1c9aaa59e..718634ef14 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java +++ b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java @@ -93,6 +93,8 @@ public class SynapseConfigUtils { private static final Log log = LogFactory.getLog(SynapseConfigUtils.class); + public static final String RESOURCES_IDENTIFIER = "resources:"; + public static final String CONVERTED_RESOURCES_IDENTIFIER = "gov:mi-resources/"; private static ConcurrentHashMap lastRegisteredSynapseConfigurationMap = new ConcurrentHashMap(); @@ -903,5 +905,13 @@ public static SynapseConfiguration getSynapseConfiguration(String tenantDomain){ public static void addSynapseConfiguration(String tenantDomain , SynapseConfiguration synapseConfiguration){ lastRegisteredSynapseConfigurationMap.put(tenantDomain,synapseConfiguration); } + + public static String transformFileKey(String fileKey) { + + if (fileKey.startsWith(RESOURCES_IDENTIFIER)) { + return CONVERTED_RESOURCES_IDENTIFIER + fileKey.substring(RESOURCES_IDENTIFIER.length()); + } + return fileKey; + } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java index ada66b0803..01aec6f80e 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java +++ b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java @@ -1018,7 +1018,7 @@ public Object getEntry(String key) { * @return its value */ public Entry getEntryDefinition(String key) { - String transformedKey = Utils.transformFileKey(key); + String transformedKey = SynapseConfigUtils.transformFileKey(key); Object o = localRegistry.get(key); if (o == null || o instanceof Entry) { if (o == null) { diff --git a/modules/core/src/main/java/org/apache/synapse/config/Utils.java b/modules/core/src/main/java/org/apache/synapse/config/Utils.java deleted file mode 100644 index 10f4d23b22..0000000000 --- a/modules/core/src/main/java/org/apache/synapse/config/Utils.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. 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.synapse.config; - -public class Utils { - - public static final String RESOURCES_IDENTIFIER = "resources:"; - public static final String CONVERTED_RESOURCES_IDENTIFIER = "gov:mi-resources/"; - - public static String transformFileKey(String fileKey) { - - if (fileKey.startsWith(RESOURCES_IDENTIFIER)) { - return CONVERTED_RESOURCES_IDENTIFIER + fileKey.substring(RESOURCES_IDENTIFIER.length()); - } - return fileKey; - } -}