Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016-2018 The Sponge authors.
*
* Licensed 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 org.openksavi.sponge.remoteapi.server.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.openksavi.sponge.action.ActionAdapter;
import org.openksavi.sponge.core.util.SpongeUtils;
import org.openksavi.sponge.engine.SpongeEngine;
import org.openksavi.sponge.remoteapi.model.RemoteActionMeta;
import org.openksavi.sponge.remoteapi.server.RemoteApiServerConstants;
import org.openksavi.sponge.remoteapi.server.RemoteApiSettings;
import org.openksavi.sponge.remoteapi.server.security.User;
import org.openksavi.sponge.remoteapi.server.security.UserContext;
import org.openksavi.sponge.remoteapi.type.converter.TypeConverter;
import org.openksavi.sponge.type.DataType;
import org.openksavi.sponge.type.ListType;
import org.openksavi.sponge.type.provided.ProvidedValue;
import org.openksavi.sponge.type.value.AnnotatedValue;
import org.openksavi.sponge.util.DataTypeUtils;
/**
* A set of Remote API server utility methods.
*/
@SuppressWarnings("rawtypes")
public abstract class RemoteApiServerUtils {
private RemoteApiServerUtils() {
//
}
public static User createAnonymousUser(String anonymousRole) {
return new User(RemoteApiServerConstants.DEFAULT_ANONYMOUS_USERNAME, null, anonymousRole);
}
public static UserContext createAnonymousUserContext(String anonymousRole) {
return new UserContext(RemoteApiServerConstants.DEFAULT_ANONYMOUS_USERNAME, anonymousRole);
}
public static boolean isActionInternal(String actionName) {
return actionName.startsWith(RemoteApiServerConstants.INTERNAL_ACTION_NAME_PREFIX);
}
/**
* Verifies if a user can access a resource (e.g. a knowledge base, an event).
*
* @param roleToResources the map of (role name to resource names regexps).
* @param userContext the user context.
* @param resourceName the resource name.
* @return {@code true} if the user can access the resource.
*/
public static boolean canAccessResource(Map> roleToResources, UserContext userContext, String resourceName) {
if (roleToResources == null) {
return false;
}
return userContext.getRoles().stream().filter(role -> roleToResources.containsKey(role)).anyMatch(role -> roleToResources.get(role)
.stream().filter(Objects::nonNull).anyMatch(resourceRegexp -> resourceName.matches(resourceRegexp)));
}
public static List