diff options
Diffstat (limited to 'ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared')
4 files changed, 132 insertions, 40 deletions
diff --git a/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.spec.ts b/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.spec.ts index e82a7ebc..8b7f857f 100644 --- a/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.spec.ts +++ b/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/interceptors/header-interceptor.spec.ts @@ -40,10 +40,12 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { HeaderInterceptor } from './header-interceptor'; +import { HttpCacheService } from '../services/cache.service'; describe('HeaderInterceptor', () => { let component: HeaderInterceptor; let fixture: ComponentFixture<HeaderInterceptor>; + let httpCacheService: HttpCacheService; beforeEach(async(() => { TestBed.configureTestingModule({ @@ -52,7 +54,7 @@ describe('HeaderInterceptor', () => { })); beforeEach(() => { - component=new HeaderInterceptor() + component=new HeaderInterceptor(httpCacheService) }); it('should create', () => { 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 diff --git a/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/services/cache.service.ts b/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/services/cache.service.ts new file mode 100644 index 00000000..157e7346 --- /dev/null +++ b/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/services/cache.service.ts @@ -0,0 +1,59 @@ +import {HttpRequest, HttpResponse} from '@angular/common/http'; +import {Injectable} from '@angular/core'; + +abstract class HttpCache { + abstract get(req: HttpRequest<any>): HttpResponse<any> | null; + + abstract put(req: HttpRequest<any>, resp: HttpResponse<any>): void; + + abstract clearCache(): void; +} + +@Injectable() +export class HttpCacheService implements HttpCache { + + private cache = {}; + private previousIdList = {}; + private routeCache = {}; + private routeGroupSelectCache = {}; + + get(req: HttpRequest<any>): HttpResponse<any> | null { + return this.cache[req.urlWithParams]; + } + + put(req: HttpRequest<any>, resp: HttpResponse<any>): void { + this.cache[req.urlWithParams] = resp; + } + + + getPreviousId(id: string): string | null { + return this.previousIdList[id]; + } + + setPreviousId(id: string, prev: string): void { + this.previousIdList[id] = prev; + } + + getRouteCache(id: string): string | null { + return this.routeCache[id]; + } + + setRouteCache(id: string, route: string): void { + this.routeCache[id] = route; + } + + getRouteGroupCache(id: string): string | null { + return this.routeGroupSelectCache[id]; + } + + setRouteGroupCache(id: string, group: string): void { + this.routeGroupSelectCache[id] = group; + } + + clearCache(): void { + this.cache = {}; + this.previousIdList = {}; + this.routeCache = {}; + this.routeGroupSelectCache = {}; + } +} diff --git a/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/services/header/header.service.ts b/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/services/header/header.service.ts index cd33e303..7290bf09 100644 --- a/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/services/header/header.service.ts +++ b/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/shared/services/header/header.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { environment } from '../../../../environments/environment'; import { HttpClient } from '@angular/common/http'; +import { Observable } from 'rxjs'; @Injectable({ @@ -15,4 +16,9 @@ export class HeaderService { return this.http.get(environment.getTopMenu,{ withCredentials: true }); } + doLogout():Observable<any>{ + return this.http.get(environment.baseUrl+"logout"); + + } + } |