var Describe = function() {
//20180730
}
Describe.splitValue = function(str) {
if (str) {
if (str.length > 0) {
return str.split(',');
}
}
return [];
}
Describe.joinValue = function(o, split) {
var s = ',';
if (split) {
s = '' + split;
}
if (o) {
if (toString.apply(o) === '[object Array]') {
return o.join(s);
}
}
return o;
}
Describe.doPost = function(address, m, data) {
$('#url').html(address);
if ((m.resultContentType) && m.resultContentType.indexOf('application/json') > -1) {
$.ajax({
url : address,
type : 'POST',
headers : {
'x-debugger' : true
},
contentType : (m.contentType.indexOf('application/json') > -1) ? m.contentType : false,
processData : (m.contentType.indexOf('application/json') > -1) ? true : false,
xhrFields : {
'withCredentials' : true
},
data : ((m.contentType.indexOf('application/json') > -1) ? JSON.stringify(data) : data)
}).always(function(resultJson) {
$('#result').html(JSON.stringify(resultJson, null, 4));
Describe.writeOut(m.outs, resultJson);
});
} else {
$.ajax({
url : address,
type : 'POST',
headers : {
'x-debugger' : true
},
contentType : (m.contentType.indexOf('application/json') > -1) ? m.contentType : false,
processData : (m.contentType.indexOf('application/json') > -1) ? true : false,
xhrFields : {
'withCredentials' : true
},
data : ((m.contentType.indexOf('application/json') > -1) ? JSON.stringify(data) : data)
});
}
}
Describe.doPut = function(address, m, data) {
$('#url').html(address);
if ((m.resultContentType) && m.resultContentType.indexOf('application/json') > -1) {
$.ajax({
url : address,
type : 'PUT',
headers : {
'x-debugger' : true
},
contentType : (m.contentType.indexOf('application/json') > -1) ? m.contentType : false,
processData : (m.contentType.indexOf('application/json') > -1) ? true : false,
xhrFields : {
'withCredentials' : true
},
data : ((m.contentType.indexOf('application/json') > -1) ? JSON.stringify(data) : data)
}).always(function(resultJson) {
$('#result').html(JSON.stringify(resultJson, null, 4));
Describe.writeOut(m.outs, resultJson);
});
} else {
$.ajax({
url : address,
type : 'PUT',
headers : {
'x-debugger' : true
},
contentType : (m.contentType.indexOf('application/json') > -1) ? m.contentType : false,
processData : (m.contentType.indexOf('application/json') > -1) ? true : false,
xhrFields : {
'withCredentials' : true
},
data : ((m.contentType.indexOf('application/json') > -1) ? JSON.stringify(data) : data)
});
}
}
Describe.doGet = function(address, m) {
$('#url').html(address);
if ((m.resultContentType) && m.resultContentType.indexOf('application/json') > -1) {
$.ajax({
type : 'GET',
dataType : 'json',
url : address,
headers : {
'x-debugger' : true
},
contentType : m.contentType,
xhrFields : {
'withCredentials' : true
},
crossDomain : true
}).always(function(resultJson) {
$('#result').html(JSON.stringify(resultJson, null, 4));
Describe.writeOut(m.outs, resultJson);
});
} else {
window.open(address, '_blank');
}
}
Describe.doDelete = function(address, m) {
$('#url').html(address);
if ((m.resultContentType) && m.resultContentType.indexOf('application/json') > -1) {
$.ajax({
type : 'DELETE',
dataType : 'json',
url : address,
headers : {
'x-debugger' : true
},
contentType : m.contentType,
xhrFields : {
'withCredentials' : true
},
crossDomain : true
}).always(function(resultJson) {
$('#result').html(JSON.stringify(resultJson, null, 4));
Describe.writeOut(m.outs, resultJson);
});
} else {
$.ajax({
type : 'DELETE',
dataType : 'json',
url : address,
headers : {
'x-debugger' : true
},
contentType : m.contentType,
xhrFields : {
'withCredentials' : true
},
crossDomain : true
});
}
}
Describe.writeOut = function(outs, json) {
if (outs && (outs.length) && json && json.data) {
$.each(Object.keys(json.data), function(i, k) {
$('#out_' + k + '_out', '#outs').html(json.data[k]);
});
}
}
Describe.prototype = {
"load" : function() {
var str = '
';
$.getJSON('../describe/describe.json?rd=' + Math.random(), function(json) {
$.each(json.jaxrs, function(ji, j) {
str += '- ' + j.name;
$.each(j.methods, function(mi, m) {
str += '';
});
str += '
'
});
str += '
';
$("#menu").html(str);
$.each(json.jaxrs, function(ji, j) {
$.each(j.methods, function(mi, m) {
$('#' + j.name + '_' + m.name).click(
function() {
$('#result').html('');
var txt = '';
if (m.pathParameters && m.pathParameters.length > 0) {
txt += '';
}
if (m.formParameters && m.formParameters.length > 0) {
txt += '';
}
if (m.queryParameters && m.queryParameters.length > 0) {
txt += '';
}
if (m.ins && m.ins.length > 0) {
txt += '';
}
if (m.useJsonElementParameter) {
txt += '';
}
if (m.outs && m.outs.length > 0) {
txt += '';
}
$('#content').html(txt);
$('#' + m.name + '_' + m.type, '#method').click(function() {
var address = '../' + m.path;
if (m.pathParameters && m.pathParameters.length > 0) {
$.each(m.pathParameters, function(pi, p) {
address = address.replace('{' + p.name + '}', encodeURIComponent($('#' + p.name, '#pathParameters').val()));
});
}
if (m.queryParameters && m.queryParameters.length > 0) {
$.each(m.queryParameters, function(pi, p) {
var query = p.name + '=' + encodeURIComponent($('#' + p.name, '#queryParameters').val());
if (address.indexOf("?") > 0) {
address += '&' + query;
} else {
address += '?' + query;
}
});
}
if (m.contentType.indexOf('application/json') > -1) {
switch (m.type) {
case 'POST':
var data = {};
if (m.ins && m.ins.length > 0) {
$.each(m.ins, function(ii, i) {
switch (i.type) {
default:
if (i.isBaseType) {
if (i.isCollection) {
data[i.name] = Describe.splitValue($('#' + i.name, '#ins').val());
} else {
data[i.name] = $('#' + i.name, '#ins').val();
}
} else {
data[i.name] = $.parseJSON($('#' + i.name, '#ins').val());
}
}
});
} else if (m.useJsonElementParameter) {
data = $.parseJSON($('#jsonElement').val());
}
Describe.doPost(address, m, data);
break;
case 'PUT':
var data = {};
if (m.ins && m.ins.length > 0) {
$.each(m.ins, function(ii, i) {
switch (i.type) {
default:
if (i.isBaseType) {
if (i.isCollection) {
data[i.name] = Describe.splitValue($('#' + i.name, '#ins').val());
} else {
data[i.name] = $('#' + i.name, '#ins').val();
}
} else {
data[i.name] = $.parseJSON($('#' + i.name, '#ins').val());
}
}
});
} else if (m.useJsonElementParameter) {
data = $.parseJSON($('#jsonElement').val());
}
Describe.doPut(address, m, data);
break;
case 'GET':
Describe.doGet(address, m);
break;
case 'DELETE':
Describe.doDelete(address, m);
break;
default:
break;
}
} else {
switch (m.type) {
case 'POST':
var formData = new FormData();
if (m.formParameters && m.formParameters.length > 0) {
$.each(m.formParameters, function(pi, p) {
if (p.type == "File") {
formData.append(p.name, $('input[type=file]', '#formParameters')[0].files[0]);
} else {
formData.append(p.name, $('#' + p.name, '#formParameters').val());
}
});
}
Describe.doPost(address, m, formData);
break;
case 'PUT':
var formData = new FormData();
if (m.formParameters && m.formParameters.length > 0) {
$.each(m.formParameters, function(pi, p) {
if (p.type == "File") {
formData.append(p.name, $('input[type=file]', '#formParameters')[0].files[0]);
} else {
formData.append(p.name, $('#' + p.name, '#formParameters').val());
}
});
}
Describe.doPut(address, m, formData);
break;
case 'GET':
Describe.doGet(address, m);
break;
case 'DELETE':
Describe.doDelete(address, m);
break;
default:
break;
}
}
})
});
});
});
});
}
}