VMware Cloud Community
smithgp
Enthusiast
Enthusiast

Unable to create vCAC catalog item icon from vCO workflow

Hi -

I am trying to configure the icon for a vCACCAFE:CatalogItem (vCACCAFECatalogItem) from a workflow in vCO, using vCACCAFECatalogIconService. I've got a workflow that has the vCACCAFE:VCACHost, the CatalogItem and a vCO ResourceElement as input parameters, and it does the following javascript:

function stringToBytes ( str ) {

  var ch, re = [];

  for (var i = 0; i < str.length; i++ ) {

    ch = str.charCodeAt(i);  // get char

    re[i] = ( ch & 0xFF );  // push byte to stack

  }

  // return an array of bytes

  return re;

}

// get the image bytes of the ResourceElement

var mime = resource.getContentAsMimeAttachment(), content;

System.log('Name: ' + mime.name);

System.log('MimeType: ' + mime.mimeType);

// mime.content says it's a string.

// we need to convert to a byte array, or the

// vCACCAFEIcon.setImage() call below will cause a

// 'Data Serialization error'

var s = mime.content;

System.log('Read ' + s.length + ' string');

content = stringToBytes(s);

// these do show the right # of bytes

System.log('Found ' + content.length + ' bytes');

System.log('Content:' + (typeof content));

var iconsvc = host.createCatalogClient().getCatalogIconService(),

    itemsvc = host.createCatalogClient().getCatalogAdminCatalogItemService(),

    icon = new vCACCAFEIcon();

System.log('Creating icon object...');

icon.setContentType(resource.mimeType);

icon.setFileName(resource.name);

icon.setId(item.getId());

System.log('Setting image...');

icon.setImage(content);

// not sure if org is needed or correct...

System.log('Setting org...');

var org = new vCACCAFECatalogOrganizationReference();

org.setTenantRef(host.tenant);

icon.setOrganization(org);

System.log('Create or update icon ' + icon.getId());

var uri = iconsvc.createOrUpdateIcon(icon);

System.log('Icon uri: ' + uri);

item.setIconId(icon.getId());

itemsvc.updateCatalogItem(item);

It appears to create the icon in the icon service and updates the catalog item iconId, but the icon then doesn't render in the UI. The browser says that it's coming back as 51 or 52 (gzipped) bytes, even though our workflow log says that the content variable is the correct # of bytes (3415 in my case). At one point, in Firebug, I saw that the body of the http response for the icon was actually coming back as a line of javascript, but I can't get to it anymore.

I'm guessing it's something to do with string vs. byte[] and/or automatic argument conversion happening between the javascript engine and the vcaccafe java code and the vcac rest endpoint.

Any ideas are welcome.

Thanks,

Greg

Reply
0 Kudos
3 Replies
smithgp
Enthusiast
Enthusiast

I found a way to make the createOrUpdateIcon() call have the right image bytes -- I base64 encoded the image bytes into a string and used that string in vCACCAFEIcon.setImage().

Greg

slahy
Enthusiast
Enthusiast

Hi Greg,

Can you post your finished code as I am having the same issue and my attmepts at base64 encoding dont seem to be working,

Thanks

sean

Reply
0 Kudos
smithgp
Enthusiast
Enthusiast

Hi Sean -

I can't really post all of the full code since it's part of a commercial product.

I had tried doing the base64'ing in javascript in a workflow, but never got that to work. Since we already had a vco plugin in our product, I added a scripting-method to one of our scripting objects, so that maps to a method in the java code. The java code then reads an image (as a class loader resource) into a byte[], uses javax.xml.bind.DatatypeConverter.printBase64Binary() on that byte array to get the base64-encoded String, and returns that out of the scripting-method. Then, in a workflow, I used basically the same javascript, except instead of doing stuff with the vco resource and the stringToBytes() function, I use the scripting-method, e.g.:

// this is the scripting-method

var emcLogoImageDetails = EdpConfiguration.getEMCLogoImageInfo();

// and this gets the base64 bytes from the java code

var base64Lightblue= emcLogoImageDetails.getBase64EncodingOfImage();

var iconsvc = vCACHost.createCatalogClient().getCatalogIconService(),

    itemsvc = vCACHost.createCatalogClient().getCatalogAdminCatalogItemService(),

    icon = new vCACCAFEIcon();

System.log('Creating icon object...');

icon.setContentType(emcLogoImageDetails.getImageType());

icon.setFileName(emcLogoImageDetails.getNameOfImage());

icon.setId(localItem.getId());

// this seems to work now, since base64Lightblue is the string from the java code

System.log('Setting image...');

icon.setImage(base64Lightblue);

System.log('Setting org...');

var org = new vCACCAFECatalogOrganizationReference();

org.setTenantRef(vCACHost.tenant);

icon.setOrganization(org);

System.log('Create or update icon ' + icon.getId());

var uri = iconsvc.createOrUpdateIcon(icon);

System.log('Icon uri: ' + uri);

Greg

Reply
0 Kudos