Search in sources :

Example 6 with LoadingByteArrayOutputStream

use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.

the class InMessageRecorder method handleMessage.

public void handleMessage(Message message) throws Fault {
    InputStream is = message.getContent(InputStream.class);
    if (is == null) {
        return;
    }
    LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
    try {
        IOUtils.copy(is, bos);
        is.close();
        bos.close();
        byte[] bytes = bos.toByteArray();
        synchronized (inbound) {
            inbound.add(bytes);
        }
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("inbound: " + bos.toString());
        }
        ByteArrayInputStream bis = bos.createInputStream();
        message.setContent(InputStream.class, bis);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 7 with LoadingByteArrayOutputStream

use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.

the class WSDLToJavaContainer method generateLocalWSDL.

@SuppressWarnings("unchecked")
private void generateLocalWSDL(ToolContext context) {
    String outputdir = (String) context.get(ToolConstants.CFG_CLASSDIR);
    File wsdlFile = new File(outputdir, (String) context.get(ToolConstants.CFG_WSDLLOCATION));
    Definition def = context.get(Definition.class);
    try {
        // get imported schemas
        int xsdCount = 0;
        SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
        Map<String, String> sourceMap = new HashMap<>();
        for (XmlSchema imp : schemas.getXmlSchemas()) {
            if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
                String schemaFileName = "schema" + (++xsdCount) + ".xsd";
                sourceMap.put(imp.getTargetNamespace(), schemaFileName);
            }
        }
        // get imported wsdls
        int wsdlImportCount = 0;
        List<Definition> defs = (List<Definition>) context.get(ToolConstants.IMPORTED_DEFINITION);
        Map<String, String> importWSDLMap = new HashMap<>();
        for (Definition importDef : defs) {
            File importedWsdlFile = null;
            if (!StringUtils.isEmpty(importDef.getDocumentBaseURI())) {
                importedWsdlFile = new File(importDef.getDocumentBaseURI());
            } else {
                importedWsdlFile = new File(importDef.getQName().getLocalPart() + ".wsdl");
            }
            if (!FileUtils.isValidFileName(importedWsdlFile.getName())) {
                importedWsdlFile = new File("import" + (++wsdlImportCount) + ".wsdl");
            }
            importWSDLMap.put(importDef.getTargetNamespace(), importedWsdlFile.getName());
        }
        OutputStreamCreator outputStreamCreator = null;
        if (context.get(OutputStreamCreator.class) != null) {
            outputStreamCreator = context.get(OutputStreamCreator.class);
        } else {
            outputStreamCreator = new OutputStreamCreator();
            context.put(OutputStreamCreator.class, outputStreamCreator);
        }
        Writer os = null;
        for (XmlSchema imp : schemas.getXmlSchemas()) {
            if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
                String schemaFileName = sourceMap.get(imp.getTargetNamespace());
                File impfile = new File(wsdlFile.getParentFile(), schemaFileName);
                Element el = imp.getSchemaDocument().getDocumentElement();
                updateImports(el, sourceMap);
                os = new FileWriterUtil(impfile.getParent(), context.get(OutputStreamCreator.class)).getWriter(impfile, StandardCharsets.UTF_8.name());
                StaxUtils.writeTo(el, os, 2);
                os.close();
            }
        }
        // change the import location in wsdl file
        OutputStream wsdloutput = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()));
        WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
        LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
        wsdlWriter.writeWSDL(def, bout);
        Element defEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
        List<Element> xsdElements = DOMUtils.findAllElementsByTagNameNS(defEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
        for (Element xsdEle : xsdElements) {
            updateImports(xsdEle, sourceMap);
        }
        updateWSDLImports(defEle, importWSDLMap);
        StaxUtils.writeTo(defEle, wsdloutput);
        wsdloutput.close();
        for (Definition importDef : defs) {
            File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace()));
            OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(importWsdlFile.toPath()));
            bout = new LoadingByteArrayOutputStream();
            wsdlWriter.writeWSDL(importDef, bout);
            Element importEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
            xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
            for (Element xsdEle : xsdElements) {
                updateImports(xsdEle, sourceMap);
            }
            updateWSDLImports(importEle, importWSDLMap);
            StaxUtils.writeTo(importEle, wsdlOs);
            wsdlOs.close();
        }
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "FAILED_TO_GEN_LOCAL_WSDL", ex);
        Message msg = new Message("FAILED_TO_GEN_LOCAL_WSDL", LOG);
        throw new ToolException(msg, ex);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) Element(org.w3c.dom.Element) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) XmlSchema(org.apache.ws.commons.schema.XmlSchema) List(java.util.List) ArrayList(java.util.ArrayList) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) ToolException(org.apache.cxf.tools.common.ToolException) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Writer(java.io.Writer) WSDLWriter(javax.wsdl.xml.WSDLWriter)

Example 8 with LoadingByteArrayOutputStream

use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.

the class EndpointReferenceUtils method createSchema.

