Skip to content

Commit adb32f3

Browse files
Merge pull request #2225 from SanojPunchihewa/variable-mediator
Add new variable mediator
2 parents 33e7ad1 + b5df7b1 commit adb32f3

File tree

13 files changed

+636
-2
lines changed

13 files changed

+636
-2
lines changed

modules/core/src/main/java/org/apache/synapse/MessageContext.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,27 @@ public interface MessageContext {
457457
* @param tracingState Set whether the message flowtracing is enabled or not
458458
*/
459459
public void setMessageFlowTracingState(int tracingState);
460+
461+
/**
462+
* Get the value of a variable on the message instance
463+
*
464+
* @param key key to look up variable
465+
* @return value for the given key
466+
*/
467+
public Object getVariable(String key);
468+
469+
/**
470+
* Set a variable with the given name on the message instance
471+
*
472+
* @param key key to be used
473+
* @param value value to be saved
474+
*/
475+
public void setVariable(String key, Object value);
476+
477+
/**
478+
* Returns the Set of keys over the variables on this message context
479+
*
480+
* @return a Set of keys over message variables
481+
*/
482+
public Set getVariableKeySet();
460483
}

modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public abstract class AbstractMediatorFactory implements MediatorFactory {
6565
= new QName(XMLConfigConstants.STATISTICS_ATTRIB_NAME);
6666
protected static final QName PROP_Q
6767
= new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "property");
68+
protected static final QName VARIABLE_Q
69+
= new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "variable");
6870
protected static final QName PROPERTY_GROUP_Q
6971
= new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "propertyGroup");
7072
protected static final QName FEATURE_Q

modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public class MediatorFactoryFinder implements XMLToObjectMapper {
102102
CommentMediatorFactory.class,
103103
ForEachMediatorFactory.class,
104104
JSONTransformMediatorFactory.class,
105-
NTLMMediatorFactory.class
105+
NTLMMediatorFactory.class,
106+
VariableMediatorFactory.class
106107
};
107108

108109
private final static MediatorFactoryFinder instance = new MediatorFactoryFinder();

modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public class MediatorSerializerFinder {
7676
CommentMediatorSerializer.class,
7777
ForEachMediatorSerializer.class,
7878
JSONTransformMediatorSerializer.class,
79-
NTLMMediatorSerializer.class
79+
NTLMMediatorSerializer.class,
80+
VariableMediatorSerializer.class
8081
};
8182

