use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.
the class ScriptMediator method getScriptMessageContext.
/**
* Get script message context according to scripting language.
*
* @param synCtx message context
* @param helper Object which help to convert xml into OMelemnt
* @return Nashorn or Common script message context according to language attribute
*/
private ScriptMessageContext getScriptMessageContext(MessageContext synCtx, XMLHelper helper) {
ScriptMessageContext scriptMC;
if (language.equals(NASHORN_JAVA_SCRIPT)) {
try {
emptyJsonObject = (ScriptObjectMirror) scriptEngine.eval("({})");
jsonSerializer = (ScriptObjectMirror) scriptEngine.eval("JSON");
} catch (ScriptException e) {
throw new SynapseException("Error occurred while evaluating empty json object", e);
}
scriptMC = new NashornJavaScriptMessageContext(synCtx, helper, emptyJsonObject, jsonSerializer);
} else {
scriptMC = new CommonScriptMessageContext(synCtx, helper);
}
return scriptMC;
}
use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.
the class ScriptMediatorFactory method createSpecificMediator.
public Mediator createSpecificMediator(OMElement elem, Properties properties) {
ScriptMediator mediator;
ClassLoader classLoader = (ClassLoader) properties.get(SynapseConstants.SYNAPSE_LIB_LOADER);
OMAttribute keyAtt = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
OMAttribute langAtt = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "language"));
OMAttribute functionAtt = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "function"));
if (langAtt == null) {
throw new SynapseException("The 'language' attribute is required for" + " a script mediator");
// TODO: couldn't this be determined from the key in some scenarios?
}
if (keyAtt == null && functionAtt != null) {
throw new SynapseException("Cannot use 'function' attribute without 'key' " + "attribute for a script mediator");
}
Map<Value, Object> includeKeysMap = getIncludeKeysMap(elem);
if (keyAtt != null) {
// ValueFactory for creating dynamic or static Key
ValueFactory keyFac = new ValueFactory();
// create dynamic or static key based on OMElement
Value generatedKey = keyFac.createValue(XMLConfigConstants.KEY, elem);
String functionName = (functionAtt == null ? null : functionAtt.getAttributeValue());
mediator = new ScriptMediator(langAtt.getAttributeValue(), includeKeysMap, generatedKey, functionName, classLoader);
} else {
mediator = new ScriptMediator(langAtt.getAttributeValue(), elem.getText(), classLoader);
}
processAuditStatus(mediator, elem);
return mediator;
}
use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.
the class XQueryMediator method performQuery.
/**
* Perform the quering and get the result and attached to the target node
*
* @param synCtx The current MessageContext
* @param synLog the Synapse log to use
*/
private void performQuery(MessageContext synCtx, SynapseLog synLog) {
boolean reLoad = false;
boolean needSet = false;
XQueryEvaluator queryEvaluator = null;
String generatedQueryKey = null;
XQueryExecutable xQueryExecutable = null;
XdmValue xdmValue;
boolean isQueryKeyGenerated = false;
if (queryKey != null) {
// Derive actual key from xpath or get static key
generatedQueryKey = queryKey.evaluateValue(synCtx);
}
if (generatedQueryKey != null) {
isQueryKeyGenerated = true;
}
if (generatedQueryKey != null && !"".equals(generatedQueryKey)) {
Entry dp = synCtx.getConfiguration().getEntryDefinition(generatedQueryKey);
// if the queryKey refers to a dynamic resource
if (dp != null && dp.isDynamic()) {
if (!dp.isCached() || dp.isExpired()) {
reLoad = true;
}
}
}
try {
synchronized (resourceLock) {
// creating processor
if (cachedProcessor == null) {
cachedProcessor = new Processor(false);
// setting up the properties to the Processor
if (processorProperties != null && !processorProperties.isEmpty()) {
synLog.traceOrDebug("Setting up properties to the XQDataSource");
for (MediatorProperty processorProperty : processorProperties) {
if (processorProperty != null) {
cachedProcessor.setConfigurationProperty(processorProperty.getName(), processorProperty.getValue());
}
}
}
}
// creating XQueryCompiler
if (cachedQueryCompiler == null) {
synLog.traceOrDebug("Creating a compiler from the Processor ");
cachedQueryCompiler = cachedProcessor.newXQueryCompiler();
}
// If already cached evaluator then load it from cachedXQueryEvaluatorMap
if (isQueryKeyGenerated) {
queryEvaluator = cachedXQueryEvaluatorMap.get(generatedQueryKey);
}
if (reLoad || queryEvaluator == null) {
if (querySource != null && !"".equals(querySource)) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Using in-lined query source - " + querySource);
synLog.traceOrDebug("Prepare an expression for the query ");
}
xQueryExecutable = cachedQueryCompiler.compile(querySource);
queryEvaluator = xQueryExecutable.load();
// if queryEvaluator is created then put it in to cachedXQueryEvaluatorMap
if (isQueryKeyGenerated) {
cachedXQueryEvaluatorMap.put(generatedQueryKey, queryEvaluator);
}
// need set because the expression just has recreated
needSet = true;
} else {
Object o = synCtx.getEntry(generatedQueryKey);
if (o == null) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Couldn't find the xquery source with a key " + queryKey);
}
throw new SynapseException("No object found for the key '" + generatedQueryKey + "'");
}
String sourceCode = null;
InputStream inputStream = null;
if (o instanceof OMElement) {
sourceCode = ((OMElement) (o)).getText();
} else if (o instanceof String) {
sourceCode = (String) o;
} else if (o instanceof OMText) {
DataHandler dataHandler = (DataHandler) ((OMText) o).getDataHandler();
if (dataHandler != null) {
try {
inputStream = dataHandler.getInputStream();
if (inputStream == null) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Couldn't get" + " the stream from the xquery source with a key " + queryKey);
}
return;
}
} catch (IOException e) {
handleException("Error in reading content as a stream ");
}
}
}
if ((sourceCode == null || "".equals(sourceCode)) && inputStream == null) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Couldn't find the xquery source with a key " + queryKey);
}
return;
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Picked up the xquery source from the " + "key " + queryKey);
synLog.traceOrDebug("Prepare an expression for the query ");
}
try {
if (sourceCode != null) {
// create an xQueryExecutable using the query source
xQueryExecutable = cachedQueryCompiler.compile(sourceCode);
} else {
xQueryExecutable = cachedQueryCompiler.compile(inputStream);
}
} catch (IOException e) {
handleException("Error during the query inputStream compilation");
}
queryEvaluator = xQueryExecutable.load();
// if queryEvaluator is created then put it in to cachedXQueryEvaluatorMap
if (isQueryKeyGenerated) {
cachedXQueryEvaluatorMap.put(generatedQueryKey, queryEvaluator);
}
// need set because the evaluator just has recreated
needSet = true;
}
}
// Set the external variables to the queryEvaluator
if (variables != null && !variables.isEmpty()) {
synLog.traceOrDebug("Binding external variables to the DynamicContext");
for (MediatorVariable variable : variables) {
if (variable != null) {
boolean hasValueChanged = variable.evaluateValue(synCtx);
// if the value has changed or need set because the evaluator has recreated
if (hasValueChanged || needSet) {
// Set the external variable to the queryEvaluator
setVariable(queryEvaluator, variable, synLog);
}
}
}
}
// executing the query
xdmValue = queryEvaluator.evaluate();
}
if (queryEvaluator == null) {
synLog.traceOrDebug("Result Sequence is null");
return;
}
// processing the result
for (XdmItem xdmItem : xdmValue) {
if (xdmItem == null) {
return;
}
XdmNodeKind xdmNodeKind = null;
ItemType itemType = null;
if (xdmItem.isAtomicValue()) {
itemType = getItemType(xdmItem, cachedProcessor);
if (itemType == null) {
return;
}
} else {
xdmNodeKind = ((XdmNode) xdmItem).getNodeKind();
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("The XQuery Result " + xdmItem.toString());
}
// The target node that is going to modify
OMNode destination = target.selectOMNode(synCtx, synLog);
if (destination != null) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("The target node " + destination);
}
// If the result is XML
if (XdmNodeKind.DOCUMENT == xdmNodeKind || XdmNodeKind.ELEMENT == xdmNodeKind) {
StAXOMBuilder builder = new StAXOMBuilder(XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xdmItem.toString())));
OMElement resultOM = builder.getDocumentElement();
if (resultOM != null) {
// replace the target node from the result
destination.insertSiblingAfter(resultOM);
destination.detach();
}
} else if (ItemType.INTEGER == itemType || ItemType.INT == itemType) {
// replace the text value of the target node by the result ,If the result is
// a basic type
((OMElement) destination).setText(String.valueOf(((XdmAtomicValue) xdmItem).getDecimalValue().intValue()));
} else if (ItemType.BOOLEAN == itemType) {
((OMElement) destination).setText(String.valueOf(((XdmAtomicValue) xdmItem).getBooleanValue()));
} else if (ItemType.DOUBLE == itemType) {
((OMElement) destination).setText(String.valueOf(((XdmAtomicValue) xdmItem).getDoubleValue()));
} else if (ItemType.FLOAT == itemType) {
((OMElement) destination).setText(String.valueOf(((XdmAtomicValue) xdmItem).getDecimalValue().floatValue()));
} else if (ItemType.LONG == itemType) {
((OMElement) destination).setText(String.valueOf(((XdmAtomicValue) xdmItem).getLongValue()));
} else if (ItemType.SHORT == itemType) {
((OMElement) destination).setText(String.valueOf(((XdmAtomicValue) xdmItem).getDecimalValue().shortValue()));
} else if (ItemType.BYTE == itemType) {
((OMElement) destination).setText(String.valueOf(((XdmAtomicValue) xdmItem).getDecimalValue().byteValue()));
} else if (ItemType.STRING == itemType) {
((OMElement) destination).setText(String.valueOf(((XdmAtomicValue) xdmItem).getValue()));
}
} else if (null == target.getXPath() && null == destination) {
// In the case soap body doesn't have the first element --> Empty soap body
destination = synCtx.getEnvelope().getBody();
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("The target node " + destination);
}
// If the result is XML
if (XdmNodeKind.ELEMENT == xdmNodeKind || XdmNodeKind.DOCUMENT == xdmNodeKind) {
StAXOMBuilder builder = new StAXOMBuilder(XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xdmItem.toString())));
OMElement resultOM = builder.getDocumentElement();
if (resultOM != null) {
((OMElement) destination).addChild(resultOM);
}
}
// No else part since soap body could have only XML part not text values
}
// Only take the *first* value of the result sequence
break;
}
// closing the result sequence
queryEvaluator.close();
} catch (SaxonApiException e) {
handleException("Error during the querying " + e.getMessage(), e);
} catch (XMLStreamException e) {
handleException("Error during retrieving the Document Node as the result " + e.getMessage(), e);
}
}
use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.
the class InboundEndpointFactory method createInboundEndpoint.
public static InboundEndpoint createInboundEndpoint(OMElement inboundEndpointElem, SynapseConfiguration config) {
InboundEndpoint inboundEndpoint = new InboundEndpoint();
if (inboundEndpointElem.getAttributeValue(ATT_NAME) != null) {
inboundEndpoint.setName(inboundEndpointElem.getAttributeValue(ATT_NAME));
} else {
String msg = "Inbound Endpoint name cannot be null";
log.error(msg);
throw new SynapseException(msg);
}
if (inboundEndpointElem.getAttributeValue(ATT_PROTOCOL) != null) {
inboundEndpoint.setProtocol(inboundEndpointElem.getAttributeValue(ATT_PROTOCOL));
}
if (inboundEndpointElem.getAttributeValue(ATT_ENDPOINT_CLASS) != null) {
inboundEndpoint.setClassImpl(inboundEndpointElem.getAttributeValue(ATT_ENDPOINT_CLASS));
}
if (inboundEndpointElem.getAttributeValue(ATT_ENDPOINT_SUSPEND) != null) {
inboundEndpoint.setSuspend(Boolean.parseBoolean(inboundEndpointElem.getAttributeValue(ATT_ENDPOINT_SUSPEND)));
} else {
inboundEndpoint.setSuspend(false);
}
if (inboundEndpointElem.getAttributeValue(ATT_SEQUENCE) != null) {
inboundEndpoint.setInjectingSeq(inboundEndpointElem.getAttributeValue(ATT_SEQUENCE));
}
if (inboundEndpointElem.getAttributeValue(ATT_ERROR_SEQUENCE) != null) {
inboundEndpoint.setOnErrorSeq(inboundEndpointElem.getAttributeValue(ATT_ERROR_SEQUENCE));
}
String nameString = inboundEndpoint.getName();
if (nameString == null || "".equals(nameString)) {
nameString = SynapseConstants.INBOUND_ENDPOINT_NAME;
}
AspectConfiguration aspectConfiguration = new AspectConfiguration(nameString);
inboundEndpoint.configure(aspectConfiguration);
OMAttribute statistics = inboundEndpointElem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.STATISTICS_ATTRIB_NAME));
if (statistics != null) {
String statisticsValue = statistics.getAttributeValue();
if (statisticsValue != null) {
if (XMLConfigConstants.STATISTICS_ENABLE.equals(statisticsValue)) {
aspectConfiguration.enableStatistics();
}
}
}
OMAttribute tracing = inboundEndpointElem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.TRACE_ATTRIB_NAME));
if (tracing != null) {
String tracingValue = tracing.getAttributeValue();
if (tracingValue != null) {
if (XMLConfigConstants.TRACE_ENABLE.equals(tracingValue)) {
aspectConfiguration.enableTracing();
}
}
}
// Set parameters
OMElement parametersElt = inboundEndpointElem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, InboundEndpointConstants.INBOUND_ENDPOINT_PARAMETERS));
if (parametersElt != null) {
Iterator parameters = parametersElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, InboundEndpointConstants.INBOUND_ENDPOINT_PARAMETER));
while (parameters.hasNext()) {
OMElement parameter = (OMElement) parameters.next();
String paramName = parameter.getAttributeValue(new QName(InboundEndpointConstants.INBOUND_ENDPOINT_PARAMETER_NAME));
String paramKey = parameter.getAttributeValue(new QName(InboundEndpointConstants.INBOUND_ENDPOINT_PARAMETER_KEY));
if (paramKey != null) {
Object obj = config.getEntry(paramKey);
if (obj == null) {
obj = config.getEntryDefinition(paramKey);
obj = config.getEntry(paramKey);
}
if (obj != null && obj instanceof OMTextImpl) {
OMText objText = (OMText) obj;
inboundEndpoint.addParameter(paramName, objText.getText(), paramKey);
} else {
String msg = "Error while deploying inbound endpoint " + inboundEndpoint.getName() + ".Registry entry defined with key: " + paramKey + " not found.";
log.error(msg);
throw new SynapseException(msg);
}
} else if (parameter.getFirstElement() != null) {
inboundEndpoint.addParameter(paramName, parameter.getFirstElement().toString());
} else {
inboundEndpoint.addParameter(paramName, parameter.getText());
}
}
}
inboundEndpoint.setFileName(inboundEndpointElem.getAttributeValue(new QName(InboundEndpointConstants.INBOUND_ENDPOINT_NAME)) + ".xml");
return inboundEndpoint;
}
use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.
the class LoadbalanceAlgorithmFactory method createLoadbalanceAlgorithm.
public static LoadbalanceAlgorithm createLoadbalanceAlgorithm(OMElement loadbalanceElement, List endpoints) {
// default algorithm is round robin
LoadbalanceAlgorithm algorithm = new RoundRobin(endpoints);
OMAttribute policyAttribute = loadbalanceElement.getAttribute(new QName(null, XMLConfigConstants.LOADBALANCE_POLICY));
OMAttribute algoAttribute = loadbalanceElement.getAttribute(new QName(null, XMLConfigConstants.LOADBALANCE_ALGORITHM));
if (policyAttribute != null && algoAttribute != null) {
String msg = "You cannot specify both the 'policy' & 'algorithm' in the configuration. " + "It is sufficient to provide only the 'algorithm'.";
// We cannot continue execution. Hence it is logged at fatal level
log.fatal(msg);
throw new SynapseException(msg);
}
if (algoAttribute != null) {
String algorithmStr = algoAttribute.getAttributeValue().trim();
try {
algorithm = (LoadbalanceAlgorithm) Class.forName(algorithmStr).newInstance();
algorithm.setEndpoints(endpoints);
} catch (Exception e) {
String msg = "Cannot instantiate LoadbalanceAlgorithm implementation class " + algorithmStr;
// We cannot continue execution. Hence it is logged at fatal level
log.fatal(msg, e);
throw new SynapseException(msg, e);
}
} else if (policyAttribute != null) {
// currently only the roundRobin policy is supported
if (!policyAttribute.getAttributeValue().trim().equals("roundRobin")) {
String msg = "Unsupported algorithm " + policyAttribute.getAttributeValue().trim() + " specified. Please use the 'algorithm' attribute to specify the " + "correct loadbalance algorithm implementation.";
// We cannot continue execution. Hence it is logged at fatal level
log.fatal(msg);
throw new SynapseException(msg);
}
}
return algorithm;
}
Aggregations