use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAllURITemplatesOldThrottle.
public ArrayList<URITemplate> getAllURITemplatesOldThrottle(String apiContext, String version) throws APIManagementException {
Connection connection = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
ArrayList<URITemplate> uriTemplates = new ArrayList<URITemplate>();
// TODO : FILTER RESULTS ONLY FOR ACTIVE APIs
String query = SQLConstants.GET_ALL_URL_TEMPLATES_SQL;
try {
connection = APIMgtDBUtil.getConnection();
prepStmt = connection.prepareStatement(query);
prepStmt.setString(1, apiContext);
prepStmt.setString(2, version);
rs = prepStmt.executeQuery();
URITemplate uriTemplate;
while (rs.next()) {
uriTemplate = new URITemplate();
String script = null;
uriTemplate.setId(rs.getInt("URL_MAPPING_ID"));
uriTemplate.setHTTPVerb(rs.getString("HTTP_METHOD"));
uriTemplate.setAuthType(rs.getString("AUTH_SCHEME"));
uriTemplate.setUriTemplate(rs.getString("URL_PATTERN"));
uriTemplate.setThrottlingTier(rs.getString("THROTTLING_TIER"));
InputStream mediationScriptBlob = rs.getBinaryStream("MEDIATION_SCRIPT");
if (mediationScriptBlob != null) {
script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
}
uriTemplate.setMediationScript(script);
uriTemplate.getThrottlingConditions().add("_default");
uriTemplates.add(uriTemplate);
}
} catch (SQLException e) {
handleException("Error while fetching all URL Templates", e);
} finally {
APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
}
return uriTemplates;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAPIProductURITemplatesAdvancedThrottle.
public ArrayList<URITemplate> getAPIProductURITemplatesAdvancedThrottle(String apiContext, String version) throws APIManagementException {
int tenantId;
ArrayList<URITemplate> uriTemplates = new ArrayList<>();
String apiTenantDomain = MultitenantUtils.getTenantDomainFromRequestURL(apiContext);
if (apiTenantDomain != null) {
tenantId = APIUtil.getTenantIdFromTenantDomain(apiTenantDomain);
} else {
tenantId = MultitenantConstants.SUPER_TENANT_ID;
}
// TODO : FILTER RESULTS ONLY FOR ACTIVE APIs
String query = SQLConstants.ThrottleSQLConstants.GET_CONDITION_GROUPS_FOR_POLICIES_IN_PRODUCTS_SQL;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(query)) {
prepStmt.setString(1, apiContext);
prepStmt.setString(2, version);
prepStmt.setInt(3, tenantId);
try (ResultSet rs = prepStmt.executeQuery()) {
uriTemplates = extractURITemplates(rs);
}
} catch (SQLException e) {
handleException("Error while fetching all URL Templates", e);
}
return uriTemplates;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate 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.URITemplate in project carbon-apimgt by wso2.
the class ApiMgtDAO method setAssociatedAPIProducts.
private void setAssociatedAPIProducts(String uuid, Map<Integer, URITemplate> uriTemplates) throws SQLException {
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SQLConstants.GET_API_PRODUCT_URI_TEMPLATE_ASSOCIATION_SQL)) {
ps.setString(1, uuid);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String productName = rs.getString("API_NAME");
String productVersion = rs.getString("API_VERSION");
String productProvider = rs.getString("API_PROVIDER");
int uriTemplateId = rs.getInt("URL_MAPPING_ID");
URITemplate uriTemplate = uriTemplates.get(uriTemplateId);
if (uriTemplate != null) {
APIProductIdentifier productIdentifier = new APIProductIdentifier(productProvider, productName, productVersion);
uriTemplate.addUsedByProduct(productIdentifier);
}
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIProviderImplTest method testSearchAPIs_NoProviderId.
@Test
public void testSearchAPIs_NoProviderId() throws APIManagementException, RegistryException {
API api1 = new API(new APIIdentifier("admin", "API1", "1.0.1"));
api1.setContext("api1context");
api1.setStatus(APIConstants.PUBLISHED);
api1.setDescription("API 1 Desciption");
Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
URITemplate uriTemplate1 = new URITemplate();
uriTemplate1.setHTTPVerb("POST");
uriTemplate1.setAuthType("Application");
uriTemplate1.setUriTemplate("/add");
uriTemplate1.setThrottlingTier("Gold");
uriTemplates.add(uriTemplate1);
api1.setUriTemplates(uriTemplates);
API api2 = new API(new APIIdentifier("admin", "API2", "1.0.0"));
api2.setContext("api2context");
api2.setStatus(APIConstants.CREATED);
api2.setDescription("API 2 Desciption");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
GenericArtifact genericArtifact1 = Mockito.mock(GenericArtifact.class);
GenericArtifact genericArtifact2 = Mockito.mock(GenericArtifact.class);
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API1");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.1");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("api1context");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION)).thenReturn("API 1 Desciption");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("Published");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API2");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.0");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("api2context");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION)).thenReturn("API 2 Desciption");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("Created");
Mockito.when(APIUtil.getAPI(genericArtifact1, apiProvider.registry)).thenReturn(api1);
Mockito.when(APIUtil.getAPI(genericArtifact2, apiProvider.registry)).thenReturn(api2);
Mockito.when(APIUtil.getAPI(genericArtifact1)).thenReturn(api1);
Mockito.when(APIUtil.getAPI(genericArtifact2)).thenReturn(api2);
GenericArtifact[] genericArtifacts = { genericArtifact1, genericArtifact2 };
Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
// Search by Name matching both APIs
List<API> foundApiList0 = apiProvider.searchAPIs("API", "Name", null);
Assert.assertNotNull(foundApiList0);
Assert.assertEquals(2, foundApiList0.size());
// Search by exact name
List<API> foundApiList1 = apiProvider.searchAPIs("API1", "Name", null);
Assert.assertNotNull(foundApiList1);
Assert.assertEquals(1, foundApiList1.size());
// Search by exact provider
List<API> foundApiList2 = apiProvider.searchAPIs("admin", "Provider", null);
Assert.assertNotNull(foundApiList2);
Assert.assertEquals(2, foundApiList2.size());
// Search by exact version
List<API> foundApiList3 = apiProvider.searchAPIs("1.0.0", "Version", null);
Assert.assertNotNull(foundApiList3);
Assert.assertEquals(1, foundApiList3.size());
// Search by exact context
List<API> foundApiList4 = apiProvider.searchAPIs("api1context", "Context", null);
Assert.assertNotNull(foundApiList4);
Assert.assertEquals(1, foundApiList4.size());
// Search by exact context
List<API> foundApiList5 = apiProvider.searchAPIs("api2context", "Context", null);
Assert.assertNotNull(foundApiList5);
Assert.assertEquals(1, foundApiList5.size());
// Search by wrong context
List<API> foundApiList6 = apiProvider.searchAPIs("test", "Context", null);
Assert.assertNotNull(foundApiList6);
Assert.assertEquals(0, foundApiList6.size());
// Search by status
List<API> foundApiList7 = apiProvider.searchAPIs("Published", "Status", null);
Assert.assertNotNull(foundApiList7);
Assert.assertEquals(1, foundApiList7.size());
// Search by Description
List<API> foundApiList8 = apiProvider.searchAPIs("API 1 Desciption", "Description", null);
Assert.assertNotNull(foundApiList8);
Assert.assertEquals(1, foundApiList8.size());
// Search by Description
List<API> foundApiList9 = apiProvider.searchAPIs("API", "Description", null);
Assert.assertNotNull(foundApiList9);
Assert.assertEquals(2, foundApiList9.size());
}
Aggregations