Search in sources :

Example 31 with URITemplate

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;
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 32 with URITemplate

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;
}
Also used : SQLException(java.sql.SQLException) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 33 with URITemplate

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;
}
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 34 with URITemplate

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);
                }
            }
        }
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) PreparedStatement(java.sql.PreparedStatement)

Example 35 with URITemplate

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());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)122 HashMap (java.util.HashMap)71 ArrayList (java.util.ArrayList)67 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)42 Scope (org.wso2.carbon.apimgt.api.model.Scope)41 API (org.wso2.carbon.apimgt.api.model.API)38 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)38 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)37 HashSet (java.util.HashSet)36 LinkedHashSet (java.util.LinkedHashSet)28 PreparedStatement (java.sql.PreparedStatement)25 ResultSet (java.sql.ResultSet)23 API (org.wso2.carbon.apimgt.core.models.API)22 Map (java.util.Map)21 Tier (org.wso2.carbon.apimgt.api.model.Tier)21 Connection (java.sql.Connection)20 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)20 LinkedHashMap (java.util.LinkedHashMap)19 List (java.util.List)19 SQLException (java.sql.SQLException)16