Message Boards

@Reference not resolving dependency while used in Servlet filter

Upender Kashyap, modified 3 Years ago.

@Reference not resolving dependency while used in Servlet filter

Junior Member Posts: 30 Join Date: 8/12/20 Recent Posts
Hello,I have created a filter for redirection of login page for default OIDC provider. I have created a servlet filter using hooks functionality in Liferay DXP 7.2. @Component(
    immediate = true,
    property = {
             "servlet-context-name=",
             "servlet-filter-name=Microsoft Login Filter",
             "url-pattern=/c/portal/login"  
    },
    service = Filter.class
)
public class MyLoginFilter implements Filter {
    
    @SuppressWarnings("rawtypes")
    @Reference
    private OpenIdConnectProviderRegistry openIdConnectProviderRegistry;
    @Reference
    private OpenIdConnectServiceHandler openIdConnectServiceHandler;

But I am getting openIdConnectProviderRegistry and openIdConnectServiceHandler objects as null. Can anyone guide me on this?Thanks in advance.
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
I think you need a unique servlet-context-name, it must mit be empty.
Upender Kashyap, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Junior Member Posts: 30 Join Date: 8/12/20 Recent Posts
Thanks for the reply.
After providing unique servlet-context-name still the issue persists.  I am attaching my module structure. My liferay-hook.xml looks like:
<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 7.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_7_2_0.dtd"><hook>
    <servlet-filter>
        <servlet-filter-name>MyLoginFilter</servlet-filter-name>
        <servlet-filter-impl>com.liferay.filter.login.MyLoginFilter</servlet-filter-impl>
        
    </servlet-filter>
    <servlet-filter-mapping>
        <servlet-filter-name>MyLoginFilter</servlet-filter-name>
        
        <url-pattern>/c/portal/login</url-pattern>
       
    </servlet-filter-mapping>
</hook>
Being a naive to liferay I am not able to find the cause of trouble. Kindly look once and suggest some pointers on debugging.
thumbnail
Olaf Kock, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
Upender Kashyap:

Thanks for the reply.
After providing unique servlet-context-name still the issue persists.  I am attaching my module structure. My liferay-hook.xml looks like:

@Reference only works within @Components (so far, so good), but only if their instantiation is managed by OSGi. I suspect that the existence of liferay-hook.xml triggers compatibility code to instantiate your filter, instead (or in addition) of OSGi mechanisms. Remove liferay-hook.xml, and it should work. Notice, you're probably also lacking a before-filter property in your component's declaration.
https://github.com/liferay/liferay-blade-samples/tree/7.2/liferay-workspace/extensions/servlet-filter contains a fully working servlet filter with some documentation that tells you what to look for.
Upender Kashyap, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Junior Member Posts: 30 Join Date: 8/12/20 Recent Posts
@Olaf thanks for replying,I have made the changes as per your suggestion but still the issue persists.  My code looks like:@Component(
    immediate = true,
    property = {
            "before-filter=Auto Login Filter",
             "servlet-context-name=LoginFilter",
             "servlet-filter-name=MyLoginFilter",
             "url-pattern=/c/portal/login"
         //    "dispatcher=REQUEST"
    },
    service = Filter.class
)
public class MyLoginFilter implements Filter {
        @Reference
    private OpenIdConnectProviderRegistry<?,?> openIdConnectProviderRegistry;
    @Reference
    private OpenIdConnectServiceHandler _openIdConnectServiceHandler;
I have also removed liferay-hook.xml and have gone through the link provided by you. Kindly suggest any pointer to debug or any possible laggings.Thanks in Advance
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Could you try to remove:
        @Reference
    private OpenIdConnectProviderRegistry<?,?> openIdConnectProviderRegistry;
Maybe the runtime has issues with <?,?>. I am just guessing here.
I checked, it should work in general and an empty servlet-context-name is fine too.
I would try to add a setter method with logging instead of a @Reference private ...
Then you can see in the logfile, if the setter is called. Maybe it makes it easier to test. Also, when you remove all references and just implement the filter + logging, is it called? What does the gogo shell say? Is your module started?
Upender Kashyap, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Junior Member Posts: 30 Join Date: 8/12/20 Recent Posts
Thanks for suggestions Chris! I changed to setter with logs but its not being called. No logs inside setters are printed. Module is also started. Gogo shell gives results like:
lb -s
1107|Active     |   10|com.liferay.filter.login (1.0.0)|1.0.0

Other logs I am attaching in file. Being new to liferay I am failing to debug deep frpm Gogo shell. Please check the logs once to see if everything is correct.
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Since I see whiteboard all over the place, could you do a "jaxrs:check" in the gogo shell?
I did not realize that this is implemented through whiteboard now. Whiteboard can be quite bitchy.
1) The filter itself: Is it called? I mean, when you access /c/portal/login (Probably not)
2) When you remove all references, does it work then? (Just insert logging into the filter method, nothing else)
If that works, start adding again services again till it stops working. Oh, and check the bnd file. Do you have any special directives or something?
Upender Kashyap, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Junior Member Posts: 30 Join Date: 8/12/20 Recent Posts
Hi Chris,I am attaching the output for  "jaxrs:check".
The filter is called on hitting /c/portal/login .
The bnd file is like:
Bundle-Name: LoginFilter
Bundle-SymbolicName: com.liferay.filter.login
Bundle-Version: 1.0.0
-sources: true
Without references code execute fine but If i remove refernence then my functionality will not work as required.  I want to implement default login for openID connect so I requires above references.
Thanks
thumbnail
Olaf Kock, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
Ok, some more things to check:
  • The OSGi runtime must be the one to instantiate this filter: I'm assuming that you're not instantiating it yourself, or have any other component do so (as discussed above through some XML configuration file)
  • Check that you use the proper import statements and don't accidentally use some other framework's annotations
    • import org.osgi.service.component.annotations.Component;
    • import org.osgi.service.component.annotations.Reference;
  • Make extra extra extra sure that you have that plugin deployed only once. Especially if you've deployed it through different means in the past (e.g. through eclipse/deploy folder/gogo shell). I've hunted ghost issues because some old version still hid somewhere because I've done weird things when I tried stuff out. Worst case: Clear the osgi/state folder and check what you find in the other osgi/* folders, e.g. osgi/modules

Upender Kashyap, modified 3 Years ago.

RE: @Reference not resolving dependency while used in Servlet filter

Junior Member Posts: 30 Join Date: 8/12/20 Recent Posts
@Olaf The issue got resolved. I had issue related to multiple deployment. Your emphasis on this point helped. Thanks a lot!!