http-servlet
Version:
Extends HttpServlet & works like in J2EE environment
172 lines (140 loc) • 5.8 kB
JavaScript
/**
* Copyright (c) 2016 Abdennour TOUMI <http://abdennoor.com> ceo@rathath-it.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*
**/
((rq) => {
((Utils, HttpServletRequest, HttpServletResponse, xclass, fs, xm) => {
class HttpServlet {
constructor(rq, rs, ctx) {
this.init(...arguments);
this.__ctx__ = ctx;
if (!rq.isServleted || !rs.isServleted) {
this.req = new HttpServletRequest(rq, rs, ctx);
this.res = new HttpServletResponse(rs, rq, ctx);
Utils.extendsRR.bind(this)();
this.contentType = 'text/html';
if (this.req.method === 'POST') {
Utils.params(rq, rs, (params, files) => {
this.params = params;
if (files) {
this.files = files;
}
this.req.getSession();
this.doFilter() && this.process();
});
} else {
this.params = Utils.params(this.req.url);
this.req.getSession();
this.doFilter() && this.process();
}
}
}
getServletContext() {
return this.__ctx__;
}
get request() {
return this.req;
}
get response() {
return this.res;
}
get session() {
return this.request.getSession();
}
set session(v) {
throw new Error(`Cannot override HttpSession with "${v}"`);
}
get contentType() {
return this.res.getHeader('Content-Type');
}
set contentType(value) {
this.res.setHeader('Content-Type', value);
}
set status(v) {
this.res.writeHead(v);
}
init() {
}
process() {
}
doFilter() {
return true;
}
destroy() {
this.response.end();
}
allow(prop, val) {
this['allow_' + prop](...Array.from(arguments).splice(1));
return this;
}
allow_method() {
this.res.setHeader('Access-Control-Allow-Methods', Array.from(arguments).map(e => e.toUpperCase()).join(','));
return this;
}
allow_origin() {
this.res.setHeader('Access-Control-Allow-Origin', Array.from(arguments).join(','));
return this;
}
enable(what) {
var args = [];
if (arguments.length >= 2) {
args = Array.from(arguments).splice(1);
}
return this[`enable_${what}`](...args);
}
enable_cors(xmlOrJson) {
this.allow('origin', '*');
this.res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (xmlOrJson) {
this.contentType = `${xmlOrJson}/application`;
}
}
write(str, unicode) {
this.res.end(str, unicode || 'utf-8');
}
writeJSON(o, unicode) {
this.contentType = `json/application`;
this.write((typeof o === 'object') ? JSON.stringify(o) : o, unicode);
}
writeXML(o) {
this.contentType = `xml/application`;
this.write(...arguments);
}
writeFile(srcPath, contentType) {
Utils.writeFile(this.res, srcPath, contentType);
}
redirect(url) {
return Utils.redirect(this.res, url);
}
delegateTo(servlet) {
servlet = (typeof servlet === 'string') ? require(servlet) : servlet;
return new servlet(this.req, this.res, this.__ctx__);
}
forward(uri, servlet) {
this.response.writeHead(302, {
'Location': uri
});
this.response.end();
}
download(srcPath, contentType) {
Utils.download(this.res, srcPath, contentType);
}
}
module.exports = HttpServlet;
})(rq('../utils/Utils.js'), rq('../utils/HttpServletRequest.js'), rq('../utils/HttpServletResponse.js'), rq('x-class'), require('fs'), require("xmimetype"));
})(require)