Usage of @Via in sling models | Community
Skip to main content
Nitiks25
Level 2
Oct 11, 2017 5:58 AM UTC
Solved

Usage of @Via in sling models

I understand from the documentation, that @Via is used to inject objects not available through the adaptable mapped to the model

In that case, if my adaptable is Resource.class and I have to inject SlingHttpServletRequset or SlingHttpServletResponse. I will have to use @Inject @Via to fetch it from SlingHttpServletRequest adaptable?

In that case, what is the parameter I have to pass to @Via?

In case of resource, it will be @Via ("resource") - what will it be for slingRequest?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
VeenaVikraman
VeenaVikramanAccepted solution
New Participant
Oct 11, 2017 8:25 PM UTC

Nitik,

  Let me try to explain you what I understand here . Let us take the simple example from what documentation Scott has shared

  - So this says ; when the model's adaptable is SlingHttpServletRequest , then when you try to inject getPropertyName() ; it will be return the property via resource method of the SlingHttpServletRequest object "request" like the below

request.getResource().getValueMap().get("propertyName", String.class)

That said , if you check the API SlingHttpServletRequest (Apache Sling Aggregate 5-incubator API)  you can understand that a resource can be fetched from a request , but the vice versa is not possible .

    So basically that means inject the value via resource API in SlingHttpServletRequest API.

  I am not aware of a way to get a request object from a resource and hence I don't believe something like below is even possible

@Model(adaptables=Resource.class)

public class MyModel { 

    @Inject

    @Via("request")

    private String someObj;

}

For further reading the below section explains what all standard types are provided via while and how it is implemented

Apache Sling :: Sling Models

The other thing you can do is to adapt the SlingHttpServletRequest and get the resource from it

@Model(adaptables = SlingHttpServletRequest.class)

public class MyModel {

  

     @Inject

     SlingHttpServletRequest request;

     @PostConstruct

     protected void init() {

         Resource resource = request.getResource() ;

     }

}

25 replies

BrijeshYadav
Level 5
Oct 12, 2017 10:59 AM UTC

Hi,

I think you should use @SlingObject injector. like below
@Model(adaptables = Resource.class)

public class ResourceExampleModel {

  @SlingObject

   private SlingHttpServletRequest request;

}

As par the documentation also @SlingObject injects commonly used sling objects if the field matches with the class: request, response, resource resolver, current resource, SlingScriptHelper
Apache Sling :: Sling Models

/Brijesh Yadav

Translate
Nitiks25
Nitiks25Author
Level 2
Oct 12, 2017 6:37 AM UTC

Thanks a lot Veena. Makes sense now. But I want clarification on one thing: I have the model adaptable as Resource. And I have injected slinghttpservletresponse object using just an @Inject i.e.

@Inject

SlingHttpServletResponse response

and a valid response object is being returned. As per documentation, this isn't supposed to happen since response object is only supposed to be available if model is set to sling request as adaptalbe

I'm just wondering, how it is working then?

Translate
VeenaVikraman
VeenaVikramanAccepted solution
New Participant
Oct 11, 2017 8:25 PM UTC

Nitik,

  Let me try to explain you what I understand here . Let us take the simple example from what documentation Scott has shared

  - So this says ; when the model's adaptable is SlingHttpServletRequest , then when you try to inject getPropertyName() ; it will be return the property via resource method of the SlingHttpServletRequest object "request" like the below

request.getResource().getValueMap().get("propertyName", String.class)

That said , if you check the API SlingHttpServletRequest (Apache Sling Aggregate 5-incubator API)  you can understand that a resource can be fetched from a request , but the vice versa is not possible .

    So basically that means inject the value via resource API in SlingHttpServletRequest API.

  I am not aware of a way to get a request object from a resource and hence I don't believe something like below is even possible

@Model(adaptables=Resource.class)

public class MyModel { 

    @Inject

    @Via("request")

    private String someObj;

}

For further reading the below section explains what all standard types are provided via while and how it is implemented

Apache Sling :: Sling Models

The other thing you can do is to adapt the SlingHttpServletRequest and get the resource from it

@Model(adaptables = SlingHttpServletRequest.class)

public class MyModel {

  

     @Inject

     SlingHttpServletRequest request;

     @PostConstruct

     protected void init() {

         Resource resource = request.getResource() ;

     }

}

Translate
Nitiks25
Nitiks25Author
Level 2
Oct 11, 2017 6:44 PM UTC

Thanks Smacdonald,

From that page it is clear what the Via Type does i.e choose a different adaptable for a particular injection. It's also given an example of how to set Resource as the adaptable for an Injection if the adaptable model is sling request:

@Inject @Via("resource")

     String getPropertyName();

But what if the model level adaptable is 'Resource' and I need to choose request as the adaptable for an Injection? that's not given on that page. Is that even possible? Tried @Inject @Via("request")  and other similar parameters but it doesn't work.

Translate
smacdonald2008
Level 10
Oct 11, 2017 4:59 PM UTC

This annotation is well explained in this topic:

Apache Sling :: Sling Models

There are some code examples too.

Translate