io.dropwizard.vavr.jersey.CollectionValueParamProvider Maven / Gradle / Ivy
package io.dropwizard.vavr.jersey;
import io.vavr.collection.Array;
import io.vavr.collection.List;
import io.vavr.collection.Vector;
import io.vavr.control.Option;
import io.vavr.control.Try;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.Providers;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ParamException;
import org.glassfish.jersey.server.internal.inject.AbstractValueParamProvider;
import org.glassfish.jersey.server.internal.inject.MultivaluedParameterExtractor;
import org.glassfish.jersey.server.internal.inject.MultivaluedParameterExtractorProvider;
import org.glassfish.jersey.server.model.Parameter;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.function.Function;
abstract class CollectionValueParamProvider extends AbstractValueParamProvider {
private final InjectionManager injectionManager;
protected CollectionValueParamProvider(final Provider mpep,
final InjectionManager injectionManager,
final Parameter.Source... compatibleSources) {
super(mpep, compatibleSources);
this.injectionManager = injectionManager;
}
@Override
protected Function createValueProvider(final Parameter parameter) {
final Option name = Option.of(parameter.getSourceName());
final Option defaultValue = Option.of(parameter.getDefaultValue());
return name
.filter(n -> !n.isEmpty())
.flatMap(n ->
findParamConverter(parameter.getType(), parameter.getAnnotations())
.map(conv -> (Function) conv::fromString)
.flatMap(conv -> buildExtractor(parameter.getRawType(), n, defaultValue, conv))
.map(extractor -> buildProvider(extractor, !parameter.isEncoded()))
)
.getOrNull();
}
private Option> buildExtractor(final Class> rawClass,
final String name,
final Option defaultValue,
final Function conv) {
if (rawClass.equals(Vector.class)) {
return Option.of(new CollectionParameterExtractor<>(name, defaultValue, conv, Vector::ofAll));
} else if (rawClass.equals(List.class)) {
return Option.of(new CollectionParameterExtractor<>(name, defaultValue, conv, List::ofAll));
} else if (rawClass.equals(Array.class)) {
return Option.of(new CollectionParameterExtractor<>(name, defaultValue, conv, Array::ofAll));
} else {
return Option.none();
}
}
protected abstract Function buildProvider(final MultivaluedParameterExtractor> extractor, final boolean decode);
@SuppressWarnings("unchecked")
private Option> findParamConverter(final Type type, final Annotation[] annotations) {
return List.ofAll(ReflectionHelper.getTypeArgumentAndClass(type))
.headOption()
.flatMap(ctp -> {
if (ctp.rawClass().equals(String.class)) {
return Option.>some(new ParamConverter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy