Search in sources :

Example 51 with Provider

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

the class ApiMgtDAO method setAPIProductFromDB.

/**
 * Get product Id from the product name and the provider.
 *
 * @param product product identifier
 * @throws APIManagementException exception
 */
public void setAPIProductFromDB(APIProduct product) throws APIManagementException {
    APIProductIdentifier apiProductIdentifier = product.getId();
    String currentApiUuid;
    APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(product.getUuid());
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        currentApiUuid = apiRevision.getApiUUID();
    } else {
        currentApiUuid = product.getUuid();
    }
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement prepStmt = connection.prepareStatement(SQLConstants.GET_API_PRODUCT_SQL)) {
        prepStmt.setString(1, currentApiUuid);
        try (ResultSet rs = prepStmt.executeQuery()) {
            if (rs.next()) {
                product.setProductId(rs.getInt("API_ID"));
                product.setProductLevelPolicy(rs.getString("API_TIER"));
            } else {
                String msg = "Unable to find the API Product : " + apiProductIdentifier.getName() + "-" + APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()) + "-" + apiProductIdentifier.getVersion() + " in the database";
                throw new APIMgtResourceNotFoundException(msg);
            }
        }
    } catch (SQLException e) {
        handleException("Error while locating API Product: " + apiProductIdentifier.getName() + "-" + APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()) + "-" + apiProductIdentifier.getVersion() + " from the database", e);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 52 with Provider

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

the class ApiMgtDAO method getAPIProductIdentifierFromUUID.

/**
 * Get API Product Identifier by the product's UUID.
 *
 * @param uuid uuid of the API
 * @return API Identifier
 * @throws APIManagementException if an error occurs
 */
public APIProductIdentifier getAPIProductIdentifierFromUUID(String uuid) throws APIManagementException {
    APIProductIdentifier identifier = null;
    String sql = SQLConstants.GET_API_IDENTIFIER_BY_UUID_SQL;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        PreparedStatement prepStmt = connection.prepareStatement(sql);
        prepStmt.setString(1, uuid);
        try (ResultSet resultSet = prepStmt.executeQuery()) {
            while (resultSet.next()) {
                String provider = resultSet.getString(1);
                String name = resultSet.getString(2);
                String version = resultSet.getString(3);
                identifier = new APIProductIdentifier(APIUtil.replaceEmailDomain(provider), name, version, uuid);
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve the API Product Identifier details for UUID : " + uuid, e);
    }
    return identifier;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 53 with Provider

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

the class OASParserUtilTest method testGenerateOASConfigWithSecuredEndpoints.

@Test
public void testGenerateOASConfigWithSecuredEndpoints() throws Exception {
    String endpoints = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("definitions" + File.separator + "endpointsData.json"), "UTF-8");
    JSONObject jsonObject = new JSONObject(endpoints);
    APIIdentifier identifier = new APIIdentifier("provider", "name", "version");
    API api = new API(identifier);
    String endUserName = "end_user";
    JsonNode prodNode;
    JsonNode sandNode;
    JsonNode securityConfig;
    String defaultEndpoints = jsonObject.getJSONObject("default_endpoints").toString();
    api.setEndpointSecured(true);
    api.setEndpointConfig(defaultEndpoints);
    api.setEndpointAuthDigest(false);
    api.setEndpointUTUsername(endUserName);
    // check default production endpoint security
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_DEFAULT, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    Assert.assertNotNull(prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
    securityConfig = prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
    Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
    Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
    // check default sandbox endpoint security
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_DEFAULT, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    Assert.assertNotNull(sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
    securityConfig = sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
    Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
    Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
    // check default production endpoint digest auth security
    api.setEndpointAuthDigest(true);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_DEFAULT, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    Assert.assertNotNull(prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
    securityConfig = prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
    Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_DIGEST.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
    Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
    // --------- check http endpoints security
    String httpHybrid = jsonObject.getJSONObject("http_hybrid").toString();
    api.setEndpointConfig(httpHybrid);
    api.setEndpointAuthDigest(false);
    api.setEndpointUTUsername(endUserName);
    // check http production
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    Assert.assertNotNull(prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
    securityConfig = prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
    Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
    Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
    // check http sandbox
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    Assert.assertNotNull(sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
    securityConfig = sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
    Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
    Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
    // ----------- check address endpoints security
    String soapHybrid = jsonObject.getJSONObject("soap_hybrid").toString();
    api.setEndpointConfig(soapHybrid);
    api.setEndpointAuthDigest(false);
    api.setEndpointUTUsername(endUserName);
    // check address production
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_ADDRESS, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    Assert.assertNotNull(prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
    securityConfig = prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
    Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
    Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
    // check address sandbox
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_ADDRESS, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    Assert.assertNotNull(sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
    securityConfig = sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
    Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
    Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
}
Also used : JSONObject(org.json.JSONObject) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 54 with Provider

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

the class APIManagerConfigurationTest method testEnvironmentsConfigWithAdditionalProperties.

@Test
public void testEnvironmentsConfigWithAdditionalProperties() throws XMLStreamException, APIManagementException {
    String envConfig = "<Environment type=\"hybrid\" api-console=\"true\" isDefault=\"true\">\n" + "                <Name>Default</Name>\n" + "                <DisplayName></DisplayName>\n" + "                <Description>This is a hybrid gateway that handles both production and sandbox token traffic.</Description>\n" + "                <!-- Server URL of the API gateway -->\n" + "                <ServerURL>https://localhost:9440/services/</ServerURL>\n" + "                <!-- Admin username for the API gateway. -->\n" + "                <Username>${admin.username}</Username>\n" + "                <!-- Admin password for the API gateway.-->\n" + "                <Password>${admin.password}</Password>\n" + "                <!-- Provider Vendor of the API gateway.-->\n" + "                <Provider>wso2</Provider>\n" + "                <!-- Endpoint URLs for the APIs hosted in this API gateway.-->\n" + "                <GatewayEndpoint>https://localhost:9440,http://localhost:9440</GatewayEndpoint>\n" + "                <!-- Additional properties for External Gateways -->\n" + "                <!-- Endpoint URLs of the WebSocket APIs hosted in this API Gateway -->\n" + "                <GatewayWSEndpoint>ws://localhost:9099,wss://localhost:8099</GatewayWSEndpoint>\n" + "                <!-- Endpoint URLs of the WebSub APIs hosted in this API Gateway -->\n" + "                <GatewayWebSubEndpoint>http://localhost:9021,https://localhost:8021</GatewayWebSubEndpoint>\n" + "                <Properties>\n" + "                    <Property name=\"Organization\">WSO2</Property>\n" + "                    <Property name=\"DisplayName\">Development Environment</Property>\n" + "                    <Property name=\"DevAccountName\">dev-1</Property>\n" + "                </Properties>\n" + "                <VirtualHosts>\n" + "                </VirtualHosts>\n" + "            </Environment>";
    OMElement element = AXIOMUtil.stringToOM(envConfig);
    APIManagerConfiguration config = new APIManagerConfiguration();
    config.setEnvironmentConfig(element);
    Map<String, Environment> environmentsList = config.getGatewayEnvironments();
    Assert.assertFalse(environmentsList.isEmpty());
    Environment defaultEnv = environmentsList.get("Default");
    Assert.assertFalse(defaultEnv.getAdditionalProperties().isEmpty());
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) OMElement(org.apache.axiom.om.OMElement) Test(org.junit.Test)

Example 55 with Provider

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

the class OASParserUtilTest method testGenerateOASConfigForHTTPEndpoints.

@Test
public void testGenerateOASConfigForHTTPEndpoints() throws Exception {
    String endpoints = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("definitions" + File.separator + "endpointsData.json"), "UTF-8");
    JSONObject jsonObject = new JSONObject(endpoints);
    APIIdentifier identifier = new APIIdentifier("provider", "name", "version");
    API api = new API(identifier);
    JsonNode prodNode;
    JsonNode sandNode;
    // start http production only
    String httpProduction = jsonObject.getJSONObject("http_production").toString();
    api.setEndpointConfig(httpProduction);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(1, prodNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNull(sandNode);
    String httpProductionFailover = jsonObject.getJSONObject("http_production_failover").toString();
    api.setEndpointConfig(httpProductionFailover);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(2, prodNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_FAILOVER, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNull(sandNode);
    String httpProductionLoadbalance = jsonObject.getJSONObject("http_production_loadbalance").toString();
    api.setEndpointConfig(httpProductionLoadbalance);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(2, prodNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_LOADBALANCE, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNull(sandNode);
    // start http sandbox only
    String httpSandbox = jsonObject.getJSONObject("http_sandbox").toString();
    api.setEndpointConfig(httpSandbox);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNull(prodNode);
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(1, sandNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    String httpSandboxFailover = jsonObject.getJSONObject("http_sandbox_failover").toString();
    api.setEndpointConfig(httpSandboxFailover);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNull(prodNode);
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(2, sandNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_FAILOVER, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    String httpSandboxLoadbalance = jsonObject.getJSONObject("http_sandbox_loadbalance").toString();
    api.setEndpointConfig(httpSandboxLoadbalance);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNull(prodNode);
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(2, sandNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_LOADBALANCE, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    // start http hybrid
    String httpHybrid = jsonObject.getJSONObject("http_hybrid").toString();
    api.setEndpointConfig(httpHybrid);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(1, prodNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(1, sandNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    String httpHybridFailover = jsonObject.getJSONObject("http_hybrid_failover").toString();
    api.setEndpointConfig(httpHybridFailover);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(2, prodNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_FAILOVER, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(2, sandNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_FAILOVER, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    String httpHybridLoadbalance = jsonObject.getJSONObject("http_hybrid_loadbalance").toString();
    api.setEndpointConfig(httpHybridLoadbalance);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(2, prodNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_LOADBALANCE, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(2, sandNode.get(APIConstants.ENDPOINT_URLS).size());
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_LOADBALANCE, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
}
Also used : JSONObject(org.json.JSONObject) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)82 ArrayList (java.util.ArrayList)70 API (org.wso2.carbon.apimgt.api.model.API)64 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)50 Test (org.junit.Test)49 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)45 HashMap (java.util.HashMap)40 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)36 IOException (java.io.IOException)35 Resource (org.wso2.carbon.registry.core.Resource)34 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)32 HashSet (java.util.HashSet)30 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)29 UserStoreException (org.wso2.carbon.user.api.UserStoreException)29 PreparedStatement (java.sql.PreparedStatement)28 Connection (java.sql.Connection)27 SQLException (java.sql.SQLException)27 ResultSet (java.sql.ResultSet)25 QName (javax.xml.namespace.QName)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25