Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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:



 

 

 

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:



 

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



 



 

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

12 Comments