Highlighted

How to get request parameter of SlingServlet in OSGi Service ?

Suraj_Kamdi

MVP

30-10-2018

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.

Replies

Highlighted

chandu_t

30-10-2018

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.

Highlighted

Suraj_Kamdi

MVP

30-10-2018

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

  }

}

Highlighted

chandu_t

30-10-2018

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

  }

Highlighted

Suraj_Kamdi

MVP

30-10-2018

@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

Highlighted

chandu_t

30-10-2018

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