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;
}
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());
}
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;
}
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());
}
}
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));
}
Aggregations