Search in sources :

Example 36 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class GetPropertyFunction method evaluate.

/**
 * Returns the string value of the property using arg one as key and arg two as scope
 *
 * @param scopeObject scope will decide from where property will be picked up from
 *        i.e. axis2, transport, default/synapse
 * @param keyObject the key of the property
 * @param navigator object model which can be used for navigation around
 * @param dateformat The dateformat that need to convert
 * @return The String value of property using arg one as key and arg two as scope
 */
public Object evaluate(Object scopeObject, Object keyObject, Object dateformat, Navigator navigator) {
    boolean traceOn = synCtx.getTracingState() == SynapseConstants.TRACING_ON;
    boolean traceOrDebugOn = traceOn || log.isDebugEnabled();
    String scope = StringFunction.evaluate(scopeObject, navigator);
    String key = StringFunction.evaluate(keyObject, navigator);
    if (key == null || "".equals(key)) {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "property-name should be provided when executing synapse:get-property" + "(scope,prop-name) or synapse:get-property(prop-name) Xpath function");
        }
        return NULL_STRING;
    }
    if (SynapseConstants.SYSTEM_DATE.equals(key)) {
        if (dateformat != null) {
            Format formatter = new SimpleDateFormat(dateformat.toString());
            return formatter.format(new java.util.Date());
        } else {
            Format formatter = new SimpleDateFormat();
            return formatter.format(new java.util.Date());
        }
    }
    // return the current system time as a string , don't care scope
    if (SynapseConstants.SYSTEM_TIME.equals(key)) {
        return Long.toString(System.currentTimeMillis());
    }
    if (XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
        if (SynapseConstants.HEADER_TO.equals(key)) {
            EndpointReference toEPR = synCtx.getTo();
            if (toEPR != null) {
                return toEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_FROM.equals(key)) {
            EndpointReference fromEPR = synCtx.getFrom();
            if (fromEPR != null) {
                return fromEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_ACTION.equals(key)) {
            String wsaAction = synCtx.getWSAAction();
            if (wsaAction != null) {
                return wsaAction;
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_FAULT.equals(key)) {
            EndpointReference faultEPR = synCtx.getFaultTo();
            if (faultEPR != null) {
                return faultEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_REPLY_TO.equals(key)) {
            EndpointReference replyToEPR = synCtx.getReplyTo();
            if (replyToEPR != null) {
                return replyToEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_RELATES_TO.equals(key)) {
            RelatesTo relatesTo = synCtx.getRelatesTo();
            if (relatesTo != null) {
                return relatesTo.getValue();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_MESSAGE_ID.equals(key)) {
            String messageID = synCtx.getMessageID();
            if (messageID != null) {
                return messageID;
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.PROPERTY_FAULT.equals(key)) {
            if (synCtx.getEnvelope().hasFault()) {
                return SynapseConstants.TRUE;
            } else if (synCtx instanceof Axis2MessageContext) {
                org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                if (axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE) != null && SynapseConstants.TRUE.equals(axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE))) {
                    return SynapseConstants.TRUE;
                }
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.PROPERTY_MESSAGE_FORMAT.equals(key)) {
            if (synCtx.isDoingPOX())
                return SynapseConstants.FORMAT_POX;
            else if (synCtx.isDoingGET())
                return SynapseConstants.FORMAT_GET;
            else if (synCtx.isSOAP11())
                return SynapseConstants.FORMAT_SOAP11;
            else
                return SynapseConstants.FORMAT_SOAP12;
        } else if (SynapseConstants.PROPERTY_OPERATION_NAME.equals(key) || SynapseConstants.PROPERTY_OPERATION_NAMESPACE.equals(key)) {
            if (synCtx instanceof Axis2MessageContext) {
                AxisOperation axisOperation = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getAxisOperation();
                if (axisOperation != null) {
                    if (SynapseConstants.PROPERTY_OPERATION_NAMESPACE.equals(key)) {
                        return axisOperation.getName().getNamespaceURI();
                    } else {
                        return axisOperation.getName().getLocalPart();
                    }
                }
            }
        } else {
            Object result = synCtx.getProperty(key);
            if (result != null) {
                return result;
            } else {
                return synCtx.getLocalEntry(key);
            }
        }
    } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
        org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        return axis2MessageContext.getProperty(key);
    } else if (XMLConfigConstants.SCOPE_FUNC.equals(scope)) {
        Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
        TemplateContext topCtxt = functionStack.peek();
        if (topCtxt != null) {
            return topCtxt.getParameterValue(key);
        }
    } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
        org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        Object headers = axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
        if (headers != null && headers instanceof Map) {
            Map headersMap = (Map) headers;
            return headersMap.get(key);
        }
    } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
        Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
        org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
        return axis2smc.getAxis2MessageContext().getOperationContext().getProperty(key);
    } else if (XMLConfigConstants.SCOPE_REGISTRY.equals(scope)) {
        String[] regParam = key.split("@");
        String regPath = null;
        String propName = null;
        if (regParam.length == 2) {
            regPath = regParam[0];
            propName = regParam[1];
        } else if (regParam.length == 1) {
            regPath = regParam[0];
        }
        Entry propEntry = synCtx.getConfiguration().getEntryDefinition(regPath);
        if (propEntry == null) {
            propEntry = new Entry();
            propEntry.setType(Entry.REMOTE_ENTRY);
            propEntry.setKey(key);
        }
        Registry registry = synCtx.getConfiguration().getRegistry();
        if (registry != null) {
            registry.getResource(propEntry, new Properties());
            if (propName != null) {
                Properties reqProperties = propEntry.getEntryProperties();
                if (reqProperties != null) {
                    if (reqProperties.get(propName) != null) {
                        return reqProperties.getProperty(propName);
                    }
                }
            } else if (propEntry.getValue() != null) {
                if (propEntry.getValue() instanceof OMText) {
                    return ((OMText) propEntry.getValue()).getText();
                }
                return propEntry.getValue().toString();
            }
        }
    } else if (XMLConfigConstants.SCOPE_SYSTEM.equals(scope)) {
        String propVal = System.getProperty(key);
        if (propVal != null) {
            return propVal;
        } else {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "System property " + key + " not found");
            }
            return NULL_STRING;
        }
    } else {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "Invalid scope : '" + scope + "' has been set for the " + "synapse:get-property(scope,prop-name) XPath function");
        }
    }
    return NULL_STRING;
}
Also used : AxisOperation(org.apache.axis2.description.AxisOperation) Properties(java.util.Properties) RelatesTo(org.apache.axis2.addressing.RelatesTo) Entry(org.apache.synapse.config.Entry) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) OMText(org.apache.axiom.om.OMText) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Registry(org.apache.synapse.registry.Registry) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) EndpointReference(org.apache.axis2.addressing.EndpointReference) Stack(java.util.Stack) SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map)

