Search in sources :

Example 36 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.

the class AsyncApiParser method buildURITemplate.

private URITemplate buildURITemplate(String target, String verb, Aai20Operation operation, Set<Scope> scopes, Aai20ChannelItem channel) throws APIManagementException {
    URITemplate template = new URITemplate();
    template.setHTTPVerb(verb);
    template.setHttpVerbs(verb);
    template.setUriTemplate(target);
    Extension authTypeExtension = channel.getExtension(APIConstants.SWAGGER_X_AUTH_TYPE);
    if (authTypeExtension != null && authTypeExtension.value instanceof String) {
        template.setAuthType(authTypeExtension.value.toString());
    }
    List<String> opScopes = getScopeOfOperations(operation);
    if (!opScopes.isEmpty()) {
        if (opScopes.size() == 1) {
            String firstScope = opScopes.get(0);
            Scope scope = APIUtil.findScopeByKey(scopes, firstScope);
            if (scope == null) {
                throw new APIManagementException("Scope '" + firstScope + "' not found.");
            }
            template.setScope(scope);
            template.setScopes(scope);
        } else {
            for (String scopeName : opScopes) {
                Scope scope = APIUtil.findScopeByKey(scopes, scopeName);
                if (scope == null) {
                    throw new APIManagementException("Resource Scope '" + scopeName + "' not found.");
                }
                template.setScopes(scope);
            }
        }
    }
    return template;
}
Also used : Extension(io.apicurio.datamodels.core.models.Extension) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Example 37 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.

the class ApiMgtDAO method getOperationPoliciesOfURITemplate.

/**
 * Get operation polycies attached to the resource identified by the url mapping ID
 *
 * @param urlMappingId URL Mapping ID of the resource
 * @return
 * @throws SQLException
 * @throws APIManagementException
 */
private List<OperationPolicy> getOperationPoliciesOfURITemplate(int urlMappingId) throws SQLException, APIManagementException {
    List<OperationPolicy> operationPolicies = new ArrayList<>();
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_BY_URI_TEMPLATE_ID)) {
        ps.setInt(1, urlMappingId);
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                OperationPolicy policy = populateOperationPolicyWithRS(rs);
                operationPolicies.add(policy);
            }
        }
    }
    return operationPolicies;
}
Also used : OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 38 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.

the class ApiMgtDAO method cloneOperationPolicy.

/**
 * Clone an operation policy to the API. This method is used to clone policy to a newly created api version.
 * Cloning a common policy to API.
 * Cloning a dependent policy of a product
 * Each of these scenarios, original APIs' policy ID will be recorded as the cloned policy ID.
 *
 * @param apiUUID      UUID of the API
 * @param operationPolicyData
 * @return cloned policyID
 * @throws APIManagementException
 */
