Skip to main content

Create Multimedia Component using Core Service Java Client

I recently stumbled upon the following question on TREX: http://tridion.stackexchange.com/questions/1379/not-able-to-upload-binary-images-using-core-service2011-api-from-java-client/

I had never actually attempted to upload a Binary and create a Multimedia Component using the Core Service. So I was intrigued how to achieve that.

The solution is pretty straightforward, but it does contain two steps:
  • First, upload the Binary to the TCM;
  • Second, create the Multimedia Component referencing the path on TCM where the binary had been uploaded;
The sample code below accomplishes the first step:

private static File uploadFile(File file) {
    try {
        byte[] fileData = new byte[(int) file.length()];
        DataInputStream dis = new DataInputStream(new FileInputStream(file));
        dis.readFully(fileData);
        dis.close();
        IStreamUpload clientUpload =
                CoreServiceFactory.getStreamUploadBasicHttpClientClient();
        String uploadFile = clientUpload.uploadBinaryByteArray(file.getName(), fileData);
        return new File(uploadFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

The method takes a local file (i.e. local on the client machine) and uploads it to the TCM using the Core Service StreamUploadBasicHttpClient endpoint. It returns a File object representing the location of the uploaded binary on the TCM (e.g. C:\Windows\TEMP\tmp2432.jpg).

Once we have the binary uploaded to the remote TCM, we can now create the Multimedia Component:

    ICoreService client = CoreServiceFactory.getBasicHttpClient();

    ReadOptions readOptions = new ReadOptions();
    ComponentData componentData = (ComponentData) client.getDefaultData(ItemType.COMPONENT, "tcm:1-1-2"readOptions);
    componentData.setTitle("MMC " + System.currentTimeMillis());

    LinkToSchemaData linkToSchema = new LinkToSchemaData();
    linkToSchema.setIdRef("tcm:1-26-8");
    componentData.setSchema(linkToSchema);

    File localFile = new File("C:\\Beagle.jpg");
    File remoteFile = uploadFile(localFile);
    BinaryContentData binaryContentData = new BinaryContentData();
    binaryContentData.setFilename(localFile.getName());
    binaryContentData.setUploadFromFile(remoteFile.getAbsolutePath());

    LinkToMultimediaTypeData linkToMultimediaType = new LinkToMultimediaTypeData();
    linkToMultimediaType.setIdRef("tcm:0-2-65544");
    binaryContentData.setMultimediaType(linkToMultimediaType);
    componentData.setBinaryContent(binaryContentData);

    componentData = (ComponentData) client.save(componentData, readOptions);
    client.checkIn(componentData.getId(), true, null, readOptions);
}

The code is pretty straight forward:
  • initial ComponentData is created using client.getDefaultData();
  • Multimedia Schema is linked from ComponentData;
  • BinaryContentData is created using the remote binary file;
  • MultimediaType is identified and specified. This is probably the ugliest part of the entire appraoch. This logic could be written by first reading all available Multimedia Types, then identifying the one we need by matching on the binary mime type;

For more information about the CoreServiceFactory object, have a look at me previous posts relating to Java Core Service clients.

Note that the default Reader Quota for my Java client was 16384 bytes, and I got the following error message:

com.sun.xml.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'UploadBinaryByteArray'. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 615.

The solution was to increase the MaxArrayLength reader quota on the WCF service by editing the file [TRIDION_HOME]\webservices\Web.config and to insert the following line below system.ServiceModel / bindings / basicHttpBinding / binding name="StreamUpload_basicHttpBinding":

<readerQuotas maxArrayLength="999999999" />


Comments

Jeremy Rambo said…
Did you ever figure out a way to overcome the quota issue without changing the WCF service? I.E. Can I configure the Java Client in any way that allows me to configure the maxArrayLength?
D .Deepak said…
hi..How Can we update the existing binary/multimdeia component?

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven