aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/services/http.interceptor.service.ts
blob: aebbdbf7af94a68e60100eb4459daf143213e9fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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";

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());
            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 {
            
        return response;

    }

    private getUuidValue = (url: string) :string => {
        let map:Dictionary<string, string> = 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 '';
    }

}