It's all about the answers!

Ask a question

Retrieve embedded images from artifacts while exporting RDNG module content to Word via OSLC


Ruben Cabello de Galdo (131) | asked Nov 17 '17, 8:44 a.m.
edited Nov 18 '17, 2:19 p.m. by Geoffrey Clemm (29.0k23035)

Hello everyone,


I am currently implementing a Java application (servlet using OSLC RM API) that will perform an export of all the artifacts included in a determined RDNG module. The application programatically creates a structure file of the module via Rational Publishing Engine, and then do the export of the contents of each artifact following the structure file order.

My problem is that some artifacts of the module include (or are) images, and I need to export them to the Word document as well. 

When I take a look to this kind of artifacts using the REST API, I get the following data:

- <jazz_rm:primaryText rdf:parseType="Literal">
  - <div>
    - <p dir="ltr" id="_1509465736372">
      <img alt="Embedded Image" class="embeddedImageLink" id="_1509465736373" src="https://jazz-server-name/rm/wrappedResources/_unb9KL5UEeetodYFjgQVUQ?accept=none&private" style=""/>
      </p>

It is clear to me that the src link points to the image stored in the server, because when I try to access this link with a browser, I get a "Save As" window that allows to download the image and store it locally.

The issue is, how to do the process of getting and inserting these images into the Word document where I do the export of artifacts?

Do I need to download all the images before doing the export? Or is there a more convenient way?

For your information, I am using the POI API XWPF libraries to do the Word export of the artifacts. 

Many thanks in advance for your help.

Kind regards,

Ruben

One answer



permanent link
Ruben Cabello de Galdo (131) | answered Nov 24 '17, 4:21 a.m.

After some research and lots of testing, I found out a solution and would like to share it with you.


The key to solve the download of images is launching a GET query with the appropiate Headers to retrieve an image and not text content.

I used the following:

query.addHeader("Accept", "application/xml");
query.addHeader("OSLC-Core-Version", "2.0");
query.addHeader("Cache-Control", "no-store");
query.addHeader("Pragma", "no-cache");
query.addHeader("Content-Type", "image/png");

Then send the GET query and retrieve the http response directly into an InputStream object:

InputStream is = httpresponse.getEntity().getContent();

And finally write the content of the image into a FileOutputStream using the destiny path we want to:

FileOutputStream fos = new FileOutputStream(new File(imgPath));

int read = 0;
byte[] buffer = new byte[32768];
while( (read = is.read(buffer)) > 0) {
  fos.write(buffer, 0, read);
}

fos.close();
is.close();

Once the image is downloaded it is easy to include it into the Word document using the XWPFRun.addPicture method.

Hope this will also be helpful to someone else!

Kind regards,

Ruben

Your answer


Register or to post your answer.