Expand my Community achievements bar.

How to get request parameter of SlingServlet in OSGi Service ?

Avatar

Community Advisor

I want to fetch the properties in OSgi Service like userName , which is set in request parameter in a my custom SlingServlet ? Anyone has an idea about how to achieve this task ?

I have one RegisterUserSlingServlet class and CustomMailService as OSGi service. I want get some request parameters properties inside my CustomMailService.

14 Replies

Avatar

Level 3

Assuming you call CustomMailService in RegisterUserSlingServlet. You get request parameters in servlet by request.getParameter("key"). You can pass this parameters to the service, while invoking service method.

Avatar

Employee

Indeed, just pass in the request object or individual values

Avatar

Community Advisor

Feike Visser​ I am still confused..!! Do you have any sample example ?

Avatar

Community Advisor

Feike Visser

Servlet Code

@Component(service = Servlet.class,

    property = {Constants.SERVICE_DESCRIPTION + "= Create Profile Servlet",

        ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_POST,

        ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=" + CreateProfile.RESOURCE_TYPE})

public class CreateProfile extends SlingAllMethodsServlet {

  public static final String RESOURCE_TYPE = "components/general/profile";

  private static final String targetPageUrl = "/content/smaple.html";

  @Reference

  private transient AccountManagementService accountManagementService;

  @Override

  protected void doPost(SlingHttpServletRequest slingRequest,

      SlingHttpServletResponse slingResponse) throws ServletException, IOException {

     //Want to pass this targetUrl into Service class

    slingRequest.setAttribute(targetPageUrl, targetPageUrl );

  }

}

Service Class:

@Component(

    name = "Mail Service",

    property = {

        "label = Mail Service",

        "description = Mail Service",

        Constants.SERVICE_RANKING + ": Integer = 10000"},

    service = MailService.class, immediate = true

)

public class ]MailService implements MessageGateway<Email>, MailService {

  /**

   * Getting reference for default mail service.

   */

  @Reference(

      target = "(service.pid=com.day.cq.mailer.DefaultMailService)",

      cardinality = ReferenceCardinality.MANDATORY,

      policy = ReferencePolicy.DYNAMIC

  )

  private volatile MailService defaultMailService;

  @Activate

  protected void activate(ComponentContext componentContext) {

    log.info("Activated Fiserv Default Mail Service");

  }

  @Override

  public boolean handles(Class<? extends Email> type) {

    return true;

  }

  @Override

  public void send(Email email) throws MailingException {

     //Here I want to Fetch the Request Parameter From Servlet Class.

  }

  @Override

  public void sendEmail(Email email) throws EmailException {

    send(email);

  }

}

Avatar

Level 3

Looks like you trying to add request parameters to mail body. Below is sample code

  @Reference

  private MailService mailService;

  @Override

  protected void doPost(SlingHttpServletRequest slingRequest,

      SlingHttpServletResponse slingResponse) throws ServletException, IOException {

      Email email = new SimpleEmail();

      StringBuilder mailTemplate = new StringBuilder();

              mailTemplate.append("<!DOCTYPE html><html><body>");

      mailTemplate.append("url: " + request.getParameter("targetUrl"));

      // you can add other parameters into body

      mailTemplate.append("</table></body></html>");

      email.setContent(mailTemplate.toString(), "text/html");

      email.setSubject("Register User");

      email.addTo(request.getParameter("email"));

      mailService.send(email);

  }

Avatar

Community Advisor

@chandu t I dont want to append parameter in servlet. I only need to fetch that parameter in osgi service. I have to check some certain logic and based on that i have to append the targetUrl

Avatar

Level 3

Then create a custom method in service. And call it.

  public void send(Email email, String targetUrl) throws MailingException {

     //Here use targetUrl for condition

  }

  Add call like below in servlet

  mailService.send(email, request.getParameter("targetUrl"));

Avatar

Community Advisor

chandu t​ But

@Override

  public void send(Email email) throws MailingException {} is an interface method

Avatar

Level 10

surajk96205881​,

write a overload send() method in your custom service which accepts the request parameter and then put your logic.

Avatar

Community Advisor

Do you have any proper example which describes the above scenario ?

Avatar

Level 10

You cannot read a servlet parameter value (value passed to a servlet) from an OSGI service. You can however - pass a servlet parameter from a servlet to a service method.

Avatar

Level 10

In your code - looks like you are writing an OSGI class that implements MessageGatewayService.

This is not necessary to use a MessageGatewayService.

Instead - write an OSGi class that implements a Java interface. Then in your implementation class - inject a MessageGatewayService.

That is - no need to write a class that does this

@Component(

    name = "Mail Service",

    property = {

        "label = Mail Service",

        "description = Mail Service",

        Constants.SERVICE_RANKING + ": Integer = 10000"},

    service = MailService.class, immediate = true

)

public class MailService implements MessageGateway<Email>, MailService {

Do this --

public interface Foo(){

public void sendMail(String parVal1)

}

@Component

public class FooImpl implements Foo

{

//Inject a MessageGatewayService

@Reference

private MessageGatewayService messageGatewayService;

public void sendMail(String parVal1){

//parVal1 is my Servlet value!

//Call methods now that belong to messageGatewayService

}

Now in your Servlet - you can call sendMail() and pass servlet value by

@Reference

private Foo myFoo;

protected void doPost(SlingHttpServletRequest slingRequest,   SlingHttpServletResponse slingResponse) throws ServletException, IOException {

//Read Servlet parm values in Servlet

myFoo.sendMail(<PASS SERVLET VALUE>) ;  // This is how you solve this use case.

...

}