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

Usage of @Via in sling models

  • October 11, 2017
  • 25 replies
  • 28623 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

smacdonald2008
Level 10
October 11, 2017

This annotation is well explained in this topic:

Apache Sling :: Sling Models

There are some code examples too.

Nitiks25
Nitiks25Author
Level 2
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.

VeenaVikraman
Community Advisor
VeenaVikramanCommunity AdvisorAccepted solution
Community Advisor
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
Level 2
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?

BrijeshYadav
Level 5
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

VeenaVikraman
Community Advisor
Community Advisor
October 12, 2017

I have not tried injecting a response object . but I will surely give it a try and get back to you.

October 12, 2017

for sling request it will be:

@Self

SlingHttpServletRequest  request;

Nitiks25
Nitiks25Author
Level 2
October 13, 2017

Thanks a lot Veena,

yes, if you could let me know that will help a lot

because as per the documentation injecting response object using @ inject when the adaptable is resource is not suppose to work but it is still working.

VeenaVikraman
Community Advisor
Community Advisor
October 13, 2017

Yes Nitik. I am wondering how that can be possible. Because you cannot inject a SlingHttpResponse object to a class which is adapted as Resource. The documentations says as below.

@Inject : marks a field or method as injectable - If this is to be believed then your class (which is adapted as a Resource) cannot inject a response object.

I am not sure if anything here make sense at all SlingObject (Apache Sling 8 API)  .

Nitiks25
Nitiks25Author
Level 2
October 13, 2017

I have tried @slingObject too but intermittently it returns a null response. And from what I understand from the documentation that is also not supposed to work since adaptable is not request. But weirdly @inject is working fine.