Search in sources :

Example 1 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class ApiDAOImpl method constructAPIFromResultSet.

private API constructAPIFromResultSet(Connection connection, PreparedStatement statement) throws SQLException, IOException, APIMgtDAOException {
    try (ResultSet rs = statement.executeQuery()) {
        while (rs.next()) {
            BusinessInformation businessInformation = new BusinessInformation();
            businessInformation.setTechnicalOwner(rs.getString("TECHNICAL_OWNER"));
            businessInformation.setTechnicalOwnerEmail(rs.getString("TECHNICAL_EMAIL"));
            businessInformation.setBusinessOwner(rs.getString("BUSINESS_OWNER"));
            businessInformation.setBusinessOwnerEmail(rs.getString("BUSINESS_EMAIL"));
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.setEnabled(rs.getBoolean("CORS_ENABLED"));
            String allowOrigins = rs.getString("CORS_ALLOW_ORIGINS");
            corsConfiguration.setAllowOrigins(DAOUtil.commaSeperatedStringToList(allowOrigins));
            corsConfiguration.setAllowCredentials(rs.getBoolean("CORS_ALLOW_CREDENTIALS"));
            String allowHeaders = rs.getString("CORS_ALLOW_HEADERS");
            corsConfiguration.setAllowHeaders(DAOUtil.commaSeperatedStringToList(allowHeaders));
            String allowMethods = rs.getString("CORS_ALLOW_METHODS");
            corsConfiguration.setAllowMethods(DAOUtil.commaSeperatedStringToList(allowMethods));
            String apiPrimaryKey = rs.getString("UUID");
            return new API.APIBuilder(rs.getString("PROVIDER"), rs.getString("NAME"), rs.getString("VERSION")).id(apiPrimaryKey).context(rs.getString("CONTEXT")).isDefaultVersion(rs.getBoolean("IS_DEFAULT_VERSION")).description(rs.getString("DESCRIPTION")).visibility(API.Visibility.valueOf(rs.getString("VISIBILITY"))).visibleRoles(getVisibleRoles(connection, apiPrimaryKey)).isResponseCachingEnabled(rs.getBoolean("IS_RESPONSE_CACHED")).cacheTimeout(rs.getInt("CACHE_TIMEOUT")).hasOwnGateway(rs.getBoolean("HAS_OWN_GATEWAY")).tags(getTags(connection, apiPrimaryKey)).labels(getLabelIdsForAPI(connection, apiPrimaryKey)).wsdlUri(ApiResourceDAO.getTextValueForCategory(connection, apiPrimaryKey, ResourceCategory.WSDL_TEXT)).transport(getTransports(connection, apiPrimaryKey)).endpoint(getEndPointsForApi(connection, apiPrimaryKey)).apiPermission(getPermissionsStringForApi(connection, apiPrimaryKey)).permissionMap(getPermissionMapForApi(connection, apiPrimaryKey)).businessInformation(businessInformation).lifecycleInstanceId(rs.getString("LIFECYCLE_INSTANCE_ID")).lifeCycleStatus(rs.getString("CURRENT_LC_STATUS")).corsConfiguration(corsConfiguration).createdBy(rs.getString("CREATED_BY")).updatedBy(rs.getString("UPDATED_BY")).createdTime(rs.getTimestamp("CREATED_TIME").toLocalDateTime()).lastUpdatedTime(rs.getTimestamp("LAST_UPDATED_TIME").toLocalDateTime()).uriTemplates(getUriTemplates(connection, apiPrimaryKey)).policies(getSubscripitonPolciesByAPIId(connection, apiPrimaryKey)).copiedFromApiId(rs.getString("COPIED_FROM_API")).workflowStatus(rs.getString("LC_WORKFLOW_STATUS")).securityScheme(rs.getInt("SECURITY_SCHEME")).apiPolicy(getApiPolicyByAPIId(connection, apiPrimaryKey)).threatProtectionPolicies(getThreatProtectionPolicies(connection, apiPrimaryKey)).build();
        }
    }
    return null;
}
Also used : BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) ResultSet(java.sql.ResultSet)

Example 2 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class ApiDAOImpl method getResourcesOfApi.

/**
 * @see org.wso2.carbon.apimgt.core.dao.ApiDAO#getResourcesOfApi(String, String)
 */
