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

io.dropwizard.jersey.optional.OptionalMessageBodyWriter Maven / Gradle / Ivy

The newest version!
package io.dropwizard.jersey.optional;

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 java.util.Optional;

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 {
        final Object entityObj = entity.orElseThrow(() -> EmptyOptionalException.INSTANCE);

        final Type innerGenericType = (genericType instanceof ParameterizedType parameterizedType) ?
            parameterizedType.getActualTypeArguments()[0] : entityObj.getClass();

        final MessageBodyWriter writer = requireNonNull(mbw).get().getMessageBodyWriter(entityObj.getClass(),
            innerGenericType, annotations, mediaType);
        writer.writeTo(entityObj, entityObj.getClass(),
            innerGenericType, annotations, mediaType, httpHeaders, entityStream);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy