Search in sources :

Example 1 with LoadingByteArrayOutputStream

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

the class CachedOutputStream method writeCacheTo.

public void writeCacheTo(StringBuilder out, String charsetName, long limit) throws IOException {
    flush();
    if (totalLength < limit || limit == -1) {
        writeCacheTo(out, charsetName);
        return;
    }
    long count = 0;
    if (inmem) {
        if (currentStream instanceof LoadingByteArrayOutputStream) {
            LoadingByteArrayOutputStream lout = (LoadingByteArrayOutputStream) currentStream;
            out.append(IOUtils.newStringFromBytes(lout.getRawBytes(), charsetName, 0, (int) limit));
        } else if (currentStream instanceof ByteArrayOutputStream) {
            byte[] bytes = ((ByteArrayOutputStream) currentStream).toByteArray();
            out.append(IOUtils.newStringFromBytes(bytes, charsetName, 0, (int) limit));
        } else {
            throw new IOException("Unknown format of currentStream");
        }
    } else {
        // read the file
        try (InputStream fin = createInputStream(tempFile);
            Reader reader = new InputStreamReader(fin, charsetName)) {
            char[] bytes = new char[1024];
            long x = reader.read(bytes);
            while (x != -1) {
                if ((count + x) > limit) {
                    x = limit - count;
                }
                out.append(bytes, 0, (int) x);
                count += x;
                if (count >= limit) {
                    x = -1;
                } else {
                    x = reader.read(bytes);
                }
            }
        }
    }
}
Also used : LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) InputStreamReader(java.io.InputStreamReader) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with LoadingByteArrayOutputStream

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

the class CachedOutputStream method writeCacheTo.

public void writeCacheTo(StringBuilder out, String charsetName) throws IOException {
    flush();
    if (inmem) {
        if (currentStream instanceof LoadingByteArrayOutputStream) {
            LoadingByteArrayOutputStream lout = (LoadingByteArrayOutputStream) currentStream;
            out.append(IOUtils.newStringFromBytes(lout.getRawBytes(), charsetName, 0, lout.size()));
        } else if (currentStream instanceof ByteArrayOutputStream) {
            byte[] bytes = ((ByteArrayOutputStream) currentStream).toByteArray();
            out.append(IOUtils.newStringFromBytes(bytes, charsetName));
        } else {
            throw new IOException("Unknown format of currentStream");
        }
    } else {
        // read the file
        try (InputStream fin = createInputStream(tempFile);
            Reader reader = new InputStreamReader(fin, charsetName)) {
            char[] bytes = new char[1024];
            int x = reader.read(bytes);
            while (x != -1) {
                out.append(bytes, 0, x);
                x = reader.read(bytes);
            }
        }
    }
}
Also used : LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) InputStreamReader(java.io.InputStreamReader) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with LoadingByteArrayOutputStream

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

the class URIResolver method tryRemote.

