blob: 7473226fcb221ce26825e33d7f74187ac5606bff [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080019/// <reference path="../typings/index.d.ts" />
20
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080021export function interceptorConfig($httpProvider: angular.IHttpProvider, $resourceProvider: angular.resource.IResourceServiceProvider) {
22 $httpProvider.interceptors.push('UserStatusInterceptor');
23 $httpProvider.interceptors.push('CredentialsInterceptor');
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080024 $httpProvider.interceptors.push('NoHyperlinksInterceptor');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080025}
26
Matteo Scandoloebe5a222017-02-27 11:09:26 -080027export function userStatusInterceptor($state: angular.ui.IStateService, $cookies: ng.cookies.ICookiesService, $q: ng.IQService) {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080028 const checkLogin = (res) => {
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070029 // NOTE this interceptor may never be called as the request is not rejected byt the "model-discoverer" service
Matteo Scandolo042ea632017-03-01 19:02:34 -080030 switch (res.status) {
31 case -1:
32 case 401:
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070033 case 403:
Matteo Scandolo042ea632017-03-01 19:02:34 -080034 $cookies.remove('sessionid', {path: '/'});
35 $state.go('login');
36 return $q.reject(res);
37 default:
Matteo Scandolo42c66922017-05-01 17:24:59 -070038 return $q.reject(res);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080039 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080040 };
41
42 return {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080043 responseError: checkLogin
44 };
45}
46
47export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) {
48 return {
49 request: (req) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080050 if (!$cookies.get('sessionid')) {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080051 return req;
52 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080053 req.headers['x-sessionid'] = $cookies.get('sessionid');
54 req.headers['x-xossession'] = $cookies.get('sessionid');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080055 return req;
56 }
57 };
58}
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080059
Matteo Scandolo42c66922017-05-01 17:24:59 -070060export function NoHyperlinksInterceptor($q: ng.IQService) {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080061 return {
62 request: (req) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080063 if (req.url.indexOf('.html') === -1) {
64 // NOTE force content type to be JSON
65 req.headers['Content-Type'] = 'application/json';
Matteo Scandolo520a8a12017-03-10 17:31:37 -080066
67 if (req.method === 'PUT') {
Matteo Scandolo02229382017-04-18 11:52:23 -070068 // XosModelStore.search add this value for visualization purpose,
Matteo Scandolo520a8a12017-03-10 17:31:37 -080069 // no one should change models
70 delete req.data.modelName;
71 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080072 }
73 return req;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080074 },
75 response: (res) => {
76 try {
77 // NOTE convert res.data from string to JSON
78 res.data = JSON.parse(res.data);
79 try {
80 // NOTE chameleon return everything inside an "items" field
81 res.data = res.data.items;
82 } catch (_e) {
83 res.data = res.data;
84 }
85 } catch (e) {
86 res.data = res.data;
87 }
88 return res;
Matteo Scandolo42c66922017-05-01 17:24:59 -070089 },
90 responseError: (res) => {
91 return $q.reject(res.data);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080092 }
93 };
94}