index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. (function () {
  19. var gatewayUrl,
  20. _csrfToken,
  21. csrfEnabled = false,
  22. restCsrfCustomHeader,
  23. restCsrfMethodsToIgnore = [],
  24. swaggerSpecFileName = "swagger.json";
  25. window.onload = function() {
  26. const ui = SwaggerUIBundle({
  27. url: getSwaggerBaseUrl(window.location.pathname) + "/" + swaggerSpecFileName,
  28. dom_id: '#swagger-ui',
  29. deepLinking: true,
  30. presets: [
  31. SwaggerUIBundle.presets.apis,
  32. SwaggerUIStandalonePreset
  33. ],
  34. plugins: [
  35. SwaggerUIBundle.plugins.DownloadUrl
  36. ],
  37. layout: "StandaloneLayout",
  38. requestInterceptor: function(request) {
  39. if (!request.url.includes(swaggerSpecFileName)) {
  40. request.url = getAPIUrl(request.url);
  41. setCsrfHeaderToRequest(request);
  42. }
  43. return request;
  44. },
  45. docExpansion: 'none',
  46. validatorUrl: 'none'
  47. })
  48. window.ui = ui;
  49. atlasLogo = gatewayUrl + "/img/atlas_logo.svg";
  50. $('#swagger-ui img').attr("src", atlasLogo);
  51. fetchCsrfHeader();
  52. }
  53. function getSwaggerBaseUrl(url) {
  54. var path = url.replace(/\/[\w-]+.(jsp|html)|\/+$/ig, '');
  55. splitPath = path.split("/");
  56. splitPath.pop();
  57. gatewayUrl = splitPath.join("/");
  58. return window.location.origin + path;
  59. };
  60. function getAPIUrl(url) {
  61. url = new URL(url);
  62. var path = url.origin + gatewayUrl + url.pathname + url.search;
  63. return path;
  64. };
  65. function fetchCsrfHeader() {
  66. var response = getSessionDetails();
  67. if (!csrfEnabled && response['atlas.rest-csrf.enabled']) {
  68. var str = "" + response['atlas.rest-csrf.enabled'];
  69. csrfEnabled = (str.toLowerCase() == 'true');
  70. }
  71. if (!restCsrfCustomHeader && response["atlas.rest-csrf.custom-header"]) {
  72. restCsrfCustomHeader = response["atlas.rest-csrf.custom-header"].trim();
  73. }
  74. if (restCsrfMethodsToIgnore == 0 && response["atlas.rest-csrf.methods-to-ignore"]) {
  75. restCsrfMethodsToIgnore = response["atlas.rest-csrf.methods-to-ignore"].split(",");
  76. }
  77. if (csrfEnabled) {
  78. _csrfToken = response['_csrfToken'];
  79. }
  80. }
  81. function setCsrfHeaderToRequest(request) {
  82. if (csrfEnabled && !restCsrfMethodsToIgnore.includes(request.method)) {
  83. request.headers[restCsrfCustomHeader] = _csrfToken;
  84. }
  85. }
  86. function getSessionDetails() {
  87. var response;
  88. $.ajax({
  89. async : false,
  90. method: "GET",
  91. url: gatewayUrl + "/api/atlas/admin/session",
  92. dataType: 'json',
  93. success: function(result){
  94. response = result;
  95. }
  96. });
  97. return response;
  98. };
  99. })();