Expand my Community achievements bar.

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

Avatar

Level 3

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

9 Replies

Avatar

Community Advisor

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");

  }

}



Arun Patidar

Avatar

Level 3

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

Level 10

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

Avatar

Level 3

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

Community Advisor

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.



Arun Patidar

Avatar

Level 3

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

Community Advisor

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



Arun Patidar

Avatar

Level 3

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

Avatar

Community Advisor

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/



Arun Patidar