How to get request parameter of SlingServlet in OSGi Service ?

Avatar

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile
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

Avatar

Avatar
Boost 5
Level 2
chandu_t
Level 2

Likes

7 likes

Total Posts

29 posts

Correct reply

5 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 5
View profile

Avatar
Boost 5
Level 2
chandu_t
Level 2

Likes

7 likes

Total Posts

29 posts

Correct reply

5 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 5
View profile
chandu_t
Level 2

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.

Avatar

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile
Suraj_Kamdi
MVP

30-10-2018

@chandu t Do you any examples on that ?

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

30-10-2018

Indeed, just pass in the request object or individual values

Avatar

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile
Suraj_Kamdi
MVP

30-10-2018

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

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

30-10-2018

Do you have any code to share?

Avatar

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile
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);

  }

}

Avatar

Avatar
Boost 5
Level 2
chandu_t
Level 2

Likes

7 likes

Total Posts

29 posts

Correct reply

5 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 5
View profile

Avatar
Boost 5
Level 2
chandu_t
Level 2

Likes

7 likes

Total Posts

29 posts

Correct reply

5 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 5
View profile
chandu_t
Level 2

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

  }

Avatar

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile

Avatar
Seeker
MVP
Suraj_Kamdi
MVP

Likes

148 likes

Total Posts

159 posts

Correct reply

42 solutions
Top badges earned
Seeker
Bedrock
Engage 1
Springboard
Validate 1
View profile
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

Avatar

Avatar
Boost 5
Level 2
chandu_t
Level 2

Likes

7 likes

Total Posts

29 posts

Correct reply

5 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 5
View profile

Avatar
Boost 5
Level 2
chandu_t
Level 2

Likes

7 likes

Total Posts

29 posts

Correct reply

5 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 5
View profile
chandu_t
Level 2

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