Search in sources :

Example 41 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class AttachmentDeserializer method initializeRootMessage.

protected void initializeRootMessage() throws IOException {
    contentType = (String) message.get(Message.CONTENT_TYPE);
    if (contentType == null) {
        throw new IllegalStateException("Content-Type can not be empty!");
    }
    if (message.getContent(InputStream.class) == null) {
        throw new IllegalStateException("An InputStream must be provided!");
    }
    if (AttachmentUtil.isTypeSupported(contentType.toLowerCase(), supportedTypes)) {
        String boundaryString = findBoundaryFromContentType(contentType);
        if (null == boundaryString) {
            boundaryString = findBoundaryFromInputStream();
        }
        // If a boundary still wasn't found, throw an exception
        if (null == boundaryString) {
            throw new IOException("Couldn't determine the boundary from the message!");
        }
        boundary = boundaryString.getBytes("utf-8");
        stream = new PushbackInputStream(message.getContent(InputStream.class), pbAmount);
        if (!readTillFirstBoundary(stream, boundary)) {
            throw new IOException("Couldn't find MIME boundary: " + boundaryString);
        }
        Map<String, List<String>> ih = loadPartHeaders(stream);
        message.put(ATTACHMENT_PART_HEADERS, ih);
        String val = AttachmentUtil.getHeader(ih, "Content-Type", "; ");
        if (!StringUtils.isEmpty(val)) {
            String cs = HttpHeaderHelper.findCharset(val);
            if (!StringUtils.isEmpty(cs)) {
                message.put(Message.ENCODING, HttpHeaderHelper.mapCharset(cs));
            }
        }
        val = AttachmentUtil.getHeader(ih, "Content-Transfer-Encoding");
        MimeBodyPartInputStream mmps = new MimeBodyPartInputStream(stream, boundary, pbAmount);
        InputStream ins = AttachmentUtil.decode(mmps, val);
        if (ins != mmps) {
            ih.remove("Content-Transfer-Encoding");
        }
        body = new DelegatingInputStream(ins, this);
        createCount++;
        message.setContent(InputStream.class, body);
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 42 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class AttachmentDeserializer method findBoundaryFromInputStream.

private String findBoundaryFromInputStream() throws IOException {
    InputStream is = message.getContent(InputStream.class);
    // boundary should definitely be in the first 2K;
    PushbackInputStream in = new PushbackInputStream(is, 4096);
    byte[] buf = new byte[2048];
    int i = in.read(buf);
    int len = i;
    while (i > 0 && len < buf.length) {
        i = in.read(buf, len, buf.length - len);
        if (i > 0) {
            len += i;
        }
    }
    String msg = IOUtils.newStringFromBytes(buf, 0, len);
    in.unread(buf, 0, len);
    // Reset the input stream since we'll need it again later
    message.setContent(InputStream.class, in);
    // Use regex to get the boundary and return null if it's not found
    Matcher m = INPUT_STREAM_BOUNDARY_PATTERN.matcher(msg);
    return m.find() ? "--" + m.group(1) : null;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) Matcher(java.util.regex.Matcher) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream)

Example 43 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class AttachmentDeserializerTest method testSmallStream.

@Test
public void testSmallStream() throws Exception {
    byte[] messageBytes = ("------=_Part_1\n\nJJJJ\n------=_Part_1\n\n" + "Content-Transfer-Encoding: binary\n\n=3D=3D=3D\n------=_Part_1\n").getBytes();
    PushbackInputStream pushbackStream = new PushbackInputStream(new ByteArrayInputStream(messageBytes), 2048);
    pushbackStream.read(new byte[4096], 0, 4015);
    pushbackStream.unread(messageBytes);
    pushbackStream.read(new byte[72]);
    MimeBodyPartInputStream m = new MimeBodyPartInputStream(pushbackStream, "------=_Part_1".getBytes(), 2048);
    assertEquals(10, m.read(new byte[1000]));
    assertEquals(-1, m.read(new byte[1000]));
    assertEquals(-1, m.read(new byte[1000]));
    m.close();
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 44 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class IOUtilsTest method testNonEmptyWithPushBack.

@Test
public void testNonEmptyWithPushBack() throws Exception {
    InputStream is = new PushbackInputStream(new ByteArrayInputStream("Hello".getBytes()));
    assertFalse(IOUtils.isEmpty(is));
    assertEquals("Hello", IOUtils.toString(is));
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 45 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class IOUtils method isEmpty.

public static boolean isEmpty(InputStream is) throws IOException {
    if (is == null) {
        return true;
    }
    // if available is 0 it does not mean it is empty
    if (is.available() > 0) {
        return false;
    }
    final byte[] bytes = new byte[1];
    if (is.markSupported()) {
        is.mark(1);
        try {
            return isEof(is.read(bytes));
        } finally {
            is.reset();
        }
    }
    if (!(is instanceof PushbackInputStream)) {
        return false;
    }
    // it may be an attachment stream
    PushbackInputStream pbStream = (PushbackInputStream) is;
    boolean isEmpty = isEof(pbStream.read(bytes));
    if (!isEmpty) {
        pbStream.unread(bytes);
    }
    return isEmpty;
}
Also used : PushbackInputStream(java.io.PushbackInputStream)

Aggregations

PushbackInputStream (java.io.PushbackInputStream)80 IOException (java.io.IOException)42 InputStream (java.io.InputStream)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FileInputStream (java.io.FileInputStream)6 CertificateException (java.security.cert.CertificateException)5 Test (org.junit.Test)5 File (java.io.File)4 CRLException (java.security.cert.CRLException)4 CertificateParsingException (java.security.cert.CertificateParsingException)4 InputStreamReader (java.io.InputStreamReader)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Charset (java.nio.charset.Charset)2