summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/http-interceptor/headers-interceptor.ts
blob: 00e2fd8fcd7ae57de2916209c11c4b060c0ace74 (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
58
59
60
61
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable, Injector } from '@angular/core';
import { SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
import { ButtonType } from 'onap-ui-angular/dist/common';
import { Observable } from 'rxjs/Observable';
import { ServerErrorResponse } from '../../models/server-error-response';
import { Cookie2Service } from '../services/cookie.service';
import { HttpHelperService } from '../services/http-hepler.service';
import { TranslateService } from '../shared/translator/translate.service';

@Injectable()
export class HeadersInterceptor implements HttpInterceptor {

    constructor(private injector: Injector, private cookieService: Cookie2Service, private httpHelperService: HttpHelperService) {}

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        let authReq = req.clone({ headers: req.headers.set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
            .set('Content-Type', 'application/json; charset=UTF-8')
            .set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
            .set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
        });

        const uuidValue = this.httpHelperService.getUuidValue(authReq.url);
        if (uuidValue !== '') {
            authReq = authReq.clone({ headers: authReq.headers.set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())});
        }
        return next.handle(authReq).do(

            (event: HttpEvent<any>) => { /* Do Nothing */ },

            (err: any) => {
                if (err instanceof HttpErrorResponse) {
                    const errorResponse: ServerErrorResponse = new ServerErrorResponse(err);
                    const modalService = this.injector.get(SdcUiServices.ModalService);
                    const translateService = this.injector.get(TranslateService);

                    const errorDetails = {
                        'Error Code': errorResponse.messageId,
                        'Status Code': errorResponse.status
                    };

                    if (errorResponse.ecompRequestId) {
                        errorDetails['Transaction ID'] = errorResponse.ecompRequestId;
                    }

                    if (errorResponse.messageId === 'POL5005') {
                        // Session and Role expiration special handling
                        modalService.openWarningModal(
                            'Warning',
                            translateService.translate('ERROR_MODAL_TEXT', errorResponse),
                            'warn-modal',
                            [ ] );
                    } else {
                        modalService.openErrorDetailModal('Error', errorResponse.message, 'error-modal', errorDetails);
                    }

                    return Observable.throwError(err);
                }
            });
    }
}