Use Nodejs CMIS To Query BTP SAP Document Management Service In A Nodejs MTA Ap...
source link: https://blogs.sap.com/2023/01/09/use-nodejs-cmis-to-query-btp-sap-document-management-service-in-a-nodejs-mta-application/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Use Nodejs CMIS To Query BTP SAP Document Management Service In A Nodejs MTA Application
In my blog Query Create And Delete Document From BTP Document Management Service In SAPUI5 , I have explained how to operate BTP SAP Document Management Service(BTP CMIS) in a sap ui5 application(frontend). Since BTP CMIS is based on the OASIS industry standard CMIS and if customer want to consume BTP CMIS in backend(server) application, we can use Nodejs CMIS to simplify the development. In this blog, I only show query. For other operates, you can refer to CmisJS Documentation and try it .
Prerequisite:
1, You have installed CF Client .
2, You have installed Nodejs .
3, You have installed Cloud MTA Build Tool .
4, You have finished Initial Setup for Document Management Service, Integration Option.
5, You have finished Onboarding Repository.
6, You have installed VSCode (optional).
7, Your BTP subaccount has been entitled the following 2 services with the plans
- Document Management Service, Integration Option(sdm) with plan free
- Authorization and Trust Management Service(xsuaa) with plan application
Steps :
Step 1: Generate SAPUI5 project with generator-saphanaacademy-mta
.
Using visual code to open it with the following command .
Step 2: Add package cmis in the file package.json under folder srv and then run the following command to install packages :
npm install
Step 3: Update mta.yaml file with the following code:
ID: cmismta
_schema-version: "3.1"
version: 0.0.1
parameters:
enable-parallel-deployments: true
modules:
- name: cmismta-srv
type: nodejs
path: srv
build-parameters:
ignore:
- default-*.json
- .env
- "*node_modules*"
- package-lock.json
parameters:
disk-quota: 512M
memory: 256M
provides:
- name: srv_api
properties:
url: ${default-url}
requires:
- name: cmismta-uaa
- name: dmsfree
- name: cmismta-app
type: approuter.nodejs
path: app
build-parameters:
ignore:
- default-*.json
- .env
- "*node_modules*"
- package-lock.json
parameters:
disk-quota: 512M
memory: 256M
requires:
- name: cmismta-uaa
- name: srv_api
group: destinations
properties:
name: srv
url: ~{url}
forwardAuthToken: true
resources:
- name: cmismta-uaa
type: org.cloudfoundry.managed-service
parameters:
path: ./xs-security.json
service-plan: application
service: xsuaa
- name: dmsfree
type: org.cloudfoundry.managed-service
parameters:
service-plan: free
service: sdm
Step 4: Update server.js file with the following code:
const express = require('express');
const app = express();
const xsenv = require('@sap/xsenv');
xsenv.loadEnv();
const services = xsenv.getServices({
uaa: { label: 'xsuaa' }
});
var axios = require('axios');
var qs = require('qs');
const servicesdm = xsenv.getServices({ sdm: { label: 'sdm' } });
const uaa = servicesdm.sdm.uaa;
var cmis = require('cmis');
const xssec = require('@sap/xssec');
const passport = require('passport');
passport.use('JWT', new xssec.JWTStrategy(services.uaa));
app.use(passport.initialize());
app.use(passport.authenticate('JWT', {
session: false
}));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/srv', function (req, res) {
if (req.authInfo.checkScope('$XSAPPNAME.User')) {
res.status(200).send('cmismta');
} else {
res.status(403).send('Forbidden');
}
});
app.get('/srv/cmis', function (req, res) {
let auth = uaa.clientid.concat(":").concat(uaa.clientsecret);
const buff = Buffer.from(auth, 'utf-8');
const base64 = buff.toString('base64');
const headauth = "Basic ".concat(base64);
var data = qs.stringify({
'grant_type': 'client_credentials'
});
var config = {
method: 'post',
url: uaa.url.concat("/oauth/token"),
headers: {
'Authorization': headauth,
'Content-Type': 'application/x-www-form-urlencoded'
},
data: data
};
axios(config)
.then(function (response) {
let session = new cmis.CmisSession(servicesdm.sdm.uri.concat("browser"));
console.log(servicesdm.sdm.uri.concat("browser"));
session.setToken(response.data.access_token).loadRepositories().then(() => session.query("select * from cmis:document"))
.then(data => {
res.status(200).send(data);
}).catch(err => {
res.status(501).send(err);
});
})
.catch(function (error) {
console.log(error);
});
});
app.get('/srv/user', function (req, res) {
if (req.authInfo.checkScope('$XSAPPNAME.User')) {
res.status(200).json(req.user);
} else {
res.status(403).send('Forbidden');
}
});
const port = process.env.PORT || 5001;
app.listen(port, function () {
console.info('Listening on http://localhost:' + port);
});
Step 5: Update index.html file to add the following code:
Step 6: Build the application with the following command :
mbt build
Step 7: Deploy the applicaiton with the following 2 commands :
cf login
cf deploy mta_archives\cmismta_0.0.1.mtar
Step 8: Test the deployed application :
The End!
Thanks for your time!
Best Regards!
Jacky Liu
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK