Skip to content

Commit

Permalink
Add Trigger Error mediator
Browse files Browse the repository at this point in the history
Add Trigger Error mediator
<triggererror type="string" (errorMessage="string" | expression="expression")/>
  • Loading branch information
GDLMadushanka committed Dec 16, 2024
1 parent 16bd786 commit bf80787
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
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>
* &lt;triggererror type="string" (errorMessage="string" | expression="expression")/&gt;
* </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;
}
}
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>
* &lt;triggererror type="string" (errorMessage="string" | expression="expression")/&gt;
* </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();
}
}
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;
}
}
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());
}
}
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")));
}
}

0 comments on commit bf80787

Please sign in to comment.