Search in sources :

Example 1 with OMTextImpl

use of org.apache.axiom.om.impl.llom.OMTextImpl in project wso2-synapse by wso2.

the class SOAPEnvelopeTextRetriever method getSourceText.

public String getSourceText(EvaluatorContext context) throws EvaluatorException {
    SOAPEnvelope envelope = context.getMessageContext().getEnvelope();
    Object result;
    try {
        if (compiledXPath == null) {
            compiledXPath = new AXIOMXPath(source);
        }
        result = compiledXPath.evaluate(envelope);
    } catch (JaxenException e) {
        throw new EvaluatorException("Error while parsing the XPath expression: " + source, e);
    }
    if (result instanceof List) {
        List list = (List) result;
        if (list.size() == 1 && list.get(0) == null) {
            return null;
        }
        StringBuffer textValue = new StringBuffer();
        for (Object o : list) {
            if (o instanceof OMTextImpl) {
                textValue.append(((OMTextImpl) o).getText());
            } else if (o instanceof OMElementImpl) {
                String s = ((OMElementImpl) o).getText();
                if (s.trim().length() == 0) {
                    s = o.toString();
                }
                textValue.append(s);
            } else if (o instanceof OMDocumentImpl) {
                textValue.append(((OMDocumentImpl) o).getOMDocumentElement().toString());
            } else if (o instanceof OMAttribute) {
                textValue.append(((OMAttribute) o).getAttributeValue());
            }
        }
        return textValue.toString();
    } else {
        return result.toString();
    }
}
Also used : OMElementImpl(org.apache.axiom.om.impl.llom.OMElementImpl) EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException) JaxenException(org.jaxen.JaxenException) OMDocumentImpl(org.apache.axiom.om.impl.llom.OMDocumentImpl) OMTextImpl(org.apache.axiom.om.impl.llom.OMTextImpl) List(java.util.List) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) AXIOMXPath(org.apache.axiom.om.xpath.AXIOMXPath) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 2 with OMTextImpl

use of org.apache.axiom.om.impl.llom.OMTextImpl in project wso2-synapse by wso2.

the class ScriptMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof ScriptMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    ScriptMediator scriptMediator = (ScriptMediator) m;
    OMElement script = fac.createOMElement("script", synNS);
    String language = scriptMediator.getLanguage();
    Value key = scriptMediator.getKey();
    String function = scriptMediator.getFunction();
    ValueSerializer keySerializer = new ValueSerializer();
    if (key != null) {
        script.addAttribute(fac.createOMAttribute("language", nullNS, language));
        // Serialize Value using ValueSerializer
        keySerializer.serializeValue(key, XMLConfigConstants.KEY, script);
        if (!function.equals("mediate")) {
            script.addAttribute(fac.createOMAttribute("function", nullNS, function));
        }
    } else {
        script.addAttribute(fac.createOMAttribute("language", nullNS, language));
        OMTextImpl textData = (OMTextImpl) fac.createOMText(scriptMediator.getScriptSrc().trim());
        textData.setType(XMLStreamConstants.CDATA);
        script.addChild(textData);
    }
    Map<Value, Object> includeMap = scriptMediator.getIncludeMap();
    for (Value includeKey : includeMap.keySet()) {
        if (includeKey != null) {
            OMElement includeKeyElement = fac.createOMElement("include", synNS);
            // Serialize Value using ValueSerializer
            keySerializer.serializeValue(includeKey, XMLConfigConstants.KEY, includeKeyElement);
            script.addChild(includeKeyElement);
        }
    }
    saveTracingState(script, scriptMediator);
    return script;
}
Also used : Value(org.apache.synapse.mediators.Value) OMTextImpl(org.apache.axiom.om.impl.llom.OMTextImpl) OMElement(org.apache.axiom.om.OMElement) ValueSerializer(org.apache.synapse.config.xml.ValueSerializer)

Example 3 with OMTextImpl

use of org.apache.axiom.om.impl.llom.OMTextImpl in project wso2-synapse by wso2.

the class EntrySerializer method serializeEntry.

/**
 * Serialize the Entry object to an OMElement representing the entry
 *
 * @param entry
 * @param parent
 * @return OMElement representing the entry
 */