@Override
public List<UriTemplate> getResourcesOfApi(String apiContext, String apiVersion) throws APIMgtDAOException {
    final String query = "SELECT operationMapping.OPERATION_ID AS OPERATION_ID,operationMapping.HTTP_METHOD AS " + "HTTP_METHOD,operationMapping.URL_PATTERN AS URL_PATTERN,operationMapping.AUTH_SCHEME AS AUTH_SCHEME," + "operationMapping.API_POLICY_ID AS API_POLICY_ID FROM AM_API_OPERATION_MAPPING operationMapping," + "AM_API api WHERE operationMapping.API_ID = api.UUID AND api.CONTEXT = ? AND api.VERSION = ?";
    List<UriTemplate> uriTemplates = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, apiContext);
        preparedStatement.setString(2, apiVersion);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                UriTemplate uriTemplate = new UriTemplate.UriTemplateBuilder().uriTemplate(resultSet.getString("URL_PATTERN")).authType(resultSet.getString("AUTH_SCHEME")).httpVerb(resultSet.getString("HTTP_METHOD")).policy(new APIPolicy(resultSet.getString("API_POLICY_ID"), "")).templateId(resultSet.getString("OPERATION_ID")).build();
                uriTemplates.add(uriTemplate);
            }
        }
    } catch (SQLException e) {
        String msg = "getting API resources for Context: " + apiContext + ", Version: " + apiVersion;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
    }
    return uriTemplates;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 3 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class ApiDAOImpl method getApiPolicyByAPIId.

private Policy getApiPolicyByAPIId(Connection connection, String apiId) throws SQLException {
    final String query = "SELECT amapipolicy.NAME AS POLICY_NAME FROM AM_API_POLICY_MAPPING apimpolicymapping," + "AM_API_POLICY amapipolicy WHERE apimpolicymapping.API_POLICY_ID=amapipolicy.UUID " + "AND apimpolicymapping.API_ID = ?";
    Policy apiPolicy = null;
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, apiId);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            if (rs.next()) {
                apiPolicy = new APIPolicy(rs.getString("POLICY_NAME"));
            }
        }
    }
    return apiPolicy;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 4 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getApiPolicyByUuid.

@Override
public APIPolicy getApiPolicyByUuid(String uuid) throws APIMgtDAOException {
    try {
        String sqlQuery = "SELECT UUID, NAME, DEFAULT_QUOTA_TYPE, DEFAULT_TIME_UNIT, DEFAULT_UNIT_TIME, " + "DEFAULT_QUOTA, " + "DEFAULT_QUOTA_UNIT, DESCRIPTION, DISPLAY_NAME, IS_DEPLOYED, APPLICABLE_LEVEL from " + "AM_API_POLICY WHERE UUID = ?";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
            preparedStatement.setString(1, uuid);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    APIPolicy apiPolicy = new APIPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    setCommonPolicyDetails(apiPolicy, resultSet);
                    apiPolicy.setUserLevel(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
                    apiPolicy.setPipelines(getPipelines(apiPolicy.getUuid(), connection));
                    return apiPolicy;
                } else {
                    // not found
                    String msg = "API Policy not found for id: " + uuid;
                    log.warn(msg);
                    throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
                }
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving API policy with id: " + uuid;
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 5 with ApiPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getAllApiPolicies.

/**
 * Retrieves all API policies.
 *
 * @return List of {@link APIPolicy} instances, or an empty list if none is found
 * @throws SQLException if an error occurs while retrieving policies
 */
private List<APIPolicy> getAllApiPolicies() throws SQLException {
    List<APIPolicy> policyList = new ArrayList<>();
    String sqlQuery = "SELECT UUID, NAME, DEFAULT_QUOTA_TYPE, DEFAULT_TIME_UNIT, DEFAULT_UNIT_TIME, " + "DEFAULT_QUOTA, DEFAULT_QUOTA_UNIT, DESCRIPTION, DISPLAY_NAME, IS_DEPLOYED, APPLICABLE_LEVEL " + "from AM_API_POLICY";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                APIPolicy apiPolicy = new APIPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                apiPolicy.setUuid(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID));
                setCommonPolicyDetails(apiPolicy, resultSet);
                apiPolicy.setUserLevel(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
                apiPolicy.setPipelines(getPipelines(apiPolicy.getUuid(), connection));
                policyList.add(apiPolicy);
            }
        }
    }
    return policyList;
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)87 Test (org.testng.annotations.Test)44 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)40 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)39 ArrayList (java.util.ArrayList)38 API (org.wso2.carbon.apimgt.core.models.API)33 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)30 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)27 HashMap (java.util.HashMap)25 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)22 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 HashSet (java.util.HashSet)19 Test (org.junit.Test)19 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)19 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 PreparedStatement (java.sql.PreparedStatement)15