class DataSourceProvider implements MessageBodyReader<T>, MessageBodyWriter<T> {

    public T readFrom(Class<T> cls, Type genericType, Annotation[] annotations,
                               MediaType type,
                               MultivaluedMap<String, String> headers, InputStream is)
        throws IOException {

        DataSource ds = null;
        if (cls == FileDataSource.class) {
            File file = new BinaryDataProvider<File>().readFrom(File.class, File.class, annotations, type, headers, is);
            ds = new FileDataSource(file);
        } else if (cls == DataSource.class || cls == DataHandler.class) {
            ds = new InputStreamDataSource(is, type.toString());
        } else {
            LOG.warning("Unsupported DataSource class: " + cls.getName());
            throw ExceptionUtils.toWebApplicationException(null, null);
        }
        return cls.cast(DataSource.class.isAssignableFrom(cls) ? ds : new DataHandler(ds));
    }

}