use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class CacheInvalidationServiceImpl method invalidateResourceCache.
@Override
public void invalidateResourceCache(String apiContext, String apiVersion, ResourceCacheInvalidationDto[] uriTemplates) {
boolean isTenantFlowStarted = false;
int tenantDomainIndex = apiContext.indexOf("/t/");
String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
if (tenantDomainIndex != -1) {
String temp = apiContext.substring(tenantDomainIndex + 3, apiContext.length());
tenantDomain = temp.substring(0, temp.indexOf('/'));
}
try {
isTenantFlowStarted = startTenantFlow(tenantDomain);
Cache cache = CacheProvider.getResourceCache();
if (apiContext.contains(APIConstants.POLICY_CACHE_CONTEXT)) {
if (log.isDebugEnabled()) {
log.debug("Cleaning cache for policy update for tenant " + tenantDomain);
}
cache.removeAll();
} else {
String apiCacheKey = APIUtil.getAPIInfoDTOCacheKey(apiContext, apiVersion);
if (cache.containsKey(apiCacheKey)) {
cache.remove(apiCacheKey);
}
for (ResourceCacheInvalidationDto uriTemplate : uriTemplates) {
String resourceVerbCacheKey = APIUtil.getResourceInfoDTOCacheKey(apiContext, apiVersion, uriTemplate.getResourceURLContext(), uriTemplate.getHttpVerb());
if (cache.containsKey(resourceVerbCacheKey)) {
cache.remove(resourceVerbCacheKey);
}
}
}
} finally {
if (isTenantFlowStarted) {
endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class CacheInvalidationServiceImpl method invalidateResourceCache.
public void invalidateResourceCache(String apiContext, String apiVersion, String resourceURLContext, String httpVerb) {
ResourceCacheInvalidationDto uriTemplate = new ResourceCacheInvalidationDto();
uriTemplate.setResourceURLContext(resourceURLContext);
uriTemplate.setHttpVerb(httpVerb);
invalidateResourceCache(apiContext, apiVersion, new ResourceCacheInvalidationDto[] { uriTemplate });
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class GraphQLSchemaDefinition method extractGraphQLOperationList.
/**
* Extract GraphQL Operations from given schema.
*
* @param typeRegistry graphQL Schema Type Registry
* @param type operation type string
* @return the arrayList of APIOperationsDTO
*/
public List<URITemplate> extractGraphQLOperationList(TypeDefinitionRegistry typeRegistry, String type) {
List<URITemplate> operationArray = new ArrayList<>();
Map<java.lang.String, TypeDefinition> operationList = typeRegistry.types();
for (Map.Entry<String, TypeDefinition> entry : operationList.entrySet()) {
Optional<SchemaDefinition> schemaDefinition = typeRegistry.schemaDefinition();
if (schemaDefinition.isPresent()) {
List<OperationTypeDefinition> operationTypeList = schemaDefinition.get().getOperationTypeDefinitions();
for (OperationTypeDefinition operationTypeDefinition : operationTypeList) {
if (entry.getValue().getName().equalsIgnoreCase(operationTypeDefinition.getTypeName().getName())) {
if (type == null) {
addOperations(entry, operationTypeDefinition.getName().toUpperCase(), operationArray);
} else if (type.equals(operationTypeDefinition.getName().toUpperCase())) {
addOperations(entry, operationTypeDefinition.getName().toUpperCase(), operationArray);
}
}
}
} else {
if (entry.getValue().getName().equalsIgnoreCase(APIConstants.GRAPHQL_QUERY) || entry.getValue().getName().equalsIgnoreCase(APIConstants.GRAPHQL_MUTATION) || entry.getValue().getName().equalsIgnoreCase(APIConstants.GRAPHQL_SUBSCRIPTION)) {
if (type == null) {
addOperations(entry, entry.getKey(), operationArray);
} else if (type.equals(entry.getValue().getName().toUpperCase())) {
addOperations(entry, entry.getKey(), operationArray);
}
}
}
}
return operationArray;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class GraphQLSchemaDefinition method buildSchemaWithAdditionalInfo.
/**
* build schema with additional info
*
* @param api api object
* @param graphqlComplexityInfo
* @return schemaDefinition
*/
public String buildSchemaWithAdditionalInfo(API api, GraphqlComplexityInfo graphqlComplexityInfo) {
Swagger swagger = null;
Map<String, String> scopeRoleMap = new HashMap<>();
Map<String, String> operationScopeMap = new HashMap<>();
Map<String, String> operationAuthSchemeMap = new HashMap<>();
Map<String, String> operationThrottlingMap = new HashMap<>();
String operationScopeType;
StringBuilder schemaDefinitionBuilder = new StringBuilder(api.getGraphQLSchema());
schemaDefinitionBuilder.append("\n");
StringBuilder operationScopeMappingBuilder = new StringBuilder();
StringBuilder scopeRoleMappingBuilder = new StringBuilder();
StringBuilder operationAuthSchemeMappingBuilder = new StringBuilder();
StringBuilder operationThrottlingMappingBuilder = new StringBuilder();
StringBuilder policyBuilder = new StringBuilder();
String swaggerDef = api.getSwaggerDefinition();
OpenAPI openAPI = null;
LinkedHashMap<String, Object> scopeBindings = null;
if (swaggerDef != null) {
OpenAPIParser parser = new OpenAPIParser();
openAPI = parser.readContents(swaggerDef, null, null).getOpenAPI();
}
Map<String, Object> extensions = null;
if (openAPI != null) {
extensions = openAPI.getComponents().getSecuritySchemes().get(APIConstants.SWAGGER_APIM_DEFAULT_SECURITY).getFlows().getImplicit().getExtensions();
}
if (extensions != null) {
scopeBindings = (LinkedHashMap<String, Object>) openAPI.getComponents().getSecuritySchemes().get(APIConstants.SWAGGER_APIM_DEFAULT_SECURITY).getFlows().getImplicit().getExtensions().get(APIConstants.SWAGGER_X_SCOPES_BINDINGS);
}
if (swaggerDef != null) {
for (URITemplate template : api.getUriTemplates()) {
String scopeInURITemplate = template.getScope() != null ? template.getScope().getKey() : null;
if (scopeInURITemplate != null) {
operationScopeMap.put(template.getUriTemplate(), scopeInURITemplate);
if (!scopeRoleMap.containsKey(scopeInURITemplate)) {
if (scopeBindings != null) {
scopeRoleMap.put(scopeInURITemplate, scopeBindings.get(scopeInURITemplate).toString());
}
}
}
}
for (URITemplate template : api.getUriTemplates()) {
operationThrottlingMap.put(template.getUriTemplate(), template.getThrottlingTier());
operationAuthSchemeMap.put(template.getUriTemplate(), template.getAuthType());
}
if (operationScopeMap.size() > 0) {
String base64EncodedURLOperationKey;
String base64EncodedURLScope;
for (Map.Entry<String, String> entry : operationScopeMap.entrySet()) {
base64EncodedURLOperationKey = Base64.getUrlEncoder().withoutPadding().encodeToString(entry.getKey().getBytes(Charset.defaultCharset()));
base64EncodedURLScope = Base64.getUrlEncoder().withoutPadding().encodeToString(entry.getValue().getBytes(Charset.defaultCharset()));
operationScopeType = "type " + APIConstants.SCOPE_OPERATION_MAPPING + "_" + base64EncodedURLOperationKey + "{\n" + base64EncodedURLScope + ": String\n}\n";
operationScopeMappingBuilder.append(operationScopeType);
}
schemaDefinitionBuilder.append(operationScopeMappingBuilder.toString());
}
if (scopeRoleMap.size() > 0) {
String[] roleList;
String scopeType;
String base64EncodedURLScopeKey;
String scopeRoleMappingType;
String base64EncodedURLRole;
String roleField;
for (Map.Entry<String, String> entry : scopeRoleMap.entrySet()) {
List<String> scopeRoles = new ArrayList<>();
base64EncodedURLScopeKey = Base64.getUrlEncoder().withoutPadding().encodeToString(entry.getKey().getBytes(Charset.defaultCharset()));
scopeType = "type " + APIConstants.SCOPE_ROLE_MAPPING + "_" + base64EncodedURLScopeKey + "{\n";
StringBuilder scopeRoleBuilder = new StringBuilder(scopeType);
roleList = entry.getValue().split(",");
for (String role : roleList) {
if (!role.equals("") && !scopeRoles.contains(role)) {
base64EncodedURLRole = Base64.getUrlEncoder().withoutPadding().encodeToString(role.getBytes(Charset.defaultCharset()));
roleField = base64EncodedURLRole + ": String\n";
scopeRoleBuilder.append(roleField);
scopeRoles.add(role);
}
}
if (scopeRoles.size() > 0 && !StringUtils.isEmpty(scopeRoleBuilder.toString())) {
scopeRoleMappingType = scopeRoleBuilder.toString() + "}\n";
scopeRoleMappingBuilder.append(scopeRoleMappingType);
}
}
schemaDefinitionBuilder.append(scopeRoleMappingBuilder.toString());
}
if (operationThrottlingMap.size() > 0) {
String operationThrottlingType;
for (Map.Entry<String, String> entry : operationThrottlingMap.entrySet()) {
String base64EncodedURLOperationKey = Base64.getUrlEncoder().withoutPadding().encodeToString(entry.getKey().getBytes(Charset.defaultCharset()));
String base64EncodedURLThrottilingTier = Base64.getUrlEncoder().withoutPadding().encodeToString(entry.getValue().getBytes(Charset.defaultCharset()));
operationThrottlingType = "type " + APIConstants.OPERATION_THROTTLING_MAPPING + "_" + base64EncodedURLOperationKey + "{\n" + base64EncodedURLThrottilingTier + ": String\n}\n";
operationThrottlingMappingBuilder.append(operationThrottlingType);
}
schemaDefinitionBuilder.append(operationThrottlingMappingBuilder.toString());
}
if (operationAuthSchemeMap.size() > 0) {
String operationAuthSchemeType;
String isSecurityEnabled;
for (Map.Entry<String, String> entry : operationAuthSchemeMap.entrySet()) {
String base64EncodedURLOperationKey = Base64.getUrlEncoder().withoutPadding().encodeToString(entry.getKey().getBytes(Charset.defaultCharset()));
if (entry.getValue().equalsIgnoreCase(APIConstants.AUTH_NO_AUTHENTICATION)) {
isSecurityEnabled = APIConstants.OPERATION_SECURITY_DISABLED;
} else {
isSecurityEnabled = APIConstants.OPERATION_SECURITY_ENABLED;
}
operationAuthSchemeType = "type " + APIConstants.OPERATION_AUTH_SCHEME_MAPPING + "_" + base64EncodedURLOperationKey + "{\n" + isSecurityEnabled + ": String\n}\n";
operationAuthSchemeMappingBuilder.append(operationAuthSchemeType);
}
schemaDefinitionBuilder.append(operationAuthSchemeMappingBuilder.toString());
}
if (operationAuthSchemeMap.size() > 0) {
// Constructing the policy definition
JSONObject jsonPolicyDefinition = policyDefinitionToJson(graphqlComplexityInfo);
String base64EncodedPolicyDefinition = Base64.getUrlEncoder().withoutPadding().encodeToString(jsonPolicyDefinition.toJSONString().getBytes(Charset.defaultCharset()));
String policyDefinition = "type " + APIConstants.GRAPHQL_ACCESS_CONTROL_POLICY + " {\n" + base64EncodedPolicyDefinition + ": String\n}\n";
policyBuilder.append(policyDefinition);
schemaDefinitionBuilder.append(policyBuilder.toString());
}
}
return schemaDefinitionBuilder.toString();
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class OASParserUtil method setScopesToTemplate.
/**
* Sets the scopes to the URL template object using the given list of scopes
*
* @param template URL template
* @param resourceScopes list of scopes of the resource
* @param apiScopes set of scopes defined for the API
* @return URL template after setting the scopes
*/
public static URITemplate setScopesToTemplate(URITemplate template, List<String> resourceScopes, Set<Scope> apiScopes) throws APIManagementException {
for (String scopeName : resourceScopes) {
if (StringUtils.isNotBlank(scopeName)) {
Scope scope = APIUtil.findScopeByKey(apiScopes, scopeName);
if (scope == null) {
throw new APIManagementException("Resource Scope '" + scopeName + "' not found.");
}
template.setScopes(scope);
}
}
return template;
}
Aggregations