Calling OSGI Service in fragments

Issue

  • Is there an option/API available to call OSGI Services directly in fragments like it's possible using serviceUtil and staticUtil?

Environment

  • DXP 7.4

Resolution

  • Yes, you can obtain this by using serviceLocator inside the HTML code of your fragment, same as it is used inside free-markers.

    The only thing is changing inside fragments is that < changes for [ and > for ], like this:
     
    [#assign exampleServiceUtil = serviceLocator.findService("com.liferay.example.service.ExampleService") /]

    Keep in mind that serviceLocator is restricted by default for security reasons. In order to enable it, you need to follow this steps: Changes in Enabling serviceLocator Calls in Liferay DXP 7.0

    It is important to understand that activating serviceLocator from a security point of view, is considered a bad idea because it opens the access to all of your services from freemarker. That means that you explicitly trust anybody who's allowed to write freemarker templates to run server-side code (and have access to the LocalService APIs)

    Because of that, we recommend using TemplateContextContributors that helps you make available only what you need. See Injecting Additional Context Variables and Functionality into Your Theme Templates

    The code is very simple, and you can inject whatever you need, see this example: Template Context Contributor

    Other article that might be of your interest: Non service builder generated services are not retrieved by ServiceLocator in freemarker template

 

 

¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0