summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/services/http.interceptor.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'catalog-ui/src/app/ng2/services/http.interceptor.service.ts')
-rw-r--r--catalog-ui/src/app/ng2/services/http.interceptor.service.ts57
1 files changed, 57 insertions, 0 deletions
diff --git a/catalog-ui/src/app/ng2/services/http.interceptor.service.ts b/catalog-ui/src/app/ng2/services/http.interceptor.service.ts
new file mode 100644
index 0000000000..aebbdbf7af
--- /dev/null
+++ b/catalog-ui/src/app/ng2/services/http.interceptor.service.ts
@@ -0,0 +1,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 '';
+ }
+
+}