public String cloneOperationPolicy(String apiUUID, OperationPolicyData operationPolicyData) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            String policyId = addAPISpecificOperationPolicy(connection, apiUUID, null, operationPolicyData, operationPolicyData.getClonedCommonPolicyId());
            connection.commit();
            return policyId;
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        }
    } catch (SQLException e) {
        throw new APIManagementException("Error while cloning Operation policies", e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 39 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.

the class ApiMgtDAO method addOperationPolicyMapping.

public void addOperationPolicyMapping(Set<URITemplate> uriTemplates) throws APIManagementException {
    if (uriTemplates != null && !uriTemplates.isEmpty()) {
        try (Connection connection = APIMgtDBUtil.getConnection()) {
            connection.setAutoCommit(false);
            try (PreparedStatement preparedStatement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.ADD_API_OPERATION_POLICY_MAPPING)) {
                for (URITemplate uriTemplate : uriTemplates) {
                    List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
                    if (operationPolicies != null && !operationPolicies.isEmpty()) {
                        for (OperationPolicy operationPolicy : operationPolicies) {
                            Gson gson = new Gson();
                            String paramJSON = gson.toJson(operationPolicy.getParameters());
                            preparedStatement.setInt(1, uriTemplate.getId());
                            preparedStatement.setString(2, operationPolicy.getPolicyId());
                            preparedStatement.setString(3, operationPolicy.getDirection());
                            preparedStatement.setString(4, paramJSON);
                            preparedStatement.setInt(5, operationPolicy.getOrder());
                            preparedStatement.addBatch();
                        }
                    }
                }
                preparedStatement.executeBatch();
                connection.commit();
            } catch (SQLException e) {
                connection.rollback();
                throw e;
            }
        } catch (SQLException e) {
            throw new APIManagementException("Error while updating operation Policy mapping for API", e);
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) Connection(java.sql.Connection) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement)

Example 40 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.

the class ApiMgtDAO method deleteAPIProductRevision.

/**
 * Delete API Product revision database records
 *
 * @param apiRevision content of the revision
 * @throws APIManagementException if an error occurs when restoring an API revision
 */
public void deleteAPIProductRevision(APIRevision apiRevision) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            // Retrieve API ID
            APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiRevision.getApiUUID());
            int apiId = getAPIID(apiRevision.getApiUUID(), connection);
            int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()));
            // Removing related revision entries from AM_REVISION table
            PreparedStatement removeAMRevisionStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.DELETE_API_REVISION);
            removeAMRevisionStatement.setString(1, apiRevision.getRevisionUUID());
            removeAMRevisionStatement.executeUpdate();
            // Removing related revision entries from AM_API_PRODUCT_MAPPING table
            PreparedStatement removeProductMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_API_PRODUCT_MAPPING_BY_REVISION_UUID);
            removeProductMappingsStatement.setInt(1, apiId);
            removeProductMappingsStatement.setString(2, apiRevision.getRevisionUUID());
            removeProductMappingsStatement.executeUpdate();
            // Removing related revision entries from AM_API_URL_MAPPING table
            PreparedStatement removeURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_PRODUCT_REVISION_ENTRIES_IN_AM_API_URL_MAPPING_BY_REVISION_UUID);
            removeURLMappingsStatement.setString(1, apiRevision.getRevisionUUID());
            removeURLMappingsStatement.executeUpdate();
            // Removing related revision entries from AM_API_CLIENT_CERTIFICATE table
            PreparedStatement removeClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_API_CLIENT_CERTIFICATE_BY_REVISION_UUID);
            removeClientCertificatesStatement.setInt(1, apiId);
            removeClientCertificatesStatement.setString(2, apiRevision.getRevisionUUID());
            removeClientCertificatesStatement.executeUpdate();
            // Removing related revision entries from AM_GRAPHQL_COMPLEXITY table
            PreparedStatement removeGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_GRAPHQL_COMPLEXITY_BY_REVISION_UUID);
            removeGraphQLComplexityStatement.setInt(1, apiId);
            removeGraphQLComplexityStatement.setString(2, apiRevision.getRevisionUUID());
            removeGraphQLComplexityStatement.executeUpdate();
            // Removing related revision entries for operation policies
            deleteAllAPISpecificOperationPoliciesByAPIUUID(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            handleException("Failed to delete API Revision entry of API Product UUID " + apiRevision.getApiUUID(), e);
        }
    } catch (SQLException e) {
        handleException("Failed to delete API Revision entry of API Product UUID " + apiRevision.getApiUUID(), e);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 HashMap (java.util.HashMap)55 ArrayList (java.util.ArrayList)43 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)42 Test (org.testng.annotations.Test)34 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)29 HttpResponse (org.wso2.carbon.automation.test.utils.http.client.HttpResponse)29 Map (java.util.Map)28 IOException (java.io.IOException)23 API (org.wso2.carbon.apimgt.api.model.API)22 List (java.util.List)20 JSONObject (org.json.simple.JSONObject)20 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)20 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)19 LinkedHashMap (java.util.LinkedHashMap)18 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)18 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)18 APIInfo (org.wso2.carbon.apimgt.api.model.APIInfo)17 Connection (java.sql.Connection)16 PreparedStatement (java.sql.PreparedStatement)16