Catalog alignment
[sdc.git] / catalog-ui / src / app / services / header-interceptor.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 'use strict';
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';
26
27 // Method name should be exactly "response" - http://docs.angularjs.org/api/ng/service/$http
28 export interface Interceptor {
29     request: Function;
30     response: Function;
31     responseError: Function;
32 }
33
34 export class HeaderInterceptor implements Interceptor {
35     public static $inject = [
36         '$injector',
37         '$q',
38         'uuid4',
39         'Sdc.Services.SharingService',
40         'sdcConfig',
41         '$location'
42     ];
43
44     constructor(private $injector: ng.auto.IInjectorService,
45                 private $q: ng.IQService,
46                 private uuid4: any,
47                 private sharingService: SharingService,
48                 private sdcConfig: IAppConfigurtaion,
49                 private $location: ng.ILocationService) {
50     }
51
52     public request = (requestSuccess): ng.IPromise<any> => {
53         requestSuccess.headers['X-ECOMP-RequestID'] = this.uuid4.generate();
54         /**
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.
57          */
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);
63                 }
64             });
65         }
66         return requestSuccess;
67     }
68
69     public response = (responseSuccess): ng.IPromise<any> => {
70         const responseData = responseSuccess.data;
71         if (responseData) {
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();
76             }
77         }
78         return responseSuccess;
79     }
80
81     public responseError = (response): ng.IPromise<any> => {
82         const errorResponse: ServerErrorResponse = new ServerErrorResponse(response, true);
83         const modalService = this.$injector.get('ModalServiceSdcUI');
84
85         const errorDetails = {
86             'Error Code': errorResponse.messageId,
87             'Status Code': errorResponse.status
88         };
89         if (errorResponse.ecompRequestId) {
90             errorDetails['Transaction ID'] = errorResponse.ecompRequestId;
91         }
92         modalService.openErrorDetailModal('Error', errorResponse.message, 'error-modal', errorDetails);
93         return this.$q.reject(errorResponse);
94     }
95 }