Uploading file in HANA cloud Repository using Document management service
in XS engine

 

 

Introduction to document services

 

Many applications need to store and retrieve unstructured content. Traditionally, a file system is used for this purpose. In a cloud environment, however, the usage of file systems is restricted. File systems are tied to individual virtual machines, but a Web application often runs distributed across several instances in a cluster. File systems also have limited capacity.

 

The document service offers persistent storage for content and provides additional functionality. The document service provides a standardized interface for content using the OASIS CMIS standard.

 

The following figure illustrates the document service’s architecture:

Architecture.JPG

 

 

 

Implementing Document Services

 

This blog is mainly based on detailed steps of uploading a file to HANA cloud repository using document management service in XS engine.

 

In this blog we have created a simple UI5 (html) page which is used to upload files from presentation server.  We are using XSJS service to upload the document onto document repository in HANA cloud. The XSJS service uses standard document management services to upload the document onto repository.

 

Notes:

 

  1. We have used HANA cloud trial account to create and test DMS service.
  2. DMS service used in this example is currently only available in trial version.

 

Prerequisites:

 

Before uploading document to DMS you need to create a repository or use an existing repository in HANA cloud account. Here in this example we create the following repository:

 

 

Login to HANA cloud account and go to Document Repository tab:

/wp-content/uploads/2015/06/repository1_725787.png

 

Create a new Repository (You may also use an existing repository):

/wp-content/uploads/2015/06/repository2_725788.png

 

/wp-content/uploads/2015/06/repository3_725792.png

 

I have created the following files to use and test the Document management service:

  1. .xsaccess:
    This is needed to expose the files.
  2. .xsprivileges:
    This is needed to specify application privileges.
  3. Ecm.xshttpdest:
    This is needed to specify cloud instance details.
  4. Index.html
    file:
    To test the service. This file creates a simple UI where user can
    upload any document. This page passes the document to XS engine for further processing
    (uploading the document in HANA cloud repository).
  5. Fileupload.xsjs:This is an XSJS script which creates a DMS session and uploads the binary
    contents of the file to HANA cloud repository.


Details of each of these files are as follows:

  • .xsaccess

First you need to create .xsaccess file which will expose your project with basic authentication.

{

“exposed” : true,
“default_file”: “index.html”

}

 

  • .xsprivileges

Next you need to define privileges which should define the type of access users will have to the services created. Specify privilege  as Basic for this test.

 

{ “privileges” :

 

[ { “name” : “Basic”, “description” : “Basic usage privilege” } ]

 

}

  •     Ecm.xshttpdest:

This file shall provide HANA cloud host details. This file is used in XSJS service to connect to HANA cloud system and access the repository using Document management services.

 

  1. To create a session you have to provide a destination containing the repository URL first.
  2. Store the following text in a file named Ecm.xshttpdest and replace the host value placeholder <account> with the name of your account.

 

 

description = “Document Service”;

host = “ecm-<account>.hanatrial.ondemand.com”;

port = 8443; (Replace this with port name if it’s different from 8443)

pathPrefix = “/s/json”;

useProxy = false;

authType = none;

useSSL = true;

timeout = 300;

 

 

  • Index.html

Next you need an HTML form, to test document management service. Here user will have an option to upload a file which shall be passed to XS engine.

 

<!DOCTYPE html>

<html>

<head>

<title>File Upload Example</title>

</head>

<body>

<h4>Upload a File to document service</h4>

<form action=”fileupload.xsjs” enctype=”multipart⁄form-data” method=”post”>

Description:

<input type=”text” name=”description” size=”30″><br/>

File:

<input type=”file” name=”datafile” size=”40″><br/>

<input type=”submit” value=”Submit”><br/>

</form>

</body>

</html>

 

This form submits a POST request to fileupload.xsjs. There the multipart request has to be parsed.

 

  • fileupload.xsjs

Next you need to create xsjs file, to parse file uploaded by the user and upload it to HANA cloud repository.

During execution this file executes the following steps:

  1. Parse the uploaded document
  2. It connects to a repository.
  3. It creates a document.

 

Please change the repository name and repository key in the following sample code:

 

// Extract the content from the multipart entity and return a CMIS content object
// This function reads the uploaded file content and returns the details to the calling service

function getCmisContentObject(entity) {
var content = {};

if (entity) {
var j;

content.stream = entity.body.asArrayBuffer();
for (j = 0; j < entity.headers.length; j++) {
if (entity.headers[j].name === “~content_filename”) {
content.filename = entity.headers[j].value;
} else if (entity.headers[j].name === “content-type”) {
content.mimeType = entity.headers[j].value;
}
}
if (!content.filename || content.filename.length === 0) {
content.filename = “data.bin”;
}
if (!content.mimeType || content.mimeType.length === 0) {
content.mimeType = “application/octet-stream”;
}
}
return content;
}

 