public static OMElement serializeEntry(Entry entry, OMElement parent) {
    String customFactory = SynapsePropertiesLoader.getPropertyValue("synapse.entry.serializer", "");
    if (customFactory != null && !"".equals(customFactory)) {
        try {
            Class c = Class.forName(customFactory);
            Object o = c.newInstance();
            if (o instanceof IEntrySerializer) {
                return ((IEntrySerializer) o).serializeEntry(entry, parent);
            }
        } catch (ClassNotFoundException e) {
            handleException("Class specified by the synapse.entry.factory " + "synapse property not found: " + customFactory, e);
        } catch (InstantiationException e) {
            handleException("Class specified by the synapse.entry.factory " + "synapse property cannot be instantiated: " + customFactory, e);
        } catch (IllegalAccessException e) {
            handleException("Class specified by the synapse.entry.factory " + "synapse property cannot be accessed: " + customFactory, e);
        }
    }
    OMElement entryElement = fac.createOMElement("localEntry", synNS);
    entryElement.addAttribute(fac.createOMAttribute("key", nullNS, entry.getKey().trim()));
    int type = entry.getType();
    if (type == Entry.URL_SRC) {
        URL srcUrl = entry.getSrc();
        if (srcUrl != null) {
            entryElement.addAttribute(fac.createOMAttribute("src", nullNS, srcUrl.toString().trim()));
        }
    } else if (type == Entry.INLINE_XML) {
        Object value = entry.getValue();
        if (value != null && value instanceof OMElement) {
            entryElement.addChild((OMElement) value);
        }
    } else if (type == Entry.INLINE_TEXT) {
        Object value = entry.getValue();
        if (value != null && value instanceof String) {
            OMTextImpl textData = (OMTextImpl) fac.createOMText(((String) value).trim());
            textData.setType(XMLStreamConstants.CDATA);
            entryElement.addChild(textData);
        }
    } else if (type == Entry.REMOTE_ENTRY) {
        // nothing to serialize
        return null;
    } else {
        handleException("Entry type undefined");
    }
    if (entry.getDescription() != null) {
        OMElement descriptionElem = fac.createOMElement(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "description"), entryElement);
        descriptionElem.setText(entry.getDescription());
        entryElement.addChild(descriptionElem);
    }
    if (parent != null) {
        parent.addChild(entryElement);
    }
    return entryElement;
}
Also used : QName(javax.xml.namespace.QName) OMTextImpl(org.apache.axiom.om.impl.llom.OMTextImpl) OMElement(org.apache.axiom.om.OMElement) URL(java.net.URL)

Example 4 with OMTextImpl

use of org.apache.axiom.om.impl.llom.OMTextImpl 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;
}
Also used : InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) OMTextImpl(org.apache.axiom.om.impl.llom.OMTextImpl) OMElement(org.apache.axiom.om.OMElement) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 5 with OMTextImpl

use of org.apache.axiom.om.impl.llom.OMTextImpl in project wso2-synapse by wso2.

the class SynapseXPath method stringValueOf.

/**
 * <P>Evaluates the XPath expression against the MessageContext of the current message and
 * returns a String representation of the result</p>
 *
 * @param synCtx the source message which holds the MessageContext against full context
 * @return a String representation of the result of evaluation
 */
