io.dropwizard.jersey.guava.OptionalMessageBodyWriter Maven / Gradle / Ivy
The newest version!
package io.dropwizard.jersey.guava;
import com.google.common.base.Optional;
import io.dropwizard.jersey.optional.EmptyOptionalException;
import jakarta.inject.Inject;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.glassfish.jersey.message.MessageBodyWorkers;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import static java.util.Objects.requireNonNull;
@Provider
@Produces(MediaType.WILDCARD)
public class OptionalMessageBodyWriter implements MessageBodyWriter> {
@Inject
private jakarta.inject.@Nullable Provider mbw;
// Jersey ignores this
@Override
public long getSize(Optional> entity, Class> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return 0;
}
@Override
public boolean isWriteable(Class> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return Optional.class.isAssignableFrom(type);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void writeTo(Optional> entity,
Class> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap httpHeaders,
OutputStream entityStream)
throws IOException {
if (!entity.isPresent()) {
throw EmptyOptionalException.INSTANCE;
}
final ParameterizedType actualGenericType = (ParameterizedType) genericType;
final Type actualGenericTypeArgument = actualGenericType.getActualTypeArguments()[0];
final MessageBodyWriter writer = requireNonNull(mbw).get().getMessageBodyWriter(entity.get().getClass(),
actualGenericTypeArgument, annotations, mediaType);
writer.writeTo(entity.get(), entity.get().getClass(),
actualGenericTypeArgument,
annotations, mediaType, httpHeaders, entityStream);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy