Search in sources :

Example 76 with Type

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;
}
Also used : ContractProvider(org.glassfish.jersey.model.ContractProvider) Annotation(java.lang.annotation.Annotation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) RuntimeType(javax.ws.rs.RuntimeType) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) RankedProvider(org.glassfish.jersey.model.internal.RankedProvider) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) PreMatching(javax.ws.rs.container.PreMatching)

Example 77 with Type

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;
}
Also used : Type(java.lang.reflect.Type) Constructor(java.lang.reflect.Constructor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Parameter(org.glassfish.jersey.server.model.Parameter) Annotation(java.lang.annotation.Annotation)

Example 78 with Type

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;
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) Invocable(org.glassfish.jersey.server.model.Invocable) MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) GenericType(javax.ws.rs.core.GenericType) WriterModel(org.glassfish.jersey.message.WriterModel)

Example 79 with Type

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);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericEntity(javax.ws.rs.core.GenericEntity) MediaType(javax.ws.rs.core.MediaType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 80 with Type

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Aggregations

Type (java.lang.reflect.Type)6423 ParameterizedType (java.lang.reflect.ParameterizedType)1761 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)690 WildcardType (java.lang.reflect.WildcardType)571 Test (org.junit.Test)512 ArrayList (java.util.ArrayList)427 Method (java.lang.reflect.Method)416 TypeVariable (java.lang.reflect.TypeVariable)337 List (java.util.List)335 Map (java.util.Map)289 Gson (com.google.gson.Gson)231 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)204 Field (java.lang.reflect.Field)163 Annotation (java.lang.annotation.Annotation)160 IOException (java.io.IOException)140 Collection (java.util.Collection)111