// get the mulitpart content entity with given name

function getContentEntity(fieldName) {
var i;
var entity = null;

if ($.request.entities) {
for (i = 0; i < $.request.entities.length; ++i) {
var j;
entity = $.request.entities[i];
for (j = 0; j < entity.headers.length; j++) {
if (entity.headers[j].name === “content_name” &&
entity.headers[j].value === encodeURIComponent(fieldName)) {
return entity.body.asArrayBuffer();
}
}
}
}
return entity;
}

function getStringEntity(fieldName) {
var i;
var entity;

if ($.request.entities) {
for (i = 0; i < $.request.entities.length; ++i) {
var j;
entity = $.request.entities[i];
for (j = 0; j < entity.headers.length; j++) {
if (entity.headers[j].name === “content-disposition” &&
entity.headers[j].value === “form-data; name=\”” +
encodeURIComponent(fieldName) + “\””)  {
return entity.body.asString();
}
}
}
}
return null;
}

if($.request.method === $.net.http.POST) {
var name;
var mimeType;
var content;
var customProperty;
var description;
var contentIndex = -1;
var i;
var j;
var id;
var body;
var headers;
var message = “”;
var records = [];

var entity = getContentEntity(“datafile”);
// Get document details
content = getCmisContentObject(entity);
// Get document description
description = getStringEntity(“description”);

if (content) {
try {
// Create Document repository session
var session = docService.cmis.createHANAXSECMSession({
destination : $.net.http.readDestination(“acme.cmistestapp.cmisjs”, “ecm”)
}, {
repositoryName : “XXXXXXXX”, // Provide repository name which was created earlier.
repositoryKey : “XXXXXXXXX” // Provide repository key which was set earlier.
});
// Create a new document in Document repository
session.init().then(function(repInfo) {
session.getRootFolder().then(function(rootFolder) {
rootFolder.createDocument({
“cmis:name” : content.filename,
“cmis:description” : description,
“cmis:objectTypeId” : “cmis:document”
}, content).then(function(newDoc) {
id = newDoc.getId();
message += “Your file was archived with:\n”;
message += “name: ” + content.filename + “\n”;
message += “mime type: ” + content.mimeType + “\n”;
message += “id: ” + id + “\n”;
});
});
});
} catch (e) {
message += “Error ” + error.name + “: ” + error.message + “\n”;
}
} else {
message += “No content found in request.\n”;
}

records.push({
message : message,
id      : id
});

// send response
$.response.contentType = “application/json”;
$.response.setBody(JSON.stringify(records));
$.response.status = $.net.http.OK;
} else {
message = “Unexpected http method called.”;
// unsupported method
var records = [];

records.push({
message : message,
id      : id
});

$.response.contentType = “application/json”;
$.response.setBody(JSON.stringify(records));
$.response.status = $.net.http.OK;
}

 

 

 

Create a folder

 

We have not created a new folder in document repository in the above example. You can use the following code if you want to create a new folder inside document repository.

 

A folder is created as child of another folder. Each repository by default has a root folder. The root folder can be retrieved from the session. Names must be unique within a folder.

 

var rootFolder = var rootFolder = session.getRootFolder();

rootFolder.createFolder({
“cmis:name” : “newFolder”,
“cmis:objectTypeId” : “cmis:folder”
});
}).then(function(newFolder) {
var folderId = newFolder.getId();
}

To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

  1. Vijay Sah

    Hi Amit,

    Can you help me in understanding the below line of code.

      destination : $.net.http.readDestination(“acme.cmistestapp.cmisjs”, “ecm”)


    I am getting Error 405


    Thanks,

    Vijay

    (0) 
      1. Seidor Hcp

        Hi Sergio Guerrero,

        Now my problem is library sap.bc.cmis

        var docService =  $.import(“sap.bc.cmis”, “cmis”); //Here I have problems with this library. Or should I use another?
        var session = docService.cmis.createHANAXSECMSession({  code …

        Thanks

        (0) 
        1. Sergio Guerrero

          I do not see how Amit declared / got the docService variable either.. i am in the middle of testing this as well but noticed the missing instantiation of that variable.

          maybe Amit can let us know where that came from

          (1) 
          1. Seidor Hcp

            Return error:

            [{"message":"Error ReferenceError: docService is not defined\n"}]
            
            
            I do not know how I can refer to variable docService

            Is there another way to use document service to upload my files?

            Thanks

            (0) 
  2. Shilpa Jain

    Hi ,

    I am getting an error while running index.html as

    404 – Not found

    We could not find the resource you’re trying to access.
    It might be misspelled or currently unavailable.

    Please help me out.

    (0) 

Leave a Reply