Search in sources :

Example 51 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class ApiMgtDAO method getURITemplatesOfAPIWithProductMapping.

public Map<Integer, URITemplate> getURITemplatesOfAPIWithProductMapping(String uuid) throws APIManagementException {
    Map<Integer, URITemplate> uriTemplates = new LinkedHashMap<>();
    Map<Integer, Set<String>> scopeToURITemplateId = new HashMap<>();
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(SQLConstants.GET_URL_TEMPLATES_OF_API_WITH_PRODUCT_MAPPINGS_SQL)) {
        ps.setString(1, uuid);
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                Integer uriTemplateId = rs.getInt("URL_MAPPING_ID");
                String scopeName = rs.getString("SCOPE_NAME");
                if (scopeToURITemplateId.containsKey(uriTemplateId) && !StringUtils.isEmpty(scopeName) && !scopeToURITemplateId.get(uriTemplateId).contains(scopeName) && uriTemplates.containsKey(uriTemplateId)) {
                    Scope scope = new Scope();
                    scope.setKey(scopeName);
                    scopeToURITemplateId.get(uriTemplateId).add(scopeName);
                    uriTemplates.get(uriTemplateId).setScopes(scope);
                    continue;
                }
                String urlPattern = rs.getString("URL_PATTERN");
                String verb = rs.getString("HTTP_METHOD");
                URITemplate uriTemplate = new URITemplate();
                uriTemplate.setUriTemplate(urlPattern);
                uriTemplate.setHTTPVerb(verb);
                uriTemplate.setHttpVerbs(verb);
                String authType = rs.getString("AUTH_SCHEME");
                String throttlingTier = rs.getString("THROTTLING_TIER");
                if (StringUtils.isNotEmpty(scopeName)) {
                    Scope scope = new Scope();
                    scope.setKey(scopeName);
                    uriTemplate.setScope(scope);
                    uriTemplate.setScopes(scope);
                    Set<String> templateScopes = new HashSet<>();
                    templateScopes.add(scopeName);
                    scopeToURITemplateId.put(uriTemplateId, templateScopes);
                }
                uriTemplate.setAuthType(authType);
                uriTemplate.setAuthTypes(authType);
                uriTemplate.setThrottlingTier(throttlingTier);
                uriTemplate.setThrottlingTiers(throttlingTier);
                InputStream mediationScriptBlob = rs.getBinaryStream("MEDIATION_SCRIPT");
                if (mediationScriptBlob != null) {
                    String script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
                    uriTemplate.setMediationScript(script);
                    uriTemplate.setMediationScripts(verb, script);
                }
                uriTemplates.put(uriTemplateId, uriTemplate);
            }
        }
        setAssociatedAPIProductsURLMappings(uuid, uriTemplates);
    } catch (SQLException e) {
        handleException("Failed to get URI Templates of API with UUID " + uuid, e);
    }
    return uriTemplates;
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LinkedHashMap(java.util.LinkedHashMap) Scope(org.wso2.carbon.apimgt.api.model.Scope) ResultSet(java.sql.ResultSet) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 52 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class ApiMgtDAO method getURITemplatesOfAPI.

