use of java.lang.reflect.Type in project jersey by jersey.
the class ApplicationHandler method filterNameBound.
/**
* Takes collection of all filters/interceptors (either request/reader or response/writer)
* and separates out all name-bound filters/interceptors, returns them as a separate MultivaluedMap,
* mapping the name-bound annotation to the list of name-bound filters/interceptors. The same key values
* are also added into the inverse map passed in {@code inverseNameBoundMap}.
* <p/>
* Note, the name-bound filters/interceptors are removed from the original filters/interceptors collection.
* If non-null collection is passed in the postMatching parameter (applicable for filters only),
* this method also removes all the global
* postMatching filters from the original collection and adds them to the collection passed in the postMatching
* parameter.
*
* @param all Collection of all filters to be processed.
* @param preMatchingFilters Collection into which pre-matching filters should be added.
* @param componentBag Component bag
* @param applicationNameBindings Collection of name binding annotations attached to the JAX-RS application.
* @param inverseNameBoundMap Inverse name bound map into which the name bound providers should be inserted. The keys
* are providers (filters, interceptor)
* @return {@link MultivaluedMap} of all name-bound filters.
*/
private static <T> MultivaluedMap<Class<? extends Annotation>, RankedProvider<T>> filterNameBound(final Iterable<RankedProvider<T>> all, final Collection<RankedProvider<ContainerRequestFilter>> preMatchingFilters, final ComponentBag componentBag, final Collection<Class<? extends Annotation>> applicationNameBindings, final MultivaluedMap<RankedProvider<T>, Class<? extends Annotation>> inverseNameBoundMap) {
final MultivaluedMap<Class<? extends Annotation>, RankedProvider<T>> result = new MultivaluedHashMap<>();
for (final Iterator<RankedProvider<T>> it = all.iterator(); it.hasNext(); ) {
final RankedProvider<T> provider = it.next();
Class<?> providerClass = provider.getProvider().getClass();
final Set<Type> contractTypes = provider.getContractTypes();
if (contractTypes != null && !contractTypes.contains(providerClass)) {
providerClass = ReflectionHelper.theMostSpecificTypeOf(contractTypes);
}
ContractProvider model = componentBag.getModel(providerClass);
if (model == null) {
// the provider was (most likely) bound in HK2 externally
model = ComponentBag.modelFor(providerClass);
}
final boolean preMatching = providerClass.getAnnotation(PreMatching.class) != null;
if (preMatching && preMatchingFilters != null) {
it.remove();
preMatchingFilters.add(new RankedProvider<>((ContainerRequestFilter) provider.getProvider(), model.getPriority(ContainerRequestFilter.class)));
}
boolean nameBound = model.isNameBound();
if (nameBound && !applicationNameBindings.isEmpty() && applicationNameBindings.containsAll(model.getNameBindings())) {
// override the name-bound flag
nameBound = false;
}
if (nameBound) {
// not application-bound
if (!preMatching) {
it.remove();
for (final Class<? extends Annotation> binding : model.getNameBindings()) {
result.add(binding, provider);
inverseNameBoundMap.add(provider, binding);
}
} else {
LOGGER.warning(LocalizationMessages.PREMATCHING_ALSO_NAME_BOUND(providerClass));
}
}
}
return result;
}
use of java.lang.reflect.Type in project jersey by jersey.
the class ParamInjectionResolver method resolve.
@Override
@SuppressWarnings("unchecked")
public Object resolve(Injectee injectee) {
AnnotatedElement annotated = injectee.getParent();
Annotation[] annotations;
if (annotated.getClass().equals(Constructor.class)) {
annotations = ((Constructor) annotated).getParameterAnnotations()[injectee.getPosition()];
} else {
annotations = annotated.getDeclaredAnnotations();
}
Class componentClass = injectee.getInjecteeClass();
Type genericType = injectee.getRequiredType();
final Type targetGenericType;
if (injectee.isFactory()) {
targetGenericType = ReflectionHelper.getTypeArgument(genericType, 0);
} else {
targetGenericType = genericType;
}
final Class<?> targetType = ReflectionHelper.erasure(targetGenericType);
final Parameter parameter = Parameter.create(componentClass, componentClass, hasEncodedAnnotation(injectee), targetType, targetGenericType, annotations);
final Supplier<?> paramValueSupplier = valueSupplierProvider.getValueSupplier(parameter);
if (paramValueSupplier != null) {
if (injectee.isFactory()) {
return paramValueSupplier;
} else {
return paramValueSupplier.get();
}
}
return null;
}
use of java.lang.reflect.Type in project jersey by jersey.
the class MethodSelectingRouter method isWriteable.
private boolean isWriteable(final RequestSpecificConsumesProducesAcceptor candidate) {
final Invocable invocable = candidate.methodRouting.method.getInvocable();
final Class<?> responseType = Primitives.wrap(invocable.getRawRoutingResponseType());
if (Response.class.isAssignableFrom(responseType) || Void.class.isAssignableFrom(responseType)) {
return true;
}
final Type genericType = invocable.getRoutingResponseType();
final Type genericReturnType = genericType instanceof GenericType ? ((GenericType) genericType).getType() : genericType;
for (final WriterModel model : workers.getWritersModelsForType(responseType)) {
if (model.isWriteable(responseType, genericReturnType, invocable.getHandlingMethod().getDeclaredAnnotations(), candidate.produces.combinedType)) {
return true;
}
}
return false;
}
use of java.lang.reflect.Type in project jersey by jersey.
the class RequestUtil method getEntityParameters.
/**
* Returns the form parameters from a request entity as a multi-valued map.
* If the request does not have a POST method, or the media type is not
* x-www-form-urlencoded, then null is returned.
*
* @param request the client request containing the entity to extract parameters from.
* @return a {@link javax.ws.rs.core.MultivaluedMap} containing the entity form parameters.
*/
@SuppressWarnings("unchecked")
public static MultivaluedMap<String, String> getEntityParameters(ClientRequestContext request, MessageBodyWorkers messageBodyWorkers) {
Object entity = request.getEntity();
String method = request.getMethod();
MediaType mediaType = request.getMediaType();
// no entity, not a post or not x-www-form-urlencoded: return empty map
if (entity == null || method == null || !HttpMethod.POST.equalsIgnoreCase(method) || mediaType == null || !mediaType.equals(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
return new MultivaluedHashMap<String, String>();
}
// it's ready to go if already expressed as a multi-valued map
if (entity instanceof MultivaluedMap) {
return (MultivaluedMap<String, String>) entity;
}
Type entityType = entity.getClass();
// if the entity is generic, get specific type and class
if (entity instanceof GenericEntity) {
final GenericEntity generic = (GenericEntity) entity;
// overwrite
entityType = generic.getType();
entity = generic.getEntity();
}
final Class entityClass = entity.getClass();
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessageBodyWriter writer = messageBodyWorkers.getMessageBodyWriter(entityClass, entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
try {
writer.writeTo(entity, entityClass, entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, out);
} catch (WebApplicationException wae) {
throw new IllegalStateException(wae);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(MultivaluedMap.class, MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
try {
return (MultivaluedMap<String, String>) reader.readFrom(MultivaluedMap.class, MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, in);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of java.lang.reflect.Type in project che by eclipse.
the class EventService method getEventType.
private Class<?> getEventType(EventSubscriber<?> subscriber) {
Class<?> eventType = null;
Class<?> clazz = subscriber.getClass();
while (clazz != null && eventType == null) {
for (Type type : clazz.getGenericInterfaces()) {
if (type instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) type;
final Type rawType = parameterizedType.getRawType();
if (EventSubscriber.class == rawType) {
final Type[] typeArguments = parameterizedType.getActualTypeArguments();
if (typeArguments.length == 1) {
if (typeArguments[0] instanceof Class) {
eventType = (Class) typeArguments[0];
}
}
}
}
}
clazz = clazz.getSuperclass();
}
if (eventType == null) {
throw new IllegalArgumentException(String.format("Unable determine type of events processed by %s", subscriber));
}
return eventType;
}
Aggregations