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

net.winterly.rxjersey.server.RxGenericBodyWriter Maven / Gradle / Ivy

There is a newer version: 0.11.0
Show newest version
package net.winterly.rxjersey.server;

import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.jersey.message.MessageBodyWorkers;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
 * Generic {@link MessageBodyWriter} that overrides writer to serialise incoming entity as type of generic class. 
* This class will only redirect writing to another {@link MessageBodyWriter}
* Requires list of supported types
*

* Providers implementing {@link RxGenericBodyWriter} must be programmatically registered in {@link ServiceLocator} */ public abstract class RxGenericBodyWriter implements MessageBodyWriter { private final List allowedTypes; @Inject private Provider workers; /** * @param allowedTypes list of types to be processed by this writer */ protected RxGenericBodyWriter(Class... allowedTypes) { this.allowedTypes = Arrays.asList(allowedTypes); } /** * @param parameterizedType type to process * @return the raw type without generics */ private static Class raw(ParameterizedType parameterizedType) { return (Class) parameterizedType.getRawType(); } /** * @param genericType type to process * @return first type from generic list */ private static Type actual(Type genericType) { final ParameterizedType actualGenericType = (ParameterizedType) genericType; return actualGenericType.getActualTypeArguments()[0]; } @Override public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { if (genericType instanceof ParameterizedType) { Class rawType = raw((ParameterizedType)genericType); return allowedTypes.contains(rawType); } return false; } @Override public long getSize(Object o, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { return 0; //skip } @SuppressWarnings("unchecked") @Override public void writeTo(Object entity, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { final Type actualTypeArgument = actual(genericType); final MessageBodyWriter writer = workers.get().getMessageBodyWriter(entity.getClass(), actualTypeArgument, annotations, mediaType); writer.writeTo(entity, entity.getClass(), actualTypeArgument, annotations, mediaType, httpHeaders, entityStream); } }