Expand my Community achievements bar.

Send DAM asset data from CQ to Hybris

Avatar

Level 2

Hi,

I have to implement program which can fetch images from CQ/JCR and then send this image to Hybris.

I have implemented program for fetching image from JCR, I am thinking of using JAX-RS for sending image from CQ to Hybris. Please find below my code

@Component
@Service
@Path("/CQContent")  
public class QueryImpl implements Queryy {

    // Inject a Sling ResourceResolverFactory
    @Reference
    private ResourceResolverFactory resolverFactory;
    
    @Reference(policy = ReferencePolicy.STATIC)
    private SlingRepository repository;

    
    
    @GET
    @Path("/get")
    @Produces("image/png")
    public Response getJCRImageData(String location) {
        String assetPath = null;
        String assetName = null;
        try {            
            Session session = repository.loginAdministrative(null);
            
            Map<String, String> map = new HashMap<String,String>();
            map.put("type", "dam.Asset");
            map.put( "nodename" , "ai.jpeg" );

            ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
            Resource resource = resourceResolver.getResource(location);

            QueryBuilder builder = resource.adaptTo(QueryBuilder.class);
            Query query = builder.createQuery(PredicateGroup.create(map), session);

            SearchResult result = query.getResult();             
            
            /* // iterating over the results
            Iterator<Node> nodes = result.getNodes();
            
              while(nodes.hasNext() ) {
                  Node node = nodes.next();
                  Property property = node.getProperty("jcr:data");
                  InputStream input = property.getStream();
                  
              }*/
            for(Hit hit : result.getHits()) {
                String path = hit.getPath();
                Resource rs = resourceResolver.getResource(path);
                Asset asset = rs.adaptTo(Asset.class);
                assetPath = asset.getPath();
                assetName = asset.getName();
                break;
            }
            File file = new File(assetPath);

            ResponseBuilder response = Response.ok((Object) file);
            response.header("Content-Disposition",
                    "attachment; filename=\'"+assetName+"\'");
            return response.build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

I have created OSGi bundle for above service and uploaded the same into Adobe CQ. But I dont understand what could be the whole URL to call this REST service. It will be definately something like http://localhost:4502/....../CQContent/get 

I dont want to use something like  sling.getService(custom.sling.Query.class);  because I dont want to call my code in JSP. I have to call this code from Hybris so I will need URL 

Can you please guide me how I can call this REST service? in normal java web application we use application name/url for service class/url for method. But I dont know how to find out URL in CQ. 

Also whether above mentioned approach for sending image from CQ to Hybris is fine? Can anyone suggest me any best approach to achieve this?

Thanks

0 Replies