0

I am using deadbolt 2.5.0 and I have created custom SubjectPresentHandler as below:

public class SubjectPresentHandler extends AbstractDeadboltHandler
{
    public SubjectPresentHandler(ExecutionContextProvider ecProvider) {
        super(ecProvider);
    }
    // other required methods
}

And, I also have :

@Singleton
public class CustomDeadboltHandlerCache implements HandlerCache
{
    private final DeadboltHandler defaultHandler = new  SubjectPresentHandler();
   // other required code
}

Now the problem that I am facing here is I cannot instantiate SubjectPresentHandler using its default contructor. I get an error as: "The constructor SubjectPresentHandler is undefined". Now when I add default constructor in SubjectPresentHandler as below:

public SubjectPresentHandler() {
   super();
}

I get an error as: The constructor AbstractDeadboltHandler is undefined. If I try removing the paramaterized constructor in SubjectPresentHandler then I get error message as

"Implicit super constructor AbstractDeadboltHandler() is undefined for default constructor. Must define an explicit constructor".

I am not sure how can I resolve this, thus seeking solution regarding this issue.

Programmer
  • 325
  • 5
  • 18

1 Answers1

1

The constructor of SubjectPresentHandler takes an ExecutionContextProvider as a parameter. The easiest way to do this is to inject one and have the creation of the handler done via Guice.

The ExecutionContextProvider is provided by DeadboltModule - you can see this here.

@Singleton
public class SubjectPresentHandler extends AbstractDeadboltHandler
{
    @Inject
    public SubjectPresentHandler(ExecutionContextProvider ecProvider) {
        super(ecProvider);
    }
    // other required methods
}

You can also inject the handler into the handler cache:

@Singleton
public class CustomDeadboltHandlerCache implements HandlerCache
{
    private final DeadboltHandler defaultHandler;

    @Inject
    public CustomDeadboltHandlerCache(final DeadboltHandler defaultHandler) {
        this.defaultHandler = defaultHandler;
    }
   // other required code
}

If you have multiple handlers, take a look at the documentation for how to handle this.

Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
  • @stevechalober: This didn't fix the issue, even if I inject SubjectPresentHandler in CustomDeadboltHandlerCache , the compiler complain for having default contructor in SubjectPresentHandler which is not present in our case, and as I mentioned I cannot add default contructor in SubjectPresentHandler . – Programmer Mar 24 '16 at 19:09
  • Here's an example implementation: https://github.com/schaloner/deadbolt-2-java/blob/master/test-app/app/be/objectify/deadbolt/java/test/security/MyDeadboltHandler.java – Steve Chaloner Mar 24 '16 at 19:11
  • @stevechalober: Even your example doesn't have default contructor, am not sure how is that working in your case. Below are the change I did in my code, but compiler is crying for default constructor in SubjectPresentHandler. I have done changes in CustomDeadboltHandlerCache as you suggested, please see edits. – Programmer Mar 24 '16 at 19:24
  • What's the exact error? Is it something like "You must have a default constructor or a single Inject-annotated constructor"? – Steve Chaloner Mar 24 '16 at 19:26
  • Could not find a suitable constructor in utils.com.comp.deadbolt.SubjectPresentHandler. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. [info] at utils.com.comp.deadbolt.SubjectPresentHandler.class(SubjectPresentHandler.java:29) [info] while locating utils.com.comp.deadbolt.SubjectPresentHandler [info] for parameter 0 at utils.com.comp.deadbolt.CustomDeadboltHandlerCache.(CustomDeadboltHandlerCache.java:20) – Programmer Mar 24 '16 at 19:29
  • 1
    `@Inject public SubjectPresentHandler(ExecutionContextProvider ecProvider)` fulfills this requirement. Are you declaring your bindings in a module, e.g. https://github.com/schaloner/deadbolt-2-java/blob/master/test-app/app/be/objectify/deadbolt/java/test/modules/CustomDeadboltHook.java ? – Steve Chaloner Mar 24 '16 at 19:37
  • This time it worked, I was missing @Inject in my code. Thanks for your help. – Programmer Mar 24 '16 at 19:48
  • 1
    @PradeepSharma please mark the answer as correct if it helped you, it helps other people find working solutions. – Steve Chaloner Mar 29 '16 at 10:11