com.palantir.conjure.java.server.jersey.GuavaOptionalParamConverterProvider Maven / Gradle / Ivy
/*
* (c) Copyright 2017 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.conjure.java.server.jersey;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
import org.glassfish.jersey.internal.inject.Custom;
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.internal.util.collection.ClassTypePair;
// The Custom annotation ensures that our custom param converters are considered first. See ParamConverterFactory.
@Custom
@Provider
public final class GuavaOptionalParamConverterProvider implements ParamConverterProvider {
private final InjectionManager injectionManager;
@Inject
public GuavaOptionalParamConverterProvider(InjectionManager injectionManager) {
this.injectionManager = injectionManager;
}
@Override
public ParamConverter getConverter(
final Class rawType, final Type genericType, final Annotation[] annotations) {
if (com.google.common.base.Optional.class.equals(rawType)) {
final List ctps = ReflectionHelper.getTypeArgumentAndClass(genericType);
final ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
if (ctp == null || ctp.rawClass() == String.class) {
return new ParamConverter() {
@Override
public T fromString(final String value) {
return rawType.cast(com.google.common.base.Optional.fromNullable(value));
}
@Override
public String toString(final T value) {
return value.toString();
}
};
}
for (ParamConverterProvider provider :
Providers.getProviders(injectionManager, ParamConverterProvider.class)) {
final ParamConverter> converter = provider.getConverter(ctp.rawClass(), ctp.type(), annotations);
if (converter != null) {
return new ParamConverter() {
@Override
public T fromString(final String value) {
return rawType.cast(com.google.common.base.Optional.fromNullable(value)
.transform(converter::fromString));
}
@Override
public String toString(final T value) {
return value.toString();
}
};
}
}
}
return null;
}
}