private List<MediaType> getEffectiveInputTypes(final ResourceMethod resourceMethod) { if (!resourceMethod.getConsumedTypes().isEmpty()) { return resourceMethod.getConsumedTypes(); } List<MediaType> result = new LinkedList<>(); if (workers != null) { for (Parameter p : resourceMethod.getInvocable().getParameters()) { if (p.getSource() == Parameter.Source.ENTITY) { result.addAll(workers.getMessageBodyReaderMediaTypes( p.getRawType(), p.getType(), p.getDeclaredAnnotations())); } } } return result.isEmpty() ? StarTypeList : result; }
@Nullable @Override protected Function<ContainerRequest, ?> createValueProvider(Parameter parameter) { final Class<?> classType = parameter.getRawType(); final Session sessionAnnotation = parameter.getAnnotation(Session.class); if (sessionAnnotation == null) { return null; } if (classType.isAssignableFrom(HttpSession.class)) { return x -> im.createAndInitialize(HttpSessionFactory.class).provide(sessionAnnotation.doNotCreate()); } else if (classType.isAssignableFrom(Flash.class)) { return x -> im.createAndInitialize(FlashFactory.class).provide(sessionAnnotation.doNotCreate()); } else { return null; } }
@Override public MultivaluedParameterExtractor<?> get(final Parameter p) { return process( paramConverterFactory.get(), p.getDefaultValue(), p.getRawType(), p.getType(), p.getAnnotations(), p.getSourceName()); }
@Override public String toString() { return String.format("Parameter [type=%s, source=%s, defaultValue=%s]", getRawType(), getSourceName(), getDefaultValue()); }
private Parameter getEntityParam(final Invocable invocable) { for (final Parameter parameter : invocable.getParameters()) { if (parameter.getSource() == Parameter.Source.ENTITY && !ContainerRequestContext.class.isAssignableFrom(parameter.getRawType())) { // there's at most one entity parameter return parameter; } } return null; }
@Override public Function<ContainerRequest, AsyncResponse> getValueProvider(final Parameter parameter) { if (parameter.getSource() != Parameter.Source.SUSPENDED) { return null; } if (!AsyncResponse.class.isAssignableFrom(parameter.getRawType())) { return null; } return containerRequest -> asyncContextProvider.get(); }
private void checkMethod(ResourceMethod method) { checkValueProviders(method); final Invocable invocable = method.getInvocable(); if ("GET".equals(method.getHttpMethod())) { final long eventSinkCount = invocable.getParameters() .stream() .filter(parameter -> SseEventSink.class.equals(parameter.getRawType())) .count(); Errors.warning(method, LocalizationMessages.MULTIPLE_EVENT_SINK_INJECTION(invocable.getHandlingMethod())); if (void.class == invocable.getHandlingMethod().getReturnType() && !method.isSuspendDeclared() && !isSse) { Errors.hint(method, LocalizationMessages.GET_RETURNS_VOID(invocable.getHandlingMethod())); for (Parameter p : invocable.getParameters()) { if (p.isAnnotationPresent(FormParam.class)) { Errors.fatal(method, LocalizationMessages.GET_CONSUMES_FORM_PARAM(invocable.getHandlingMethod())); break;
private boolean isReadable(final RequestSpecificConsumesProducesAcceptor candidate) { final Invocable invocable = candidate.methodRouting.method.getInvocable(); final Method handlingMethod = invocable.getHandlingMethod(); final Parameter entityParam = getEntityParam(invocable); if (entityParam == null) { return true; } else { final Class<?> entityType = entityParam.getRawType(); for (final ReaderModel model : workers.getReaderModelsForType(entityType)) { if (model.isReadable( entityType, entityParam.getType(), handlingMethod.getDeclaredAnnotations(), candidate.consumes.combinedType)) { return true; } } } return false; }
private void checkParameters(ResourceMethod method) { final Invocable invocable = method.getInvocable(); final Method handlingMethod = invocable.getHandlingMethod(); int paramCount = 0; int nonAnnotatedParameters = 0; for (Parameter p : invocable.getParameters()) { validateParameter(p, handlingMethod, handlingMethod.toGenericString(), Integer.toString(++paramCount), false); if (method.getType() == ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR && Parameter.Source.ENTITY == p.getSource()) { Errors.fatal(method, LocalizationMessages.SUBRES_LOC_HAS_ENTITY_PARAM(invocable.getHandlingMethod())); } else if (p.getAnnotations().length == 0) { nonAnnotatedParameters++; if (nonAnnotatedParameters > 1) { Errors.fatal(method, LocalizationMessages.AMBIGUOUS_NON_ANNOTATED_PARAMETER(invocable.getHandlingMethod(), invocable.getHandlingMethod().getDeclaringClass())); } } } }
private void fillInputTypesFromWorkers(final Set<MediaType> effectiveInputTypes, final Invocable invocableMethod) { for (Parameter p : invocableMethod.getParameters()) { if (p.getSource() == Parameter.Source.ENTITY) { effectiveInputTypes.addAll(workers.getMessageBodyReaderMediaTypesByType(p.getRawType())); // there's at most one entity parameter break; } } }
/** * @param ar abstract resource * @param arm abstract resource method * @param mt media type * @return respresentation type * @see org.glassfish.jersey.server.wadl.WadlGenerator#createRequestRepresentation(org.glassfish.jersey.server.model.Resource, * org.glassfish.jersey.server.model.ResourceMethod, javax.ws.rs.core.MediaType) */ public Representation createRequestRepresentation( final org.glassfish.jersey.server.model.Resource ar, final org.glassfish.jersey.server.model.ResourceMethod arm, final MediaType mt) { final Representation rt = wadlGeneratorDelegate.createRequestRepresentation(ar, arm, mt); for (final Parameter p : arm.getInvocable().getParameters()) { if (p.getSource() == Parameter.Source.ENTITY) { nameCallbacks.add(new TypeCallbackPair( new GenericType(p.getType()), new NameCallbackSetter() { @Override public void setName(final QName name) { rt.setElement(name); } })); } } return rt; }
@Override protected Function<ContainerRequest, ?> createValueProvider(Parameter parameter) { final Class<?> rawType = parameter.getRawType(); if (Parameter.Source.ENTITY == parameter.getSource()) { if (FormDataMultiPart.class.isAssignableFrom(rawType)) { return new FormDataMultiPartProvider(); return null; } else if (parameter.getSourceAnnotation().annotationType() == FormDataParam.class) { final String paramName = parameter.getSourceName(); if (paramName == null || paramName.isEmpty()) { final Class clazz = ReflectionHelper.getGenericTypeArgumentClasses(parameter.getType()).get(0);
for (Parameter parameter : invocable.getParameters()) { if (parameter.isAnnotationPresent(Validated.class)) { groups.add(parameter.getAnnotation(Validated.class).value()); if (!Arrays.deepEquals(groups.get(i), groups.get(j))) { throw new WebApplicationException("Parameters must have the same validation groups in " + invocable.getHandlingMethod().getName(), 500);
@Override public Param createParam(org.glassfish.jersey.server.model.Resource r, ResourceMethod m, final Parameter p) { if (p.getSource() == Parameter.Source.UNKNOWN) { return null; wadlParam.setName(p.getSourceName()); switch (p.getSource()) { case FORM: wadlParam.setStyle(ParamStyle.QUERY); wadlParam.setPath(p.getSourceName()); break; default: if (p.hasDefaultValue()) { wadlParam.setDefault(p.getDefaultValue()); Class<?> pClass = p.getRawType(); if (pClass.isArray()) { wadlParam.setRepeating(true);
@Nullable @Override protected Function<ContainerRequest, ?> createValueProvider(Parameter parameter) { if (!parameter.isAnnotationPresent(Auth.class)) { return null; } else if (principalClassSet.contains(parameter.getRawType())) { return request -> new PrincipalContainerRequestValueFactory(request).provide(); } else { final boolean isOptionalPrincipal = parameter.getRawType() == Optional.class && ParameterizedType.class.isAssignableFrom(parameter.getType().getClass()) && principalClassSet.contains(((ParameterizedType) parameter.getType()).getActualTypeArguments()[0]); return isOptionalPrincipal ? request -> new OptionalPrincipalContainerRequestValueFactory(request).provide() : null; } }
@Override protected Function<ContainerRequest, ?> createValueProvider(Parameter parameter) { if (parameter.isAnnotationPresent(Pac4JProfileManager.class)) { if (ProfileManager.class.isAssignableFrom(parameter.getRawType())) { return manager.get(); } throw new IllegalStateException("Cannot inject a Pac4J profile manager into a parameter of type " + parameter.getRawType().getName()); } if (parameter.isAnnotationPresent(Pac4JProfile.class)) { if (CommonProfile.class.isAssignableFrom(parameter.getRawType())) { return profile.get(); } if (Optional.class.isAssignableFrom(parameter.getRawType())) { List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(parameter.getRawType()); ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null; if (ctp == null || CommonProfile.class.isAssignableFrom(ctp.rawClass())) { return optProfile.get(); } } throw new IllegalStateException( "Cannot inject a Pac4J profile into a parameter of type " + parameter.getRawType().getName()); } return null; } }
@Override protected Function<ContainerRequest, SseEventSink> createValueProvider(Parameter parameter) { if (parameter == null) { return null; } final Class<?> rawParameterType = parameter.getRawType(); if (rawParameterType == SseEventSink.class && parameter.isAnnotationPresent(Context.class)) { return new SseEventSinkValueSupplier(asyncContextSupplier); } return null; }
@Override public Object apply(ContainerRequest containerRequest) { final Class<?> rawType = parameter.getRawType(); Object value; if ((Request.class.isAssignableFrom(rawType) || ContainerRequestContext.class.isAssignableFrom(rawType)) && rawType.isInstance(containerRequest)) { value = containerRequest; } else { value = containerRequest.readEntity(rawType, parameter.getType(), parameter.getAnnotations()); if (rawType.isPrimitive() && value == null) { throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST) .entity(LocalizationMessages.ERROR_PRIMITIVE_TYPE_NULL()).build()); } } return value; } }
/** * Creates a new object {@link Injectee} corresponding to the injecting point. The injectee contains basic information * about the injection point types and {@link ForeignDescriptor} of the underlying DI provider to make delegated injection * resolver as simple as possible. * * @param parameter jersey-like parameter corresponding to one resource-method's parameter. * @return injectee instance as a source of the information about the injecting point. */ private Injectee getInjectee(Parameter parameter) { InjecteeImpl injectee = new InjecteeImpl(); injectee.setRequiredType(parameter.getType()); injectee.setInjecteeClass(parameter.getRawType()); ForeignDescriptor proxyDescriptor = descriptorCache.apply(parameter); if (proxyDescriptor != null) { injectee.setInjecteeDescriptor(proxyDescriptor); } return injectee; }
final Request wadlRequest) { for (Parameter parameter : requestParameters) { if (parameter.getSource() == Parameter.Source.ENTITY || parameter.getSource() == Parameter.Source.UNKNOWN) { for (MediaType mediaType : resourceMethod.getConsumedTypes()) { setRepresentationForMediaType(parentResource, resourceMethod, mediaType, wadlRequest); } else if (parameter.getSourceAnnotation().annotationType() == FormParam.class) { List<MediaType> supportedInputTypes = resourceMethod.getConsumedTypes(); if (supportedInputTypes.isEmpty() || (supportedInputTypes.size() == 1 && supportedInputTypes.get(0).isWildcardType())) { final Representation wadlRepresentation = setRepresentationForMediaType(parentResource, resourceMethod, mediaType, wadlRequest); if (getParamByName(wadlRepresentation.getParam(), parameter.getSourceName()) == null) { final Param wadlParam = generateParam(parentResource, resourceMethod, parameter); if (wadlParam != null) { parameter.getSourceAnnotation().annotationType().getName())) { // jersey-multipart support final Representation wadlRepresentation = setRepresentationForMediaType(parentResource, resourceMethod, mediaType, wadlRequest); if (getParamByName(wadlRepresentation.getParam(), parameter.getSourceName()) == null) { final Param wadlParam = generateParam(parentResource, resourceMethod, parameter); if (wadlParam != null) {