Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Can we fetch configuration value of osgi service(osgi.config) directly in slingServlet?

Avatar

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
shridhar_sahu
Level 2

11-12-2018

Hi,

I am currently working on aem 6.2.

I want to fetch values of service  directly in slingServlet.

For that i have created a Service

ConfigurationService.java

public abstract interface ConfigurationService {

       public abstract String getSimpleString();

}

ConfigurationServiceImpl.java

@Service({ConfigurationServiceImpl.class})

@Component(immediate=true, metatype=true, label="Configuration Service")

public class ConfigurationServiceImpl implements ConfigurationService{

private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceImpl.class);

@Property(label=" Link", description="Link")

private static final String AZURE_STRING = "azureString";

private String azureString;

@Activate

protected void activate(Map<String, Object> properties)

{

LOG.info("[*** AEM ConfigurationService]: activating configuration service");

readProperties(properties);

}

protected void readProperties(Map<String, Object> properties)

{

LOG.info(properties.toString());

this.azureString = PropertiesUtil.toString(properties.get("azureString"), "default");

LOG.info("Simple String: " + this.azureString);

}

@Override

public String getSimpleString() {

// TODO Auto-generated method stub

return this.azureString;

}

}

UrlGenerationServlet.java

@SlingServlet(paths="/bin/adobe/urlGenerationServlet", methods = "POST", metatype=true)

public class urlCreationServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet{

protected final Logger log = LoggerFactory.getLogger(this.getClass());

private static final long serialVersionUID = 1L;

@Reference

private ConfigurationService configurationService;

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {

         //here i tried using  getSlingScriptHelper().getService(ConfigurationService.class);

          //and ended up because we can't use WCMUsePojo in SlingServlet.

         

}

}

Regards,

Shridhar

Replies

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

11-12-2018

Yes, You can do that. You can directly use service. there is no need to write below method to get service @Reference would do same getSlingScriptHelper().getService(ConfigurationService.class);

Sample Servlet Example.

@Component(service = Servlet.class, immediate = true, property = {

  Constants.SERVICE_DESCRIPTION + "=Get Features Servlet", "sling.servlet.paths=" + "/bin/page/getOsgiCong" })

public class GetOsgiConfigServlet extends SlingSafeMethodsServlet {

  @Reference

  private DeveloperInfo developerInfo;

  private static final long serialVersionUID = 1L;

  Logger logger = LoggerFactory.getLogger(this.getClass());

  @Override

  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

  throws ServletException, IOException {

  response.getWriter().print("Developer Info : "+developerInfo.getDeveloperInfo());

  response.getWriter().close();

  response.setContentType("text/html");

  }

}

Avatar

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
shridhar_sahu
Level 2

11-12-2018

Hi Arun,

I have created a maven archetype 10 project.

I'm facing following errors in eclipse:

Multiple markers at this line

- The attribute property is undefined for the annotation type Component

- The attribute service is undefined for the annotation type Component

- Constants cannot be resolved to a variable

Thanks,

Shridhar

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

12-12-2018

Post your code so the community can see what you are trying to do.

Avatar

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
shridhar_sahu
Level 2

13-12-2018

Hi,

I have a requirement in which:

  • I need to fetch values from osgi:config and further use that value for Hitting a API

ConfigurationService.java

public abstract interface ConfigurationService {

       public abstract String getSimpleString();

}

ConfigurationServiceImpl.java

@Service({ConfigurationServiceImpl.class})

@Component(immediate=true, metatype=true, label="Configuration Service")

public class ConfigurationServiceImpl implements ConfigurationService{

     @Property(label=" Link", description="Link")

     private static final String AZURE_STRING = "azureString";

     private String azureString;

    

     @Activate

     protected void activate(Map<String, Object> properties)

     {

          readProperties(properties);

     }

     protected void readProperties(Map<String, Object> properties)

     {

          this.azureString = PropertiesUtil.toString(properties.get("azureString"), "default");

     }

     @Override

     public String getSimpleString() {

          return this.azureString;

     }

}

UrlGenerationServlet.java

@SlingServlet(paths="/bin/adobe/urlGenerationServlet", methods = "POST", metatype=true)

public class UrlCreationServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet{

     @Reference

     private ConfigurationService configurationService;

     @Override

     protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException

     {    

          String url = configurationService.getSimpleString(); //here i'm getting null as response          

          URL object=new URL(url);

          con = (HttpURLConnection) object.openConnection();

                  //  .......   

     }

}

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

13-12-2018

Hi,

Can you please check your imports.

I used Osgi annotations only but you are using Felix. Sometimes we mix annotations reference which cause this error.

Avatar

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
shridhar_sahu
Level 2

17-12-2018

compile.PNG

Hi Arun,

I tried using osgi annotations and i am getting following compilation error.

I'm using Maven Archetype 10 project.

import java.io.IOException;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.adobe.sample.core.aem.QuotationConfigurationService;

@Component(service = Servlet.class, immediate = true, property = {Constants.SERVICE_DESCRIPTION + "=Get Features Servlet", "sling.servlet.paths=" + "/bin/page/getOsgiCong" })  //Constants cannot be resolved to a variable

public class GetOsgiConfigServlet extends SlingSafeMethodsServlet {

@Reference //The annotation @Reference is disallowed for this location

private QuotationConfigurationService developerInfo;

private static final long serialVersionUID = 1L;

Logger logger = LoggerFactory.getLogger(this.getClass());

@Override

protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

response.getWriter().print("Developer Info : "+developerInfo.getURLString());

response.getWriter().close();

response.setContentType("text/html");

}

}

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

17-12-2018

Can you try to create project using archetype 11 or 12.

It seems dependencies are missing thats why it is throwing error.

Can you check your POM?

<!-- OSGi Dependencies -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.cmpn</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.annotation</artifactId>
</dependency>

sample POM for 6.4

aem63app-repo/java/POMs at master · arunpatidar02/aem63app-repo · GitHub

Avatar

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
shridhar_sahu
Level 2

Likes

3 likes

Total Posts

36 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
shridhar_sahu
Level 2

17-12-2018

Hi Arun, I need to implement it in an existing maven Archetype 10 Project(AEM 6.2)

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

17-12-2018

Hi,

In 6.2 is also possible with src annotations and services can be used inside servlet.

https://www.conexiogroup.com/component-for-beginners-with-focus-on-servlets/