Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/SoapResponseBodyHandler.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';

const log = require('lambda-log');
const Parser = require('fast-xml-parser').j2xParser;
const SoapError = require('./SoapError.js');

const parser = new Parser();

let xmlns_soap = 'http://www.w3.org/2001/12/soap-envelope';
let soap_es = 'http://www.w3.org/2001/12/soap-encoding';

let soapBodyStart = '<soap:Envelope\n';
soapBodyStart += ' xmlns:soap="http://www.w3.org/2001/12/soap-envelope"\n';
soapBodyStart +=
' soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">\n';
soapBodyStart += ' xmlns:soap="' + xmlns_soap + '"\n';
soapBodyStart += ' soap:encodingStyle="' + soap_es + '">\n';
soapBodyStart += ' <soap:Body>\n';

let soapBodyEnd = ' </soap:Body>\n';
Expand All @@ -20,12 +23,42 @@ soapBodyEnd += '</soap:Envelope>';
* This class will be responsible for creating the soap response from the actual response.
*/
class SoapResponseBodyHandler {

//Handle customized namespaces from server object
constructor(servobj = null) {
//log.debug("SoapResponseBodyHandler constructor called", servobj);
if(servobj !== null) {
configure(servobj);
}
} //end constructor

//Configure header accordingly to server definition
async configure(servobj) {
//log.debug("SoapResponseBodyHandler configure called", servobj);
if(servobj !== null && typeof servobj === 'object') {

if('xmlns_soap' in servobj) {
xmlns_soap = servobj.xmlns_soap;
}

if('soap_es' in servobj) {
soap_es = servobj.soap_es;
}

soapBodyStart = '<soap:Envelope\n' +
' xmlns:soap="' + xmlns_soap + '"\n' +
' soap:encodingStyle="' + soap_es + '">\n' +
' <soap:Body>\n';
}
}

/**
* Build the success response from the actual response object
*
* @param {Object} response the response object
*/
async success(response) {
//log.debug("SUCCESS called, soapBodyStart is ", soapBodyStart);
let responseBody = soapBodyStart;
try {
responseBody += parser.parse(response);
Expand Down
50 changes: 26 additions & 24 deletions src/SoapServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const path = require('path');
const parser = require('fast-xml-parser');
const log = require('lambda-log');

const SoapRequestHandler = require('./SoapRequestHandler.js');
const SoapResposeHandler = require('./SoapResponseBodyHandler.js');
const SoapRequestHandler = require('./SoapRequestHandler.js');
const SoapResponseHandler = require('./SoapResponseBodyHandler.js');
const SoapError = require('./SoapError.js');

const soapRequestHandler = new SoapRequestHandler();
const soapReponseHandler = new SoapResposeHandler();
const soapRequestHandler = new SoapRequestHandler();
const soapResponseHandler = new SoapResponseHandler();

/**
* Soap Server
Expand Down Expand Up @@ -63,12 +63,17 @@ class SoapServer {
log.options.debug = options.debug ? true : false;
}
return async (event, context) => {
log.debug('Received an event', event);
//log.debug("event", event);
// check this service exists
if (this.services.hasOwnProperty(event.pathParameters.proxy)) {
if (event.pathParameters != null && this.services.hasOwnProperty(event.pathParameters.proxy)) {
log.info('Received a request for service', {
service: event.pathParameters.proxy,
});

//get optionally enforced content type used in responses
var contentType = ('contentType' in this.services[event.pathParameters.proxy])? this.services[event.pathParameters.proxy].contentType : 'application/xml';
await soapResponseHandler.configure(this.services[event.pathParameters.proxy]);

// get calls
if (
event.httpMethod === 'GET' &&
Expand All @@ -77,36 +82,33 @@ class SoapServer {
log.info('Received a request for wsdl', {
service: event.pathParameters.proxy,
});
log.debug(
'The wsdl is: ',
this.services[event.pathParameters.proxy].wsdl,
);
// return the wsdl
return {
body: this.services[event.pathParameters.proxy].wsdl,
statusCode: 200,
headers: {
'Content-Type': 'application/xml',
'Content-Type': contentType,
},
};
} else if (event.httpMethod === 'POST') {

// all post calls to service methods
let requestOperation;
try {
requestOperation = await soapRequestHandler.getOperation(
event.body,
);
log.debug(
log.info(
'Received a request for an operation: ',
requestOperation,
);
} catch (error) {
log.error(error);
return {
body: soapReponseHandler.fault(error),
body: soapResponseHandler.fault(error),
statusCode: error.status ? error.status : 500,
headers: {
'Content-Type': 'application/xml',
'Content-Type': contentType,
},
};
}
Expand All @@ -125,40 +127,40 @@ class SoapServer {
null,
params,
);
log.debug('The response received from server', response);
//log.debug('The response received from server', response);
} else {
throw new SoapError(501, 'Operation didn\'t implemented');
throw new SoapError(501, 'Operation was not implemented');
}
const responseBody = await soapReponseHandler.success(response);
log.debug('Sending the reponse body as: ', responseBody);
const responseBody = await soapResponseHandler.success(response);
//log.debug('Sending the reponse body as: ', responseBody);
return {
body: responseBody,
statusCode: 200,
headers: {
'Content-Type': 'application/xml',
'Content-Type': contentType,
},
};
} catch (error) {
log.error(error);
return {
body: await soapReponseHandler.fault(error),
body: await soapResponseHandler.fault(error),
statusCode: error.status ? error.status : 500,
headers: {
'Content-Type': 'application/xml',
'Content-Type': contentType,
},
};
}
}
} else {
log.error('The service not found');
log.error('The service was not found');
log.debug('Available services are:', this.services);
return {
body: await soapReponseHandler.fault(
body: await soapResponseHandler.fault(
new SoapError(404, 'Service not found'),
),
statusCode: 404,
headers: {
'Content-Type': 'application/xml',
'Content-Type': contentType,
},
};
}
Expand Down