forked from community/product-iots
Conflicts: modules/distribution/pom.xmlapplication-manager-new
commit
d9fc247bf4
@ -0,0 +1,883 @@
|
||||
<%
|
||||
var site = require("/site/conf/site.json");
|
||||
//TODO : remove this when log configs are implemented
|
||||
//Log.prototype.isDebugEnabled = function () {
|
||||
// return false;
|
||||
//};
|
||||
|
||||
//TODO : remove this when Context HO is implemented.
|
||||
var context = context || {
|
||||
put:function (key, value) {
|
||||
session.put(key, value);
|
||||
},
|
||||
get:function (key) {
|
||||
return session.get(key);
|
||||
},
|
||||
remove:function (key) {
|
||||
session.remove(key);
|
||||
}
|
||||
};
|
||||
|
||||
var jagg = jagg || (function () {
|
||||
var ctx = context;
|
||||
|
||||
var modules = {};
|
||||
|
||||
var requirs = {};
|
||||
|
||||
var templates = {};
|
||||
|
||||
var initializers = {};
|
||||
|
||||
var bloks = {};
|
||||
|
||||
var data;
|
||||
|
||||
var reverse_proxy;
|
||||
|
||||
var setData = function (d) {
|
||||
data = d;
|
||||
};
|
||||
|
||||
var getData = function () {
|
||||
return data;
|
||||
};
|
||||
|
||||
var getUser = function () {
|
||||
var user = session.get("logged.user");
|
||||
|
||||
// If user is null then check for mutual auth
|
||||
if (!user) {
|
||||
user = mutualAuthVerifier(user);
|
||||
}
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
var setUser = function (user) {
|
||||
//if jaggery allow session reset
|
||||
if (typeof request.getSession == "function") {
|
||||
if (session && !session.isNew()) {
|
||||
session.invalidate();
|
||||
}
|
||||
session = request.getSession(true);
|
||||
}
|
||||
session.put("logged.user", user);
|
||||
};
|
||||
|
||||
var mutualAuthVerifier = function(user){
|
||||
|
||||
var log = new Log();
|
||||
var site = require("/site/conf/site.json");
|
||||
|
||||
if(site.mutualAuthConfiguration){
|
||||
if (site.mutualAuthConfiguration.enabled == "true") {
|
||||
|
||||
// cert will be available only if trust store holds client certificate. Otherwise it is null
|
||||
var cert = request.getAttribute("javax.servlet.request.X509Certificate");
|
||||
var userName = request.getHeader("MutualAuthUserName");
|
||||
|
||||
// proceed mutul ssl validation if cert and user name set properly
|
||||
|
||||
if (cert != null) {
|
||||
if (userName) {
|
||||
var security = require("apimstore");
|
||||
var mutualAuthHostObject = new security.MutualAuthHostObject();
|
||||
var isValidUser = mutualAuthHostObject.validateUserNameHeader(userName);
|
||||
// Group ID feature not supported here
|
||||
var groupId="";
|
||||
session.put("groupId", groupId);
|
||||
if (isValidUser) {
|
||||
log.info("Mutual Auth authentication success for user : " + userName);
|
||||
user = {username: userName, cookie: null, hasPublisherAccess: false};
|
||||
return user;
|
||||
} else {
|
||||
log.debug("Mutual authentication failed for invalid user : " + userName);
|
||||
}
|
||||
|
||||
} else {
|
||||
log.debug("Mutual authentication failed for invalid user : MutualAuthUserName header is empty");
|
||||
}
|
||||
} else {
|
||||
log.debug("Mutual Authentication failed due to no trusted certificate");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var getThemeFile = function (path) {
|
||||
var p, index, theme = getUserTheme();
|
||||
if(theme.tenant_theme) {
|
||||
p = getTenantThemePath() + path;
|
||||
index = p.indexOf("?");
|
||||
if(new File(p.substring(0, index == -1 ? p.length : index)).isExists()) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
if(theme.subtheme) {
|
||||
p = getThemePath() + "subthemes/" + theme.subtheme + "/" + path;
|
||||
index = p.indexOf("?");
|
||||
if(new File(p.substring(0, index == -1 ? p.length : index)).isExists()) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return getThemePath() + path;
|
||||
};
|
||||
|
||||
var getThemesPath = function () {
|
||||
return "/site/themes/";
|
||||
};
|
||||
|
||||
var getTenantThemesPath = function(){
|
||||
return "/site/tenant_themes/";
|
||||
};
|
||||
|
||||
var getThemePath = function () {
|
||||
return getThemesPath() + getUserTheme().base + "/";
|
||||
};
|
||||
|
||||
var getTenantThemePath = function (){
|
||||
return getTenantThemesPath() + getTheme().tenant_theme + "/";
|
||||
}
|
||||
|
||||
var getBlockFile = function (name) {
|
||||
return getBlocksDir() + name + "/block.jag";
|
||||
};
|
||||
|
||||
var getInitializerFile = function (name) {
|
||||
return getThemeFile("templates/" + name + "/initializer.jag");
|
||||
};
|
||||
|
||||
var getTemplateFile = function (name) {
|
||||
return getThemeFile("templates/" + name + "/template.jag");
|
||||
};
|
||||
|
||||
var getTemplatePath = function(themeDir, name) {
|
||||
return themeDir + "templates/" + name + "/template.jag";
|
||||
};
|
||||
|
||||
var getModuleFile = function (name) {
|
||||
return getModulesDir() + name + "/module.jag";
|
||||
};
|
||||
|
||||
var getBlocksDir = function () {
|
||||
return "/site/blocks/";
|
||||
};
|
||||
|
||||
var getThemesDir = function () {
|
||||
return "/site/themes/";
|
||||
};
|
||||
|
||||
var getModulesDir = function () {
|
||||
return "/modules/";
|
||||
};
|
||||
|
||||
var getTheme = function () {
|
||||
//TODO : remove following lines if theme switching need to be avoided
|
||||
var site = require("/site/conf/site.json"),
|
||||
theme = request.getParameter("theme"),
|
||||
subtheme = request.getParameter("subtheme");
|
||||
var r = {
|
||||
base : theme ? theme : site.theme.base,
|
||||
subtheme : subtheme ? subtheme : site.theme.subtheme,
|
||||
};
|
||||
//load the tenant theme if exists
|
||||
var tenant = getTenantDomain();
|
||||
if(tenant){
|
||||
tenant = tenant.replace("/",".");
|
||||
r.tenant_theme = getTenantDomain();
|
||||
}
|
||||
return r;
|
||||
};
|
||||
|
||||
var getUserTheme = function () {
|
||||
return session.get("theme") ? session.get("theme") : getTheme();
|
||||
};
|
||||
|
||||
var mergeParams = function (extInputs, defInputs) {
|
||||
var key, obj;
|
||||
extInputs = extInputs || {};
|
||||
for (key in defInputs) {
|
||||
if (defInputs.hasOwnProperty(key)) {
|
||||
obj = extInputs[key];
|
||||
if (!obj) {
|
||||
extInputs[key] = defInputs[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return extInputs;
|
||||
};
|
||||
|
||||
var renderBlock = function (name, inputs, outputs, populate) {
|
||||
//initializeTemplate({name:name, params:null}, jagg);
|
||||
|
||||
var init, fn, blok, log = new Log();
|
||||
|
||||
fn = template(name);
|
||||
if (!fn) {
|
||||
log.error("Template header and footer includes are missing for : " + name);
|
||||
}
|
||||
if (populate) {
|
||||
blok = block(name);
|
||||
|
||||
if (!inputs) {
|
||||
inputs = blok.getInputs ? blok.getInputs() : {};
|
||||
} else {
|
||||
mergeParams(inputs, blok.getInputs ? blok.getInputs() : null);
|
||||
}
|
||||
|
||||
if (blok.getOutputs) {
|
||||
outputs = blok.getOutputs(inputs);
|
||||
} else if (blok.getInputs) {
|
||||
outputs = inputs;
|
||||
} else {
|
||||
outputs = {};
|
||||
}
|
||||
}
|
||||
init = initializer(name);
|
||||
if (init.postInitialize) {
|
||||
init.postInitialize(inputs, outputs);
|
||||
}
|
||||
fn(inputs, outputs, jagg);
|
||||
};
|
||||
|
||||
var inheritParent = function (blok, name) {
|
||||
var parent = require(getBlockFile(name));
|
||||
for (var prop in parent) {
|
||||
if (parent.hasOwnProperty(prop)) {
|
||||
if (!blok[prop]) {
|
||||
blok[prop] = parent[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var initializeBlock = function (obj) {
|
||||
if (!obj) {
|
||||
return;
|
||||
}
|
||||
var extInputs, defInputs, parent, tmpl, inputBlocks, outputBlocks, outputs, tmplInitializer, bloks, i, length,
|
||||
name = obj.name, blok = block(name), log = new Log();
|
||||
|
||||
template(name);
|
||||
extInputs = obj.inputs || (obj.inputs = {});
|
||||
defInputs = blok.getInputs ? blok.getInputs() : {};
|
||||
mergeParams(extInputs, defInputs);
|
||||
|
||||
if (blok.getInputBlocks) {
|
||||
inputBlocks = blok.getInputBlocks();
|
||||
length = inputBlocks.length;
|
||||
for (i = 0; i < length; i++) {
|
||||
initializeBlocks(inputBlocks[i], extInputs);
|
||||
}
|
||||
}
|
||||
|
||||
if (blok.getOutputs) {
|
||||
outputs = blok.getOutputs(extInputs);
|
||||
} else if (blok.getInputs) {
|
||||
outputs = extInputs;
|
||||
} else {
|
||||
outputs = {};
|
||||
}
|
||||
|
||||
obj.outputs = outputs;
|
||||
if (blok.getOutputBlocks) {
|
||||
outputBlocks = blok.getOutputBlocks();
|
||||
length = outputBlocks.length;
|
||||
for (i = 0; i < length; i++) {
|
||||
initializeBlocks(outputBlocks[i], outputs);
|
||||
}
|
||||
}
|
||||
|
||||
if (blok.getStaticBlocks) {
|
||||
bloks = blok.getStaticBlocks();
|
||||
length = bloks.length;
|
||||
for (i = 0; i < length; i++) {
|
||||
initializeBlock({name:bloks[i], inputs:null});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// [ "foo", "bar", "mar"]
|
||||
// [{ "name" : "foo/bar", params : {}}]
|
||||
var initializeBlocks = function (keys, inputs) {
|
||||
if (!inputs) {
|
||||
return;
|
||||
}
|
||||
var i, length, values, last;
|
||||
if (typeof keys !== "string") {
|
||||
length = keys.length;
|
||||
values = inputs[keys[0]];
|
||||
last = (length == 1);
|
||||
if (values instanceof Array) {
|
||||
length = values.length;
|
||||
for (i = 0; i < length; i++) {
|
||||
if (last) {
|
||||
initializeBlock(values[i]);
|
||||
} else {
|
||||
initializeBlocks(keys.slice(1), values[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (last) {
|
||||
initializeBlock(values);
|
||||
} else {
|
||||
initializeBlocks(keys.slice(1), values);
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
values = inputs[keys];
|
||||
}
|
||||
|
||||
if (values instanceof Array) {
|
||||
length = values.length;
|
||||
for (i = 0; i < length; i++) {
|
||||
initializeBlock(values[i]);
|
||||
}
|
||||
} else {
|
||||
initializeBlock(values);
|
||||
}
|
||||
};
|
||||
|
||||
var insertData = function (jagg, template, parent, name, key, value) {
|
||||
var keys, values, data = getData();
|
||||
data = data[parent] || (data[parent] = {});
|
||||
data = data[name] || (data[name] = {});
|
||||
data = data[template] || (data[template] = {});
|
||||
|
||||
keys = data.keys || (data.keys = []);
|
||||
values = data.values || (data.values = {});
|
||||
|
||||
keys.push(key);
|
||||
values[key] = value;
|
||||
};
|
||||
|
||||
|
||||
var printData = function (tmpls) {
|
||||
var key, tmpl, keys, values, i, length;
|
||||
for (key in tmpls) {
|
||||
if (tmpls.hasOwnProperty(key)) {
|
||||
tmpl = tmpls[key];
|
||||
keys = tmpl.keys;
|
||||
values = tmpl.values;
|
||||
length = keys.length;
|
||||
for (i = 0; i < length; i++) {
|
||||
print(values[keys[i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var getUrlMapping = function (path) {
|
||||
var urlMap = ctx.get("url.map"), url, configs, i, length, mapping, mappings, file;
|
||||
if (urlMap) {
|
||||
url = urlMap[path];
|
||||
return url ? url : path;
|
||||
}
|
||||
file = new File("/jaggery.conf");
|
||||
file.open("r");
|
||||
configs = parse(file.readAll());
|
||||
file.close();
|
||||
|
||||
urlMap = {};
|
||||
mappings = configs.urlMappings;
|
||||
length = mappings.length;
|
||||
for (i = 0; i < length; i++) {
|
||||
mapping = mappings[i];
|
||||
urlMap[mapping.path] = mapping.url;
|
||||
}
|
||||
ctx.put("url.map", urlMap);
|
||||
url = urlMap[path];
|
||||
return url ? url : path;
|
||||
};
|
||||
|
||||
var getRequestSegments = function(){
|
||||
var href = request.getRequestURL()
|
||||
var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)(\/[^?#]*)(\?[^#]*|)(#.*|)$/);
|
||||
return match && {
|
||||
protocol: match[1],
|
||||
host: match[2],
|
||||
hostname: match[3],
|
||||
port: match[4],
|
||||
pathname: match[5],
|
||||
search: match[6],
|
||||
hash: match[7]
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
var getMappedUrl = function (path) {
|
||||
return getAbsoluteUrl(getUrlMapping(path));
|
||||
};
|
||||
|
||||
var getAbsoluteUrl = function (path) {
|
||||
var host = ""
|
||||
if(isReverseProxyEnabled()){
|
||||
host = "https://" + site.reverseProxy.host ;
|
||||
}else{
|
||||
var match = getRequestSegments();
|
||||
var host = match.protocol + "//" + match.host;
|
||||
}
|
||||
return host + url(path);
|
||||
};
|
||||
|
||||
var getSiteDomainFromRequest = function(){
|
||||
var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)(\/[^?#]*)(\?[^#]*|)(#.*|)$/);
|
||||
}
|
||||
|
||||
var getHttpsUrl = function(path, parameters){
|
||||
var hostname = "";
|
||||
var requestSegments = getRequestSegments();
|
||||
mod = jagg.module("manager");
|
||||
hostname = mod.getHTTPsURL();
|
||||
hostname = hostname.replace("https://","");
|
||||
|
||||
// if the site is fronted by a proxy server
|
||||
if(isReverseProxyEnabled()){
|
||||
hostname = site.reverseProxy.host ;
|
||||
//if a custom https port is used
|
||||
if(site.reverseProxy.hosts_port){
|
||||
hostname = hostname + ":" + site.reverseProxy.hosts_port;
|
||||
}
|
||||
}
|
||||
|
||||
return "https://" + hostname + url(path, parameters);
|
||||
}
|
||||
|
||||
var url = function(path,parameters){
|
||||
var tail = "";
|
||||
if(parameters){
|
||||
var params = [];
|
||||
for (var key in parameters) {
|
||||
params.push(key+"="+parameters[key]) ;
|
||||
}
|
||||
if(/\?/.test(path)){
|
||||
tail = "&";
|
||||
}
|
||||
else{
|
||||
tail = "?";
|
||||
}
|
||||
tail = tail + params.join("&");
|
||||
}
|
||||
return getSiteContext()+ path + tail;
|
||||
};
|
||||
|
||||
// following function will generate a url with the currently activated tenant
|
||||
var urlTenanted = function(path, parameters){
|
||||
//if tenented add tenant url
|
||||
if(getTenantDomain() != null && !(/(\?tenant\=|\&tenant\=)/i.test(path))){
|
||||
if(!parameters){
|
||||
parameters = {};
|
||||
}
|
||||
parameters.tenant = getTenantDomain();
|
||||
}
|
||||
if(isReverseProxyEnabled()){
|
||||
return getHttpsUrl(path, parameters)
|
||||
}
|
||||
return url(path,parameters)
|
||||
};
|
||||
|
||||
var getCarbonProxyContextPath = function(){
|
||||
var CarbonUtils = Packages.org.wso2.carbon.utils.CarbonUtils;
|
||||
var carbonUtils = new CarbonUtils();
|
||||
var context = carbonUtils.getServerConfiguration().getFirstProperty("ProxyContextPath");
|
||||
if(context != null)
|
||||
return context;
|
||||
else
|
||||
return '';
|
||||
};
|
||||
|
||||
var isReverseProxyEnabled = function(){
|
||||
if(reverse_proxy != undefined){
|
||||
return reverse_proxy;
|
||||
}
|
||||
if(site.reverseProxy.enabled){
|
||||
if(site.reverseProxy.enabled == "auto"){
|
||||
var xfwd = request.getHeader("X-Forwarded-Host");
|
||||
if(xfwd != null){
|
||||
var xfwd = xfwd.split(",")[0];
|
||||
//if(xfwd.trim() == site.reverseProxy.host){
|
||||
reverse_proxy = true;
|
||||
site.reverseProxy.host = xfwd.trim();
|
||||
//}
|
||||
}else{
|
||||
reverse_proxy = false;
|
||||
}
|
||||
}
|
||||
else{
|
||||
reverse_proxy = true;
|
||||
}
|
||||
}
|
||||
else{
|
||||
reverse_proxy = false;
|
||||
}
|
||||
return reverse_proxy;
|
||||
};
|
||||
|
||||
var getSiteContext = function(){
|
||||
if(isReverseProxyEnabled()){
|
||||
//If we use a custom domain mapping we will not use the context.
|
||||
if( site.reverseProxy.tenantHeader != null &&
|
||||
request.getHeader(site.reverseProxy.tenantHeader) != null){
|
||||
return "";
|
||||
}
|
||||
else{
|
||||
return site.reverseProxy.context
|
||||
}
|
||||
}
|
||||
|
||||
var proxyContext = getCarbonProxyContextPath();
|
||||
return proxyContext + site.context;
|
||||
};
|
||||
|
||||
var getRegistryPath = function(path){
|
||||
if(isReverseProxyEnabled()){
|
||||
if(site.reverseProxy.regContext != undefined){
|
||||
return site.reverseProxy.regContext + path;
|
||||
}
|
||||
return site.reverseProxy.context + path;
|
||||
}
|
||||
|
||||
var ProxyContextPath = getCarbonProxyContextPath();
|
||||
return ProxyContextPath + path;
|
||||
}
|
||||
|
||||
var module = function (name, module) {
|
||||
if (module) {
|
||||
return modules[name] = module;
|
||||
}
|
||||
module = modules[name];
|
||||
if (module) {
|
||||
return module;
|
||||
}
|
||||
include(getModuleFile(name));
|
||||
return modules[name];
|
||||
};
|
||||
|
||||
var requir = function (path) {
|
||||
var obj = requirs[path];
|
||||
return obj ? obj : requirs[path] = require(path);
|
||||
};
|
||||
|
||||
var block = function (name, blok) {
|
||||
var parent;
|
||||
if (blok) {
|
||||
return bloks[name] = blok;
|
||||
}
|
||||
blok = bloks[name];
|
||||
if (blok) {
|
||||
return blok;
|
||||
}
|
||||
//we need to include and initialize
|
||||
include(getBlockFile(name));
|
||||
blok = bloks[name];
|
||||
parent = blok.getParent;
|
||||
if (parent) {
|
||||
parent = parent();
|
||||
inheritParent(blok, parent);
|
||||
}
|
||||
if (blok.initialize) {
|
||||
//TODO which to pass into initialize method
|
||||
blok.initialize(getData());
|
||||
}
|
||||
return bloks[name];
|
||||
};
|
||||
|
||||
var template = function (name, tmpl) {
|
||||
var blok, parent, init;
|
||||
if (tmpl) {
|
||||
return templates[name] = tmpl;
|
||||
}
|
||||
tmpl = templates[name];
|
||||
if (tmpl) {
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
blok = block(name);
|
||||
parent = blok.getParent;
|
||||
if (parent) {
|
||||
name = parent();
|
||||
}
|
||||
|
||||
tmpl = templates[name];
|
||||
if (tmpl) {
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
include(getTemplateFile(name));
|
||||
init = initializer(name);
|
||||
if (init.preInitialize) {
|
||||
init.preInitialize();
|
||||
}
|
||||
return templates[name];
|
||||
};
|
||||
|
||||
var initializer = function (name, init) {
|
||||
var blok, parent;
|
||||
if (init) {
|
||||
return initializers[name] = init;
|
||||
}
|
||||
init = initializers[name];
|
||||
if (init) {
|
||||
return init;
|
||||
}
|
||||
|
||||
blok = block(name);
|
||||
parent = blok.getParent;
|
||||
if (parent) {
|
||||
name = parent();
|
||||
}
|
||||
|
||||
init = initializers[name];
|
||||
if (init) {
|
||||
return init;
|
||||
}
|
||||
|
||||
include(getInitializerFile(name));
|
||||
return initializers[name];
|
||||
};
|
||||
|
||||
var render = function (obj) {
|
||||
var init, fn, inputs, outputs, name = obj.name, log = new Log(), blok;
|
||||
setData(obj);
|
||||
initializeBlock(obj);
|
||||
include(getTemplateFile(name));
|
||||
fn = template(name);
|
||||
if (!fn) {
|
||||
log.error("Template header and footer includes are missing for : " + name);
|
||||
}
|
||||
inputs = obj.inputs;
|
||||
blok = block(name);
|
||||
if (blok.getOutputs) {
|
||||
outputs = blok.getOutputs(inputs);
|
||||
} else if (blok.getInputs) {
|
||||
outputs = inputs;
|
||||
} else {
|
||||
outputs = {};
|
||||
}
|
||||
init = initializer(name);
|
||||
if (init.postInitialize) {
|
||||
init.postInitialize(inputs, outputs);
|
||||
}
|
||||
fn(inputs, outputs, jagg);
|
||||
};
|
||||
|
||||
var includeBlock = function (name, inputs) {
|
||||
renderBlock(name, inputs, null, true);
|
||||
};
|
||||
|
||||
var includeBlocks = function (bloks) {
|
||||
if (!bloks) {
|
||||
return;
|
||||
}
|
||||
|
||||
var i, d, length;
|
||||
if (bloks instanceof Array) {
|
||||
length = bloks.length;
|
||||
for (i = 0; i < length; i++) {
|
||||
d = bloks[i];
|
||||
renderBlock(d.name, d.inputs, d.outputs, false);
|
||||
}
|
||||
} else {
|
||||
renderBlock(bloks.name, bloks.inputs, bloks.outputs, false);
|
||||
}
|
||||
};
|
||||
|
||||
var addHeaderCSS = function (template, key, css) {
|
||||
css = '<link type="text/css" rel="stylesheet" href="' + url(getThemeFile(css)) + '"/>';
|
||||
insertData(this, template, "header", "css", key, css);
|
||||
};
|
||||
|
||||
var addHeaderCSSCode = function (template, key, css) {
|
||||
css = '<style type="text/css">' + css + '</style>';
|
||||
insertData(this, template, "header", "css", key, css);
|
||||
};
|
||||
|
||||
var addHeaderJS = function (template, key, js) {
|
||||
js = '<script type="text/javascript" src="' + url(getThemeFile(js)) + '"></script>\n';
|
||||
insertData(this, template, "header", "js", key, js);
|
||||
};
|
||||
|
||||
var addHeaderJSCode = function (template, key, js) {
|
||||
js = '<script type="text/javascript">' + js + '</script>';
|
||||
insertData(this, template, "header", "js", key, js);
|
||||
};
|
||||
|
||||
var addHeaderCode = function (template, key, code) {
|
||||
insertData(this, template, "header", "code", key, code);
|
||||
};
|
||||
|
||||
var addFooterCSS = function (template, key, css) {
|
||||
css = '<link type="text/css" rel="stylesheet" href="' + url(getThemeFile(css)) + '"/>';
|
||||
insertData(this, template, "footer", "css", key, css);
|
||||
};
|
||||
|
||||
var addFooterCSSCode = function (template, key, css) {
|
||||
css = '<style type="text/css">' + css + '</style>';
|
||||
insertData(this, template, "footer", "css", key, css);
|
||||
};
|
||||
|
||||
var addFooterJS = function (template, key, js) {
|
||||
js = '\t<script type="text/javascript" src="' + url(getThemeFile(js)) + '"></script>\n';
|
||||
insertData(this, template, "footer", "js", key, js);
|
||||
};
|
||||
|
||||
var addFooterJSCode = function (template, key, js) {
|
||||
js = '<script type="text/javascript">' + js + '</script>';
|
||||
insertData(this, template, "footer", "js", key, js);
|
||||
};
|
||||
|
||||
var addFooterCode = function (template, key, code) {
|
||||
insertData(this, template, "footer", "code", key, code);
|
||||
};
|
||||
|
||||
var includeJag = function (path) {
|
||||
include(getThemeFile(path));
|
||||
};
|
||||
|
||||
var getTenantDomain = function(){
|
||||
if(isReverseProxyEnabled()){
|
||||
// check if tenant header exists
|
||||
if(site.reverseProxy.tenantHeader != undefined && site.reverseProxy.tenantHeader != null
|
||||
&& request.getHeader(site.reverseProxy.tenantHeader) != null){
|
||||
return request.getHeader(site.reverseProxy.tenantHeader);
|
||||
}
|
||||
}
|
||||
return request.getParameter("tenant");
|
||||
}
|
||||
|
||||
var setCSRFToken = function(){
|
||||
var cookie = request.getCookie("csrftoken");
|
||||
var user = jagg.getUser();
|
||||
var csrfuser = session.get('csrfuser');
|
||||
//set CSRF if it is not set + you need to refresh the token if the user has changed.
|
||||
if( !cookie || user != csrfuser){
|
||||
//Use a secure random as the CSRF token.
|
||||
var SecureRandom = Packages.java.security.SecureRandom;
|
||||
var random = new SecureRandom();
|
||||
var BigInteger = Packages.java.math.BigInteger;
|
||||
var token = new BigInteger(130, random).toString(32);
|
||||
|
||||
var cookie= {'name':'csrftoken','value': token , 'maxAge': 86400, 'path':"/"};
|
||||
session.put('csrfuser',user);
|
||||
response.addCookie(cookie);
|
||||
}
|
||||
}
|
||||
|
||||
var isCSRFTokenValid = function(){
|
||||
var log = new Log();
|
||||
var cookie = request.getCookie("csrftoken");
|
||||
var token = request.getHeader("X-CSRFToken");
|
||||
var user = jagg.getUser();
|
||||
if(cookie == null || cookie.value == token){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
log.info("CSRF Token error at "+request.getRequestURI());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var validateInputs = function(config){
|
||||
var errors = [];
|
||||
//set most used parapeters
|
||||
config.name = { type:"name"};
|
||||
config.provider = { type:"provider"};
|
||||
config.version = { type:"name"};
|
||||
|
||||
for(var key in config){
|
||||
var value = request.getParameter(key);
|
||||
if(value == null){
|
||||
if(config[key].required)
|
||||
errors.push(key);
|
||||
continue;
|
||||
}
|
||||
switch (config[key].type) {
|
||||
case "url":
|
||||
break;
|
||||
case "input":
|
||||
var regex = /([<>\"\'])/;
|
||||
if(regex.test(value)) errors.push(key);
|
||||
break;
|
||||
case "number":
|
||||
var regex = /^[0-9]*$/;
|
||||
if(!regex.test(value)) errors.push(key);
|
||||
break;
|
||||
case "safetext":
|
||||
var regex = /^[a-zA-Z0-9]*$/;
|
||||
if(!regex.test(value)) errors.push(key);
|
||||
break;
|
||||
case "uuid":
|
||||
var regex = /^[a-zA-Z0-9\-]*$/;
|
||||
if(!regex.test(value)) errors.push(key);
|
||||
break;
|
||||
case "name":
|
||||
var regex = /([~!#$;%^*+={}\|\\<>\"\'\/,])/;
|
||||
if(regex.test(value)) errors.push(key);
|
||||
break;
|
||||
case "password":
|
||||
var regex = /^[\S]{5,30}$/;
|
||||
if(!regex.test(value)) errors.push(key);
|
||||
break;
|
||||
case "email":
|
||||
var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
|
||||
if(!regex.test(value)) errors.push(key);
|
||||
break;
|
||||
case "provider":
|
||||
var regex = /([~!#$;%^*+={}\|\\<>\"\'\,])/;
|
||||
if(regex.test(value)) errors.push(key);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
if(errors.length > 0){
|
||||
return errors;
|
||||
}
|
||||
else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
setUser:setUser,
|
||||
getUser:getUser,
|
||||
block:block,
|
||||
module:module,
|
||||
initializer:initializer,
|
||||
includeBlock:includeBlock,
|
||||
includeBlocks:includeBlocks,
|
||||
render:render,
|
||||
template:template,
|
||||
require:requir,
|
||||
getAbsoluteUrl:getAbsoluteUrl,
|
||||
getMappedUrl:getMappedUrl,
|
||||
printData:printData,
|
||||
getUserTheme:getUserTheme,
|
||||
getThemeFile:getThemeFile,
|
||||
getModulesDir:getModulesDir,
|
||||
data:getData,
|
||||
addHeaderCSS:addHeaderCSS,
|
||||
addHeaderCSSCode:addHeaderCSSCode,
|
||||
addHeaderJS:addHeaderJS,
|
||||
addHeaderJSCode:addHeaderJSCode,
|
||||
addHeaderCode:addHeaderCode,
|
||||
addFooterCSS:addFooterCSS,
|
||||
addFooterCSSCode:addFooterCSSCode,
|
||||
addFooterJS:addFooterJS,
|
||||
addFooterJSCode:addFooterJSCode,
|
||||
addFooterCode:addFooterCode,
|
||||
includeJag:includeJag,
|
||||
url:url,
|
||||
urlTenanted:urlTenanted,
|
||||
getRegistryPath:getRegistryPath,
|
||||
getSiteContext:getSiteContext,
|
||||
getHttpsUrl:getHttpsUrl,
|
||||
getTenantDomain:getTenantDomain,
|
||||
setCSRFToken:setCSRFToken,
|
||||
isCSRFTokenValid:isCSRFTokenValid,
|
||||
validateInputs:validateInputs,
|
||||
};
|
||||
|
||||
}());
|
||||
%>
|
@ -0,0 +1,168 @@
|
||||
<%
|
||||
var getAuthServerURL = function() {
|
||||
return getAPIStoreObj().getAuthServerURL();
|
||||
};
|
||||
|
||||
var getHTTPsURL = function() {
|
||||
return getAPIStoreObj().getHTTPsURL(request.getRequestURL());
|
||||
};
|
||||
|
||||
var getHTTPURL = function() {
|
||||
return getAPIStoreObj().getHTTPURL();
|
||||
};
|
||||
|
||||
var getHostname = function() {
|
||||
return getAPIStoreObj().getHostName();
|
||||
};
|
||||
|
||||
var getAPIPublisherURL = function() {
|
||||
var result,log=new Log();
|
||||
var store = getAPIStoreObj();
|
||||
try {
|
||||
result = store.getAPIPublisherURL();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("getAPIPublisherURL : ");
|
||||
}
|
||||
return {
|
||||
error:false,
|
||||
url:result
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
error:true,
|
||||
message:e.message.split(":")[1]
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
var getServer = function() {
|
||||
return {
|
||||
server : "localhost",
|
||||
port : "9443"
|
||||
};
|
||||
};
|
||||
|
||||
var isSelfSignupEnabled = function(){
|
||||
return getAPIStoreObj().isSelfSignupEnabled();
|
||||
};
|
||||
|
||||
var isSelfSignupEnabledForTenantUser = function(tenantDomain){
|
||||
var log = new Log();
|
||||
try {
|
||||
|
||||
if (tenantDomain == null) {
|
||||
return getAPIStoreObj().isSelfSignupEnabledForTenant("carbon.super");
|
||||
} else {
|
||||
return getAPIStoreObj().isSelfSignupEnabledForTenant(tenantDomain);
|
||||
}
|
||||
} catch (e) {
|
||||
log.error(e.message);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
var getAdminCookie = function() {
|
||||
//TODO : this should be set in the Context during the deployment
|
||||
|
||||
};
|
||||
|
||||
var getAPIStoreObj = function() {
|
||||
|
||||
var tenantDomain = jagg.getTenantDomain();
|
||||
var user = jagg.getUser();
|
||||
var store;
|
||||
if (user == null) {
|
||||
store = require('apimstore');
|
||||
var storeHostObj = new store.APIStore();
|
||||
if(tenantDomain != null && tenantDomain != ""){
|
||||
storeHostObj.loadRegistryOfTenant(tenantDomain);
|
||||
}
|
||||
return storeHostObj;
|
||||
} else {
|
||||
store = require('apimstore');
|
||||
return new store.APIStore(user.username);
|
||||
}
|
||||
};
|
||||
|
||||
var getAPIConsumerObj = function() {
|
||||
var user = jagg.getUser();
|
||||
|
||||
var APIManagerFactory = Packages.org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
||||
|
||||
return APIManagerFactory.getInstance().getAPIConsumer(user);
|
||||
};
|
||||
|
||||
var loadTenantRegistry = function (tenantDomain) {
|
||||
try {
|
||||
if (tenantDomain != null && tenantDomain != "") {
|
||||
getAPIStoreObj().loadRegistryOfTenant(tenantDomain);
|
||||
return
|
||||
{
|
||||
error:false
|
||||
}
|
||||
;
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
error:true,
|
||||
message:e.message
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
var loadTenantAxisConfiguration = function (tenantDomain) {
|
||||
|
||||
try {
|
||||
if (tenantDomain != null && tenantDomain != "") {
|
||||
|
||||
getAPIStoreObj().loadAxisConfigOfTenant(String(tenantDomain));
|
||||
return
|
||||
{
|
||||
error:false
|
||||
}
|
||||
;
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
error:true,
|
||||
message:e.message
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
var getActiveTenantDomains=function(){
|
||||
var tenantDomains,
|
||||
log = new Log(),
|
||||
store = jagg.module("manager").getAPIStoreObj();
|
||||
|
||||
try {
|
||||
tenantDomains = store.getActiveTenantDomains();
|
||||
tenantDomains = parse(stringify(tenantDomains));
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("isTenantMode : " + stringify(api));
|
||||
}
|
||||
|
||||
return {
|
||||
error:false,
|
||||
tenantDomains:tenantDomains
|
||||
};
|
||||
} catch (e) {
|
||||
//log.error(e.message);
|
||||
return {
|
||||
error:true,
|
||||
tenantDomains:null,
|
||||
message:e.message
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
var getUsageClient = function(){
|
||||
var user = jagg.getUser();
|
||||
return org.wso2.carbon.apimgt.usage.client.UsageClient.getClient(user);
|
||||
}
|
||||
|
||||
%>
|
@ -0,0 +1,10 @@
|
||||
<module name="apimstore" namespace="ns" expose="true" xmlns="http://wso2.org/projects/jaggery/module.xml">
|
||||
<hostObject>
|
||||
<className>org.wso2.carbon.apimgt.hostobjects.APIStoreHostObject</className>
|
||||
<name>APIStore</name>
|
||||
</hostObject>
|
||||
<hostObject>
|
||||
<className>org.wso2.carbon.apimgt.hostobjects.MutualAuthHostObject</className>
|
||||
<name>MutualAuthHostObject</name>
|
||||
</hostObject>
|
||||
</module>
|
Loading…
Reference in new issue