2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 import { SharingService } from 'app/services-ng2';
23 import { IAppConfigurtaion } from '../models/app-config';
24 import { ServerErrorResponse } from '../models/server-error-response';
25 import { Dictionary } from '../utils/dictionary/dictionary';
27 // Method name should be exactly "response" - http://docs.angularjs.org/api/ng/service/$http
28 export interface Interceptor {
31 responseError: Function;
34 export class HeaderInterceptor implements Interceptor {
35 public static $inject = [
39 'Sdc.Services.SharingService',
44 constructor(private $injector: ng.auto.IInjectorService,
45 private $q: ng.IQService,
47 private sharingService: SharingService,
48 private sdcConfig: IAppConfigurtaion,
49 private $location: ng.ILocationService) {
52 public request = (requestSuccess): ng.IPromise<any> => {
53 requestSuccess.headers['X-ECOMP-RequestID'] = this.uuid4.generate();
55 * 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.
56 * Check if the unique id exists in uuidMap, and if so get the UUID and add it to the header.
58 const map: Dictionary<string, string> = this.sharingService.getUuidMap();
59 if (map && requestSuccess.url.indexOf(this.sdcConfig.api.root) === 0) {
60 map.forEach((key: string) => {
61 if (requestSuccess.url.indexOf(key) !== -1) {
62 requestSuccess.headers['X-ECOMP-ServiceID'] = this.sharingService.getUuidValue(key);
66 return requestSuccess;
69 public response = (responseSuccess): ng.IPromise<any> => {
70 const responseData = responseSuccess.data;
72 const data = JSON.stringify(responseData);
73 if (data && (data.indexOf('Global Logon: Login') > 0)) {
74 this.$location.path('dashboard/welcome');
75 window.location.reload();
78 return responseSuccess;
81 public responseError = (response): ng.IPromise<any> => {
82 const errorResponse: ServerErrorResponse = new ServerErrorResponse(response, true);
83 const modalService = this.$injector.get('ModalServiceSdcUI');
85 const errorDetails = {
86 'Error Code': errorResponse.messageId,
87 'Status Code': errorResponse.status
89 if (errorResponse.ecompRequestId) {
90 errorDetails['Transaction ID'] = errorResponse.ecompRequestId;
92 modalService.openErrorDetailModal('Error', errorResponse.message, 'error-modal', errorDetails);
93 return this.$q.reject(errorResponse);