All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.muserver.rest.BinaryEntityProviders Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package io.muserver.rest;

import io.muserver.Mutils;

import javax.activation.DataSource;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.util.List;

import static java.util.Arrays.asList;

class BinaryEntityProviders {

    static final List binaryEntityReaders = asList(
        new InputStreamReader(),
        new ByteArrayReaderWriter(),
        new FileReaderWriter(),
        new DataSourceReaderWriter()
    );
    static final List binaryEntityWriters = asList(
        new StreamingOutputWriter(),
        new ByteArrayReaderWriter(),
        new FileReaderWriter(),
        new DataSourceReaderWriter()
    );

    @Produces("*/*")
    @Consumes("*/*")
    static class ByteArrayReaderWriter implements MessageBodyReader, MessageBodyWriter {
        public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return type.isArray() && type.getComponentType().equals(byte.class);
        }
        public byte[] readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
            if (!EntityProviders.requestHasContent(httpHeaders)) {
                return new byte[0];
            }
            return Mutils.toByteArray(entityStream, 2048);
        }
        public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return type.isArray() && type.getComponentType().equals(byte.class);
        }
        public long getSize(byte[] bytes, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return bytes.length;
        }
        public void writeTo(byte[] bytes, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
            entityStream.write(bytes);
        }
    }

    @Produces("*/*")
    static class StreamingOutputWriter implements MessageBodyWriter {
        public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return StreamingOutput.class.isAssignableFrom(type);
        }
        public void writeTo(StreamingOutput streamingOutput, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
            streamingOutput.write(entityStream);
        }
    }


    @Consumes("*/*")
    static class InputStreamReader implements MessageBodyReader {
        public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return InputStream.class.isAssignableFrom(type);
        }
        public InputStream readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
            return entityStream;
        }
    }

    @Produces("*/*")
    @Consumes("*/*")
    static class FileReaderWriter implements MessageBodyReader, MessageBodyWriter {

        @Override
        public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return File.class.isAssignableFrom(type);
        }

        @Override
        public File readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
            File temp = Files.createTempFile("MuServer", "tmp").toFile();
            temp.deleteOnExit();
            try (FileOutputStream fileWriter = new FileOutputStream(temp)) {
                Mutils.copy(entityStream, fileWriter, 8192);
            }
            return temp;
        }

        @Override
        public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return File.class.isAssignableFrom(type);
        }

        @Override
        public long getSize(File file, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return file.length();
        }

        @Override
        public void writeTo(File file, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
            try (FileInputStream fileInputStream = new FileInputStream(file)) {
                Mutils.copy(fileInputStream, entityStream, 8192);
            }
        }
    }

    @Produces("*/*")
    @Consumes("*/*")
    static class DataSourceReaderWriter implements MessageBodyReader, MessageBodyWriter {
        // Disclaimer: I don't even know what a javax.activation.DataSource is

        @Override
        public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return DataSource.class.isAssignableFrom(type);
        }

        @Override
        public DataSource readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
            return new DataSource() {
                @Override
                public InputStream getInputStream() throws IOException {
                    return entityStream;
                }

                @Override
                public OutputStream getOutputStream() throws IOException {
                    throw new IOException("Output streams not available when reading");
                }

                @Override
                public String getContentType() {
                    return mediaType.toString();
                }

                @Override
                public String getName() {
                    return type.getName();
                }
            };
        }

        @Override
        public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return DataSource.class.isAssignableFrom(type);
        }

        @Override
        public void writeTo(DataSource dataSource, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
            Mutils.copy(dataSource.getInputStream(), entityStream, 2048);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy