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

smacdonald2008
New Participant
July 16, 2018

OK -- I am going to try to do this.

rajeevy89244319
New Participant
July 16, 2018

It is in customizing the logic section on Customizing Core Components

Below is code snippet provided:

package com.mysite.components.models;

@Model(adaptables = SlingHttpServletRequest.class,

       adapters = Breadcrumb.class,

       resourceType = "my-site/components/breadcrumb")

public class MyCustomBreadcrumbImpl implements Breadcrumb {

    /* ... */

    // get the default implementation for delegating

    @Self @Via(resourceType="core/wcm/components/breadcrumb/v2/breadcrumb", type=ResourceSuperType.class)

    private Breadcrumb delegate;

}

smacdonald2008
New Participant
July 16, 2018

Where does it state that when customizing Core Components - you can use @Via annotation. I want to follow along.

rajeevy89244319
New Participant
July 16, 2018

I am trying to follow documentation on Customizing Core Components

// get the default implementation for delegating

@Self @Via(resourceType="core/wcm/components/breadcrumb/v2/breadcrumb", type=ResourceSuperType.class)

When I am trying to use resourceType with @Via, I am getting compilation error saying that resourceType attribute is not supported for @via annotation. Has anyone tried this and can assist me with correct dependencies to use this.

Thanks,

Rajeev

VeenaVikraman
New Participant
October 13, 2017

I would request some expert advice Jörg HohFeike Visser

Nitiks25
Nitiks25Author
New Participant
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.

VeenaVikraman
New Participant
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
New Participant
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.

October 12, 2017

for sling request it will be:

@Self

SlingHttpServletRequest  request;

VeenaVikraman
New Participant
October 12, 2017

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