Usage of @Via in sling models | Community
Skip to main content
Nitiks25
New Participant
October 11, 2017
Solved

Usage of @Via in sling models

  • October 11, 2017
  • 25 replies
  • 28618 views

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.
Best answer by VeenaVikraman

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
New Participant
October 12, 2017

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

Nitiks25
Nitiks25Author
New Participant
October 12, 2017

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?

VeenaVikraman
VeenaVikramanAccepted solution
New Participant
October 11, 2017

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

     }

}

Nitiks25
Nitiks25Author
New Participant
October 11, 2017

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.

smacdonald2008
New Participant
October 11, 2017

This annotation is well explained in this topic:

Apache Sling :: Sling Models

There are some code examples too.