2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
19 import { ReplaceAction } from '../actions/navigationActions';
20 import { AddErrorInfoAction } from '../actions/errorActions';
22 import { storeService } from './storeService';
24 const baseUri = `${ window.location.origin }`;
25 const absUrlPattern = /^https?:\/\//;
27 export const formEncode = (params: { [key: string]: string | number }) => Object.keys(params).map((key) => {
28 return encodeURIComponent(key) + '=' + encodeURIComponent(params[key].toString());
31 const wildcardToRegexp = (pattern: string) => {
32 return new RegExp('^' + pattern.split(/\*\*/).map((p) => p.split(/\*+/).map((i) => i.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')).join('^[/]')).join('.*') + '$');
35 export const getAccessPolicyByUrl = (url: string) => {
44 if (!storeService.applicationStore) return result;
46 const { state: { framework: { applicationState: { enablePolicy }, authenticationState: { policies } } } } = storeService.applicationStore!;
54 if (!enablePolicy || !policies || policies.length === 0) return result;
56 policies.forEach(p => {
57 const re = wildcardToRegexp(p.path);
59 result.GET = p.methods.get != null ? p.methods.get : result.GET ;
60 result.POST = p.methods.post != null ? p.methods.post : result.POST ;
61 result.PUT = p.methods.put != null ? p.methods.put : result.PUT ;
62 result.PATCH = p.methods.patch != null ? p.methods.patch : result.PATCH ;
63 result.DELETE = p.methods.delete != null ? p.methods.delete : result.DELETE ;
71 /** Sends a rest request to the given path.
72 * @returns The data, or null it there was any error
74 export async function requestRest<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<TData | null | undefined> {
75 const res = await requestRestExt<TData>(path, init, authenticate, isResource);
76 if (res && res.status >= 200 && res.status < 300) {
82 /** Sends a rest request to the given path and reports the server state.
83 * @returns An object with the server state, a message and the data or undefined in case of a json parse error.
85 export async function requestRestExt<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<{ status: number; message?: string; data: TData | null | undefined }> {
86 const result: { status: number; message?: string; data: TData | null } = {
90 const isAbsUrl = absUrlPattern.test(path);
91 const uri = isAbsUrl ? path : isResource ? path.replace(/\/{2,}/i, '/') : (baseUri) + ('/' + path).replace(/\/{2,}/i, '/');
96 'Content-Type': 'application/json',
97 'Accept': 'application/json',
101 if (!isAbsUrl && authenticate && storeService.applicationStore) {
102 const { state: { framework: { authenticationState: { user } } } } = storeService.applicationStore;
103 // do not request if the user is not valid
105 if (!user || !user.isValid) {
108 message: 'User is not valid or not logged in.',
113 'Authorization': `${user.tokenType} ${user.token}`,
114 //'Authorization': 'Basic YWRtaW46YWRtaW4='
118 const fetchResult = await fetch(uri, init);
120 if (fetchResult.status === 403) {
121 storeService.applicationStore && storeService.applicationStore.dispatch(new AddErrorInfoAction({ title: 'Forbidden', message:'Status: [403], access denied.' }));
125 message: 'Forbidden.',
127 } else if (fetchResult.status === 401) {
128 storeService.applicationStore && storeService.applicationStore.dispatch(new ReplaceAction(`/login?returnTo=${storeService.applicationStore.state.framework.navigationState.pathname}`));
132 message: 'Authentication requested by server.',
135 const contentType = fetchResult.headers.get('Content-Type') || fetchResult.headers.get('content-type');
136 const isJson = contentType && (contentType.toLowerCase().startsWith('application/json') || contentType.toLowerCase().startsWith('application/yang-data+json'));
138 const data = (isJson ? await fetchResult.json() : await fetchResult.text()) as TData;
141 status: fetchResult.status,
142 message: fetchResult.statusText,
148 status: fetchResult.status,
149 message: error && error.message || String(error),