Expand my Community achievements bar.

Sling Model Binary Inject

Avatar

Level 10

Hi,

 

trying to adapt to resource class and i have a binary field with @inject. I try accessing the binary field and it says org.apache.sling.models.impl.injectors.ResourcePathInjector Could not retrieve resource at path {{the values stored at the variable }} for field {{property name}}. Since it is required it won't be injected.

 

So tried adapting to valuemapvalue,error is gone but the property is null  or does not exist

any inputs?

12 Replies

Avatar

Community Advisor

I would say adapt as resource or then read the binary property using InputStream.



Arun Patidar

Avatar

Level 10

I did the same but are you trying to say adapt the property as resource? Can you please elaborate and give some more details on how this can be done 

 

nodeA has property 

binaryprop binary value 

Avatar

Community Advisor

HI @NitroHazeDev 
I meant the image node resources can be adapted/injected as resources/Node and then read jcr:data property

https://github.com/arunpatidar02/aem63app-repo/blob/07cfd85afdd5970096b6f4c7b9d1e0db976cb6d3/java/dr... 



Arun Patidar

Avatar

Level 10

 This is a binary property on a node say custom .. and custom is adapted as a resource . @arunpatidar  

with that, the value is null

apologies if I am not able to follow

Avatar

Community Advisor

could you please share the content node structure?



Arun Patidar

Avatar

Level 10

Sure .. 

/content

  /xxx

    /page

       /jcr:content

          /par

              /samplenode(node)

                     (Not a node) binaryproperty binary binaryval

 

samplenode is adapted as a resource class 

 

Avatar

Community Advisor

HI @NitroHazeDev 

 

Content

arunpatidar_1-1680785449015.png

 

Code to read binary property

arunpatidar_0-1680785418707.png

 



Arun Patidar

Avatar

Level 10

Hi Arun

thanks,
Maybe I didn state the concern well.. my concern is more of adapting the node samplenode of my:unstructured type to a resource class via sling model and getting the binary property Into it 

 

 

what i observed is that other custom properties injected were retrieved but not the binary, I had taken the route of creating init method within sling model and obtaining the binary by reading the property from node path again via resolver.getresource(node path)  

that worked but if I do a get on the binary property that does not work 

 

 

Avatar

Community Advisor

Hi @NitroHazeDev 
could you please share your code which you are you using to retrieve Binary property inside init method?



Arun Patidar

Avatar

Level 10

Hi @arunpatidar    "samplenode" in the above structure is adapted to a resource, and its properties, path and binaryPath are injected..

 

@Model(adaptables = Resource.class,defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL)
public class customModel {


@Inject
private String path;

 

@Self
private Resource resource;

@SlingObject
private ResourceResolver resolver;

 

@ValueMapValue(injectionStrategy=InjectionStrategy.OPTIONAL)
private Binary binaryProperty;

 

//Getters for the properties

 

@PostConstruct
protected void init() {

//use the getter to get the binary property or use this. and no //result

 

//alternatively use below and it works

content = resolver.getResource(resource.getPath()).adaptTo(Node.class).getProperty("binaryProperty").getStream();

}

}

Avatar

Level 10

@arunpatidar so in the above the injections or valuemap for binary does not seem to work 

kindly let me know

Avatar

Community Advisor

Hi @NitroHazeDev 
Please check https://stackoverflow.com/questions/16932789/accessing-resource-data-from-jcr-repo

 

You can try adapting to Property

@ValueMapValue(injectionStrategy=InjectionStrategy.OPTIONAL)
private Property binaryProperty;

Binary b = (null != binaryProperty) ? binaryProperty.getBinary() : null;

 

 

 



Arun Patidar