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)); }
public void writeTo(T o, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType type, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException { if (InputStream.class.isAssignableFrom(o.getClass())) { copyInputToOutput((InputStream)o, os, annotations, headers); } else if (File.class.isAssignableFrom(o.getClass())) { copyInputToOutput(new BufferedInputStream( Files.newInputStream(((File)o).toPath())), os, annotations, headers); } else if (byte[].class.isAssignableFrom(o.getClass())) { copyInputToOutput(new ByteArrayInputStream((byte[])o), os, annotations, headers); } else if (Reader.class.isAssignableFrom(o.getClass())) { try { Writer writer = new OutputStreamWriter(os, getEncoding(type)); IOUtils.copy((Reader)o, writer, bufferSize); writer.flush(); } finally { ((Reader)o).close(); } } else if (StreamingOutput.class.isAssignableFrom(o.getClass())) { ((StreamingOutput)o).write(os); } else { throw new IOException("Unrecognized class"); } }
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)) { return clazz.cast(is); } if (Reader.class.isAssignableFrom(clazz)) { return clazz.cast(new InputStreamReader(is, getEncoding(type))); } if (byte[].class.isAssignableFrom(clazz)) { String enc = getCharset(type); if (enc == null) { return clazz.cast(IOUtils.readBytesFromStream(is)); } else { return clazz.cast(IOUtils.toString(is, enc).getBytes(enc)); } } 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"); }
protected void copyInputToOutput(InputStream is, OutputStream os, Annotation[] anns, MultivaluedMap<String, Object> outHeaders) throws IOException { if (isRangeSupported()) { Message inMessage = PhaseInterceptorChain.getCurrentMessage().getExchange().getInMessage(); handleRangeRequest(is, os, new HttpHeadersImpl(inMessage), outHeaders); } else { boolean nioWrite = AnnotationUtils.getAnnotation(anns, UseNio.class) != null; if (nioWrite) { ContinuationProvider provider = getContinuationProvider(); if (provider != null) { copyUsingNio(is, os, provider.getContinuation()); } return; } if (closeResponseInputStream) { IOUtils.copyAndCloseInput(is, os, bufferSize); } else { IOUtils.copy(is, os, bufferSize); } } }
BinaryDataProvider<Object> p = new BinaryDataProvider<>(); p.setProduceMediaTypes(Collections.singletonList("application/bar")); p.setEnableBuffering(true); p.setReportByteArraySize(true); providers.add(p); providers.add(new BookStore.PrimitiveIntArrayReaderWriter());
protected void copyInputToOutput(InputStream is, OutputStream os, MultivaluedMap<String, Object> outHeaders) throws IOException { if (isRangeSupported()) { Message inMessage = PhaseInterceptorChain.getCurrentMessage().getExchange().getInMessage(); handleRangeRequest(is, os, new HttpHeadersImpl(inMessage), outHeaders); } else if (closeResponseInputStream) { IOUtils.copyAndCloseInput(is, os); } else { IOUtils.copy(is, os); } }
protected static void initFactory(ProviderFactory factory) { // ensure to not load providers not available in a module environment if not needed factory.setProviders(false, false, new BinaryDataProvider<Object>(), new SourceProvider<Object>(), tryCreateInstance("org.apache.cxf.jaxrs.provider.DataSourceProvider"), new FormEncodingProvider<Object>(), new StringTextProvider(), new PrimitiveTextProvider<Object>(), tryCreateInstance(JAXB_PROVIDER_NAME), tryCreateInstance("org.apache.cxf.jaxrs.provider.JAXBElementTypedProvider"), tryCreateInstance("org.apache.cxf.jaxrs.provider.MultipartProvider")); Object prop = factory.getBus().getProperty("skip.default.json.provider.registration"); if (!PropertyUtils.isTrue(prop)) { factory.setProviders(false, false, createProvider(JSON_PROVIDER_NAME, factory.getBus())); } }
return clazz.cast(new InputStreamReader(is, getEncoding(type)));
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)); }
public void writeTo(T o, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType type, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException { if (InputStream.class.isAssignableFrom(o.getClass())) { copyInputToOutput((InputStream)o, os, headers); } else if (File.class.isAssignableFrom(o.getClass())) { copyInputToOutput(new BufferedInputStream( new FileInputStream((File)o)), os, headers); } else if (byte[].class.isAssignableFrom(o.getClass())) { copyInputToOutput(new ByteArrayInputStream((byte[])o), os, headers); } else if (Reader.class.isAssignableFrom(o.getClass())) { try { Writer writer = new OutputStreamWriter(os, getEncoding(type)); IOUtils.copy((Reader)o, writer, BUFFER_SIZE); writer.flush(); } finally { ((Reader)o).close(); } } else if (StreamingOutput.class.isAssignableFrom(o.getClass())) { ((StreamingOutput)o).write(os); } else { throw new IOException("Unrecognized class"); } }