Search in sources :

Example 71 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class ApiMgtDAO method isKeyManagerConfigurationExistByName.

public boolean isKeyManagerConfigurationExistByName(String name, String organization) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        final String query = "SELECT 1 FROM AM_KEY_MANAGER WHERE NAME = ? AND ORGANIZATION = ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, organization);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    return true;
                }
            }
        }
    } catch (SQLException e) {
        throw new APIManagementException("Error while retrieving key manager existence", e);
    }
    return false;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 72 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class CertificateMgtDAO method updateClientCertificate.

/**
 * To update an already existing client certificate.
 *
 * @param certificate : Specific certificate.
 * @param alias       : Alias of the certificate.
 * @param tier        : Name of tier related with the certificate.
 * @param tenantId    : ID of the tenant.
 * @param organization : Organization
 * @return true if the update succeeds, unless false.
 * @throws CertificateManagementException Certificate Management Exception.
 */
public boolean updateClientCertificate(String certificate, String alias, String tier, int tenantId, String organization) throws CertificateManagementException {
    List<ClientCertificateDTO> clientCertificateDTOList = getClientCertificates(tenantId, alias, null, organization);
    ClientCertificateDTO clientCertificateDTO;
    if (clientCertificateDTOList.size() == 0) {
        if (log.isDebugEnabled()) {
            log.debug("Client certificate update request is received for a non-existing alias " + alias + " of " + "tenant " + tenantId);
        }
        return false;
    }
    clientCertificateDTO = clientCertificateDTOList.get(0);
    if (StringUtils.isNotEmpty(certificate)) {
        clientCertificateDTO.setCertificate(certificate);
    }
    if (StringUtils.isNotEmpty(tier)) {
        clientCertificateDTO.setTierName(tier);
    }
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            deleteClientCertificate(connection, null, alias, tenantId);
            addClientCertificate(connection, clientCertificateDTO.getCertificate(), clientCertificateDTO.getApiIdentifier(), alias, clientCertificateDTO.getTierName(), tenantId, organization);
            connection.commit();
        } catch (SQLException e) {
            handleConnectionRollBack(connection);
            handleException("Error while updating client certificate for the API for the alias " + alias, e);
        }
    } catch (SQLException e) {
        handleException("Error while updating client certificate for the API for the alias " + alias, e);
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)

Example 73 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class ApiMgtDAO method updateSubscription.

/**
 * This method is used to update the subscription
 *
 * @param identifier    APIIdentifier
 * @param subStatus     Subscription Status[BLOCKED/UNBLOCKED]
 * @param applicationId Application id
 * @param organization  Organization
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to update subscriber
 */
