From bf80787d626674fe24df8a19b8c66fc84cc3e757 Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Mon, 16 Dec 2024 15:54:20 +0530 Subject: [PATCH 1/2] Add Trigger Error mediator Add Trigger Error mediator --- .../org/apache/synapse/SynapseConstants.java | 2 + .../config/xml/MediatorFactoryFinder.java | 3 +- .../config/xml/MediatorSerializerFinder.java | 3 +- .../xml/TriggerErrorMediatorFactory.java | 76 ++++++++++++++++ .../xml/TriggerErrorMediatorSerializer.java | 57 ++++++++++++ .../synapse/mediators/v2/TriggerError.java | 89 +++++++++++++++++++ .../xml/TriggerErrorMediatorFactoryTest.java | 55 ++++++++++++ .../TriggerErrorMediatorSerializerTest.java | 31 +++++++ 8 files changed, 314 insertions(+), 2 deletions(-) create mode 100644 modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java create mode 100644 modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializer.java create mode 100644 modules/core/src/main/java/org/apache/synapse/mediators/v2/TriggerError.java create mode 100644 modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactoryTest.java create mode 100644 modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java diff --git a/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java b/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java index edcdbeb1b0..be73b0d127 100644 --- a/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java +++ b/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java @@ -628,4 +628,6 @@ public enum ENDPOINT_TIMEOUT_TYPE { ENDPOINT_TIMEOUT, GLOBAL_TIMEOUT, HTTP_CONNE public static final String SCATTER_MESSAGES = "SCATTER_MESSAGES"; public static final String CONTINUE_FLOW_TRIGGERED_FROM_MEDIATOR_WORKER = "CONTINUE_FLOW_TRIGGERED_FROM_MEDIATOR_WORKER"; + + public static final String DEFAULT_ERROR_TYPE = "ANY"; } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java b/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java index cda214f563..a851aba0c4 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java @@ -104,7 +104,8 @@ public class MediatorFactoryFinder implements XMLToObjectMapper { JSONTransformMediatorFactory.class, NTLMMediatorFactory.class, VariableMediatorFactory.class, - ScatterGatherMediatorFactory.class + ScatterGatherMediatorFactory.class, + TriggerErrorMediatorFactory.class }; private final static MediatorFactoryFinder instance = new MediatorFactoryFinder(); diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java b/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java index eb12240762..dfca0234b7 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java @@ -78,7 +78,8 @@ public class MediatorSerializerFinder { JSONTransformMediatorSerializer.class, NTLMMediatorSerializer.class, VariableMediatorSerializer.class, - ScatterGatherMediatorSerializer.class + ScatterGatherMediatorSerializer.class, + TriggerErrorMediatorSerializer.class }; private final static MediatorSerializerFinder instance = new MediatorSerializerFinder(); diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java new file mode 100644 index 0000000000..21bac68966 --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * 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.xml; + +import org.apache.axiom.om.OMAttribute; +import org.apache.axiom.om.OMElement; +import org.apache.synapse.Mediator; +import org.apache.synapse.SynapseConstants; +import org.apache.synapse.SynapseException; +import org.apache.synapse.mediators.v2.TriggerError; +import org.jaxen.JaxenException; + +import javax.xml.namespace.QName; +import java.util.Properties; + +/** + * Creates a trigger error mediator through the supplied XML configuration + *

+ *

+ * <triggererror type="string" (errorMessage="string" | expression="expression")/>
+ * 
+ */ +public class TriggerErrorMediatorFactory extends AbstractMediatorFactory { + + private static final QName ATT_ERROR_MSG = new QName("errorMessage"); + private static final QName ATT_TYPE = new QName("type"); + private static final QName TRIGGER_ERROR_Q + = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "triggererror"); + + @Override + protected Mediator createSpecificMediator(OMElement elem, Properties properties) { + TriggerError triggerErrorMediator = new TriggerError(); + OMAttribute type = elem.getAttribute(ATT_TYPE); + OMAttribute errorMsg = elem.getAttribute(ATT_ERROR_MSG); + OMAttribute expression = elem.getAttribute(ATT_EXPRN); + + if (type == null || type.getAttributeValue().isEmpty()) { + triggerErrorMediator.setType(SynapseConstants.DEFAULT_ERROR_TYPE); + } else { + triggerErrorMediator.setType(type.getAttributeValue()); + } + if (expression != null) { + try { + triggerErrorMediator.setExpression(SynapsePathFactory.getSynapsePath(elem, ATT_EXPRN)); + } catch (JaxenException e) { + throw new SynapseException("Invalid expression for attribute 'expression' : " + + expression.getAttributeValue()); + } + } else if (errorMsg != null && !errorMsg.getAttributeValue().isEmpty()) { + triggerErrorMediator.setErrorMsg(errorMsg.getAttributeValue()); + } else { + triggerErrorMediator.setErrorMsg("Error occurred in the mediation flow"); + } + return triggerErrorMediator; + } + + @Override + public QName getTagQName() { + return TRIGGER_ERROR_Q; + } +} diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializer.java new file mode 100644 index 0000000000..62fefbf41e --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializer.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * 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.xml; + +import org.apache.axiom.om.OMElement; +import org.apache.synapse.Mediator; +import org.apache.synapse.mediators.v2.TriggerError; + +/** + *
+ * <triggererror type="string" (errorMessage="string" | expression="expression")/>
+ * 
+ */ +public class TriggerErrorMediatorSerializer extends AbstractMediatorSerializer{ + @Override + protected OMElement serializeSpecificMediator(Mediator m) { + if (!(m instanceof TriggerError)) { + handleException("Unsupported mediator passed in for serialization : " + m.getType()); + } + + TriggerError mediator = (TriggerError) m; + OMElement triggerError = fac.createOMElement("triggererror", synNS); + saveTracingState(triggerError, mediator); + + if (mediator.getType() != null) { + triggerError.addAttribute(fac.createOMAttribute("type", nullNS, mediator.getType())); + } + if (mediator.getExpression() != null) { + SynapsePathSerializer.serializePath(mediator.getExpression(), triggerError, "expression"); + } else if (mediator.getErrorMsg() != null) { + triggerError.addAttribute(fac.createOMAttribute("errorMessage", nullNS, mediator.getErrorMsg())); + } + + serializeComments(triggerError, mediator.getCommentsList()); + return triggerError; + } + + @Override + public String getMediatorClassName() { + return TriggerError.class.getName(); + } +} diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/v2/TriggerError.java b/modules/core/src/main/java/org/apache/synapse/mediators/v2/TriggerError.java new file mode 100644 index 0000000000..7801471d99 --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/mediators/v2/TriggerError.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * 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.mediators.v2; + +import org.apache.synapse.MessageContext; +import org.apache.synapse.SynapseConstants; +import org.apache.synapse.SynapseException; +import org.apache.synapse.SynapseLog; +import org.apache.synapse.config.xml.SynapsePath; +import org.apache.synapse.mediators.AbstractMediator; + +/** + * This mediator is used to trigger an error in the mediation flow. + */ +public class TriggerError extends AbstractMediator { + private String type = null; + private String errorMsg = null; + private SynapsePath expression = null; + + @Override + public boolean mediate(MessageContext synCtx) { + if (synCtx.getEnvironment().isDebuggerEnabled()) { + if (super.divertMediationRoute(synCtx)) { + return true; + } + } + + SynapseLog synLog = getLog(synCtx); + if (synLog.isTraceOrDebugEnabled()) { + synLog.traceOrDebug("Start : TriggerError mediator"); + if (synLog.isTraceTraceEnabled()) { + synLog.traceTrace("Message : " + synCtx.getEnvelope()); + } + } + + String desc = null; + if (errorMsg != null) { + desc = errorMsg.toString(); + } else if (expression != null) { + desc = expression.stringValueOf(synCtx); + } + + synLog.traceOrDebug("End : TriggerError mediator"); + synCtx.setProperty(SynapseConstants.ERROR_CODE, type); + synCtx.setProperty(SynapseConstants.ERROR_MESSAGE, desc); + throw new SynapseException(desc, new Throwable(type)); + } + + @Override + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + + public SynapsePath getExpression() { + return expression; + } + + public void setExpression(SynapsePath expression) { + this.expression = expression; + } + + public String getErrorMsg() { + return errorMsg; + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } +} diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactoryTest.java b/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactoryTest.java new file mode 100644 index 0000000000..6eabf59f53 --- /dev/null +++ b/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactoryTest.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * 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.xml; + +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.util.AXIOMUtil; +import org.apache.synapse.mediators.v2.TriggerError; +import org.junit.Assert; +import org.junit.Test; + +import javax.xml.stream.XMLStreamException; + +/** + * This is the test class for TriggerErrorMediatorFactory class. + */ +public class TriggerErrorMediatorFactoryTest { + + /** + * Test create TriggerError with given XML configuration and asserting it is created. + * + * @throws XMLStreamException - XMLStreamException + */ + @Test + public void testTriggerErrorExp() throws XMLStreamException { + String inputXML = ""; + OMElement element = AXIOMUtil.stringToOM(inputXML); + TriggerErrorMediatorFactory triggerErrorMediatorFactory = new TriggerErrorMediatorFactory(); + TriggerError triggerError = (TriggerError) triggerErrorMediatorFactory.createSpecificMediator(element,null); + Assert.assertNotNull("${$.abc}", triggerError.getExpression().toString()); + } + + @Test + public void testTriggerErrorMsg() throws XMLStreamException { + String inputXML = ""; + OMElement element = AXIOMUtil.stringToOM(inputXML); + TriggerErrorMediatorFactory triggerErrorMediatorFactory = new TriggerErrorMediatorFactory(); + TriggerError triggerError = (TriggerError) triggerErrorMediatorFactory.createSpecificMediator(element,null); + Assert.assertNotNull("abc", triggerError.getErrorMsg()); + } +} diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java b/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java new file mode 100644 index 0000000000..3fac601831 --- /dev/null +++ b/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java @@ -0,0 +1,31 @@ +package org.apache.synapse.config.xml; + +import org.apache.axiom.om.OMElement; +import org.apache.synapse.mediators.v2.TriggerError; +import org.apache.synapse.util.xpath.SynapseExpression; +import org.jaxen.JaxenException; +import org.junit.Assert; +import org.junit.Test; + +import javax.xml.namespace.QName; + +public class TriggerErrorMediatorSerializerTest { + + @Test + public void testSerializeTriggerErrorMediator() throws JaxenException { + TriggerErrorMediatorSerializer triggerErrorMediatorSerializer = new TriggerErrorMediatorSerializer(); + TriggerError mediator = new TriggerError(); + String type = "TRANSPORT:TIMEOUT"; + String err = "Error occurred"; + String exp = "${payload.err}"; + mediator.setExpression(new SynapseExpression(exp)); + mediator.setType(type); + OMElement element = triggerErrorMediatorSerializer.serializeSpecificMediator(mediator); + Assert.assertEquals("invalid type", type, element.getAttributeValue(new QName("type"))); + Assert.assertEquals("invalid expression", exp, element.getAttributeValue(new QName("expression"))); + mediator.setExpression(null); + mediator.setErrorMsg(err); + element = triggerErrorMediatorSerializer.serializeSpecificMediator(mediator); + Assert.assertEquals("invalid error message", err, element.getAttributeValue(new QName("errorMessage"))); + } +} From c7d3f461be91e86c05e29c674fe25373ed17371a Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Tue, 17 Dec 2024 09:16:15 +0530 Subject: [PATCH 2/2] Address review comments Address review comments --- .../config/xml/MediatorFactoryFinder.java | 2 +- .../config/xml/MediatorSerializerFinder.java | 2 +- .../config/xml/SynapsePathSerializer.java | 8 ++ .../config/xml/ThrowErrorMediatorFactory.java | 71 +++++++++++++++++ ...java => ThrowErrorMediatorSerializer.java} | 30 ++++---- .../xml/TriggerErrorMediatorFactory.java | 76 ------------------- .../v2/{TriggerError.java => ThrowError.java} | 37 +++------ ...ava => ThrowErrorMediatorFactoryTest.java} | 26 +++---- .../xml/ThrowErrorMediatorSerializerTest.java | 31 ++++++++ .../TriggerErrorMediatorSerializerTest.java | 31 -------- 10 files changed, 151 insertions(+), 163 deletions(-) create mode 100644 modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactory.java rename modules/core/src/main/java/org/apache/synapse/config/xml/{TriggerErrorMediatorSerializer.java => ThrowErrorMediatorSerializer.java} (53%) delete mode 100644 modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java rename modules/core/src/main/java/org/apache/synapse/mediators/v2/{TriggerError.java => ThrowError.java} (64%) rename modules/core/src/test/java/org/apache/synapse/config/xml/{TriggerErrorMediatorFactoryTest.java => ThrowErrorMediatorFactoryTest.java} (53%) create mode 100644 modules/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializerTest.java delete mode 100644 modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java b/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java index a851aba0c4..ea3c352b65 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java @@ -105,7 +105,7 @@ public class MediatorFactoryFinder implements XMLToObjectMapper { NTLMMediatorFactory.class, VariableMediatorFactory.class, ScatterGatherMediatorFactory.class, - TriggerErrorMediatorFactory.class + ThrowErrorMediatorFactory.class }; private final static MediatorFactoryFinder instance = new MediatorFactoryFinder(); diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java b/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java index dfca0234b7..8ccd913b0d 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java @@ -79,7 +79,7 @@ public class MediatorSerializerFinder { NTLMMediatorSerializer.class, VariableMediatorSerializer.class, ScatterGatherMediatorSerializer.class, - TriggerErrorMediatorSerializer.class + ThrowErrorMediatorSerializer.class }; private final static MediatorSerializerFinder instance = new MediatorSerializerFinder(); diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/SynapsePathSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/SynapsePathSerializer.java index e2d52acf7d..67b30b497b 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/SynapsePathSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/SynapsePathSerializer.java @@ -71,6 +71,14 @@ public static OMElement serializePath(SynapsePath path, String expression, } else if(path.getPathType() == SynapsePath.X_PATH) { elem.addAttribute(elem.getOMFactory().createOMAttribute( attribName, nullNS, expression)); + } else if (path.getPathType() == SynapsePath.SYNAPSE_EXPRESSIONS_PATH) { + if (expression.startsWith("{") && expression.endsWith("}")) { + elem.addAttribute(elem.getOMFactory().createOMAttribute( + attribName, nullNS, expression)); + } else { + elem.addAttribute(elem.getOMFactory().createOMAttribute( + attribName, nullNS, "{" + expression + "}")); + } } serializeNamespaces(elem, path); diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactory.java new file mode 100644 index 0000000000..86d5171b15 --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactory.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * 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.xml; + +import org.apache.axiom.om.OMAttribute; +import org.apache.axiom.om.OMElement; +import org.apache.synapse.Mediator; +import org.apache.synapse.SynapseConstants; +import org.apache.synapse.mediators.Value; +import org.apache.synapse.mediators.v2.ThrowError; + +import javax.xml.namespace.QName; +import java.util.Properties; + +/** + * Creates a throw error mediator through the supplied XML configuration + *

+ *

+ * <throwError (type="string") (errorMessage=("string" | expression))/>
+ * 
+ */ +public class ThrowErrorMediatorFactory extends AbstractMediatorFactory { + + private static final String ERROR_MESSAGE = "errorMessage"; + private static final String THROW_ERROR = "throwError"; + private static final QName ATT_ERROR_MSG = new QName(ERROR_MESSAGE); + private static final QName ATT_TYPE = new QName("type"); + private static final QName THROW_ERROR_Q + = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, THROW_ERROR); + + @Override + protected Mediator createSpecificMediator(OMElement elem, Properties properties) { + ThrowError throwErrorMediator = new ThrowError(); + OMAttribute type = elem.getAttribute(ATT_TYPE); + OMAttribute errorMsg = elem.getAttribute(ATT_ERROR_MSG); + + if (type == null || type.getAttributeValue().isEmpty()) { + throwErrorMediator.setType(SynapseConstants.DEFAULT_ERROR_TYPE); + } else { + throwErrorMediator.setType(type.getAttributeValue()); + } + if (errorMsg == null || errorMsg.getAttributeValue().isEmpty()) { + throwErrorMediator.setErrorMsg(new Value("Error occurred in the mediation flow")); + } else { + ValueFactory msgFactory = new ValueFactory(); + Value generatedMsg = msgFactory.createValue(ERROR_MESSAGE, elem); + throwErrorMediator.setErrorMsg(generatedMsg); + } + return throwErrorMediator; + } + + @Override + public QName getTagQName() { + return THROW_ERROR_Q; + } +} diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializer.java similarity index 53% rename from modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializer.java rename to modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializer.java index 62fefbf41e..1cd5790c03 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializer.java @@ -19,39 +19,39 @@ import org.apache.axiom.om.OMElement; import org.apache.synapse.Mediator; -import org.apache.synapse.mediators.v2.TriggerError; +import org.apache.synapse.mediators.v2.ThrowError; /** *
- * <triggererror type="string" (errorMessage="string" | expression="expression")/>
+ * <throwError (type="string") (errorMessage=("string" | expression))/>
  * 
*/ -public class TriggerErrorMediatorSerializer extends AbstractMediatorSerializer{ +public class ThrowErrorMediatorSerializer extends AbstractMediatorSerializer{ + @Override protected OMElement serializeSpecificMediator(Mediator m) { - if (!(m instanceof TriggerError)) { + if (!(m instanceof ThrowError)) { handleException("Unsupported mediator passed in for serialization : " + m.getType()); } - TriggerError mediator = (TriggerError) m; - OMElement triggerError = fac.createOMElement("triggererror", synNS); - saveTracingState(triggerError, mediator); + ThrowError mediator = (ThrowError) m; + OMElement throwError = fac.createOMElement("throwError", synNS); + saveTracingState(throwError, mediator); if (mediator.getType() != null) { - triggerError.addAttribute(fac.createOMAttribute("type", nullNS, mediator.getType())); + throwError.addAttribute(fac.createOMAttribute("type", nullNS, mediator.getType())); } - if (mediator.getExpression() != null) { - SynapsePathSerializer.serializePath(mediator.getExpression(), triggerError, "expression"); - } else if (mediator.getErrorMsg() != null) { - triggerError.addAttribute(fac.createOMAttribute("errorMessage", nullNS, mediator.getErrorMsg())); + if (mediator.getErrorMsg() != null) { + ValueSerializer valueSerializer = new ValueSerializer(); + valueSerializer.serializeValue(mediator.getErrorMsg(), "errorMessage", throwError); } - serializeComments(triggerError, mediator.getCommentsList()); - return triggerError; + serializeComments(throwError, mediator.getCommentsList()); + return throwError; } @Override public String getMediatorClassName() { - return TriggerError.class.getName(); + return ThrowError.class.getName(); } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java deleted file mode 100644 index 21bac68966..0000000000 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. - * - * 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.xml; - -import org.apache.axiom.om.OMAttribute; -import org.apache.axiom.om.OMElement; -import org.apache.synapse.Mediator; -import org.apache.synapse.SynapseConstants; -import org.apache.synapse.SynapseException; -import org.apache.synapse.mediators.v2.TriggerError; -import org.jaxen.JaxenException; - -import javax.xml.namespace.QName; -import java.util.Properties; - -/** - * Creates a trigger error mediator through the supplied XML configuration - *

- *

- * <triggererror type="string" (errorMessage="string" | expression="expression")/>
- * 
- */ -public class TriggerErrorMediatorFactory extends AbstractMediatorFactory { - - private static final QName ATT_ERROR_MSG = new QName("errorMessage"); - private static final QName ATT_TYPE = new QName("type"); - private static final QName TRIGGER_ERROR_Q - = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "triggererror"); - - @Override - protected Mediator createSpecificMediator(OMElement elem, Properties properties) { - TriggerError triggerErrorMediator = new TriggerError(); - OMAttribute type = elem.getAttribute(ATT_TYPE); - OMAttribute errorMsg = elem.getAttribute(ATT_ERROR_MSG); - OMAttribute expression = elem.getAttribute(ATT_EXPRN); - - if (type == null || type.getAttributeValue().isEmpty()) { - triggerErrorMediator.setType(SynapseConstants.DEFAULT_ERROR_TYPE); - } else { - triggerErrorMediator.setType(type.getAttributeValue()); - } - if (expression != null) { - try { - triggerErrorMediator.setExpression(SynapsePathFactory.getSynapsePath(elem, ATT_EXPRN)); - } catch (JaxenException e) { - throw new SynapseException("Invalid expression for attribute 'expression' : " + - expression.getAttributeValue()); - } - } else if (errorMsg != null && !errorMsg.getAttributeValue().isEmpty()) { - triggerErrorMediator.setErrorMsg(errorMsg.getAttributeValue()); - } else { - triggerErrorMediator.setErrorMsg("Error occurred in the mediation flow"); - } - return triggerErrorMediator; - } - - @Override - public QName getTagQName() { - return TRIGGER_ERROR_Q; - } -} diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/v2/TriggerError.java b/modules/core/src/main/java/org/apache/synapse/mediators/v2/ThrowError.java similarity index 64% rename from modules/core/src/main/java/org/apache/synapse/mediators/v2/TriggerError.java rename to modules/core/src/main/java/org/apache/synapse/mediators/v2/ThrowError.java index 7801471d99..62969dcd86 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/v2/TriggerError.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/v2/ThrowError.java @@ -21,16 +21,15 @@ import org.apache.synapse.SynapseConstants; import org.apache.synapse.SynapseException; import org.apache.synapse.SynapseLog; -import org.apache.synapse.config.xml.SynapsePath; import org.apache.synapse.mediators.AbstractMediator; +import org.apache.synapse.mediators.Value; /** - * This mediator is used to trigger an error in the mediation flow. + * This mediator is used to throw an error from the mediation flow. */ -public class TriggerError extends AbstractMediator { +public class ThrowError extends AbstractMediator { private String type = null; - private String errorMsg = null; - private SynapsePath expression = null; + private Value errorMsg = null; @Override public boolean mediate(MessageContext synCtx) { @@ -42,23 +41,18 @@ public boolean mediate(MessageContext synCtx) { SynapseLog synLog = getLog(synCtx); if (synLog.isTraceOrDebugEnabled()) { - synLog.traceOrDebug("Start : TriggerError mediator"); + synLog.traceOrDebug("Start : ThrowError mediator"); if (synLog.isTraceTraceEnabled()) { synLog.traceTrace("Message : " + synCtx.getEnvelope()); } } - String desc = null; - if (errorMsg != null) { - desc = errorMsg.toString(); - } else if (expression != null) { - desc = expression.stringValueOf(synCtx); - } + String error = errorMsg.evaluateValue(synCtx); - synLog.traceOrDebug("End : TriggerError mediator"); + synLog.traceOrDebug("End : ThrowError mediator"); synCtx.setProperty(SynapseConstants.ERROR_CODE, type); - synCtx.setProperty(SynapseConstants.ERROR_MESSAGE, desc); - throw new SynapseException(desc, new Throwable(type)); + synCtx.setProperty(SynapseConstants.ERROR_MESSAGE, error); + throw new SynapseException(error, new Throwable(type)); } @Override @@ -70,20 +64,11 @@ public void setType(String type) { this.type = type; } - - public SynapsePath getExpression() { - return expression; - } - - public void setExpression(SynapsePath expression) { - this.expression = expression; - } - - public String getErrorMsg() { + public Value getErrorMsg() { return errorMsg; } - public void setErrorMsg(String errorMsg) { + public void setErrorMsg(Value errorMsg) { this.errorMsg = errorMsg; } } diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactoryTest.java b/modules/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactoryTest.java similarity index 53% rename from modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactoryTest.java rename to modules/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactoryTest.java index 6eabf59f53..3a2595310e 100644 --- a/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactoryTest.java +++ b/modules/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactoryTest.java @@ -19,16 +19,16 @@ import org.apache.axiom.om.OMElement; import org.apache.axiom.om.util.AXIOMUtil; -import org.apache.synapse.mediators.v2.TriggerError; +import org.apache.synapse.mediators.v2.ThrowError; import org.junit.Assert; import org.junit.Test; import javax.xml.stream.XMLStreamException; /** - * This is the test class for TriggerErrorMediatorFactory class. + * This is the test class for ThrowErrorMediatorFactory class. */ -public class TriggerErrorMediatorFactoryTest { +public class ThrowErrorMediatorFactoryTest { /** * Test create TriggerError with given XML configuration and asserting it is created. @@ -36,20 +36,20 @@ public class TriggerErrorMediatorFactoryTest { * @throws XMLStreamException - XMLStreamException */ @Test - public void testTriggerErrorExp() throws XMLStreamException { - String inputXML = ""; + public void testThrowErrorExp() throws XMLStreamException { + String inputXML = ""; OMElement element = AXIOMUtil.stringToOM(inputXML); - TriggerErrorMediatorFactory triggerErrorMediatorFactory = new TriggerErrorMediatorFactory(); - TriggerError triggerError = (TriggerError) triggerErrorMediatorFactory.createSpecificMediator(element,null); - Assert.assertNotNull("${$.abc}", triggerError.getExpression().toString()); + ThrowErrorMediatorFactory throwErrorMediatorFactory = new ThrowErrorMediatorFactory(); + ThrowError throwError = (ThrowError) throwErrorMediatorFactory.createSpecificMediator(element,null); + Assert.assertEquals("$.abc", throwError.getErrorMsg().getExpression().toString()); } @Test - public void testTriggerErrorMsg() throws XMLStreamException { - String inputXML = ""; + public void testThrowErrorMsg() throws XMLStreamException { + String inputXML = ""; OMElement element = AXIOMUtil.stringToOM(inputXML); - TriggerErrorMediatorFactory triggerErrorMediatorFactory = new TriggerErrorMediatorFactory(); - TriggerError triggerError = (TriggerError) triggerErrorMediatorFactory.createSpecificMediator(element,null); - Assert.assertNotNull("abc", triggerError.getErrorMsg()); + ThrowErrorMediatorFactory throwErrorMediatorFactory = new ThrowErrorMediatorFactory(); + ThrowError throwError = (ThrowError) throwErrorMediatorFactory.createSpecificMediator(element,null); + Assert.assertEquals("string error message", throwError.getErrorMsg().getKeyValue()); } } diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializerTest.java b/modules/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializerTest.java new file mode 100644 index 0000000000..a2dcce68c2 --- /dev/null +++ b/modules/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializerTest.java @@ -0,0 +1,31 @@ +package org.apache.synapse.config.xml; + +import org.apache.axiom.om.OMElement; +import org.apache.synapse.mediators.Value; +import org.apache.synapse.mediators.v2.ThrowError; +import org.apache.synapse.util.xpath.SynapseExpression; +import org.jaxen.JaxenException; +import org.junit.Assert; +import org.junit.Test; + +import javax.xml.namespace.QName; + +public class ThrowErrorMediatorSerializerTest { + + @Test + public void testSerializeThrowErrorMediator() throws JaxenException { + ThrowErrorMediatorSerializer throwErrorMediatorSerializer = new ThrowErrorMediatorSerializer(); + ThrowError mediator = new ThrowError(); + String type = "TRANSPORT:TIMEOUT"; + String err = "Error occurred"; + String exp = "${payload.err}"; + mediator.setErrorMsg(new Value(err)); + mediator.setType(type); + OMElement element = throwErrorMediatorSerializer.serializeSpecificMediator(mediator); + Assert.assertEquals("invalid type", type, element.getAttributeValue(new QName("type"))); + Assert.assertEquals("invalid error message", err, element.getAttributeValue(new QName("errorMessage"))); + mediator.setErrorMsg(new Value(new SynapseExpression(exp))); + element = throwErrorMediatorSerializer.serializeSpecificMediator(mediator); + Assert.assertEquals("invalid error message", "{" + exp + "}", element.getAttributeValue(new QName("errorMessage"))); + } +} diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java b/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java deleted file mode 100644 index 3fac601831..0000000000 --- a/modules/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.apache.synapse.config.xml; - -import org.apache.axiom.om.OMElement; -import org.apache.synapse.mediators.v2.TriggerError; -import org.apache.synapse.util.xpath.SynapseExpression; -import org.jaxen.JaxenException; -import org.junit.Assert; -import org.junit.Test; - -import javax.xml.namespace.QName; - -public class TriggerErrorMediatorSerializerTest { - - @Test - public void testSerializeTriggerErrorMediator() throws JaxenException { - TriggerErrorMediatorSerializer triggerErrorMediatorSerializer = new TriggerErrorMediatorSerializer(); - TriggerError mediator = new TriggerError(); - String type = "TRANSPORT:TIMEOUT"; - String err = "Error occurred"; - String exp = "${payload.err}"; - mediator.setExpression(new SynapseExpression(exp)); - mediator.setType(type); - OMElement element = triggerErrorMediatorSerializer.serializeSpecificMediator(mediator); - Assert.assertEquals("invalid type", type, element.getAttributeValue(new QName("type"))); - Assert.assertEquals("invalid expression", exp, element.getAttributeValue(new QName("expression"))); - mediator.setExpression(null); - mediator.setErrorMsg(err); - element = triggerErrorMediatorSerializer.serializeSpecificMediator(mediator); - Assert.assertEquals("invalid error message", err, element.getAttributeValue(new QName("errorMessage"))); - } -}