private static synchronized Schema createSchema(ServiceInfo serviceInfo, Bus b) {
    if (b == null) {
        b = BusFactory.getThreadDefaultBus(false);
    }
    Schema schema = serviceInfo.getProperty(Schema.class.getName(), Schema.class);
    if (schema == null) {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Map<String, byte[]> schemaSourcesMap = new LinkedHashMap<String, byte[]>();
        Map<String, Source> schemaSourcesMap2 = new LinkedHashMap<String, Source>();
        XMLStreamWriter writer = null;
        try {
            for (SchemaInfo si : serviceInfo.getSchemas()) {
                Element el = si.getElement();
                unsetReadonly(el);
                String baseURI = null;
                try {
                    baseURI = el.getBaseURI();
                } catch (Exception ex) {
                // ignore - not DOM level 3
                }
                if (baseURI == null) {
                    baseURI = si.getSystemId();
                }
                DOMSource ds = new DOMSource(el, baseURI);
                schemaSourcesMap2.put(si.getSystemId() + ":" + si.getNamespaceURI(), ds);
                LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
                writer = StaxUtils.createXMLStreamWriter(out);
                StaxUtils.copy(el, writer);
                writer.flush();
                schemaSourcesMap.put(si.getSystemId() + ":" + si.getNamespaceURI(), out.toByteArray());
            }
            for (XmlSchema sch : serviceInfo.getXmlSchemaCollection().getXmlSchemas()) {
                if (sch.getSourceURI() != null && !schemaSourcesMap.containsKey(sch.getSourceURI() + ":" + sch.getTargetNamespace())) {
                    InputStream ins = null;
                    try {
                        URL url = new URL(sch.getSourceURI());
                        ins = url.openStream();
                    } catch (Exception e) {
                    // ignore, we'll just use what we have.  (though
                    // bugs in XmlSchema could make this less useful)
                    }
                    LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
                    if (ins == null) {
                        sch.write(out);
                    } else {
                        IOUtils.copyAndCloseInput(ins, out);
                    }
                    schemaSourcesMap.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), out.toByteArray());
                    Source source = new StreamSource(out.createInputStream(), sch.getSourceURI());
                    schemaSourcesMap2.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), source);
                }
            }
            factory.setResourceResolver(new SchemaLSResourceResolver(schemaSourcesMap, b));
            schema = factory.newSchema(schemaSourcesMap2.values().toArray(new Source[schemaSourcesMap2.size()]));
        } catch (Exception ex) {
            // Something not right with the schema from the wsdl.
            LOG.log(Level.WARNING, "SAXException for newSchema()", ex);
            for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
                String s = StaxUtils.toString(schemaInfo.getElement(), 4);
                LOG.log(Level.INFO, "Schema for: " + schemaInfo.getNamespaceURI() + "\n" + s);
            }
        } finally {
            for (Source src : schemaSourcesMap2.values()) {
                if (src instanceof DOMSource) {
                    Node nd = ((DOMSource) src).getNode();
                    unsetReadonly(nd);
                }
            }
            StaxUtils.close(writer);
        }
        serviceInfo.setProperty(Schema.class.getName(), schema);
    }
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) XmlSchema(org.apache.ws.commons.schema.XmlSchema) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 9 with LoadingByteArrayOutputStream

use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.

the class ColocUtil method convertObjectToSource.

public static void convertObjectToSource(Message message) {
    List<Object> content = CastUtils.cast(message.getContent(List.class));
    if (content == null || content.size() < 1) {
        // nothing to convert
        return;
    }
    // only supporting the wrapped style for now  (one pojo <-> one source)
    Object object = content.get(0);
    DataWriter<OutputStream> writer = message.getExchange().getService().getDataBinding().createWriter(OutputStream.class);
    LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
    writer.write(object, bos);
    content.set(0, new StreamSource(bos.createInputStream()));
}
Also used : LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) OutputStream(java.io.OutputStream) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List)

Example 10 with LoadingByteArrayOutputStream

use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.

the class CachedOutputStream method resetOut.

/**
 * Replace the original stream with the new one, optionally copying the content of the old one
 * into the new one.
 * When with Attachment, needs to replace the xml writer stream with the stream used by
 * AttachmentSerializer or copy the cached output stream to the "real"
 * output stream, i.e. onto the wire.
 *
 * @param out the new output stream
 * @param copyOldContent flag indicating if the old content should be copied
 * @throws IOException
 */
public void resetOut(OutputStream out, boolean copyOldContent) throws IOException {
    if (out == null) {
        out = new LoadingByteArrayOutputStream();
    }
    if (currentStream instanceof CachedOutputStream) {
        CachedOutputStream ac = (CachedOutputStream) currentStream;
        InputStream in = ac.getInputStream();
        IOUtils.copyAndCloseInput(in, out);
    } else {
        if (inmem) {
            if (currentStream instanceof ByteArrayOutputStream) {
                ByteArrayOutputStream byteOut = (ByteArrayOutputStream) currentStream;
                if (copyOldContent && byteOut.size() > 0) {
                    byteOut.writeTo(out);
                }
            } else {
                throw new IOException("Unknown format of currentStream");
            }
        } else {
            // read the file
            try {
                currentStream.close();
                if (copyOldContent) {
                    InputStream fin = createInputStream(tempFile);
                    IOUtils.copyAndCloseInput(fin, out);
                }
            } finally {
                streamList.remove(currentStream);
                deleteTempFile();
                inmem = true;
            }
        }
    }
    currentStream = out;
    outputLocked = false;
}
Also used : LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

LoadingByteArrayOutputStream (org.apache.cxf.helpers.LoadingByteArrayOutputStream)13 IOException (java.io.IOException)9 InputStream (java.io.InputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileInputStream (java.io.FileInputStream)4 CipherInputStream (javax.crypto.CipherInputStream)4 OutputStream (java.io.OutputStream)3 URISyntaxException (java.net.URISyntaxException)3 List (java.util.List)3 StreamSource (javax.xml.transform.stream.StreamSource)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2