Search in sources :

Example 1 with OMDocumentImpl

use of org.apache.axiom.om.impl.llom.OMDocumentImpl 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 OMDocumentImpl

use of org.apache.axiom.om.impl.llom.OMDocumentImpl in project carbon-apimgt by wso2.

the class ApplicationThrottleController method lookup.

private static OMNode lookup(String key, int tenantId) {
    try {
        Resource resource = getResource(key, tenantId);
        if (resource instanceof Collection || resource == null) {
            return null;
        }
        ByteArrayInputStream inputStream = null;
        Object content = resource.getContent();
        if (content instanceof String) {
            inputStream = new ByteArrayInputStream(content.toString().getBytes(Charset.defaultCharset()));
        } else if (content instanceof byte[]) {
            inputStream = new ByteArrayInputStream((byte[]) content);
        }
        OMNode result = null;
        try {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
            XMLStreamReader parser = factory.createXMLStreamReader(inputStream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            result = builder.getDocumentElement();
        } catch (OMException ignored) {
            result = readNonXML(resource);
        } catch (XMLStreamException ignored) {
            result = readNonXML(resource);
        } catch (Exception e) {
            // a more general exception(e.g. a Runtime exception if the XML doc has an
            // external DTD deceleration and if not connected to internet) which in case
            // just log for debugging
            log.error("Error while reading the resource '" + key + '\'', e);
        } finally {
            try {
                resource.discard();
                if (result != null && result.getParent() != null) {
                    result.detach();
                    OMDocumentImpl parent = new OMDocumentImpl(OMAbstractFactory.getOMFactory());
                    parent.addChild(result);
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error("Error while closing the input stream", e);
            }
        }
        return result;
    } catch (RegistryException e) {
        handleException("Error while fetching the resource " + key, e);
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Resource(org.wso2.carbon.registry.core.Resource) OMDocumentImpl(org.apache.axiom.om.impl.llom.OMDocumentImpl) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) Collection(org.wso2.carbon.registry.core.Collection) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with OMDocumentImpl

use of org.apache.axiom.om.impl.llom.OMDocumentImpl 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)

Example 4 with OMDocumentImpl

use of org.apache.axiom.om.impl.llom.OMDocumentImpl in project ballerina by ballerina-lang.

the class BXMLItem method copy.

/**
 * {@inheritDoc}
 */
@Override
public BXMLItem copy() {
    OMNode clonedNode = null;
    switch(nodeType) {
        case ELEMENT:
            clonedNode = ((OMElement) omNode).cloneOMElement();
            break;
        case TEXT:
            TextImpl text = new TextImpl();
            text.setTextContent(((OMText) omNode).getText());
            clonedNode = text;
            break;
        case COMMENT:
            CommentImpl comment = new CommentImpl();
            comment.setTextContent(((OMComment) omNode).getValue());
            clonedNode = comment;
            break;
        case PI:
            OMProcessingInstructionImpl pi = new OMProcessingInstructionImpl();
            pi.setTarget(((OMProcessingInstruction) omNode).getTarget());
            pi.setValue(((OMProcessingInstruction) omNode).getValue());
            clonedNode = pi;
            break;
        default:
            clonedNode = omNode;
            break;
    }
    // adding the document element as parent, to get xpPaths work
    OMDocument doc = new OMDocumentImpl();
    doc.addChild(clonedNode);
    return new BXMLItem(clonedNode);
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMDocumentImpl(org.apache.axiom.om.impl.llom.OMDocumentImpl) CommentImpl(org.apache.axiom.om.impl.dom.CommentImpl) OMProcessingInstructionImpl(org.apache.axiom.om.impl.llom.OMProcessingInstructionImpl) TextImpl(org.apache.axiom.om.impl.dom.TextImpl) OMDocument(org.apache.axiom.om.OMDocument)

Aggregations

OMDocumentImpl (org.apache.axiom.om.impl.llom.OMDocumentImpl)4 IOException (java.io.IOException)2 List (java.util.List)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 OMAttribute (org.apache.axiom.om.OMAttribute)2 OMElementImpl (org.apache.axiom.om.impl.llom.OMElementImpl)2 OMTextImpl (org.apache.axiom.om.impl.llom.OMTextImpl)2 JaxenException (org.jaxen.JaxenException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 OMDocument (org.apache.axiom.om.OMDocument)1 OMElement (org.apache.axiom.om.OMElement)1 OMNode (org.apache.axiom.om.OMNode)1 StAXOMBuilder (org.apache.axiom.om.impl.builder.StAXOMBuilder)1 CommentImpl (org.apache.axiom.om.impl.dom.CommentImpl)1 TextImpl (org.apache.axiom.om.impl.dom.TextImpl)1 OMProcessingInstructionImpl (org.apache.axiom.om.impl.llom.OMProcessingInstructionImpl)1