Search in sources :

Example 1 with MessageDigestInputStream

use of org.apache.cxf.common.util.MessageDigestInputStream in project cxf by apache.

the class BinaryDataProvider method readFrom.

public T readFrom(Class<T> clazz, Type genericType, Annotation[] annotations, MediaType type, MultivaluedMap<String, String> headers, InputStream is) throws IOException {
    try {
        if (InputStream.class.isAssignableFrom(clazz)) {
            if (DigestInputStream.class.isAssignableFrom(clazz)) {
                is = new MessageDigestInputStream(is);
            }
            return clazz.cast(is);
        }
        if (Reader.class.isAssignableFrom(clazz)) {
            return clazz.cast(new InputStreamReader(is, getEncoding(type)));
        }
        if (byte[].class.isAssignableFrom(clazz)) {
            return clazz.cast(IOUtils.readBytesFromStream(is));
        }
        if (File.class.isAssignableFrom(clazz)) {
            LOG.warning("Reading data into File objects with the help of pre-packaged" + " providers is not recommended - use InputStream or custom File reader");
            // create a temp file, delete on exit
            File f = FileUtils.createTempFile("File" + UUID.randomUUID().toString(), "jaxrs", null, true);
            OutputStream os = Files.newOutputStream(f.toPath());
            IOUtils.copy(is, os, bufferSize);
            os.close();
            return clazz.cast(f);
        }
        if (StreamingOutput.class.isAssignableFrom(clazz)) {
            return clazz.cast(new ReadingStreamingOutput(is));
        }
    } catch (ClassCastException e) {
        String msg = "Unsupported class: " + clazz.getName();
        LOG.warning(msg);
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }
    throw new IOException("Unrecognized class");
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) NioOutputStream(org.apache.cxf.jaxrs.nio.NioOutputStream) IOException(java.io.IOException) MessageDigestInputStream(org.apache.cxf.common.util.MessageDigestInputStream) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 MessageDigestInputStream (org.apache.cxf.common.util.MessageDigestInputStream)1 NioOutputStream (org.apache.cxf.jaxrs.nio.NioOutputStream)1