5

I am trying to upload binary images through the Java Client using Core Service API. I am facing an issue while uploading the binary content using the IStreamUpload.uploadBinaryByteArray() method. While invoking this method, I am getting the following exception:

The message with Action 'http://www.sdltridion.com/ContentManager/CoreService/2011/IStreamUpload/UploadBinaryByteArray' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).`

Can you please suggest what needs to be added to the below code to make it work?

public void createBinaryComponent(String title, String folderUri,
        String schemaUri, ICoreService client, IStreamUpload streamUpload)
        throws Exception {
    // folder uri tcm:62-12340-2
    // title PWRFTPXL
    // schemaUril tcm:62-1523-8

    ObjectFactory objectFactory = new ObjectFactory();
    ReadOptions readOptions = new ReadOptions();
    ComponentData newComponent = (ComponentData) client.getDefaultData(
            ItemType.COMPONENT, "tcm:62-13493-2");
    LinkToSchemaData schema = objectFactory.createLinkToSchemaData();
    schema.setIdRef("tcm:62-1509-8");
    newComponent.setSchema(schema);
    newComponent.setComponentType(ComponentType.MULTIMEDIA);
    newComponent.setTitle("PWRTESTGIF");
    String customMetadata = "<Metadata xmlns=\"http://www.metlife.com/Schemas/Asset\"><AssetMeta><Title>pwrasset</Title><LongDescription>pwr</LongDescription></AssetMeta></Metadata>";
    newComponent.setMetadata(customMetadata);

    LinkToApprovalStatusData approvalStatus = new LinkToApprovalStatusData();
    approvalStatus.setIdRef("tcm:0-0-0");
    approvalStatus.setTitle("Undefined");
    newComponent.setApprovalStatus(approvalStatus);

    // UploadResponse us = new UploadResponse();
    // D:\\13.4 release\\Power Reviews\\Test\\badge_vp_fr_CA.gif
    // C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg
    File file = new File(
            "C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg");
    file.setReadable(true);
    file.setExecutable(true);
    // FileInputStream fileinputStream = new FileInputStream(file);
    // byte[] result = new byte[(int)file.length()];
    //      
    byte[] bytearr = read("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg");

    String tempLoc = streamUpload.uploadBinaryByteArray(file.getName(),
            bytearr);
    BinaryContentData binaryData = new BinaryContentData();
    // binaryData.setBinaryId(value);
    binaryData.setFilename(file.getName());
    newComponent.setIsBasedOnTridionWebSchema(true);
    binaryData.setIsExternal(false);
    // binaryData.setMimeType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

    LinkToMultimediaTypeData multimeidatype = objectFactory
            .createLinkToMultimediaTypeData();
    multimeidatype.setIdRef("tcm:0-2-65544");
    multimeidatype.setTitle("Jpeg image");
    binaryData.setMultimediaType(multimeidatype);

    binaryData.setUploadFromFile(tempLoc);
    // newComponent.setId("tcm:0-0-0");
    newComponent.setBinaryContent(binaryData);
    client.create(newComponent, null);
}
0

2 Answers 2

8

Check out the sample code below. You will of course have to change all the references to TCM URIs to meaningful values on your system. Also, I'm uploading a file called Beagle.jpg which initially exists in the C:\ folder of my client machine.

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);

And the uploadFile method is like this:

private 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) {
        return null;
    }
}

More info about the CoreServiceFactory class and this version of Java Core Service client is available on my blog http://yatb.mitza.net and http://yatb.mitza.net/search?q=Core+Service+Java

9
  • Hi Mihai, Thanku so much for the reply...Actually the issue for me is i am not able to upload the file into content manager.It is faling when i am accessing the String uploadFile = clientUpload.uploadBinaryByteArray(file.getName(), fileData); May 22, 2013 at 10:45
  • Getting the following error: The message with Action 'sdltridion.com/ContentManager/CoreService/2011/IStreamUpload/…' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None). May 22, 2013 at 10:48
  • I am using the same core-service-client-2011sp1hr1.jar provided in your blog. I am facing issue to access streamDownload_basicHttp end point methods.Please suggest is there anything else we need to add to access the streamDownload_basicHttp end point.I am able to access the basicHttp end point and create simple components. May 22, 2013 at 10:50
  • I have not used the streamDownload endpoint, but only the streamUpload to push the binary to the server. All worked out of the box for me. For binaries larger than 16K, I had to increase the reader quota, as explained in my blog post yatb.mitza.net/2013/05/… May 22, 2013 at 16:29
  • i am really Sorry ...it was my typo mistake...i am using the same Stream Upload end point and the code as given by you. I am using the same CoreServiceFactory and the Test class from your blog only. I am using the same uploadFromFile() method private static File May 23, 2013 at 4:54
2

Per my knowledge, the issue should be because of the difference in the byte[] in .NET and Java.

As far as I know, byte[] in java stores data in Big Endian format, whereas byte[] in .NET stores data in Little Endian.

May be you can try converting your byte[] representation in Java client in Little Endian, and then try calling the IStreamUpload.uploadBinaryByteArray() method with this updated byte[].

See, the cause is obvious from the error message, whatever string and byte[] parameter in JAVA you are sending from java client, the .NET WCF service is not treated that as same string and byte[] array in .NET.

Your Answer

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

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