Search in sources :

Example 1 with ScopeEntry

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;
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with ScopeEntry

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);
    }
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ScopeEntry

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);
}
Also used : BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)

Example 4 with ScopeEntry

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;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)

Example 5 with ScopeEntry

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());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) HashMap(java.util.HashMap) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) Map(java.util.Map) HashMap(java.util.HashMap) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Aggregations

HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 TokenStream (org.antlr.v4.runtime.TokenStream)3 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)3 Scope (org.wso2.ballerinalang.compiler.semantics.model.Scope)3 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)3 Scope (org.wso2.carbon.apimgt.core.models.Scope)3 Token (org.antlr.v4.runtime.Token)2 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)2 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)2 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 API (org.wso2.carbon.apimgt.core.models.API)2 IOException (java.io.IOException)1 LocalDateTime (java.time.LocalDateTime)1 Arrays (java.util.Arrays)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1