8283
private final static MediatorSerializerFinder instance = new MediatorSerializerFinder();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.apache.synapse.config.xml;
20+
21+
import org.apache.axiom.om.OMAttribute;
22+
import org.apache.axiom.om.OMElement;
23+
import org.apache.synapse.Mediator;
24+
import org.apache.synapse.SynapseException;
25+
import org.apache.synapse.mediators.v2.VariableMediator;
26+
import org.jaxen.JaxenException;
27+
28+
import java.util.Properties;
29+
import javax.xml.namespace.QName;
30+
31+
/**
32+
* Creates a variable mediator through the supplied XML configuration
33+
* <p/>
34+
* <pre>
35+
* &lt;variable name="string" [action=set/remove] (value="literal" | expression="expression") type="string|integer|JSON"/&gt;
36+
* </pre>
37+
*/
38+
public class VariableMediatorFactory extends AbstractMediatorFactory {
39+
40+
private static final QName ATT_ACTION = new QName("action");
41+
private static final QName ATT_TYPE = new QName("type");
42+
43+
public Mediator createSpecificMediator(OMElement elem, Properties properties) {
44+
45+
VariableMediator variableMediator = new VariableMediator();
46+
OMAttribute name = elem.getAttribute(ATT_NAME);
47+
OMAttribute value = elem.getAttribute(ATT_VALUE);
48+
OMAttribute expression = elem.getAttribute(ATT_EXPRN);
49+
OMAttribute action = elem.getAttribute(ATT_ACTION);
50+
OMAttribute type = elem.getAttribute(ATT_TYPE);
51+
52+
if (name == null || name.getAttributeValue().isEmpty()) {
53+
String msg = "The 'name' attribute is required for the configuration of a variable mediator";
54+
log.error(msg);
55+
throw new SynapseException(msg);
56+
} else if ((value == null && expression == null) &&
57+
!(action != null && "remove".equals(action.getAttributeValue()))) {
58+
String msg = "'value' or 'expression' attributes is required for a variable mediator when action is SET";
59+
log.error(msg);
60+
throw new SynapseException(msg);
61+
}
62+
variableMediator.setName(name.getAttributeValue());
63+
64+
String dataType = null;
65+
if (type != null) {
66+
dataType = type.getAttributeValue();
67+
}
68+
69+
if (value != null) {
70+
variableMediator.setValue(value.getAttributeValue(), dataType);
71+
} else if (expression != null) {
72+
try {
73+
variableMediator.setExpression(SynapsePathFactory.getSynapsePath(elem, ATT_EXPRN),
74+
dataType);
75+
} catch (JaxenException e) {
76+
String msg = "Invalid expression for attribute 'expression' : " +
77+
expression.getAttributeValue();
78+
log.error(msg);
79+
throw new SynapseException(msg);
80+
}
81+
}
82+
83+
if (action != null && "remove".equals(action.getAttributeValue())) {
84+
variableMediator.setAction(VariableMediator.ACTION_REMOVE);
85+
}
86+
processAuditStatus(variableMediator, elem);
87+
addAllCommentChildrenToList(elem, variableMediator.getCommentsList());
88+
return variableMediator;
89+
}
90+
91+
public QName getTagQName() {
92+
93+
return VARIABLE_Q;
94+
}
95+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.apache.synapse.config.xml;
20+
21+
import org.apache.axiom.om.OMElement;
22+
import org.apache.synapse.Mediator;
23+
import org.apache.synapse.mediators.v2.VariableMediator;
24+
25+
/**
26+
* <pre>
27+
* &lt;variable name="string" [action=set/remove] (value="literal" | expression="expression") type="string|integer|JSON"/&gt;
28+
* </pre>
29+
*/
30+
public class VariableMediatorSerializer extends AbstractMediatorSerializer {
31+
32+
public OMElement serializeSpecificMediator(Mediator m) {
33+
34+
if (!(m instanceof VariableMediator)) {
35+
handleException("Unsupported mediator passed in for serialization : " + m.getType());
36+
}
37+
38+
VariableMediator mediator = (VariableMediator) m;
39+
OMElement variable = fac.createOMElement("variable", synNS);
40+
saveTracingState(variable, mediator);
41+
42+
if (mediator.getName() != null) {
43+
variable.addAttribute(fac.createOMAttribute(
44+
"name", nullNS, mediator.getName()));
45+
} else {
46+
handleException("Invalid variable mediator. Name is required");
47+
}
48+
49+
if (mediator.getValue() != null) {
50+
variable.addAttribute(fac.createOMAttribute(
51+
"value", nullNS, mediator.getValue().toString()));
52+
} else if (mediator.getExpression() != null) {
53+
SynapsePathSerializer.serializePath((SynapsePath) mediator.getExpression(),
54+
variable, "expression");
55+
} else if (mediator.getAction() == VariableMediator.ACTION_SET) {
56+
handleException("Invalid variable mediator. Value or expression is required if " +
57+
"action is SET");
58+
}
59+
60+
if (mediator.getAction() == VariableMediator.ACTION_REMOVE) {
61+
variable.addAttribute(fac.createOMAttribute(
62+
"action", nullNS, "remove"));
63+
} else if (mediator.getType() != null) {
64+
variable.addAttribute(fac.createOMAttribute(
65+
"type", nullNS, mediator.getType()));
66+
}
67+
68+
serializeComments(variable, mediator.getCommentsList());
69+
70+
return variable;
71+
}
72+
73+
public String getMediatorClassName() {
74+
75+
return VariableMediator.class.getName();
76+
}
77+
}

modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2MessageContext.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public class Axis2MessageContext implements MessageContext {
6868
*/
6969
private final Map<String, Object> properties = new HashMap<String, Object>();
7070

71+
/**
72+
* Synapse Message Context variables
73+
*/
74+
private final Map<String, Object> variables = new HashMap<>();
75+
7176
/**
7277
* Local entries fetched from the configuration or from the registry for the transactional
7378
* resource access
@@ -725,4 +730,22 @@ public HashMap<String, Object> getAnalyticsMetadata() {
725730
//noinspection unchecked
726731
return (HashMap<String, Object>) getProperty(SynapseConstants.ANALYTICS_METADATA);
727732
}
733+
734+
@Override
735+
public Object getVariable(String key) {
736+
return variables.get(key);
737+
}
738+
739+
@Override
740+
public void setVariable(String key, Object value) {
741+
if (value == null) {
742+
return;
743+
}
744+
variables.put(key, value);
745+
}
746+
747+
@Override
748+
public Set getVariableKeySet() {
749+
return variables.keySet();
750+
}
728751
}

0 commit comments

Comments
 (0)