public String stringValueOf(MessageContext synCtx) {
    try {
        if (forceFailoverEvaluation) {
            if (log.isDebugEnabled()) {
                log.debug("Forced evaluation of the expression with the DOM parser by bypassing the Jaxen: " + getExpression());
            }
            throw new UnresolvableException("Forced to evaluate with DOM parser bypassing Jaxen");
        }
        InputStream inputStream = null;
        Object result = null;
        org.apache.axis2.context.MessageContext axis2MC = null;
        if (!forceDisableStreamXpath && "true".equals(enableStreamingXpath) && streamingXPATH != null && (((Axis2MessageContext) synCtx).getEnvelope() == null || ((Axis2MessageContext) synCtx).getEnvelope().getBody().getFirstElement() == null)) {
            try {
                axis2MC = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                String contentType = (String) axis2MC.getProperty(SynapseConstants.AXIS2_PROPERTY_CONTENT_TYPE);
                if (!isStreamingXpathSupportedContentType(contentType) && !Boolean.TRUE.equals(PassThroughConstants.MESSAGE_BUILDER_INVOKED)) {
                    RelayUtils.buildMessage(axis2MC);
                } else {
                    inputStream = getMessageInputStreamPT(axis2MC);
                }
            } catch (XMLStreamException e) {
                handleException("Error occurred while building the message from the message context", e);
            } catch (IOException e) {
                log.error("Error occurred while obtaining input stream from the message context", e);
            }
            if (inputStream != null) {
                try {
                    result = streamingXPATH.getStringValue(inputStream);
                } catch (XMLStreamException e) {
                    handleException("Error occurred while parsing the XPATH String", e);
                } catch (StreamingXPATHException e) {
                    handleException("Error occurred while parsing the XPATH String", e);
                }
            } else {
                try {
                    result = streamingXPATH.getStringValue(synCtx.getEnvelope());
                } catch (XMLStreamException e) {
                    handleException("Error occurred while parsing the XPATH String", e);
                } catch (StreamingXPATHException e) {
                    handleException("Error occurred while parsing the XPATH String", e);
                }
            }
        } else {
            result = evaluate(synCtx);
        }
        if (result == null) {
            return null;
        }
        StringBuffer textValue = new StringBuffer();
        if (result instanceof List) {
            List list = (List) result;
            for (Object o : list) {
                if (o == null && list.size() == 1) {
                    return null;
                }
                if (o instanceof OMTextImpl) {
                    textValue.append(((OMTextImpl) o).getText());
                } else if (o instanceof OMElementImpl) {
                    String s = ((OMElementImpl) o).getText();
                    // We use StringUtils.trim as String.trim does not remove U+00A0 (int 160) (No-break space)
                    if (s.replace(String.valueOf((char) 160), " ").trim().length() == 0) {
                        s = o.toString();
                    }
                    textValue.append(s);
                } else if (o instanceof OMDocumentImpl) {
                    textValue.append(((OMDocumentImpl) o).getOMDocumentElement().toString());
                } else if (o instanceof OMAttribute) {
                    textValue.append(((OMAttribute) o).getAttributeValue());
                } else if (o instanceof SynapseXPath) {
                    textValue.append(((SynapseXPath) o).stringValueOf(synCtx));
                }
            }
        } else if ("true".equals(enableStreamingXpath) && streamingXPATH != null) {
            if (!"".equals((String) result)) {
                OMElement re = AXIOMUtil.stringToOM((String) result);
                if (re != null) {
                    textValue.append(re.getText());
                } else {
                    textValue.append(result.toString());
                }
            }
        } else {
            textValue.append(result.toString());
        }
        return textValue.toString();
    } catch (UnresolvableException ex) {
        // xpath processing in DOM fashion which can support XPATH2.0 with supported XAPTH engine like SAXON
        if ("true".equals(domXpathConfig)) {
            if (log.isDebugEnabled()) {
                log.debug("AXIOM xpath evaluation failed with UnresolvableException, " + "trying to perform DOM based XPATH", ex);
            }
            try {
                return evaluateDOMXPath(synCtx);
            } catch (Exception e) {
                handleException("Evaluation of the XPath expression " + this.toString() + " resulted in an error", e);
            }
        } else {
            handleException("Evaluation of the XPath expression " + this.toString() + " resulted in an error", ex);
        }
    } catch (JaxenException je) {
        handleException("Evaluation of the XPath expression " + this.toString() + " resulted in an error", je);
    } catch (XMLStreamException e) {
        handleException("Evaluation of the XPath expression " + this.toString() + " resulted in an error", e);
    }
    return null;
}
Also used : StreamingXPATHException(org.apache.synapse.util.streaming_xpath.exception.StreamingXPATHException) InputStream(java.io.InputStream) OMDocumentImpl(org.apache.axiom.om.impl.llom.OMDocumentImpl) OMTextImpl(org.apache.axiom.om.impl.llom.OMTextImpl) OMElement(org.apache.axiom.om.OMElement) IOException(java.io.IOException) JaxenException(org.jaxen.JaxenException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnresolvableException(org.jaxen.UnresolvableException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) StreamingXPATHException(org.apache.synapse.util.streaming_xpath.exception.StreamingXPATHException) StreamingXPATHCompilerException(org.apache.synapse.util.streaming_xpath.compiler.exception.StreamingXPATHCompilerException) OMElementImpl(org.apache.axiom.om.impl.llom.OMElementImpl) XMLStreamException(javax.xml.stream.XMLStreamException) JaxenException(org.jaxen.JaxenException) SingletonList(org.jaxen.util.SingletonList) List(java.util.List) UnresolvableException(org.jaxen.UnresolvableException) OMAttribute(org.apache.axiom.om.OMAttribute) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

OMTextImpl (org.apache.axiom.om.impl.llom.OMTextImpl)7 OMElement (org.apache.axiom.om.OMElement)4 OMAttribute (org.apache.axiom.om.OMAttribute)3 SynapseException (org.apache.synapse.SynapseException)3 IOException (java.io.IOException)2 List (java.util.List)2 QName (javax.xml.namespace.QName)2 OMText (org.apache.axiom.om.OMText)2 OMDocumentImpl (org.apache.axiom.om.impl.llom.OMDocumentImpl)2 OMElementImpl (org.apache.axiom.om.impl.llom.OMElementImpl)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 Value (org.apache.synapse.mediators.Value)2 JaxenException (org.jaxen.JaxenException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)1 ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)1 JsonSchema (com.github.fge.jsonschema.main.JsonSchema)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1