forked from community/device-mgt-core
parent
a45820e845
commit
438b7b64af
@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package io.entgra.device.mgt.core.apimgt.webapp.publisher.util;
|
||||
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIInfo.Scope;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
|
||||
import io.entgra.device.mgt.core.apimgt.webapp.publisher.exception.APIManagerPublisherException;
|
||||
import io.entgra.device.mgt.core.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SelfSyncingScopeTree {
|
||||
private static final Log log = LogFactory.getLog(SelfSyncingScopeTree.class);
|
||||
private static final PublisherRESTAPIServices publisherRESTAPIServices =
|
||||
APIPublisherDataHolder.getInstance().getPublisherRESTAPIServices();
|
||||
private final APIApplicationKey apiApplicationKey;
|
||||
private final AccessTokenInfo accessTokenInfo;
|
||||
private final Map<String, SelfSyncingScopeTree> childTrees = new ConcurrentHashMap<>();
|
||||
private String pathKey;
|
||||
private Scope scope;
|
||||
|
||||
public SelfSyncingScopeTree(Scope scope, String permission, APIApplicationKey apiApplicationKey,
|
||||
AccessTokenInfo accessTokenInfo) throws APIManagerPublisherException {
|
||||
this.apiApplicationKey = apiApplicationKey;
|
||||
this.accessTokenInfo = accessTokenInfo;
|
||||
add(scope, permission);
|
||||
}
|
||||
|
||||
public void add(Scope scope, String permission) throws APIManagerPublisherException {
|
||||
List<String> pathKeys = Arrays.stream(StringUtils.split(permission, "/")).collect(Collectors.toList());
|
||||
|
||||
if (pathKeys.size() == 1) {
|
||||
this.pathKey = pathKeys.get(0);
|
||||
|
||||
if (this.scope == null) {
|
||||
this.scope = scope;
|
||||
} else {
|
||||
this.update(scope);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.pathKey == null) {
|
||||
this.pathKey = pathKeys.get(0);
|
||||
}
|
||||
|
||||
pathKeys.remove(0);
|
||||
|
||||
if (childTrees.containsKey(pathKeys.get(0))) {
|
||||
childTrees.get(pathKeys.get(0)).add(scope, String.join("/", pathKeys));
|
||||
} else {
|
||||
childTrees.put(pathKeys.get(0), new SelfSyncingScopeTree(scope, String.join("/", pathKeys),
|
||||
apiApplicationKey, accessTokenInfo));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Scope findScope(String permission) {
|
||||
if (this.scope != null) {
|
||||
return this.scope;
|
||||
}
|
||||
|
||||
List<String> pathKeys = Arrays.stream(StringUtils.split(permission, "/")).collect(Collectors.toList());
|
||||
pathKeys.remove(0);
|
||||
return childTrees.get(pathKeys.get(0)).findScope(String.join("/", pathKeys));
|
||||
}
|
||||
|
||||
private void update(Scope scope) throws APIManagerPublisherException {
|
||||
try {
|
||||
if (publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo,
|
||||
this.scope.getName())) {
|
||||
if (publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope)) {
|
||||
this.scope = scope;
|
||||
}
|
||||
} else {
|
||||
log.warn("Found a scope which is not reflect in the APIM end. Scope [ " + scope.getName() + " ]");
|
||||
}
|
||||
} catch (APIServicesException | BadRequestException | UnexpectedResponseException e) {
|
||||
String msg = "Error encountered while updating the scope [ " + scope.getName() + "]";
|
||||
log.error(msg, e);
|
||||
throw new APIManagerPublisherException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue