Provide UI support to upload csar to update service
[sdc.git] / catalog-ui / src / app / ng2 / http-interceptor / headers-interceptor.ts
1 import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
2 import { Injectable, Injector } from '@angular/core';
3 import { SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
4 import { ButtonType } from 'onap-ui-angular/dist/common';
5 import { Observable } from 'rxjs/Observable';
6 import { ServerErrorResponse } from '../../models/server-error-response';
7 import { Cookie2Service } from '../services/cookie.service';
8 import { HttpHelperService } from '../services/http-hepler.service';
9 import { TranslateService } from '../shared/translator/translate.service';
10
11 @Injectable()
12 export class HeadersInterceptor implements HttpInterceptor {
13
14     constructor(private injector: Injector, private cookieService: Cookie2Service, private httpHelperService: HttpHelperService) {}
15
16     intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
17         let authReq: HttpRequest<any>;
18         if (req.body instanceof FormData) {
19             authReq = req.clone({ headers: req.headers.set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
20                 .set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
21                 .set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
22             });
23         } else {
24             authReq = req.clone({ headers: req.headers.set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
25                 .set('Content-Type', 'application/json; charset=UTF-8')
26                 .set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
27                 .set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())
28             });
29         }
30
31         const uuidValue = this.httpHelperService.getUuidValue(authReq.url);
32         if (uuidValue !== '') {
33             authReq = authReq.clone({ headers: authReq.headers.set(this.cookieService.getUserIdSuffix(), this.cookieService.getUserId())});
34         }
35         return next.handle(authReq).do(
36
37             (event: HttpEvent<any>) => { /* Do Nothing */ },
38
39             (err: any) => {
40                 if (err instanceof HttpErrorResponse) {
41                     const errorResponse: ServerErrorResponse = new ServerErrorResponse(err);
42                     const modalService = this.injector.get(SdcUiServices.ModalService);
43                     const translateService = this.injector.get(TranslateService);
44
45                     const errorDetails = {
46                         'Error Code': errorResponse.messageId,
47                         'Status Code': errorResponse.status
48                     };
49
50                     if (errorResponse.ecompRequestId) {
51                         errorDetails['Transaction ID'] = errorResponse.ecompRequestId;
52                     }
53
54                     if (errorResponse.messageId === 'POL5005') {
55                         // Session and Role expiration special handling
56                         modalService.openWarningModal(
57                             'Warning',
58                             translateService.translate('ERROR_MODAL_TEXT', errorResponse),
59                             'warn-modal',
60                             [ ] );
61                     } else {
62                         modalService.openErrorDetailModal('Error', errorResponse.message, 'error-modal', errorDetails);
63                     }
64
65                     return Observable.throwError(err);
66                 }
67             });
68     }
69 }