-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Trigger Error mediator <triggererror type="string" (errorMessage="string" | expression="expression")/>
- Loading branch information
1 parent
16bd786
commit bf80787
Showing
8 changed files
with
314 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
* <p/> | ||
* <pre> | ||
* <triggererror type="string" (errorMessage="string" | expression="expression")/> | ||
* </pre> | ||
*/ | ||
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; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
modules/core/src/main/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/** | ||
* <pre> | ||
* <triggererror type="string" (errorMessage="string" | expression="expression")/> | ||
* </pre> | ||
*/ | ||
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(); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
modules/core/src/main/java/org/apache/synapse/mediators/v2/TriggerError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...les/core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "<triggererror expression=\"${$.abc}\"/>"; | ||
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 = "<triggererror errorMessage=\"abc\"/>"; | ||
OMElement element = AXIOMUtil.stringToOM(inputXML); | ||
TriggerErrorMediatorFactory triggerErrorMediatorFactory = new TriggerErrorMediatorFactory(); | ||
TriggerError triggerError = (TriggerError) triggerErrorMediatorFactory.createSpecificMediator(element,null); | ||
Assert.assertNotNull("abc", triggerError.getErrorMsg()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../core/src/test/java/org/apache/synapse/config/xml/TriggerErrorMediatorSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"))); | ||
} | ||
} |