use of org.wso2.ballerinalang.compiler.semantics.model.Scope.ScopeEntry in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method populateScopeMap.
/**
* Populate Scope Object map from OAuth2SecurityDefinitions.
*
* @param oAuth2SecurityDefinitionMap oAuth2SecurityDefinition Map<String, String> to be converted to Scope objects
* @return Scope object map
*/
private Map<String, Scope> populateScopeMap(Map<String, String> oAuth2SecurityDefinitionMap) {
Map<String, Scope> scopeMap = new HashMap<>();
for (Map.Entry<String, String> scopeEntry : oAuth2SecurityDefinitionMap.entrySet()) {
Scope scope = new Scope();
scope.setName(scopeEntry.getKey());
scope.setDescription(scopeEntry.getValue());
scopeMap.put(scopeEntry.getKey(), scope);
}
return scopeMap;
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope.ScopeEntry in project carbon-apimgt by wso2.
the class APIPublisherImpl method addApiFromDefinition.
/**
* Create api from Definition
*
* @param apiDefinition API definition stream.
* @return UUID of the added API.
* @throws APIManagementException If failed to add the API.
*/
@Override
public String addApiFromDefinition(InputStream apiDefinition) throws APIManagementException {
try {
String apiDefinitionString = IOUtils.toString(apiDefinition);
validateScope(apiDefinitionString);
API.APIBuilder apiBuilder = apiDefinitionFromSwagger20.generateApiFromSwaggerResource(getUsername(), apiDefinitionString);
Map<String, String> scopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(apiDefinitionString);
for (Map.Entry<String, String> scopeEntry : scopes.entrySet()) {
getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
}
apiBuilder.corsConfiguration(new CorsConfiguration());
apiBuilder.apiDefinition(apiDefinitionString);
addAPI(apiBuilder);
return apiBuilder.getId();
} catch (IOException e) {
throw new APIManagementException("Couldn't Generate ApiDefinition from file", ExceptionCodes.API_DEFINITION_MALFORMED);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope.ScopeEntry in project ballerina by ballerina-lang.
the class SymbolResolver method addNamespacesInScope.
private void addNamespacesInScope(Map<Name, BXMLNSSymbol> namespaces, SymbolEnv env) {
if (env == null) {
return;
}
env.scope.entries.forEach((name, scopeEntry) -> {
if (scopeEntry.symbol.kind == SymbolKind.XMLNS) {
BXMLNSSymbol nsSymbol = (BXMLNSSymbol) scopeEntry.symbol;
// Skip if the namespace is already added, by a child scope. That means it has been overridden.
if (!namespaces.containsKey(name)) {
namespaces.put(name, nsSymbol);
}
}
});
addNamespacesInScope(namespaces, env.enclEnv);
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope.ScopeEntry in project ballerina by ballerina-lang.
the class SymbolResolver method resolveOperator.
private BSymbol resolveOperator(ScopeEntry entry, List<BType> types) {
BSymbol foundSymbol = symTable.notFoundSymbol;
while (entry != NOT_FOUND_ENTRY) {
BInvokableType opType = (BInvokableType) entry.symbol.type;
if (types.size() == opType.paramTypes.size()) {
boolean match = true;
for (int i = 0; i < types.size(); i++) {
if (types.get(i).tag != opType.paramTypes.get(i).tag) {
match = false;
}
}
if (match) {
foundSymbol = entry.symbol;
break;
}
}
entry = entry.next;
}
return foundSymbol;
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope.ScopeEntry in project carbon-apimgt by wso2.
the class APIPublisherImpl method saveSwagger20Definition.
/**
* {@inheritDoc}
*/
@Override
public void saveSwagger20Definition(String apiId, String jsonText) throws APIManagementException {
try {
LocalDateTime localDateTime = LocalDateTime.now();
Map<String, String> oldScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(getApiSwaggerDefinition(apiId));
Map<String, String> newScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(jsonText);
Map<String, String> updatedScopes = new HashMap<>(newScopes);
updatedScopes.keySet().retainAll(oldScopes.keySet());
oldScopes.keySet().removeAll(updatedScopes.keySet());
newScopes.keySet().removeAll(updatedScopes.keySet());
for (Map.Entry<String, String> scopeEntry : newScopes.entrySet()) {
getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
}
for (Map.Entry<String, String> scopeEntry : oldScopes.entrySet()) {
getKeyManager().deleteScope(scopeEntry.getKey());
}
for (Map.Entry<String, String> scopeEntry : updatedScopes.entrySet()) {
Scope scope = getKeyManager().retrieveScope(scopeEntry.getKey());
scope.setDescription(scopeEntry.getValue());
getKeyManager().updateScope(scope);
}
API api = getAPIbyUUID(apiId);
Map<String, UriTemplate> oldUriTemplateMap = api.getUriTemplates();
List<APIResource> apiResourceList = apiDefinitionFromSwagger20.parseSwaggerAPIResources(new StringBuilder(jsonText));
Map<String, UriTemplate> updatedUriTemplateMap = new HashMap<>();
for (APIResource apiResource : apiResourceList) {
updatedUriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
}
Map<String, UriTemplate> uriTemplateMapNeedTobeUpdate = APIUtils.getMergedUriTemplates(oldUriTemplateMap, updatedUriTemplateMap);
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.uriTemplates(uriTemplateMapNeedTobeUpdate);
createUriTemplateList(apiBuilder, true);
apiBuilder.updatedBy(getUsername());
apiBuilder.lastUpdatedTime(localDateTime);
api = apiBuilder.build();
GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
String existingGatewayConfig = getApiGatewayConfig(apiId);
String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(existingGatewayConfig, jsonText);
getApiDAO().updateAPI(apiId, api);
getApiDAO().updateApiDefinition(apiId, jsonText, getUsername());
getApiDAO().updateGatewayConfig(apiId, updatedGatewayConfig, getUsername());
} catch (APIMgtDAOException e) {
String errorMsg = "Couldn't update the Swagger Definition";
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
Aggregations