Uploading file in HANA cloud Repository using DMS in XS engine
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:
- We have used HANA cloud trial account to create and test DMS service.
- 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:
- .xsaccess:
This is needed to expose the files. - .xsprivileges:
This is needed to specify application privileges. - Ecm.xshttpdest:
This is needed to specify cloud instance details. - 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). - 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.
- To create a session you have to provide a destination containing the repository URL first.
- 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:
- Parse the uploaded document
- It connects to a repository.
- 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();
}
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
I get the same error
Will you have any solution?
the readDestination methos is part of the outbound connectivity api - documentation is below
http://help.sap.com/hana/SAP_HANA_XS_JavaScript_API_Reference_en/$.net.http.html
hope this helps
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
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
Return error:
Is there another way to use document service to upload my files?
Thanks
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.
Hi,
Seidor Hcp...Are you able to resolve this issue of docService Variable. If yes please suggest as I am also stuck at that point.
Thanks
Not is possible, i applied java for connect to document service.
Please ignore the instructions from this blog and use the Java Proxy Bridge to communicate with the Document Service.
hi, after following the instructions at help and this detailed youtube video about SAP HANA Academy - Java Development on HCP: Document Service and Unstructured Content
i am getting authorization error. Any idea why?
HTTP Status 403 - Access to the requested resource has been denied
Could you please check the http traces from F12? Try to identify where that 403 came from.
You need to figure out which http request is being denied by which server first.