import 'rxjs/add/operator/map'; import 'rxjs/add/operator/toPromise'; import 'rxjs/Rx'; import { sdc2Config } from './../../../main'; import { Interceptor, InterceptedRequest, InterceptedResponse } from 'ng2-interceptors'; import {SharingService} from "../../services/sharing-service"; import {ReflectiveInjector} from '@angular/core'; import {Cookie2Service} from "./cookie.service"; import {UUID} from "angular2-uuid"; import {Dictionary} from "../../utils/dictionary/dictionary"; import {SEVERITY} from "../../utils/constants"; import {IServerMessageModalModel} from "../../view-models/modals/message-modal/message-server-modal/server-message-modal-view-model"; export class HttpInterceptor implements Interceptor { private cookieService: Cookie2Service; private sharingService:SharingService; constructor() { let injector = ReflectiveInjector.resolveAndCreate([Cookie2Service,SharingService]); this.cookieService = injector.get(Cookie2Service); this.sharingService = injector.get(SharingService); } public interceptBefore(request: InterceptedRequest): InterceptedRequest { /** * For every request to the server, that the service id, or resource id is sent in the URL, need to pass UUID in the header. * Check if the unique id exists in uuidMap, and if so get the UUID and add it to the header. */ request.options.headers.append(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId()); request.options.withCredentials = true; var uuidValue = this.getUuidValue(request.url); if (uuidValue != '') { request.options.headers.set('X-ECOMP-ServiceID', uuidValue); } request.options.headers.set('X-ECOMP-RequestID', UUID.UUID()); return request; } public interceptAfter(response:InterceptedResponse):InterceptedResponse { if (response.response.status !== 200 && response.response.status !== 201) { this.responseError(response.response.json()); //console.log("Error from BE:",response); } return response; } private getUuidValue = (url: string) :string => { let map:Dictionary = this.sharingService.getUuidMap(); if (map && url.indexOf(sdc2Config.api.root) > 0) { map.forEach((key:string) => { if (url.indexOf(key) !== -1) { return this.sharingService.getUuidValue(key); } }); } return ''; }; public formatMessageArrays = (message:string, variables:Array)=> { return message.replace(/\[%(\d+)\]/g, function (_, m) { let tmp = []; let list = variables[--m].split(";"); list.forEach(function (item) { tmp.push("
  • " + item + "
  • "); }); return ""; }); }; public responseError = (rejection:any)=> { let text:string; let variables; let messageId:string = ""; let isKnownException = false; if (rejection && rejection.serviceException) { text = rejection.serviceException.text; variables = rejection.serviceException.variables; messageId = rejection.serviceException.messageId; isKnownException = true; } else if (rejection && rejection.requestError && rejection.requestError.serviceException) { text = rejection.requestError.serviceException.text; variables = rejection.requestError.serviceException.variables; messageId = rejection.requestError.serviceException.messageId; isKnownException = true; } else if (rejection && rejection.requestError && rejection.requestError.policyException) { text = rejection.requestError.policyException.text; variables = rejection.requestError.policyException.variables; messageId = rejection.requestError.policyException.messageId; isKnownException = true; } else if (rejection) { text = 'Wrong error format from server'; console.error(text); isKnownException = false; } let data:IServerMessageModalModel; if (isKnownException) { // Remove the "Error: " text at the begining if (text.trim().indexOf("Error:") === 0) { text = text.replace("Error:", "").trim(); } //mshitrit DE199895 bug fix let count:number = 0; variables.forEach(function (item) { variables[count] = item ? item.replace('<', '<').replace('>', '>') : ''; count++; }); // Format the message in case has array to