public void updateSubscription(APIIdentifier identifier, String subStatus, int applicationId, String organization) throws APIManagementException {
    Connection conn = null;
    ResultSet resultSet = null;
    PreparedStatement ps = null;
    PreparedStatement updatePs = null;
    int apiId = -1;
    try {
        conn = APIMgtDBUtil.getConnection();
        conn.setAutoCommit(false);
        String getApiQuery = SQLConstants.GET_API_ID_SQL;
        ps = conn.prepareStatement(getApiQuery);
        ps.setString(1, APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
        ps.setString(2, identifier.getApiName());
        ps.setString(3, identifier.getVersion());
        resultSet = ps.executeQuery();
        if (resultSet.next()) {
            apiId = resultSet.getInt("API_ID");
        }
        if (apiId == -1) {
            String msg = "Unable to get the API ID for: " + identifier;
            log.error(msg);
            throw new APIManagementException(msg);
        }
        String subsCreateStatus = getSubscriptionCreaeteStatus(identifier, applicationId, organization, conn);
        if (APIConstants.SubscriptionCreatedStatus.UN_SUBSCRIBE.equals(subsCreateStatus)) {
            deleteSubscriptionByApiIDAndAppID(apiId, applicationId, conn);
        }
        // This query to update the AM_SUBSCRIPTION table
        String sqlQuery = SQLConstants.UPDATE_SUBSCRIPTION_OF_APPLICATION_SQL;
        // Updating data to the AM_SUBSCRIPTION table
        updatePs = conn.prepareStatement(sqlQuery);
        updatePs.setString(1, subStatus);
        updatePs.setString(2, identifier.getProviderName());
        updatePs.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
        updatePs.setInt(4, apiId);
        updatePs.setInt(5, applicationId);
        updatePs.execute();
        // finally commit transaction
        conn.commit();
    } catch (SQLException e) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                log.error("Failed to rollback the add subscription ", e1);
            }
        }
        handleException("Failed to update subscription data ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
        APIMgtDBUtil.closeAllConnections(updatePs, null, null);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 74 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class ApiMgtDAO method restoreOperationPolicyRevision.

/**
 * This method is used to restore an API specific operation policy revision.
 *
 * @param connection   DB connection
 * @param apiUUID      UUID of the API
 * @param policyId     Original policy's ID that needs to be cloned
 * @param revisionId   The revision number
 * @param organization Organization name
 * @throws SQLException
 * @throws APIManagementException
 */
private String restoreOperationPolicyRevision(Connection connection, String apiUUID, String policyId, int revisionId, String organization) throws SQLException, APIManagementException {
    OperationPolicyData revisionedPolicy = getAPISpecificOperationPolicyByPolicyID(connection, policyId, apiUUID, organization, true);
    String restoredPolicyId = null;
    if (revisionedPolicy != null) {
        // First check whether there exists a API specific policy for same policy name with revision uuid null
        // This is the state where we record the policies applied in the working copy.
        OperationPolicyData apiSpecificPolicy = getAPISpecificOperationPolicyByPolicyName(connection, revisionedPolicy.getSpecification().getName(), revisionedPolicy.getApiUUID(), null, organization, false);
        if (apiSpecificPolicy != null) {
            if (apiSpecificPolicy.getMd5Hash().equals(revisionedPolicy.getMd5Hash())) {
                if (log.isDebugEnabled()) {
                    log.debug("Matching API specific operation policy found for the revisioned policy and " + "MD5 hashes match");
                }
            } else {
                updateAPISpecificOperationPolicyWithClonedPolicyId(connection, apiSpecificPolicy.getPolicyId(), revisionedPolicy);
                if (log.isDebugEnabled()) {
                    log.debug("Even though a matching API specific operation policy found for name," + " MD5 hashes does not match. Policy " + apiSpecificPolicy.getPolicyId() + " has been updated from the revision.");
                }
            }
            restoredPolicyId = apiSpecificPolicy.getPolicyId();
        } else {
            if (revisionedPolicy.isClonedPolicy()) {
                // Check for a common operation policy only if it is a cloned policy.
                OperationPolicyData commonPolicy = getCommonOperationPolicyByPolicyID(connection, revisionedPolicy.getClonedCommonPolicyId(), organization, false);
                if (commonPolicy != null) {
                    if (commonPolicy.getMd5Hash().equals(revisionedPolicy.getMd5Hash())) {
                        if (log.isDebugEnabled()) {
                            log.debug("Matching common operation policy found. MD5 hash match");
                        }
                        // This means the common policy is same with our revision. A clone is created and original
                        // common policy ID is referenced as the ClonedCommonPolicyId
                        restoredPolicyId = addAPISpecificOperationPolicy(connection, apiUUID, null, revisionedPolicy, revisionedPolicy.getClonedCommonPolicyId());
                    } else {
                        // This means the common policy is updated since we created the revision.
                        // we have to create a clone and since policy is different, we can't refer the original common
                        // policy as ClonedCommonPolicyId. This should be a new API specific policy
                        revisionedPolicy.getSpecification().setName(revisionedPolicy.getSpecification().getName() + "_restored-" + revisionId);
                        revisionedPolicy.getSpecification().setDisplayName(revisionedPolicy.getSpecification().getDisplayName() + " Restored from revision " + revisionId);
                        revisionedPolicy.setMd5Hash(APIUtil.getMd5OfOperationPolicy(revisionedPolicy));
                        revisionedPolicy.setRevisionUUID(null);
                        restoredPolicyId = addAPISpecificOperationPolicy(connection, apiUUID, null, revisionedPolicy, null);
                        if (log.isDebugEnabled()) {
                            log.debug("An updated matching common operation policy found. A new API specific operation " + "policy created by the display name " + revisionedPolicy.getSpecification().getName());
                        }
                    }
                } else {
                    // This means this is a clone of a deleted common policy. A new API specific policy will be created.
                    revisionedPolicy.getSpecification().setName(revisionedPolicy.getSpecification().getName() + "_restored-" + revisionId);
                    revisionedPolicy.getSpecification().setDisplayName(revisionedPolicy.getSpecification().getDisplayName() + " Restored from revision " + revisionId);
                    revisionedPolicy.setMd5Hash(APIUtil.getMd5OfOperationPolicy(revisionedPolicy));
                    revisionedPolicy.setRevisionUUID(null);
                    restoredPolicyId = addAPISpecificOperationPolicy(connection, apiUUID, null, revisionedPolicy, null);
                    if (log.isDebugEnabled()) {
                        log.debug("No matching operation policy found. A new API specific operation " + "policy created by the name " + revisionedPolicy.getSpecification().getName());
                    }
                }
            } else {
                // This means this is a completely new policy and we don't have any reference of a previous state in
                // working copy. A new API specific policy will be created.
                revisionedPolicy.setRevisionUUID(null);
                restoredPolicyId = addAPISpecificOperationPolicy(connection, apiUUID, null, revisionedPolicy, null);
                if (log.isDebugEnabled()) {
                    log.debug("No matching operation policy found. A new API specific operation " + "policy created by the name " + revisionedPolicy.getSpecification().getName());
                }
            }
        }
    } else {
        throw new APIManagementException("A revisioned operation policy not found for " + policyId);
    }
    return restoredPolicyId;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 75 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testIsAPIAvailable.

@Test
public void testIsAPIAvailable() throws APIManagementException {
    APIIdentifier apiIdentifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String organization = "org1";
    String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiMgtDAO);
    Mockito.when(apiMgtDAO.getUUIDFromIdentifier(apiIdentifier, organization)).thenReturn("xxxxx");
    Assert.assertTrue(abstractAPIManager.isAPIAvailable(apiIdentifier, organization));
}
Also used : APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)304 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)106 API (org.wso2.carbon.apimgt.api.model.API)100 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)89 ArrayList (java.util.ArrayList)79 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)72 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)65 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)64 IOException (java.io.IOException)61 Registry (org.wso2.carbon.registry.core.Registry)58 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)57 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)56 HashMap (java.util.HashMap)54 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)53 Resource (org.wso2.carbon.registry.core.Resource)51 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)49 JSONObject (org.json.simple.JSONObject)45 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 URISyntaxException (java.net.URISyntaxException)42