io.muserver.rest.PrimitiveEntityProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mu-server Show documentation
Show all versions of mu-server Show documentation
A simple but powerful web server framework
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")
@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 content.toString().length();
}
public void writeTo(T content, Class> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
Charset charset = EntityProviders.charsetFor(mediaType);
entityStream.write(String.valueOf(content).getBytes(charset));
}
}