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

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

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

import io.muserver.Mutils;

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.NoContentException;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.List;
import java.util.function.Function;

import static java.util.Arrays.asList;

@Produces("text/plain; charset=UTF-8")
@Consumes("text/plain")
class PrimitiveEntityProvider implements MessageBodyWriter, MessageBodyReader {

    static final List primitiveEntryProviders = asList(
        new PrimitiveEntityProvider<>(int.class, Integer.class, Integer::parseInt),
        new PrimitiveEntityProvider<>(long.class, Long.class, Long::parseLong),
        new PrimitiveEntityProvider<>(short.class, Short.class, Short::parseShort),
        new PrimitiveEntityProvider<>(char.class, Character.class, s -> s.charAt(0)),
        new PrimitiveEntityProvider<>(byte.class, Byte.class, Byte::parseByte),
        new PrimitiveEntityProvider<>(float.class, Float.class, Float::parseFloat),
        new PrimitiveEntityProvider<>(double.class, Double.class, Double::parseDouble),
        new PrimitiveEntityProvider<>(boolean.class, Boolean.class, Boolean::parseBoolean)
    );

    private final Class primitiveClass;
    final Class boxedClass;
    private final Function stringToValue;

    private PrimitiveEntityProvider(Class primitiveClass, Class boxedClass, Function stringToValue) {
        this.primitiveClass = primitiveClass;
        this.boxedClass = boxedClass;
        this.stringToValue = stringToValue;
    }

    public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return primitiveClass.equals(type) || boxedClass.equals(type);
    }

    public T readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
        if (!EntityProviders.requestHasContent(httpHeaders)) {
            throw new NoContentException("No value specified for this " + type.getName() + " parameter. If optional, then use a @DefaultValue annotation.");
        }
        Charset charset = EntityProviders.charsetFor(mediaType);
        byte[] bytes = Mutils.toByteArray(entityStream, 2048);
        String stringVal = new String(bytes, charset);
        return stringToValue.apply(stringVal);
    }

    public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return primitiveClass.equals(type) || boxedClass.equals(type);
    }

    public long getSize(T content, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return toBytes(content, mediaType).length;
    }

    public void writeTo(T content, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
        entityStream.write(toBytes(content, mediaType));
    }

    private static byte[] toBytes(Object content, MediaType mediaType) {
        Charset charset = EntityProviders.charsetFor(mediaType);
        return String.valueOf(content).getBytes(charset);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy