Expand my Community achievements bar.

SOLVED

How to refer a sling model with in a sling servlet?

Avatar

Level 2

I have a sling model which inject an OSGi service. The request parameter for the service call is configurable. This service request is also generated in the sling model. This will invoke an API call and process the result in the sling model. I need to invoke this sling model inside a sling servlet call because a click event should trigger this functionality. So is there a way to invoke a sling model from a sling servlet.

1 Accepted Solution

Avatar

Correct answer by
Level 3

Which url do you use to execute your click request to your servlet?

It is Best Practice to register your serlvet with a resourceType and if needed some selector or extension. With this you simply then can call an url like this for example: /content/myweb/par/componentOne.html

Then in the Servlet you will have the current resource available with request.getResource(). From this you can easily read your authored data or also adapt it to some Sling model, etc.

View solution in original post

14 Replies

Avatar

Employee Advisor

In most cases you can adapt a request or a resource to a SlingModel.

SlingModel model = request.adaptTo(SlingModel.class);

Avatar

Level 2

Thanks @Jorg for the suggestion but the @ValueMapValue in the slingmodels are not getting value. I debug the code and find out that. Actually I need the method inside @Postconstruct to work before I call the model. Is there any way to solve this problem.

My model class snippet is like this.

@Model(adaptables = SlingHttpServletRequest.class)

public class StandardrateConfig {

@ValueMapValue

@Via("resource")

@Optional

@Default(values = "")

private String[] rateconfigurations;

.......

.......

@Self

SlingHttpServletRequest request;

@Inject

StandardRateService standardRateService;

........

........

@PostConstruct

protected void init() {

     mymethod();

}

My Sling servlet class snippet is like this

protected void doGet(final SlingHttpServletRequest req,

            final SlingHttpServletResponse resp) throws ServletException,

            IOException {

StandardrateConfig model = req.adaptTo(StandardrateConfig.class);

Avatar

Level 3

Hi,

I don't know how your content is structured and what you exactly try to achieve, but it Looks like that you may also solve this Problem, by simply refactoring your code such you clearly separate the concerns or also restructuring your content.

For me it Looks like that this "StandardConfig" actually should not have any references to a request and that this concern may be moved to the StandardRateService which handles all These loading of These rate configurations (via Service users for example).

And then you simply Need to call this Service from your Sling servlet, so no Need to use the Sling model in the servlet.

This just as a thought of me, but as said, it depends on the Details in your context.

Avatar

Level 2

Hi ,

Actually 'StandardConfig' is referred to request because I have to read some values from cookie inside that slingModel. The reason why I can't call the service directly from the servlet is that the request parameter for the service call is based on the author configured value. So I have used the slingModel to get all those values and created a requestobject to call the service. This service will return the result and I am able to access that from the HTL.

The one option is to reload the content page on event trigger. So that automatically sling model will bind all the data and i'll get the result. But for my requirement one of the servlet request parameter should also be considered for creating request object.

Avatar

Level 10

What exactly are you trying to do. Purhaps there is another way of performing you use case.

Avatar

Level 2

I need to call a rest service by passing a JSON parameter and accept a JSON result. This service invocation should be triggered by a click event. Some request parameters should be configurable to the author, some should be read from cookie and another parameter should be passed from front end by a click event.

I created a sling model to read all JCR properties and to read some values from cookie. The JSON request Param will be generated inside this slingModel. I created an OSGi service which call the rest service . This OSGi service is referred inside the sling model. I also created a sling servlet which accept one value during event click. This value should be passed to the sling model. If that is possible then all the needed request parameters would be available at the sling model and it can generate the request JSON and thereby can call the OSGi service.

Avatar

Level 10

Why do you need Sling Model to generate JSON? How do plan on invoking the model? I have never seen a servlet invoke a model like it can a Service.

You can invoke a Service using @Reference from a servlet. 

Avatar

Level 2

As I have authorable parameters I have to use the Sling Model to bring the author configured data to backend. Is there any better option to bring JCR content data through servlet.

Avatar

Level 10

You can read JCR data from servlet using Node API directly from servlet

Avatar

Level 2

Thanks for your suggestion but it may need to include login credentials specific for each environment, right. I was just curious about whether a sling model can be referred from a sling servlet. As it is not possible, I'll look for a different approach that suits my requirements. Thank you once again.

Avatar

Correct answer by
Level 3

Which url do you use to execute your click request to your servlet?

It is Best Practice to register your serlvet with a resourceType and if needed some selector or extension. With this you simply then can call an url like this for example: /content/myweb/par/componentOne.html

Then in the Servlet you will have the current resource available with request.getResource(). From this you can easily read your authored data or also adapt it to some Sling model, etc.

Avatar

Level 2

Thanks OlivBur for reminding me that. My servlet is already registered as resource type. I almost forgot about the getResource, my bad.