Skip to content

Commit

Permalink
handle resource file key in xslt,jsontransform,payload,script,validat…
Browse files Browse the repository at this point in the history
…e,xquery mediators
  • Loading branch information
chathuranga-jayanath-99 committed Nov 30, 2024
1 parent 4ea7214 commit 76f646f
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 25 deletions.
17 changes: 17 additions & 0 deletions modules/core/src/main/java/org/apache/synapse/mediators/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "<jsonObject>\n" +
" <fruit>12345</fruit>\n" +
" <quantity>10</quantity>\n" +
"</jsonObject>";

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\"]"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = "<soapenv:Body xmlns:soapenv=\"http://schemas.xmlsoap"
+ ".org/soap/envelope/\"><p:addCustomer xmlns:p=\"http://ws.wso2.org/dataservice\">\n"
+ "<xs:name xmlns:xs=\"http://ws.wso2.org/dataservice\">John</xs:name>\n"
+ "<xs:request_time xmlns:xs=\"http://ws.wso2.org/dataservice\">2017.09.26</xs:request_time>\n"
+ "<xs:tp_number xmlns:xs=\"http://ws.wso2.org/dataservice\">1234564632</xs:tp_number>\n"
+ "<xs:address xmlns:xs=\"http://ws.wso2.org/dataservice\">Colombo, Sri Lanka</xs:address>\n"
+ "</p:addCustomer></soapenv:Body>";

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 = "<soapenv:Body xmlns:soapenv=\"http://schemas.xmlsoap"
+ ".org/soap/envelope/\"><p:addCustomer xmlns:p=\"http://ws.wso2.org/dataservice\">\n"
+ "<xs:name xmlns:xs=\"http://ws.wso2.org/dataservice\">John</xs:name>\n"
+ "<xs:request_time xmlns:xs=\"http://ws.wso2.org/dataservice\">2017.09.26</xs:request_time>\n"
+ "<xs:tp_number xmlns:xs=\"http://ws.wso2.org/dataservice\">1234564632</xs:tp_number>\n"
+ "<xs:address xmlns:xs=\"http://ws.wso2.org/dataservice\">Colombo, Sri Lanka</xs:address>\n"
+ "</p:addCustomer></soapenv:Body>";

assertEquals("PayloadFactory mediator has not "
+ "set expected format", expectedEnv.replaceAll("[\\r\\n]", ""), synCtx.getEnvelope().getBody().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 76f646f

Please sign in to comment.