blob: 616063b9f03b249b1e5e5e52ff9a2f90112cfea8 [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 Scandolo29edc0f2018-04-26 17:19:10 +020029
30 // NOTE canceled request (as the modeldefs one) have no res
31 if (angular.isUndefined(res) || res === null) {
32 return $q.reject(res);
33 }
34
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070035 // 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 -080036 switch (res.status) {
Matteo Scandolo042ea632017-03-01 19:02:34 -080037 case 401:
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070038 case 403:
Matteo Scandolo042ea632017-03-01 19:02:34 -080039 $cookies.remove('sessionid', {path: '/'});
Matteo Scandolo042ea632017-03-01 19:02:34 -080040 return $q.reject(res);
41 default:
Matteo Scandolo42c66922017-05-01 17:24:59 -070042 return $q.reject(res);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080043 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080044 };
45
46 return {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080047 responseError: checkLogin
48 };
49}
50
51export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) {
52 return {
53 request: (req) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080054 if (!$cookies.get('sessionid')) {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080055 return req;
56 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080057 req.headers['x-sessionid'] = $cookies.get('sessionid');
58 req.headers['x-xossession'] = $cookies.get('sessionid');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080059 return req;
60 }
61 };
62}
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080063
Matteo Scandolo42c66922017-05-01 17:24:59 -070064export function NoHyperlinksInterceptor($q: ng.IQService) {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080065 return {
66 request: (req) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080067 if (req.url.indexOf('.html') === -1) {
68 // NOTE force content type to be JSON
69 req.headers['Content-Type'] = 'application/json';
Matteo Scandolo520a8a12017-03-10 17:31:37 -080070
71 if (req.method === 'PUT') {
Matteo Scandolo02229382017-04-18 11:52:23 -070072 // XosModelStore.search add this value for visualization purpose,
Matteo Scandolo520a8a12017-03-10 17:31:37 -080073 // no one should change models
74 delete req.data.modelName;
75 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080076 }
77 return req;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080078 },
79 response: (res) => {
80 try {
81 // NOTE convert res.data from string to JSON
82 res.data = JSON.parse(res.data);
83 try {
84 // NOTE chameleon return everything inside an "items" field
85 res.data = res.data.items;
86 } catch (_e) {
87 res.data = res.data;
88 }
89 } catch (e) {
90 res.data = res.data;
91 }
92 return res;
Matteo Scandolo42c66922017-05-01 17:24:59 -070093 },
94 responseError: (res) => {
95 return $q.reject(res.data);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080096 }
97 };
98}