Example 37 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class SimpleMapImpl method getOMElement.

public OMElement getOMElement(OMFactory fac) {
    OMElement mapElement = fac.createOMElement(PayloadHelper.MAPELT);
    for (Object entryObj : this.entrySet()) {
        Object key = ((Map.Entry) entryObj).getKey();
        Object o = ((Map.Entry) entryObj).getValue();
        if (key instanceof String) {
            OMElement entry = fac.createOMElement(new QName(PayloadHelper.AXIOMPAYLOADNS, ENTRY), mapElement);
            entry.addAttribute(NAME, (String) key, attrNS);
            if (o instanceof Character) {
                entry.addAttribute(TYPE, CHAR, attrNS);
                entry.setText(o.toString());
            } else if (o instanceof Boolean) {
                entry.addAttribute(TYPE, BOOLEAN, attrNS);
                entry.setText(o.toString());
            } else if (o instanceof String) {
                entry.addAttribute(TYPE, STRING, attrNS);
                entry.setText(o.toString());
            } else if (o instanceof Byte) {
                entry.addAttribute(TYPE, BYTE, attrNS);
                entry.setText(o.toString());
            } else if (o instanceof byte[]) {
                entry.addAttribute(TYPE, BYTEARRAY, attrNS);
                OMText text = fac.createOMText(new DataHandler(new ByteArrayDataSource((byte[]) o)), true);
                entry.addChild(text);
            } else if (o instanceof Float) {
                entry.addAttribute(TYPE, FLOAT, attrNS);
                entry.setText(o.toString());
            } else if (o instanceof Double) {
                entry.addAttribute(TYPE, DOUBLE, attrNS);
                entry.setText(o.toString());
            } else if (o instanceof Long) {
                entry.addAttribute(TYPE, LONG, attrNS);
                entry.setText(o.toString());
            } else if (o instanceof Short) {
                entry.addAttribute(TYPE, SHORT, attrNS);
                entry.setText(o.toString());
            } else if (o instanceof Integer) {
                entry.addAttribute(TYPE, INTEGER, attrNS);
                entry.setText(o.toString());
            }
        } else {
        // shouldn't be any non-string keys. Ignore!
        }
    }
    return mapElement;
}
Also used : QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) OMText(org.apache.axiom.om.OMText) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 38 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class EnrichMediatorSerializer method serializeSource.