private void tryRemote(String uriStr) throws IOException {
    try {
        LoadingByteArrayOutputStream bout = cache.get(uriStr);
        url = new URL(uriStr);
        uri = new URI(url.toString());
        if (bout == null) {
            URLConnection connection = url.openConnection();
            is = connection.getInputStream();
            bout = new LoadingByteArrayOutputStream(1024);
            IOUtils.copy(is, bout);
            is.close();
            cache.put(uriStr, bout);
        }
        is = bout.createInputStream();
    } catch (MalformedURLException e) {
    // do nothing
    } catch (URISyntaxException e) {
    // do nothing
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 4 with LoadingByteArrayOutputStream

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

the class SwAOutInterceptor method processAttachments.

protected void processAttachments(SoapMessage message, SoapBodyInfo sbi) {
    Collection<Attachment> atts = setupAttachmentOutput(message);
    List<Object> outObjects = CastUtils.cast(message.getContent(List.class));
    for (MessagePartInfo mpi : sbi.getAttachments()) {
        String partName = mpi.getConcreteName().getLocalPart();
        String ct = (String) mpi.getProperty(Message.CONTENT_TYPE);
        String id = new StringBuilder().append(partName).append("=").append(UUID.randomUUID()).append("@apache.org").toString();
        // this assumes things are in order...
        int idx = mpi.getIndex();
        Object o = outObjects.get(idx);
        if (o == null) {
            continue;
        }
        outObjects.set(idx, null);
        DataHandler dh = null;
        // This code could probably be refactored out somewhere...
        if (o instanceof Source) {
            dh = new DataHandler(createDataSource((Source) o, ct));
        } else if (o instanceof Image) {
            final Image img = (Image) o;
            final String contentType = ct;
            dh = new DataHandler(o, ct) {

                @Override
                public InputStream getInputStream() throws IOException {
                    LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
                    writeTo(bout);
                    return bout.createInputStream();
                }

                @Override
                public void writeTo(OutputStream out) throws IOException {
                    ImageWriter writer = null;
                    Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(contentType);
                    if (writers.hasNext()) {
                        writer = writers.next();
                    }
                    if (writer != null) {
                        BufferedImage bimg = convertToBufferedImage(img);
                        ImageOutputStream iout = ImageIO.createImageOutputStream(out);
                        writer.setOutput(iout);
                        writer.write(bimg);
                        writer.dispose();
                        iout.flush();
                        out.flush();
                    }
                }
            };
        } else if (o instanceof DataHandler) {
            dh = (DataHandler) o;
            ct = dh.getContentType();
            try {
                if ("text/xml".equals(ct) && dh.getContent() instanceof Source) {
                    dh = new DataHandler(createDataSource((Source) dh.getContent(), ct));
                }
            } catch (IOException e) {
            // ignore, use same dh
            }
        } else if (o instanceof byte[]) {
            if (ct == null) {
                ct = "application/octet-stream";
            }
            dh = new DataHandler(new ByteDataSource((byte[]) o, ct));
        } else if (o instanceof String) {
            if (ct == null) {
                ct = "text/plain; charset=\'UTF-8\'";
            }
            dh = new DataHandler(new ByteDataSource(((String) o).getBytes(StandardCharsets.UTF_8), ct));
        } else {
            throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_NOT_SUPPORTED", LOG, o.getClass()));
        }
        AttachmentImpl att = new AttachmentImpl(id);
        att.setDataHandler(dh);
        att.setHeader("Content-Type", ct);
        att.setHeader("Content-ID", "<" + id + ">");
        atts.add(att);
    }
}
Also used : LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ImageWriter(javax.imageio.ImageWriter) Attachment(org.apache.cxf.message.Attachment) Fault(org.apache.cxf.interceptor.Fault) DataHandler(javax.activation.DataHandler) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) BufferedImage(java.awt.image.BufferedImage) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) List(java.util.List) ArrayList(java.util.ArrayList) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IOException(java.io.IOException) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl)

Example 5 with LoadingByteArrayOutputStream

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

the class StaxSerializer method createStreamContext.

InputStream createStreamContext(byte[] source, Node ctx) throws XMLEncryptionException {
    Vector<InputStream> v = new Vector<>(2);
    LoadingByteArrayOutputStream byteArrayOutputStream = new LoadingByteArrayOutputStream();
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
        outputStreamWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><dummy");
        // Run through each node up to the document node and find any xmlns: nodes
        Map<String, String> storedNamespaces = new HashMap<>();
        Node wk = ctx;
        while (wk != null) {
            NamedNodeMap atts = wk.getAttributes();
            if (atts != null) {
                for (int i = 0; i < atts.getLength(); ++i) {
                    Node att = atts.item(i);
                    String nodeName = att.getNodeName();
                    if (("xmlns".equals(nodeName) || nodeName.startsWith("xmlns:")) && !storedNamespaces.containsKey(att.getNodeName())) {
                        outputStreamWriter.write(" ");
                        outputStreamWriter.write(nodeName);
                        outputStreamWriter.write("=\"");
                        outputStreamWriter.write(att.getNodeValue());
                        outputStreamWriter.write("\"");
                        storedNamespaces.put(nodeName, att.getNodeValue());
                    }
                }
            }
            wk = wk.getParentNode();
        }
        outputStreamWriter.write(">");
        outputStreamWriter.close();
        v.add(byteArrayOutputStream.createInputStream());
        v.addElement(new ByteArrayInputStream(source));
        byteArrayOutputStream = new LoadingByteArrayOutputStream();
        outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
        outputStreamWriter.write("</dummy>");
        outputStreamWriter.close();
        v.add(byteArrayOutputStream.createInputStream());
    } catch (UnsupportedEncodingException e) {
        throw new XMLEncryptionException(e);
    } catch (IOException e) {
        throw new XMLEncryptionException(e);
    }
    return new SequenceInputStream(v.elements());
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) Vector(java.util.Vector)

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