Package org.apache.synapse.util

Examples of org.apache.synapse.util.TemporaryData$InputStreamImpl


    private void performXSLT(MessageContext synCtx, final boolean traceOrDebugOn,
        final boolean traceOn) {

        boolean reCreate = false;
        OMNode sourceNode = getTransformSource(synCtx);
        TemporaryData tempTargetData = null;
        OutputStream osForTarget;
        boolean isSoapEnvelope = (sourceNode == synCtx.getEnvelope());
        boolean isSoapBody = (sourceNode == synCtx.getEnvelope().getBody());

        if (traceOrDebugOn) {
            trace.trace("Transformation source : " + sourceNode.toString());
        }

        Source transformSrc;
        Result transformTgt = null;

        if (useDOMSourceAndResults) {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Using a DOMSource for transformation");
            }

            // for fast transformations create a DOMSource - ** may not work always though **
            transformSrc = new DOMSource(
                ((Element) ElementHelper.importOMElement((OMElement) sourceNode,
                DOOMAbstractFactory.getOMFactory())).getOwnerDocument());
            DocumentBuilderFactoryImpl.setDOOMRequired(true);

            try {
                transformTgt = new DOMResult(
                    DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().newDocument());
            } catch (ParserConfigurationException e) {
                handleException("Error creating a DOMResult for the transformation," +
                    " Consider setting optimization feature : " + USE_DOM_SOURCE_AND_RESULTS +
                    " off", e, synCtx);
            }

        } else {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Using byte array serialization for transformation");
            }

            transformSrc = AXIOMUtils.asSource(sourceNode);
           
            tempTargetData = synCtx.getEnvironment().createTemporaryData();
            osForTarget = tempTargetData.getOutputStream();
            transformTgt = new StreamResult(osForTarget);
        }

        if (transformTgt == null) {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Was unable to get a javax.xml.transform.Result created");
            }
            return;
        }

        // build transformer - if necessary
        Entry dp = synCtx.getConfiguration().getEntryDefinition(xsltKey);

        // if the xsltKey refers to a dynamic resource
        if (dp != null && dp.isDynamic()) {
            if (!dp.isCached() || dp.isExpired()) {
                reCreate = true;
            }
        }

        synchronized (transformerLock) {
            if (reCreate || cachedTemplates == null) {
                try {
                    cachedTemplates = transFact.newTemplates(
                        SynapseConfigUtils.getStreamSource(synCtx.getEntry(xsltKey)));
                    if (cachedTemplates == null) {
                        handleException("Error compiling the XSLT with key : " + xsltKey, synCtx);
                    }
                } catch (Exception e) {
                    handleException("Error creating XSLT transformer using : "
                        + xsltKey, e, synCtx);
                }
            }
        }

        try {
            // perform transformation
            Transformer transformer = cachedTemplates.newTransformer();
            if (!properties.isEmpty()) {
                // set the parameters which will pass to the Transformation
                for (MediatorProperty prop : properties) {
                    if (prop != null) {
                        if (prop.getValue() != null) {
                            transformer.setParameter(prop.getName(), prop.getValue());
                        } else {
                            transformer.setParameter(prop.getName(),
                                    prop.getExpression().stringValueOf(synCtx));
                        }
                    }
                }
            }

            transformer.setErrorListener(new ErrorListener() {

                public void warning(TransformerException e) throws TransformerException {

                    if (traceOrDebugOn) {
                        traceOrDebugWarn(
                                traceOn, "Warning encountered during transformation : " + e);
                    }
                }
               
                public void error(TransformerException e) throws TransformerException {
                    log.error("Error occured in XSLT transformation : " + e);
                    throw e;
                }
               
                public void fatalError(TransformerException e) throws TransformerException {
                    log.error("Fatal error occured in the XSLT transformation : " + e);
                    throw e;
                }
            });
           
            transformer.transform(transformSrc, transformTgt);

            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Transformation completed - processing result");
            }

            // get the result OMElement
            OMElement result = null;
            if (transformTgt instanceof DOMResult) {

                Node node = ((DOMResult) transformTgt).getNode();
                if (node == null) {
                    if (traceOrDebugOn) {
                        traceOrDebug(traceOn, ("Transformation result (DOMResult) was null"));
                    }
                    return;
                }

                Node resultNode = node.getFirstChild();
                if (resultNode == null) {
                    if (traceOrDebugOn) {
                        traceOrDebug(traceOn, ("Transformation result (DOMResult) was empty"));
                    }
                    return;
                }

                result = ElementHelper.importOMElement(
                    (OMElement) resultNode, OMAbstractFactory.getOMFactory());

            } else {

                String outputMethod = transformer.getOutputProperty(OutputKeys.METHOD);
                String encoding = transformer.getOutputProperty(OutputKeys.ENCODING);

                if (traceOrDebugOn) {
                    traceOrDebug(traceOn, "output method: " + outputMethod
                            + "; encoding: " + encoding);
                }
               
                if ("text".equals(outputMethod)) {
                    result = handleNonXMLResult(tempTargetData, Charset.forName(encoding),
                                                traceOrDebugOn, traceOn);
                } else {
                    try {
                        XMLStreamReader reader = StAXUtils.createXMLStreamReader(
                            tempTargetData.getInputStream());
                        if (isSoapEnvelope) {
                            result = new StAXSOAPModelBuilder(reader).getSOAPEnvelope();
                        } else {
                            result = new StAXOMBuilder(reader).getDocumentElement();
                        }                       
View Full Code Here


        String tempPrefix = synapseConfig.getProperty(SynapseConstants.TEMP_FILE_PREFIX,
                SynapseConstants.DEFAULT_TEMPFILE_PREFIX);
        String tempSuffix = synapseConfig.getProperty(SynapseConstants.TEMP_FILE_SUFIX,
                SynapseConstants.DEFAULT_TEMPFILE_SUFIX);

        return new TemporaryData(numberOfChunks, chunkSize, tempPrefix, tempSuffix);
    }
View Full Code Here

TOP

Related Classes of org.apache.synapse.util.TemporaryData$InputStreamImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.