private OMElement serializeSource(Source source) {
    OMElement sourceEle = fac.createOMElement("source", synNS);
    if (source.getSourceType() != EnrichMediator.CUSTOM) {
        sourceEle.addAttribute(fac.createOMAttribute("type", nullNS, intTypeToString(source.getSourceType())));
    }
    sourceEle.addAttribute(fac.createOMAttribute("clone", nullNS, Boolean.toString(source.isClone())));
    if (source.getSourceType() == EnrichMediator.PROPERTY) {
        sourceEle.addAttribute(fac.createOMAttribute("property", nullNS, source.getProperty()));
    } else if (source.getSourceType() == EnrichMediator.CUSTOM) {
        SynapseXPathSerializer.serializeXPath(source.getXpath(), sourceEle, "xpath");
    } else if (source.getSourceType() == EnrichMediator.INLINE) {
        if (source.getInlineOMNode() instanceof OMElement) {
            sourceEle.addChild(((OMElement) source.getInlineOMNode()).cloneOMElement());
        } else if (source.getInlineOMNode() instanceof OMText) {
            /*Text as inline content*/
            sourceEle.setText(((OMText) source.getInlineOMNode()).getText());
        } else if (source.getInlineKey() != null) {
            sourceEle.addAttribute("key", source.getInlineKey(), null);
        }
    }
    return sourceEle;
}
Also used : OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 39 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class NashornJavaScriptMediatorTest method testExternalScriptWithCommentsOnNashornEngine.

/**
 * Test functionality of mediate with external script in nashornJS.
 *
 * @throws Exception
 */
public void testExternalScriptWithCommentsOnNashornEngine() throws Exception {
    String request = "{\n" + "    \"results\": [\n" + "        {\n" + "            \"geometry\": {\n" + "                \"location\": {\n" + "                    \"lat\": -33.86726,\n" + "                    \"lng\": 151.195813\n" + "                }\n" + "            },\n" + "            \"icon\": \"bar-71.png\",\n" + "            \"id\": \"7eaf7\",\n" + "            \"name\": \"Biaggio Cafe\",\n" + "            \"opening_hours\": {\n" + "                \"open_now\": true\n" + "            },\n" + "            \"photos\": [\n" + "                {\n" + "                    \"height\": 600,\n" + "                    \"html_attributions\": [],\n" + "                    \"photo_reference\": \"CoQBegAAAI\",\n" + "                    \"width\": 900\n" + "                }\n" + "            ],\n" + "            \"price_level\": 1,\n" + "            \"reference\": \"CnRqAAAAtz\",\n" + "            \"types\": [\n" + "                \"bar\",\n" + "                \"restaurant\",\n" + "                \"food\",\n" + "                \"establishment\"\n" + "            ],\n" + "            \"vicinity\": \"48 Pirrama Road, Pyrmont\"\n" + "        },\n" + "        {\n" + "            \"geometry\": {\n" + "                \"location\": {\n" + "                    \"lat\": -33.866804,\n" + "                    \"lng\": 151.195579\n" + "                }\n" + "            },\n" + "            \"icon\": \"generic_business-71.png\",\n" + "            \"id\": \"3ef98\",\n" + "            \"name\": \"Doltone House\",\n" + "            \"photos\": [\n" + "                {\n" + "                    \"height\": 600,\n" + "                    \"html_attributions\": [],\n" + "                    \"photo_reference\": \"CqQBmgAAAL\",\n" + "                    \"width\": 900\n" + "                }\n" + "            ],\n" + "            \"reference\": \"CnRrAAAAV\",\n" + "            \"types\": [\n" + "                \"food\",\n" + "                \"establishment\"\n" + "            ],\n" + "            \"vicinity\": \"48 Pirrama Road, Pyrmont\"\n" + "        }\n" + "    ],\n" + "    \"status\": \"OK\"\n" + "}";
    MessageContext mc = TestUtils.getTestContextJson(request, null);
    String scriptSrc = "function transform(mc) {\n" + "    payload = mc.getPayloadJSON();\n" + "    results = payload.results;\n" + "    var response = new Array();\n" + "    for (i = 0; i < results.length; ++i) {\n" + "        // this is a comment\n" + "        location_object = results[i];\n" + "        l = new Object();\n" + "        l.name = location_object.name;\n" + "        l.tags = location_object.types;\n" + "        l.id = \"ID:\" + (location_object.id);\n" + "        response[i] = l;\n" + "    }\n" + "    mc.setPayloadJSON(response);\n" + "}";
    String scriptSrcKey = "conf:/repository/esb/transform.js";
    Entry e = new Entry();
    DataSource dataSource = new ByteArrayDataSource(scriptSrc.getBytes());
    DataHandler dataHandler = new DataHandler(dataSource);
    OMText text = OMAbstractFactory.getOMFactory().createOMText(dataHandler, true);
    e.setKey(scriptSrcKey);
    e.setValue(text);
    mc.getConfiguration().addEntry(scriptSrcKey, e);
    Value v = new Value(scriptSrcKey);
    ScriptMediator mediator = new ScriptMediator("nashornJs", new LinkedHashMap<Value, Object>(), v, "transform", null);
    boolean result = mediator.mediate(mc);
    String response = JsonUtil.jsonPayloadToString(((Axis2MessageContext) mc).getAxis2MessageContext());
    String expectedResponse = "[{\"name\":\"Biaggio Cafe\",\"tags\":[\"bar\",\"restaurant\",\"food\"," + "\"establishment\"],\"id\":\"ID:7eaf7\"},{\"name\":\"Doltone House\",\"tags\":[\"food\"," + "\"establishment\"],\"id\":\"ID:3ef98\"}]";
    assertEquals(expectedResponse, response);
    assertEquals(true, result);
}
Also used : Entry(org.apache.synapse.config.Entry) ScriptMediator(org.apache.synapse.mediators.bsf.ScriptMediator) OMText(org.apache.axiom.om.OMText) Value(org.apache.synapse.mediators.Value) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 40 with OMText

