0

I am implementing the custom parameter annotation using AbstractDataProvider. I have went through some link but still same problem persists. https://psamsotha.github.io/jersey/2015/11/01/jersey-method-parameter-injection.html

Jersey custom method parameter injection with inbuild injection

I am getting this error

No injection source found for a parameter of type public gandalf.models.authz.Permission

@Singleton
@Provider
public class RequestInfoProviders extends AbstractValueFactoryProvider {
    private static final Logger log = LoggerFactory.getLogger(RequestInfoProviders.class);

    @Inject
    public RequestInfoProviders(MultivaluedParameterExtractorProvider mpep, ServiceLocator locator) {
        super(mpep, locator, new Source[]{Source.UNKNOWN});
    }

    protected AbstractContainerRequestValueFactory<?> createValueFactory(Parameter parameter) {
        Class<?> classType = parameter.getRawType();
        if (classType != null && classType.equals(UserAuthContext.class)) {
            return new RequestInfoProviders.RequestInfoParamValueFactory();
        } else {
            log.warn("class {}", classType);
            log.warn("RequestContext annotation was not placed on correct object type; Injection might not work correctly!");
            return null;
        }
    }


    private static final class RequestInfoParamValueFactory extends AbstractContainerRequestValueFactory<UserAuthenticationContext> {

        @Context
        private ResourceContext requestContext;

        private RequestInfoParamValueFactory() {
        }

        public UserAuthenticationContext provide() {
            return UserAuthenticationContext.builder()
                    .build();
        }
    }

    @Singleton
    public static final class InjectionResolver extends ParamInjectionResolver<UserAuthContext> {
        public InjectionResolver() {
            super(RequestInfoProviders.class);
        }
    }
}

This way I have registered this

environment.jersey().register(new AbstractBinder() {
    protected void configure() {
        this.bind(RequestInfoProviders.class).to(ValueFactoryProvider.class).in(Singleton.class);
        this.bind(RequestInfoProviders.InjectionResolver.class).to(new TypeLiteral<InjectionResolver<UserAuthContext>>() {
        }).in(Singleton.class);
    }
});

My Resource class

   @POST
    @Timed
    @ExceptionMetered
    @Authenticate
    @ApiOperation("Create a Permission")
    public Permission createPermission(@Valid PermissionRequest permissionRequest,
                                       @HeaderParam(AUTHORIZATION) String authorisation,
                                       @Context ContainerRequestContext context,
                                       @UserAuthContext UserAuthenticationContext authenticationContext) throws Exception {

        Permission permission = Permission.builder().permission(permissionRequest.getPermission())
                .active(permissionRequest.isActive())
                .createdBy(authenticationContext.getSession().getUserId())
                .namespace(authenticationContext.getSession().getNamespace())
                .build();

        return permissionService.create(permission);
    }

Annotaion class

@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, PARAMETER, METHOD, FIELD })
public @interface UserAuthContext {
}
praneet drolia
  • 671
  • 1
  • 6
  • 15
  • Can you post the whole stractrace and also what version of DW are you using? Can you also show your dependencies related to DW and Jersey. – Paul Samsotha Feb 17 '19 at 19:48
  • Also, does your `createValueFactory()` get called? And if it does, does it output your log in the else block? Can you also try to make a complete test using the test framework, like I did in your second link. It would be easier to test if you could do that. – Paul Samsotha Feb 18 '19 at 03:57

0 Answers0