1

I am using SDL Web 8.5 and it has been configured to publish content and images to Broker DB.

I have a Restful web service (written in Java) which is using CIL to get Component Presentation as JSON. As part of the JSON output, I am getting the image path.

This JSON is consumed by some frontend application. But the image is just a path in JSON.

Could anyone guide me on how to send the image to the calling application?

2
  • why do you want to send image as binary at all? All you need is to let consumer of your service read the image from that location. I hope that your image location is relative path.
    – Marko Milic
    Commented Feb 20, 2019 at 10:17
  • Yes @MarkoMilic, it is relative path.
    – GVK
    Commented Feb 25, 2019 at 8:20

2 Answers 2

3

@Shiva already answered might helpful for using .NET CIL.

To get the Binary using Java CIL.

Java Example:

import com.sdl.web.api.dynamic.BinaryContentRetrieverImpl;
import com.sdl.web.api.dynamic.DynamicMetaRetrieverImpl;
import com.tridion.data.BinaryData;
import com.tridion.dynamiccontent.DynamicMetaRetriever;
import com.tridion.meta.BinaryMeta;
import com.sdl.web.api.content.BinaryContentRetriever;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;

public class SDLCILTest {
    public static void main(String[] args) throws Exception {
        DynamicMetaRetriever dynamicMetaRetriever = new DynamicMetaRetriever();
        String urlPath = "/media/map_tcm5-316.jpg";
        String path = "c://temp/test.jpg";

        final File file = new File(path);
        BinaryMeta binaryMeta = dynamicMetaRetriever.getBinaryMetaByURL(urlPath);
        int itemId = (int) binaryMeta.getURI().getItemId();
        int publicationId = 5;

        //WebComponentMetaFactory factory = new WebComponentMetaFactoryImpl(publicationId);
        //ComponentMeta componentMeta = factory.getMeta(itemId);
        //long componentTime = componentMeta.getLastPublicationDate().getTime();

        //TODO based on component metadata date modified - fetch only if file is modified

        BinaryContentRetriever binaryContentRetriever = new BinaryContentRetrieverImpl();
        BinaryData binaryData = binaryContentRetriever.getBinary(publicationId, itemId, binaryMeta.getVariantId());

        try {
            Files.write(binaryData.getBytes(), file);
        } catch (IOException e) {
            throw new Exception("Cannot write new loaded content to a file " + file, e);
        }
    }
}

For some DXA references for CIL to download Binary could be useful for you.

CilStaticContentResolver.java

GenericStaticContentResolver.java

For Microservices URLs for your reference how that CIL API's get converted to get the Binaries, you can play around with the postman.

http://localhost:8081/client/v4/content.svc/GetDynamicMetaFunctionImport(Url='%252Fmedia%252Fmap_tcm5-316.jpg',ItemType='BinaryMeta')

http://localhost:8081/client/v2/content.svc/BinaryContents(PublicationId=5,BinaryId=316,VariantId=WyNkZWYjXQ==,StreamContent=false)

http://localhost:8081/client/v2/content.svc/BinaryContents(BinaryId=316,PublicationId=5,VariantId='ZGVmYXVsdA==')/$value 

I hope it helps.

1
  • Thanks @Velmurugan, but getBinaryMetaByURL(urlPath) is returning Null. Also, if I used binaryContentRetriever.getBinary("tcm:5-316"), I am getting the BinaryData.
    – GVK
    Commented Feb 25, 2019 at 9:42
4

Binary should be treated as a separate entity similar to pages and component presentations. In your case, you would need your Rest service to serve your binary as a stream.

Like Mark highlighted, you would only need the relative path in the JSON to avoid further manipulation.

A DD4T relevant example can be found at https://github.com/dd4t/DD4T.RestService.WebApi. You can retrieve a page by using http://myservice/page/getcontentbyurl/{publicationId}/{extension}/{url} and a binary with something like http://myservice/binary/GetBinaryByUri/{publicationId:int}/{id:int}

1
  • Thanks @Shiva. Will send the response as a Stream or byte[]
    – GVK
    Commented Feb 25, 2019 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.