use of org.apache.axiom.om.OMText in project carbon-business-process by wso2.

the class OMUtils method toDOM.

public static Element toDOM(OMElement element, Document doc, boolean deepNS) {
    final Element domElement = doc.createElementNS(element.getQName().getNamespaceURI(), element.getQName().getLocalPart());
    if (deepNS) {
        NSContext nscontext = new NSContext();
        buildNScontext(nscontext, element);
        DOMUtils.injectNamespaces(domElement, nscontext);
    } else {
        if (element.getAllDeclaredNamespaces() != null) {
            for (Iterator i = element.getAllDeclaredNamespaces(); i.hasNext(); ) {
                OMNamespace omns = (OMNamespace) i.next();
                if (omns.getPrefix().equals("")) {
                    domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns", omns.getNamespaceURI() == null ? "" : omns.getNamespaceURI());
                } else {
                    domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + omns.getPrefix(), omns.getNamespaceURI());
                }
            }
        }
    }
    for (Iterator i = element.getAllAttributes(); i.hasNext(); ) {
        final OMAttribute attr = (OMAttribute) i.next();
        Attr newAttr;
        if (attr.getNamespace() != null) {
            newAttr = doc.createAttributeNS(attr.getNamespace().getNamespaceURI(), attr.getLocalName());
        } else {
            newAttr = doc.createAttributeNS(null, attr.getLocalName());
        }
        newAttr.appendChild(doc.createTextNode(attr.getAttributeValue()));
        domElement.setAttributeNodeNS(newAttr);
        // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually...
        int colonIdx = attr.getAttributeValue().indexOf(":");
        if (colonIdx > 0) {
            OMNamespace attrValNs = element.findNamespaceURI(attr.getAttributeValue().substring(0, colonIdx));
            if (attrValNs != null) {
                domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + attrValNs.getPrefix(), attrValNs.getNamespaceURI());
            }
        }
    }
    for (Iterator i = element.getChildren(); i.hasNext(); ) {
        OMNode omn = (OMNode) i.next();
        switch(omn.getType()) {
            case OMNode.CDATA_SECTION_NODE:
                domElement.appendChild(doc.createCDATASection(((OMText) omn).getText()));
                break;
            case OMNode.TEXT_NODE:
                domElement.appendChild(doc.createTextNode(((OMText) omn).getText()));
                break;
            case OMNode.ELEMENT_NODE:
                domElement.appendChild(toDOM((OMElement) omn, doc, false));
                break;
        }
    }
    return domElement;
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) Element(org.w3c.dom.Element) Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) Attr(org.w3c.dom.Attr)

Aggregations

OMText (org.apache.axiom.om.OMText)92 OMElement (org.apache.axiom.om.OMElement)62 OMFactory (org.apache.axiom.om.OMFactory)39 DataHandler (javax.activation.DataHandler)26 OMNode (org.apache.axiom.om.OMNode)21 OMNamespace (org.apache.axiom.om.OMNamespace)10 QName (javax.xml.namespace.QName)8 OMException (org.apache.axiom.om.OMException)8 IOException (java.io.IOException)7 Iterator (java.util.Iterator)7 InputStream (java.io.InputStream)6 OMAttribute (org.apache.axiom.om.OMAttribute)6 Entry (org.apache.synapse.config.Entry)6 ArrayList (java.util.ArrayList)5 DataSource (javax.activation.DataSource)5 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)5 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)5 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)5 OutputStream (java.io.OutputStream)4 StringReader (java.io.StringReader)4