public Set<URITemplate> getURITemplatesOfAPI(String uuid) throws APIManagementException {
    String currentApiUuid;
    APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        currentApiUuid = apiRevision.getApiUUID();
    } else {
        currentApiUuid = uuid;
    }
    Map<Integer, URITemplate> uriTemplates = new LinkedHashMap<>();
    Map<Integer, Set<String>> scopeToURITemplateId = new HashMap<>();
    // Check If the API is a Revision
    if (apiRevision != null) {
        try (Connection conn = APIMgtDBUtil.getConnection();
            PreparedStatement ps = conn.prepareStatement(SQLConstants.GET_URL_TEMPLATES_OF_API_REVISION_SQL)) {
            ps.setString(1, currentApiUuid);
            ps.setString(2, uuid);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    Integer uriTemplateId = rs.getInt("URL_MAPPING_ID");
                    String scopeName = rs.getString("SCOPE_NAME");
                    if (scopeToURITemplateId.containsKey(uriTemplateId) && !StringUtils.isEmpty(scopeName) && !scopeToURITemplateId.get(uriTemplateId).contains(scopeName) && uriTemplates.containsKey(uriTemplateId)) {
                        Scope scope = new Scope();
                        scope.setKey(scopeName);
                        scopeToURITemplateId.get(uriTemplateId).add(scopeName);
                        uriTemplates.get(uriTemplateId).setScopes(scope);
                        continue;
                    }
                    String urlPattern = rs.getString("URL_PATTERN");
                    String verb = rs.getString("HTTP_METHOD");
                    URITemplate uriTemplate = new URITemplate();
                    uriTemplate.setUriTemplate(urlPattern);
                    uriTemplate.setHTTPVerb(verb);
                    uriTemplate.setHttpVerbs(verb);
                    uriTemplate.setId(uriTemplateId);
                    String authType = rs.getString("AUTH_SCHEME");
                    String throttlingTier = rs.getString("THROTTLING_TIER");
                    if (StringUtils.isNotEmpty(scopeName)) {
                        Scope scope = new Scope();
                        scope.setKey(scopeName);
                        uriTemplate.setScope(scope);
                        uriTemplate.setScopes(scope);
                        Set<String> templateScopes = new HashSet<>();
                        templateScopes.add(scopeName);
                        scopeToURITemplateId.put(uriTemplateId, templateScopes);
                    }
                    uriTemplate.setAuthType(authType);
                    uriTemplate.setAuthTypes(authType);
                    uriTemplate.setThrottlingTier(throttlingTier);
                    uriTemplate.setThrottlingTiers(throttlingTier);
                    uriTemplate.setId(uriTemplateId);
                    InputStream mediationScriptBlob = rs.getBinaryStream("MEDIATION_SCRIPT");
                    if (mediationScriptBlob != null) {
                        String script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
                        uriTemplate.setMediationScript(script);
                        uriTemplate.setMediationScripts(verb, script);
                    }
                    uriTemplates.put(uriTemplateId, uriTemplate);
                }
            }
            setAssociatedAPIProducts(currentApiUuid, uriTemplates);
            setOperationPolicies(apiRevision.getRevisionUUID(), uriTemplates);
        } catch (SQLException e) {
            handleException("Failed to get URI Templates of API with UUID " + uuid, e);
        }
    } else {
        try (Connection conn = APIMgtDBUtil.getConnection();
            PreparedStatement ps = conn.prepareStatement(SQLConstants.GET_URL_TEMPLATES_OF_API_SQL)) {
            ps.setString(1, currentApiUuid);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    Integer uriTemplateId = rs.getInt("URL_MAPPING_ID");
                    String scopeName = rs.getString("SCOPE_NAME");
                    if (scopeToURITemplateId.containsKey(uriTemplateId) && !StringUtils.isEmpty(scopeName) && !scopeToURITemplateId.get(uriTemplateId).contains(scopeName) && uriTemplates.containsKey(uriTemplateId)) {
                        Scope scope = new Scope();
                        scope.setKey(scopeName);
                        scopeToURITemplateId.get(uriTemplateId).add(scopeName);
                        uriTemplates.get(uriTemplateId).setScopes(scope);
                        continue;
                    }
                    String urlPattern = rs.getString("URL_PATTERN");
                    String verb = rs.getString("HTTP_METHOD");
                    URITemplate uriTemplate = new URITemplate();
                    uriTemplate.setUriTemplate(urlPattern);
                    uriTemplate.setHTTPVerb(verb);
                    uriTemplate.setHttpVerbs(verb);
                    String authType = rs.getString("AUTH_SCHEME");
                    String throttlingTier = rs.getString("THROTTLING_TIER");
                    if (StringUtils.isNotEmpty(scopeName)) {
                        Scope scope = new Scope();
                        scope.setKey(scopeName);
                        uriTemplate.setScope(scope);
                        uriTemplate.setScopes(scope);
                        Set<String> templateScopes = new HashSet<>();
                        templateScopes.add(scopeName);
                        scopeToURITemplateId.put(uriTemplateId, templateScopes);
                    }
                    uriTemplate.setAuthType(authType);
                    uriTemplate.setAuthTypes(authType);
                    uriTemplate.setThrottlingTier(throttlingTier);
                    uriTemplate.setThrottlingTiers(throttlingTier);
                    uriTemplate.setId(uriTemplateId);
                    InputStream mediationScriptBlob = rs.getBinaryStream("MEDIATION_SCRIPT");
                    if (mediationScriptBlob != null) {
                        String script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
                        uriTemplate.setMediationScript(script);
                        uriTemplate.setMediationScripts(verb, script);
                    }
                    uriTemplates.put(uriTemplateId, uriTemplate);
                }
            }
            setAssociatedAPIProducts(currentApiUuid, uriTemplates);
            setOperationPolicies(currentApiUuid, uriTemplates);
        } catch (SQLException e) {
            handleException("Failed to get URI Templates of API with UUID " + currentApiUuid, e);
        }
    }
    return new LinkedHashSet<>(uriTemplates.values());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) ResultSet(java.sql.ResultSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LinkedHashMap(java.util.LinkedHashMap) Scope(org.wso2.carbon.apimgt.api.model.Scope) ResultSet(java.sql.ResultSet) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 53 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class ApiMgtDAO method getAllSharedScopes.

/**
 * Get all shared scopes for tenant.
 *
 * @param tenantDomain Tenant Domain
 * @return shared scope list
 * @throws APIManagementException if an error occurs while getting all shared scopes for tenant
 */
public List<Scope> getAllSharedScopes(String tenantDomain) throws APIManagementException {
    List<Scope> scopeList = null;
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(SQLConstants.GET_SHARED_SCOPE_USAGE_COUNT_BY_TENANT)) {
        statement.setInt(1, tenantId);
        statement.setInt(2, tenantId);
        try (ResultSet rs = statement.executeQuery()) {
            scopeList = new ArrayList<>();
            while (rs.next()) {
                Scope scope = new Scope();
                scope.setId(rs.getString("UUID"));
                scope.setKey(rs.getString("NAME"));
                scope.setUsageCount(rs.getInt("usages"));
                scopeList.add(scope);
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get all Shared Scopes for tenant: " + tenantDomain, e);
    }
    return scopeList;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 54 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class APIProviderImpl method updateScope.

private void updateScope(Scope scope, int tenantId) throws APIManagementException {
    if (scope != null) {
        scopesDAO.updateScope(scope, tenantId);
        ScopeEvent scopeEvent = new ScopeEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SCOPE_UPDATE.name(), tenantId, tenantDomain, scope.getKey(), scope.getName(), scope.getDescription());
        if (StringUtils.isNotEmpty(scope.getRoles()) && scope.getRoles().trim().length() > 0) {
            scopeEvent.setRoles(Arrays.asList(scope.getRoles().split(",")));
        }
        APIUtil.sendNotification(scopeEvent, APIConstants.NotifierType.SCOPE.name());
    }
}
Also used : ScopeEvent(org.wso2.carbon.apimgt.impl.notifier.events.ScopeEvent)

Example 55 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class APIProviderImpl method deleteSharedScope.

/**
 * Delete shared scope.
 *
 * @param scopeName    Shared scope name
 * @param tenantDomain tenant domain
 * @throws APIManagementException If failed to delete the scope
 */
@Override
public void deleteSharedScope(String scopeName, String tenantDomain) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Deleting shared scope " + scopeName);
    }
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.deleteScope(scopeName);
            } catch (APIManagementException e) {
                log.error("Error while Deleting Shared Scope " + scopeName + " from Key Manager " + keyManagerEntry.getKey(), e);
            }
        }
    }
    apiMgtDAO.deleteSharedScope(scopeName, tenantDomain);
    deleteScope(scopeName, APIUtil.getTenantIdFromTenantDomain(tenantDomain));
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Aggregations

Scope (org.wso2.carbon.apimgt.api.model.Scope)97 HashMap (java.util.HashMap)76 ArrayList (java.util.ArrayList)58 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)50 Scope (org.wso2.carbon.apimgt.core.models.Scope)41 Map (java.util.Map)39 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)39 LinkedHashSet (java.util.LinkedHashSet)32 LinkedHashMap (java.util.LinkedHashMap)29 HashSet (java.util.HashSet)26 RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)25 List (java.util.List)24 Test (org.testng.annotations.Test)23 JSONObject (org.json.simple.JSONObject)22 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 PreparedStatement (java.sql.PreparedStatement)17 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)17 SQLException (java.sql.SQLException)16 Gson (com.google.gson.Gson)15 Connection (java.sql.Connection)15