diff options
Diffstat (limited to 'ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.ts')
-rw-r--r-- | ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.ts | 103 |
1 files changed, 64 insertions, 39 deletions
diff --git a/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.ts b/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.ts index 1539877c..6ce8524d 100644 --- a/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.ts +++ b/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.ts @@ -33,7 +33,7 @@ * * ============LICENSE_END============================================ * - * + * */ import { @@ -41,53 +41,78 @@ import { HttpInterceptor, HttpHandler, HttpRequest, - HttpHeaders, - HttpErrorResponse + HttpHeaders, HttpErrorResponse, HttpResponse, } from '@angular/common/http'; -import { Observable, throwError} from 'rxjs'; -import { v4 as uuid } from 'uuid'; -import { catchError } from 'rxjs/internal/operators/catchError'; -import { environment } from '../../../environments/environment'; +import {Observable} from 'rxjs'; +import {v4 as uuid} from 'uuid'; +import {Injectable} from '@angular/core'; +import {tap} from 'rxjs/operators'; +import {environment} from '../../../environments/environment'; +import {HttpCacheService} from '../services/cache.service'; + +declare const getWebJunctionXSRFToken: any; function nthIndex(str, pat, n) { var L = str.length, i = -1; while (n-- && i++ < L) { - i = str.indexOf(pat, i); - if (i < 0) break; + i = str.indexOf(pat, i); + if (i < 0) { + break; + } } return i; } -export class HeaderInterceptor implements HttpInterceptor { - intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { - const headerSettings: { [name: string]: string | string[]; } = {}; - headerSettings['X-ECOMP-RequestID'] = uuid(); - const requestType = req.params.get('requestType'); - if(requestType!=null && requestType==='fileUpload'){ - //headerSettings['Content-Type'] = 'multipart/form-data'; - }else{ - headerSettings['Content-Type'] = 'application/json'; - } - const newHeader = new HttpHeaders(headerSettings); - const clonedRequest = req.clone({ headers: newHeader, withCredentials: true }); - // Pass the cloned request instead of the original request to the next handle - //return next.handle(clonedRequest); +@Injectable() +export class HeaderInterceptor implements HttpInterceptor { + + constructor(private httpCacheService: HttpCacheService) { + } + + intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { + + const cachedResponse = this.httpCacheService.get(req) || null; + if (cachedResponse) { + if (req.urlWithParams.indexOf('raptor.htm?action=report.run.container&c_master=') > 0 && + req.urlWithParams.indexOf('child=true') <= 0) { + return Observable.of(cachedResponse); + } + } + + + const XSRFToken = getWebJunctionXSRFToken(); + const headerSettings: { [name: string]: string | string[]; } = {}; + headerSettings['X-ECOMP-RequestID'] = uuid(); + const requestType = req.responseType; + if (XSRFToken.name && XSRFToken.value) { + headerSettings['X-XSRF-TOKEN'] = XSRFToken.value; + } + headerSettings['Content-Type'] = 'application/json'; + + const newHeader = new HttpHeaders(headerSettings); + const clonedRequest = req.clone({headers: newHeader, withCredentials: true}); var url = ''; - return next.handle(clonedRequest).pipe( - catchError(error => { - if ( error instanceof HttpErrorResponse ) { - if ( error.status === 0 ) { // If 0(302) Redirect to Login Page - if(window.location.pathname.split('/').length > 3) { - var portNum = ( window.location.port === '' || window.location.port === '0' ) ? '' : ':'+ window.location.port; - url = window.location.protocol + "//" + window.location.hostname + portNum + window.location.pathname.substring(0, nthIndex(window.location.pathname, "/", 2) + 0) + '/login.htm'; - window.open( url, '_self' ); - } else { - window.open( environment.baseUrl + 'login.htm', '_self' ); + return next.handle(clonedRequest).pipe( + + tap(event => { + if (event instanceof HttpResponse && + req.urlWithParams.indexOf('raptor.htm?action=report.run.container&c_master=') > 0 && + req.urlWithParams.indexOf('child=true') <= 0) { + this.httpCacheService.put(req, event); + } + }, + (err: any) => { + if (err instanceof HttpErrorResponse) { + if ( err.status === 0 ) { // If 0(302) Redirect to Login Page + if(window.location.pathname.split('/').length > 3) { + var portNum = ( window.location.port === '' || window.location.port === '0' ) ? '' : ':'+ window.location.port; + url = window.location.protocol + "//" + window.location.hostname + portNum + window.location.pathname.substring(0, nthIndex(window.location.pathname, "/", 2) + 0) + '/login.htm'; + window.open( url, '_self' ); + } else { + window.open( environment.baseUrl + 'login.htm', '_self' ); + } } - } - } - return throwError(error); - }) - ); - } + } + })); + } }
\ No newline at end of file |