-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2266 from GDLMadushanka/triggerError
Add Trigger Error mediator
- Loading branch information
Showing
9 changed files
with
302 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
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
71 changes: 71 additions & 0 deletions
71
modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactory.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,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 | ||
* <p/> | ||
* <pre> | ||
* <throwError (type="string") (errorMessage=("string" | expression))/> | ||
* </pre> | ||
*/ | ||
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; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
modules/core/src/main/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializer.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.ThrowError; | ||
|
||
/** | ||
* <pre> | ||
* <throwError (type="string") (errorMessage=("string" | expression))/> | ||
* </pre> | ||
*/ | ||
public class ThrowErrorMediatorSerializer extends AbstractMediatorSerializer{ | ||
|
||
@Override | ||
protected OMElement serializeSpecificMediator(Mediator m) { | ||
if (!(m instanceof ThrowError)) { | ||
handleException("Unsupported mediator passed in for serialization : " + m.getType()); | ||
} | ||
|
||
ThrowError mediator = (ThrowError) m; | ||
OMElement throwError = fac.createOMElement("throwError", synNS); | ||
saveTracingState(throwError, mediator); | ||
|
||
if (mediator.getType() != null) { | ||
throwError.addAttribute(fac.createOMAttribute("type", nullNS, mediator.getType())); | ||
} | ||
if (mediator.getErrorMsg() != null) { | ||
ValueSerializer valueSerializer = new ValueSerializer(); | ||
valueSerializer.serializeValue(mediator.getErrorMsg(), "errorMessage", throwError); | ||
} | ||
|
||
serializeComments(throwError, mediator.getCommentsList()); | ||
return throwError; | ||
} | ||
|
||
@Override | ||
public String getMediatorClassName() { | ||
return ThrowError.class.getName(); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
modules/core/src/main/java/org/apache/synapse/mediators/v2/ThrowError.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,74 @@ | ||
/* | ||
* 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.mediators.AbstractMediator; | ||
import org.apache.synapse.mediators.Value; | ||
|
||
/** | ||
* This mediator is used to throw an error from the mediation flow. | ||
*/ | ||
public class ThrowError extends AbstractMediator { | ||
private String type = null; | ||
private Value errorMsg = 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 : ThrowError mediator"); | ||
if (synLog.isTraceTraceEnabled()) { | ||
synLog.traceTrace("Message : " + synCtx.getEnvelope()); | ||
} | ||
} | ||
|
||
String error = errorMsg.evaluateValue(synCtx); | ||
|
||
synLog.traceOrDebug("End : ThrowError mediator"); | ||
synCtx.setProperty(SynapseConstants.ERROR_CODE, type); | ||
synCtx.setProperty(SynapseConstants.ERROR_MESSAGE, error); | ||
throw new SynapseException(error, new Throwable(type)); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public Value getErrorMsg() { | ||
return errorMsg; | ||
} | ||
|
||
public void setErrorMsg(Value errorMsg) { | ||
this.errorMsg = errorMsg; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
modules/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorFactoryTest.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.ThrowError; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
|
||
/** | ||
* This is the test class for ThrowErrorMediatorFactory class. | ||
*/ | ||
public class ThrowErrorMediatorFactoryTest { | ||
|
||
/** | ||
* Test create TriggerError with given XML configuration and asserting it is created. | ||
* | ||
* @throws XMLStreamException - XMLStreamException | ||
*/ | ||
@Test | ||
public void testThrowErrorExp() throws XMLStreamException { | ||
String inputXML = "<throwError type=\"HTTP:TIMEOUT\" errorMessage=\"{${$.abc}}\"/>"; | ||
OMElement element = AXIOMUtil.stringToOM(inputXML); | ||
ThrowErrorMediatorFactory throwErrorMediatorFactory = new ThrowErrorMediatorFactory(); | ||
ThrowError throwError = (ThrowError) throwErrorMediatorFactory.createSpecificMediator(element,null); | ||
Assert.assertEquals("$.abc", throwError.getErrorMsg().getExpression().toString()); | ||
} | ||
|
||
@Test | ||
public void testThrowErrorMsg() throws XMLStreamException { | ||
String inputXML = "<throwError type=\"HTTP:TIMEOUT\" errorMessage=\"string error message\"/>"; | ||
OMElement element = AXIOMUtil.stringToOM(inputXML); | ||
ThrowErrorMediatorFactory throwErrorMediatorFactory = new ThrowErrorMediatorFactory(); | ||
ThrowError throwError = (ThrowError) throwErrorMediatorFactory.createSpecificMediator(element,null); | ||
Assert.assertEquals("string error message", throwError.getErrorMsg().getKeyValue()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...es/core/src/test/java/org/apache/synapse/config/xml/ThrowErrorMediatorSerializerTest.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.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"))); | ||
} | ||
} |